(**** * * Auxillary functions for working with srings. The exported functions are * IndexOf and Reverse. See their descriptions for details. * * The non-exported functions are helpers used by the exported functions. * *) module Strings; export IndexOf, Reverse; function IndexOf(base_string:string, search_string:string) -> integer = ( if search_string = "" or base_string = "" then 0 else IndexOf1(base_string, search_string, 1); ) description: (* Return the first position of search_string within base_string. If search_string appears nowhere in base_string, return 0. Also return 0 if base_string and/or search_string is the empty string. *); end IndexOf; function IndexOf1(base_string:string, search_string:string, pos:integer) -> integer = ( if pos > #base_string then 0 else if base_string[pos .. pos + #search_string - 1] = search_string then pos else IndexOf1(base_string, search_string, pos+1) ) description: (* Work doer for IndexOf. *); end IndexOf1; function Reverse(s:string) -> string = ( Reverse1(s, "") ) description: (* Return the characterwise reversal of the given string. *); end Reverse; function Reverse1(s:string, rs:string) -> string = ( if s = "" then rs else Reverse1(s[1 .. #s-1], rs + s[#s]) ) description: (* Work doer for Reverse. *); end Reverse1; end Strings;