Jump to content

Debugging


FredSRichardson

Recommended Posts

Okay, I'm having trouble with "balance_quotes" (in the iwgrules.ml) raising "Invalid_argument(_)". It took me a long time to find this. Here's the snippet (goes back to iwg2 days):

 

let balance_quotes fn s =
 let num_quotes = ref 0 in
 for i = 0 to String.length s do
   if s.[i] = '"' then incr num_quotes
   else if s.[i] = '\'' then (s.[i] <- '"'; incr num_quotes) 
 done;
 if !num_quotes mod 2 <> 0 then begin
   error "DLG" "[%s] has %d quotes in ~%s~: balancing.\n" fn !num_quotes s;
   let s = Str.global_replace left_quote_regexp "\"\\1\"\\2" s in
   let s = Str.global_replace right_quote_regexp "\\1\"\\2\"" s in
   s
 end else s

 

Could it be that the index should only go up to "(String.length s) - 1"?

Link to comment

Ah, bounds checking is a good thing! But what a pain to track this down. I had to do a search with print statements to figure out where the routine was exiting early and then add a "try ... with" wrapper to figure out what the exception was. Sheesh! There's got to be a better way.

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...