123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196(**The String Manipulation Module of the B# STD*)(**A Generic function to extract two arguments and apply a function*)lettwo_argument_funcfunclist_of_arguments=ifList.lengthlist_of_arguments<2thenParser.Exception(newParser.arg("This function requires two arguments and you supplied "^string_of_int(List.lengthlist_of_arguments)^"arguments"))elselethead,tail=(List.hdlist_of_arguments,List.tllist_of_arguments)inlethead2=List.hdtailinfuncheadhead2(**A Generic function to extract three arguments and apply a function*)letthree_argument_funcfunclist_of_arguments=ifList.lengthlist_of_arguments<3thenParser.Exception(newParser.arg("This function requires three arguments and you supplied "^string_of_int(List.lengthlist_of_arguments)^"arguments"))elselethead,tail=(List.hdlist_of_arguments,List.tllist_of_arguments)inlethead2,tail2=(List.hdtail,List.tltail)inlethead3=List.hdtail2infuncheadhead2head3(**Takes an integer and a string and returns a string of n times the inputed one*)letproto_createns=match(n,s)with|Parser.Argument(Parser.In),Parser.Argument(Parser.Strs)->Parser.Argument(Parser.Str(Array.makens|>Array.to_list|>String.concat""))|_->Parser.Exception(newParser.type_error"arguments must be an integer and a string")(**Take three strings and returns a string where all occurence of s2 have been replaced by s3 in s1*)letproto_replaces1s2s3=match(s1,s2,s3)with|(Parser.Argument(Parser.Strs1),Parser.Argument(Parser.Strs2),Parser.Argument(Parser.Strs3))->letregex=Str.regexps2inlets=Str.global_replaceregexs3s1inParser.Argument(Parser.Strs)|_->Parser.Exception(newParser.type_error"arguments must be strings")(**Transform a string into a list of character (but in the type String)*)letexplodes=List.init(String.lengths)(funi->String.getsi|>Char.escaped)(**Takes a string and transform it into an array of char with explode*)lettransform_to_arraylist_of_arguments=letauxs1=matchs1with|Parser.Argument(Parser.Strs1)->explodes1|>List.mapParser.create_string_argument|>Array.of_list|>funa->Parser.TBLa|_->Parser.Exception(newParser.type_error"argument must be a string")inmatchlist_of_argumentswith|[]->Parser.Exception(newParser.arg"This function requires one argument and you supplied none")|t::_->auxt(**Extract the string from a parameter*)letextract_string_from_argparam=matchparamwith|Parser.Argument(Parser.Strs)->s|Parser.Argument(Parser.Ii)->string_of_inti|Parser.Argument(Parser.Dd)->string_of_floatd|Parser.Argument(Parser.Boolb)->string_of_boolb|_->""(**Transform an array into a string*)lettransform_from_arraylist_of_arguments=letauxarr=matcharrwith|Parser.TBLarr->Parser.Argument(Parser.Str(Array.mapextract_string_from_argarr|>Array.to_list|>String.concat""))|_->Parser.Exception(newParser.type_error"argument must be an array")inmatchlist_of_argumentswith|[]->Parser.Exception(newParser.arg"This function requires one argument and you supplied none")|t::_->auxt(**Takes two string and returns s1^s2*)letproto_concats1s2=match(s1,s2)with|Parser.Argument(Parser.Strs1),Parser.Argument(Parser.Strs2)->Parser.Argument(Parser.Str(s1^s2))|_->Parser.Exception(newParser.type_error"arguments must be strings")(**Takes an integer and a string and returns s.[n]*)letproto_accessns=match(n,s)with|Parser.Argument(Parser.In),Parser.Argument(Parser.Strs)->ifn>=String.lengthsthenParser.Exception(newParser.outofbound("The string is only of length"^string_of_int(String.lengths)))elseParser.Argument(Parser.Str(String.make1s.[n]))|_->Parser.Exception(newParser.type_error"arguments must be an integer and a string")(**Split s1 at every s2 occurences*)letproto_splits1s2=match(s1,s2)with|Parser.Argument(Parser.Strs1),Parser.Argument(Parser.Strs2)->letregex=Str.regexps2inletarr=Str.splitregexs1inletal=List.mapParser.create_string_argumentarrinParser.TBL(Array.of_listal)|_->Parser.Exception(newParser.type_error"arguments must be strings")(**Creates the function with the generic method and the proto one*)letreplace=three_argument_funcproto_replaceletconcat=two_argument_funcproto_concatletcreate=two_argument_funcproto_createletaccess=two_argument_funcproto_accessletsplit=two_argument_funcproto_split(**Conversion*)(**Converts an element to a string*)letconvert_to_stringlist_of_arguments=matchlist_of_argumentswith|[]->Parser.Exception(newParser.arg"this function requires an argument and you supplied none")|Parser.Argument(Parser.Ii)::_->Parser.create_string_argument(string_of_inti)|Parser.Argument(Parser.Dd)::_->Parser.create_string_argument(string_of_floatd)|Parser.Argument(Parser.Boolb)::_->Parser.create_string_argument(string_of_boolb)|_->Parser.Exception(newParser.type_error"the supplied types are not convertible to string")(**Converts a string to an integer*)letint_from_stringlist_of_arguments=matchlist_of_argumentswith|[]->Parser.Exception(newParser.arg"this function requires an argument and you supplied none")|Parser.Argument(Parser.Strs)::_->(tryParser.create_int_argument(int_of_strings)withFailure_->Parser.Exception(newParser.bag_exception"Error while converting"))|_->Parser.Exception(newParser.type_error"the argument must be a string")(**Converts a string to a float*)letdouble_from_stringlist_of_arguments=matchlist_of_argumentswith|[]->Parser.Exception(newParser.arg"this function requires an argument and you supplied none")|Parser.Argument(Parser.Strs)::_->(tryParser.create_float_argument(float_of_strings)withFailure_->Parser.Exception(newParser.bag_exception"Error while converting"))|_->Parser.Exception(newParser.type_error"the argument must be a string")(**Converts a string to a boolean*)letbool_from_stringlist_of_arguments=matchlist_of_argumentswith|[]->Parser.Exception(newParser.arg"this function requires an argument and you supplied none")|Parser.Argument(Parser.Strs)::_->(tryParser.create_bool_argument(bool_of_strings)withFailure_->Parser.Exception(newParser.bag_exception"Error while converting"))|_->Parser.Exception(newParser.type_error"the argument must be a string")