Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file netstring_str.ml
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121(* $Id$
* ----------------------------------------------------------------------
*
*)openPrintfmoduleDebug=structletenable=reffalseendletdlog=Netlog.Debug.mk_dlog"Netstring_str" Debug.enableletdlogr =Netlog.Debug.mk_dlogr"Netstring_str"Debug.enablelet()=Netlog.Debug.register_module"Netstring_str"Debug.enableletexplodes=letl=String.lengthsinletrecloopk=ifk<lthens.[k]::loop(k+1)else[]inloop0letimplodel=letn=List.lengthlinlets=Bytes.createninletk=ref0inList.iter(func->Bytes.sets!kc;incrk)l;Bytes.to_stringsletquote_sets=letl=explode sinlethave_circum =List.mem'^'linlethave_minus=List.mem'-'linlethave_rbracket=List.mem']'linletl1=List.filter(func->c<>'^'&&c<>'-'&&c<>']')linletl2=ifhave_rbracketthen']'::l1elsel1inletl3=ifhave_circumthenl2@['^']elsel2inletl4=ifhave_minusthenl3@['-']elsel3inlets4=implodel4inlets'=matchs4with|""->failwith"Netstring_str.quote_set: empty"|"^"->"^"|"^-"->"[-^]"|_->"["^s4^"]"inif!Debug.enablethendlogr(fun()->sprintf"quote_set: orig: %s - quoted: %s"ss');s'(**********************************************************************)(* HAVE_PCRE *)(**********************************************************************)#ifndefENABLE_STR_EXTERNALS#ifdefHAVE_PCRE(* This implementation of Netstring_str uses the PCRE engine. The
* syntax for regular expressions is compatible with previous versions.
*)(**********************************************************************)(* Parsing types *)typesetatom=|Scharofchar|Srangeof(char*char)andset=setatomlist;;typere_term=Texactofstring(* literal characters (except NUL) *)|Tnullchar(* NUL characer *)|Tany(* . but no newline *)|Tnull(* emptiness *)|Tconcatofre_termlist|Tstarofre_term(* x* *)|Tplusofre_term(* x+ *)|Toption of re_term (* x? *)|Tsetofset(* [...] *)|Tnegsetofset(* [^...] *)|Tbegline(* ^ *)|Tendline(* $ *)|Taltofre_termlist(* x\|y *)|Tgroupof(int*re_term)(* \(...\)*)|Treferofint(* \i *)|Tintervalof(re_term*int*int)(* x{n,m}. m=-1 means infinite *)|Twordchar(* \w *)|Tnowordchar(* \W *)|Twordbeg(* \< *)|Twordend(* \> *)|Twordbound(* \b *)|Tnowordbound(* \B *)|Tbegbuf(* \` *)|Tendbuf(* \' *);;(**********************************************************************)(* Final types *)typeregexp=Pcre.regexp;;typesplit_result=Str.split_result=Textofstring|Delimofstring;;typeresult=Netstring_pcre.result;;(**********************************************************************)(* Parse Str-style regexps, and convertto Pcre-style regexps *)letscan_str_regexpre_string =letl=String.lengthre_stringinletk=ref(-1)inletc=ref' 'inletesc=reffalseinletgroup=ref1inletn_open_groups=ref0inletclosed_groups=Array.make10falseinletnext()=incrk;if(!k<l)thenbeginletc1=re_string.[!k]inifc1='\\'thenbeginif!k<lthenbeginincrk;c:=re_string.[!k];esc:=trueendelsefailwith"regexp: bad backslash"endelsebeginesc:=false;c:=c1endendinletnext_noesc()=incrk;if(!k<l)thenbeginc:=re_string.[!k];esc:=falseendinletrecscan_alternative ()=lett1=scan_concatenation()inif!k<lthen beginif!esc&!c='|'thenbeginnext();matchscan_alternative()withTaltalist->Talt(t1::alist)|t->Talt[t1;t]endelset1endelset1andscan_concatenation()=lett1=scan_repetition()inift1=Tnullthent1elselett2=scan_concatenation()inmatch t2withTnull->t1|Texacts2->beginmatcht1withTexacts1-> Texact(s1^s2)|_->Tconcat[t1;t2]end|Tconcatclist->Tconcat(t1::clist)|_->Tconcat[t1;t2]andscan_repetition()=lett1=ref(scan_literal_or_group())inletcontinue=ref trueinwhile!continuedoif!k<l¬!escthenbeginmatch!cwith'*' ->next();t1:=Tstar!t1|'+'->next();t1:=Tplus !t1|'?'->next();t1:=Toption!t1(* {...} is notimplemented in Str *)(*
| '{' -> next_noesc();
let n1 = ref None in
let n2 = ref None in
let j = ref 0 in
if !k < l & !c >='0' & !c <= '9' then begin
while !k < l & !c >= '0' & !c <= '9' do
j:= 10* !j + (Char.code !c - Char.code '0');
next_noesc() done;
n1 := Some !j
end; if !k < l & !n1 <> None & !c = '}' then begin next();
t1 := Tinterval (!t1, !j, !j)
end
else begin
if !k >=l or !c <> ',' then
failwith "regexp: error in {...} phrase";
next_noesc();
j := 0;
if !k < l & !c >= '0' & !c <= '9' then begin
while !k < l & !c >= '0' & !c <= '9' do
j :=10* !j + (Char.code !c - Char.code '0');
next_noesc()
done;
n2 := Some !j
end;
if !k>= l || !c <> '}' then
failwith "regexp: errorin {...} phrase";
next();
( match !n1 with
None ->
( match !n2 with
None ->
failwith "regexp: error in {...} phrase";
| Some m2 ->
t1:= Tinterval (!t1, 0, m2)
)
| Some m1 -> ( match !n2 with
None ->
t1 := Tinterval (!t1, m1, -1) | Some m2 ->
t1 := Tinterval (!t1, m1, m2)
)
)
end
*)|_->continue:=falseendelsecontinue:=falsedone;!t1andscan_literal_or_group()=if!k>=lthenTnullelseif!escthenbeginmatch!cwith'('->next();letn=!groupinincrgroup;incrn_open_groups;lett=scan_alternative()indecrn_open_groups;if!k<l&!esc&!c=')'thenbeginnext();closed_groups.(n)<-true;Tgroup(n,t)endelsefailwith"regexp: closing paranthesis \\) not found"|('1'..'9')->letn=(Char.code!c-Char.code'0')inifclosed_groups.(n)thenbeginnext();Trefernendelsefailwith"regexp: bad reference to group"(*
| 'w' -> next(); Twordchar
| 'W' -> next(); Tnowordchar
*)|'b'->next();Twordbound(*
| 'B' -> next(); Tnowordbound
| '<' -> next(); Twordbeg
| '>' -> next(); Twordend
| '`' -> next(); Tbegbuf
| '\'' -> next(); Tendbuf
*)|'\\'->next();Texact(String.make1'\\')|'|'->Tnull|')'->if!n_open_groups>0thenTnullelsefailwith"regexp: unmatched closing parenthesis"|ch->next();Texact(String.make1ch)endelsebeginmatch!cwith'*'->Tnull|'+'->Tnull|'?'->Tnull|'{'->Tnull|'^'->next();Tbegline|'$'->next();Tendline|'.'->next();Tany|'\000'->next();Tnullchar|'['->next_noesc();if!k<lthenbeginletnegated=reffalseinletset=ref[]inletadd_charc=set:=Scharc::!setinletadd_rangec1c2=set:=Srange(c1,c2)::!setinif!c='^'thenbeginnext_noesc();negated:=trueend;letcontinue=reftrueinletfirst=reftruein(* the character after [ or [^ ? *)while!continue&!k<ldomatch()with()when!c='['&!k+1<l&re_string.[!k+1]=':'->failwith"regexp: Character classes such as [[:digit:]] not implemented";(* TODO: check for predefined sets *)|()when!c=']'¬!first->next();continue:=false|()when(!k+2<l)&(re_string.[!k+1]='-')&(re_string.[!k+2]<>']')->(* range *)add_range!c(re_string.[!k+2]);next_noesc();next_noesc();next_noesc();first:=false;|()->add_char!c;next_noesc();first:=false;done;if!continuethenfailwith"regexp: closing bracket ] not found";if!negatedthenTnegset!setelseTset!setendelsefailwith"regexp: closing bracket ] not found"|ch->next();Texact(String.make1ch)endintrynext();scan_alternative()with|Failuremsg->failwith(msg^" - regexp: "^re_string);;letpcre_safe_quotec=(* for print_set *)matchcwith'a'..'z'|'A'..'Z'|'0'..'9'|'_'->String.make1c|'\000'->"\\000"|_->"\\"^String.make1c;;letrecprint_pcre_regexpret=matchretwithTexacts->Pcre.quotes|Tnullchar->(* Pcre.quote "\000" returns nonsense *)"[\\000]"|Tany->"."|Tnull->"(?:)"|Tconcatl->String.concat""(List.mapprint_pcre_regexpl)|Tstarret'->print_pcre_subregexpret'^"*"|Tplusret'->print_pcre_subregexpret'^"+"|Toptionret'->print_pcre_subregexpret'^"?"|Tsets->"["^print_sets^"]"|Tnegsets->"[^"^print_sets^"]"|Taltl->String.concat"|"(List.mapprint_pcre_subregexpl)|Tgroup(_,ret')->"("^print_pcre_regexpret'^")"|Trefern->(* Put parentheses around \n to disambiguate from \nn *)"(?:\\"^string_of_intn^")"|Tinterval(ret',m,n)->print_pcre_subregexpret'^"{"^string_of_intm^","^(ifn>=0thenstring_of_intnelse"")^"}"|Tbegline->"^"|Tendline->"(?:$)"|Twordchar->"\\w"|Tnowordchar->"\\W"|Twordbeg->"\\b(?=\\w)"|Twordend->"(?<=\\w)\\b"|Twordbound->"\\b"|Tnowordbound->"\\B"|Tbegbuf->"\\A"|Tendbuf->"\\z"andprint_pcre_subregexpret=(* Print ret, but put parentheses around ret *)matchretwithTset_|Tnegset_|Tgroup(_,_)->(* No additional parentheses needed *)print_pcre_regexpret|_->(* Print (?:ret). This is the "neutral" form of grouping that only
* changes precedence
*)"(?:"^print_pcre_regexpret^")"andprint_sets=String.concat""(List.map(functionScharc->pcre_safe_quotec|Srange(c1,c2)->pcre_safe_quotec1^"-"^pcre_safe_quotec2)s);;(**********************************************************************)(* Emulation *)letregexps=letret=scan_str_regexpsinlets'=print_pcre_regexpretinif!Debug.enablethendlogr(fun()->sprintf"regexp: orig: %s - translated: %s"ss');Pcre.regexp~flags:[`MULTILINE]s';;letregexp_case_folds=letret=scan_str_regexpsinlets'=print_pcre_regexpretinif!Debug.enablethendlogr(fun()->sprintf"regexp_case_fold: orig: %s - translated: %s"ss');Pcre.regexp~flags:[`MULTILINE;`CASELESS]s';;letpcre_quotes=(* Note that Pcre.quote is incorrect for NUL chars, which simply remain
in place, although they need to be encoded
*)lets1=Pcre.quotesinlets'=Pcre.qreplace~pat:"\\000"~templ:"\\000"s1inif!Debug.enablethendlogr(fun()->sprintf"quote: orig: %s - quoted: %s"ss');s';;letunsafe_str_re=Pcre.regexp"[\\]\\[+*?.\\\\^$]"letquotes=(* This returns, of course, a Str-syntax regexp! *)Pcre.replace~rex:unsafe_str_re~templ:"\\$&"sletregexp_strings=Pcre.regexp~flags:[`MULTILINE](pcre_quotes);;letregexp_string_case_folds=Pcre.regexp~flags:[`MULTILINE;`CASELESS](pcre_quotes);;letstring_match=Netstring_pcre.string_match;;letbytes_match=Netstring_pcre.bytes_match;;(* let string_partial_match = Netstring_pcre.string_partial_match ;; *)(* N/A *)letsearch_forward=Netstring_pcre.search_forward;;letsearch_backward=Netstring_pcre.search_backward;;letsearch_forward_bytes=Netstring_pcre.search_forward_bytes;;letsearch_backward_bytes=Netstring_pcre.search_backward_bytes;;letmatched_string=Netstring_pcre.matched_string;;letmatched_bytes=Netstring_pcre.matched_bytes;;letmatch_beginning=Netstring_pcre.match_beginning;;letmatch_end=Netstring_pcre.match_end;;letmatched_group=Netstring_pcre.matched_group;;letmatched_group_bytes=Netstring_pcre.matched_group_bytes;;letgroup_beginning=Netstring_pcre.group_beginning;;letgroup_end=Netstring_pcre.group_end;;letglobal_replacepattempls=Netstring_pcre.global_replacepattempls;;letreplace_firstpattempls=Netstring_pcre.replace_firstpattempls;;letglobal_substitute=Netstring_pcre.global_substitute;;letsubstitute_first=Netstring_pcre.substitute_first;;(* replace_matched: n/a *)letsplit=Netstring_pcre.split;;letbounded_split=Netstring_pcre.bounded_split;;letsplit_delim=Netstring_pcre.split_delim;;letbounded_split_delim=Netstring_pcre.bounded_split_delim;;lettr_split_resultr=List.map(functionPcre.Textt->Textt|Pcre.Delimd->Delimd|_->assertfalse)(List.filter(functionPcre.Group(_,_)|Pcre.NoGroup->false|_->true)r);;letfull_splitseps=tr_split_result(Netstring_pcre.full_splitseps);;letbounded_full_splitsepsmax=tr_split_result(Netstring_pcre.bounded_full_splitsepsmax);;letstring_before=Netstring_pcre.string_before;;letstring_after=Netstring_pcre.string_after;;letfirst_chars=Netstring_pcre.first_chars;;letlast_chars=Netstring_pcre.last_chars;;#endif#else(* i.e. ENABLE_STR_EXTERNALS *)(**********************************************************************)(* ENABLE_STR_EXTERNALS *)(**********************************************************************)(* We use here the Str externals directly, and reimplement parts of
the Str module to make it thread-safe
*)typeregexp=Str.regexp;;typesplit_result=Str.split_result=Textofstring|Delimofstring;;typeresult={pos:int;sr:intarray;}(* sr.(2*k) is the beginning of group k
sr.(2*k+1) is the end of group k
sr.(0) is match beginning
sr.(1) is match end
*)letmatch_begsr=sr.(0)letmatch_esr=sr.(1)letgroup_begsrk=sr.(k+k)letgroup_esrk=sr.(k+k+1)letn_groupssr=(Array.lengthsr-2)lsr1(* Groups are numbered 1 .. n_groups *)externalre_string_match:regexp->string->int->intarray="re_string_match"externalre_partial_match:regexp->string->int->intarray="re_partial_match"externalre_search_forward:regexp->string->int->intarray="re_search_forward"externalre_search_backward:regexp->string->int->intarray="re_search_backward"externalre_replacement_text:string->intarray->string->string="re_replacement_text"letregexps=letre=Str.regexpsinif!Debug.enablethendlogr(fun()->sprintf"regexp: %s"s);reletregexp_case_folds=letre=Str.regexp_case_foldsinif!Debug.enablethendlogr(fun()->sprintf"regexp_case_fold: %s"s);reletquotes=lets'=Str.quotesinif!Debug.enablethendlogr(fun()->sprintf"quote: orig: %s - quoted: %s"ss');s'letregexp_string=Str.regexp_stringletregexp_string_case_fold=Str.regexp_string_case_foldletreturn_resultpossr={pos=pos;sr=sr}letstring_matchpatspos=letsr=re_string_matchpatsposinifArray.lengthsr>0thenSome(return_resultpossr)elseNoneletbytes_matchpatspos=string_matchpat(Bytes.unsafe_to_strings)posletsearch_forwardpatspos=letsr=re_search_forwardpatsposinifArray.lengthsr=0thenraiseNot_found;sr.(0),return_resultpossrletsearch_forward_bytespatspos=search_forwardpat(Bytes.unsafe_to_strings)posletsearch_backwardpatspos=letsr=re_search_backwardpatsposinifArray.lengthsr=0thenraiseNot_found;sr.(0),return_resultpossrletsearch_backward_bytespatspos=search_backwardpat(Bytes.unsafe_to_strings)posletmatched_genericsubresults=ifmatch_begresult.sr<0||match_eresult.sr<0thenraiseNot_found;subs(match_begresult.sr)(match_eresult.sr-match_begresult.sr)letmatched_string=matched_genericString.subletmatched_bytes=matched_genericBytes.subletmatch_beginningresult=ifmatch_begresult.sr<0thenraiseNot_found;match_begresult.srletmatch_endresult=ifmatch_eresult.sr<0thenraiseNot_found;match_eresult.srletmatched_group_genericsubresultns=ifn<0||n>n_groupsresult.srthenraiseNot_found;ifn=0thenmatched_genericsubresultselseletgbeg=group_begresult.srninletgend=group_eresult.srninifgbeg<0||gend<0thenraiseNot_found;subsgbeg(gend-gbeg)letmatched_group=matched_group_genericString.subletmatched_group_bytes=matched_group_genericBytes.subletgroup_beginningresultn=ifn<0||n>n_groupsresult.srthenraiseNot_found;ifn=0thenmatch_beginningresultelseletgbeg=group_begresult.srninifgbeg<0thenraiseNot_foundelsegbegletgroup_endresultn=ifn<0||n>n_groupsresult.srthenraiseNot_found;ifn=0thenmatch_eresult.srelseletgend=group_eresult.srninifgend<0thenraiseNot_foundelsegendletsubstitute_firstpatsubsts=tryletpos,m=search_forwardpats0inString.concat""[Str.string_beforespos;substms;Str.string_afters(match_endm)]withNot_found->sexceptionContofintletglobal_substitutepatsubsts=letl=String.lengthsinletb=Buffer.create(l/2)inletrecloopk=tryifk<=lthen(letpos,m=search_forwardpatskin(* or Not_found *)Buffer.add_stringb(String.subsk(pos-k));letrepl=substmsinBuffer.add_stringbrepl;letpos'=match_endminifpos=pos'then(ifpos<lthenBuffer.add_charbs.[pos];raise(Cont(pos'+1)))elseraise(Contpos'))with|Contk_next->loopk_next|Not_found->Buffer.add_stringb(String.subsk(l-k))inloop0;Buffer.contentsbletreplace_matchedreplms=re_replacement_textreplm.srsletglobal_replacepatrepls=global_substitutepat(replace_matchedrepl)sletreplace_firstpatrepls=substitute_firstpat(replace_matchedrepl)s(* The splitting functions are practically copied from str.ml *)letopt_search_forwardrespos=trySome(search_forwardrespos)withNot_found->Noneletopt_search_forward_progressexprtextstart=matchopt_search_forwardexprtextstartwith|None->None|Some(pos,m)->ifmatch_endm>startthenSome(pos,m)elseifstart<String.lengthtextthenopt_search_forwardexprtext(start+1)elseNoneletbounded_splitexprtextnum=letstart=matchstring_matchexprtext0with|Somem->match_endm|None->0inletrecsplitaccustartn=ifstart>=String.lengthtextthenaccuelseifn=1thenStr.string_aftertextstart::accuelsematchopt_search_forward_progressexprtextstartwith|None->Str.string_aftertextstart::accu|Some(pos,m)->split(String.subtextstart(pos-start)::accu)(match_endm)(n-1)inList.rev(split[]startnum)letsplitexprtext=bounded_splitexprtext0letbounded_split_delimexprtextnum=letrecsplitaccustartn=ifstart>String.lengthtextthenaccuelseifn=1thenStr.string_aftertextstart::accuelsematchopt_search_forward_progressexprtextstartwith|None->Str.string_aftertextstart::accu|Some(pos,m)->split(String.subtextstart(pos-start)::accu)(match_endm)(n-1)iniftext=""then[]elseList.rev(split[]0num)letsplit_delimexprtext=bounded_split_delimexprtext0letbounded_full_splitexprtextnum=letrecsplitaccustartn=ifstart>=String.lengthtextthenaccuelseifn=1thenText(Str.string_aftertextstart)::accuelsematchopt_search_forward_progressexprtextstartwith|None->Text(Str.string_aftertextstart)::accu|Some(pos,m)->lets=matched_stringmtextinifpos>startthensplit(Delim(s)::Text(String.subtextstart(pos-start))::accu)(match_endm)(n-1)elsesplit(Delim(s)::accu)(match_endm)(n-1)inList.rev(split[]0num)letfull_splitexprtext=bounded_full_splitexprtext0letstring_before=Str.string_before;;letstring_after=Str.string_after;;letfirst_chars=Str.first_chars;;letlast_chars=Str.last_chars;;#endif#ifndefHAVE_PCRE#ifndefENABLE_STR_EXTERNALS(**********************************************************************)(* DEFAULT *)(**********************************************************************)(* Alternate implementation without Pcre: Just use Str directly,
and ensure thread-safety with mutexes
*)letmutex=(!Netsys_oothr.provider)#create_mutex()letprotectfarg=Netsys_oothr.serializemutexfargtyperegexp=Str.regexp;;typesplit_result=Str.split_result=Textofstring|Delimofstring;;typeresult={pos:int;match_beg:int;match_end:int;group_beg:intarray;group_end:intarray;}letregexp=protect(funs->letre=Str.regexpsinif!Debug.enablethendlogr(fun()->sprintf"regexp: %s"s);re)letregexp_case_fold=protect(funs->letre=Str.regexp_case_foldsinif!Debug.enablethendlogr(fun()->sprintf"regexp_case_fold: %s"s);re)letquote=protect(funs->lets'=Str.quotesinif!Debug.enablethendlogr(fun()->sprintf"quote: orig: %s - quoted: %s"ss');s')letregexp_string=protectStr.regexp_stringletregexp_string_case_fold=protectStr.regexp_string_case_foldletn_groups=9letreturn_resultpos=letr={pos=pos;match_beg=(tryStr.match_beginning()withNot_found->-1);match_end=(tryStr.match_end()withNot_found->-1);group_beg=Array.maken_groups(-1);group_end=Array.maken_groups(-1);}inforg=0ton_groups-1dor.group_beg.(g)<-(tryStr.group_beginning(g+1)withNot_found|Invalid_argument_->-1);r.group_end.(g)<-(tryStr.group_end(g+1)withNot_found|Invalid_argument_->-1);done;rletstring_matchpats=protect(funpos->ifStr.string_matchpatsposthenSome(return_resultpos)elseNone)letbytes_matchpatspos=string_matchpat(Bytes.unsafe_to_strings)posletsearch_forwardpats=protect(funpos->leti=Str.search_forwardpatsposini,return_resultpos)letsearch_forward_bytespatspos=search_forwardpat(Bytes.unsafe_to_strings)posletsearch_backwardpats=protect(funpos->leti=Str.search_backwardpatsposini,return_resultpos)letsearch_backward_bytespatspos=search_backwardpat(Bytes.unsafe_to_strings)posletmatched_genericsubresults=ifresult.match_beg<0||result.match_end<0thenraiseNot_found;subsresult.match_beg(result.match_end-result.match_beg)letmatched_string=matched_genericString.subletmatched_bytes=matched_genericBytes.subletmatch_beginningresult=ifresult.match_beg<0thenraiseNot_found;result.match_begletmatch_endresult=ifresult.match_end<0thenraiseNot_found;result.match_endletmatched_group_genericsubresultns=ifn<0||n>=Array.lengthresult.group_begthenraiseNot_found;ifn=0thenmatched_genericsubresultselseletgbeg=result.group_beg.(n-1)inletgend=result.group_end.(n-1)inifgbeg<0||gend<0thenraiseNot_found;subsgbeg(gend-gbeg)letmatched_group=matched_group_genericString.subletmatched_group_bytes=matched_group_genericBytes.subletgroup_beginningresultn=ifn<0||n>=Array.lengthresult.group_begthenraiseNot_found;ifn=0thenmatch_beginningresultelseletgbeg=result.group_beg.(n-1)inifgbeg<0thenraiseNot_foundelsegbegletgroup_endresultn=ifn<0||n>=Array.lengthresult.group_endthenraiseNot_found;ifn=0thenmatch_endresultelseletgend=result.group_end.(n-1)inifgend<0thenraiseNot_foundelsegendletglobal_replacepattempl=protect(funs->Str.global_replacepattempls)letreplace_firstpattempl=protect(funs->Str.replace_firstpattempls)letglobal_substitutepatsubst=protect(funs->letxsubsts=letr=return_result0insubstrsinStr.global_substitutepatxsubsts)letsubstitute_firstpatsubst=protect(funs->letxsubsts=letr=return_result0insubstrsinStr.substitute_firstpatxsubsts)letsplitsep=protect(funs->Str.splitseps)letbounded_splitseps=protect(funmax->Str.bounded_splitsepsmax)letsplit_delimsep=protect(funs->Str.split_delimseps)letbounded_split_delimseps=protect(funmax->Str.bounded_split_delimsepsmax)letfull_splitsep=protect(funs->Str.full_splitseps)letbounded_full_splitseps=protect(funmax->Str.bounded_full_splitsepsmax)letstring_before=Str.string_before;;letstring_after=Str.string_after;;letfirst_chars=Str.first_chars;;letlast_chars=Str.last_chars;;#endif#endif