Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file name.ml
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364(** Generation of fresh names. *)openLplibopenBaseopenExtraopenDebug(** If [s] ends with some digits, then [root_and_index s = (root,i)] such that
[i] is the biggest integer such that [s = root^string_of_int
i]. Otherwise, [root_and_index s = (s,-1)].*)letroot_and_index=letre=Str.regexp"[^0-9][0-9]+$"infuns->(*print_endline("root_and_index "^s);*)letn=String.lengthsintryleti=1+Str.search_backwardres(n-1)in(* skip leading zeros *)leti=ifs.[i]<>'0'thenielsebeginletj=ref(i+1)inwhile!j<n&&s.[!j]='0'doincrjdone;!j-1endin(*print_endline("search_backward = "^string_of_int i);*)String.subs0i,int_of_string(String.subsi(n-i))withNot_found->s,-1(* unit tests *)let_=assert(root_and_index"x"=("x",-1));assert(root_and_index"x0"=("x",0));assert(root_and_index"xy0"=("xy",0));assert(root_and_index"x0y"=("x0y",-1));assert(root_and_index"x00"=("x0",0));assert(root_and_index"x000"=("x00",0))(*let strings (idmap:int StrMap.t) : StrSet.t =
StrMap.fold (fun p i set -> if i < 0 then p else p^string_of_int i)
idmap StrSet.empty*)letadd_namesidmap=letr,i=root_and_indexsinStrMap.addriidmap(** Assume that [root_and_index p = (r,i)]. If [i<0] then [get_safe_prefix p
idmap] returns [p,StrMap.add p (-1) idmap] and, for all non-negative
integer [k], [StrSet.mem (p^string_of_int k) (strings
idmap')=false]. Otherwise, [get_safe_prefix p idmap] returns
[r^string_of_int k,StrMap.add r k idmap], where [k] is greater than or
equal to [i] and 1 + the index of [r] in [idmap]. *)letget_safe_prefix(s:string)(idmap:intStrMap.t):string*intStrMap.t=letr,i=root_and_indexsinmatchStrMap.find_optridmapwith|None->s,StrMap.addriidmap|Somej->letk=maxij+1inr^string_of_intk,StrMap.addrkidmap(* unit tests *)let_=letidmap=StrMap.(add"x"1(add"x0"0empty))inlettests1s2=assert(fst(get_safe_prefixs1idmap)=s2)intest"y""y";test"x""x2";test"x0""x2";test"x00""x01";test"xy""xy"