.. _gm2-libs-iso-strings: gm2-libs-iso/Strings ^^^^^^^^^^^^^^^^^^^^ .. code-block:: modula2 DEFINITION MODULE Strings; (* Facilities for manipulating strings *) TYPE String1 (type) String1 = ARRAY [0..0] OF CHAR; (* String1 is provided for constructing a value of a single-character string type from a single character value in order to pass CHAR values to ARRAY OF CHAR parameters. *) Length PROCEDURE Length (stringVal: ARRAY OF CHAR): CARDINAL; (* Returns the length of stringVal (the same value as would be returned by the pervasive function LENGTH). *) (* The following seven procedures construct a string value, and attempt to assign it to a variable parameter. They all have the property that if the length of the constructed string value exceeds the capacity of the variable parameter, a truncated value is assigned, while if the length of the constructed string value is less than the capacity of the variable parameter, a string terminator is appended before assignment is performed. *) Assign PROCEDURE Assign (source: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR); (* Copies source to destination *) Extract PROCEDURE Extract (source: ARRAY OF CHAR; startIndex, numberToExtract: CARDINAL; VAR destination: ARRAY OF CHAR); (* Copies at most numberToExtract characters from source to destination, starting at position startIndex in source. *) Delete PROCEDURE Delete (VAR stringVar: ARRAY OF CHAR; startIndex, numberToDelete: CARDINAL); (* Deletes at most numberToDelete characters from stringVar, starting at position startIndex. *) Insert PROCEDURE Insert (source: ARRAY OF CHAR; startIndex: CARDINAL; VAR destination: ARRAY OF CHAR); (* Inserts source into destination at position startIndex *) Replace PROCEDURE Replace (source: ARRAY OF CHAR; startIndex: CARDINAL; VAR destination: ARRAY OF CHAR); (* Copies source into destination, starting at position startIndex. Copying stops when all of source has been copied, or when the last character of the string value in destination has been replaced. *) Append PROCEDURE Append (source: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR); (* Appends source to destination. *) Concat PROCEDURE Concat (source1, source2: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR); (* Concatenates source2 onto source1 and copies the result into destination. *) (* The following predicates provide for pre-testing of the operation-completion conditions for the procedures above. *) CanAssignAll PROCEDURE CanAssignAll (sourceLength: CARDINAL; VAR destination: ARRAY OF CHAR): BOOLEAN; (* Returns TRUE if a number of characters, indicated by sourceLength, will fit into destination; otherwise returns FALSE. *) CanExtractAll PROCEDURE CanExtractAll (sourceLength, startIndex, numberToExtract: CARDINAL; VAR destination: ARRAY OF CHAR): BOOLEAN; (* Returns TRUE if there are numberToExtract characters starting at startIndex and within the sourceLength of some string, and if the capacity of destination is sufficient to hold numberToExtract characters; otherwise returns FALSE. *) CanDeleteAll PROCEDURE CanDeleteAll (stringLength, startIndex, numberToDelete: CARDINAL): BOOLEAN; (* Returns TRUE if there are numberToDelete characters starting at startIndex and within the stringLength of some string; otherwise returns FALSE. *) CanInsertAll PROCEDURE CanInsertAll (sourceLength, startIndex: CARDINAL; VAR destination: ARRAY OF CHAR): BOOLEAN; (* Returns TRUE if there is room for the insertion of sourceLength characters from some string into destination starting at startIndex; otherwise returns FALSE. *) CanReplaceAll PROCEDURE CanReplaceAll (sourceLength, startIndex: CARDINAL; VAR destination: ARRAY OF CHAR): BOOLEAN; (* Returns TRUE if there is room for the replacement of sourceLength characters in destination starting at startIndex; otherwise returns FALSE. *) CanAppendAll PROCEDURE CanAppendAll (sourceLength: CARDINAL; VAR destination: ARRAY OF CHAR): BOOLEAN; (* Returns TRUE if there is sufficient room in destination to append a string of length sourceLength to the string in destination; otherwise returns FALSE. *) CanConcatAll PROCEDURE CanConcatAll (source1Length, source2Length: CARDINAL; VAR destination: ARRAY OF CHAR): BOOLEAN; (* Returns TRUE if there is sufficient room in destination for a two strings of lengths source1Length and source2Length; otherwise returns FALSE. *) (* The following type and procedures provide for the comparison of string values, and for the location of substrings within strings. *) TYPE CompareResults (type) CompareResults = (less, equal, greater); Compare PROCEDURE Compare (stringVal1, stringVal2: ARRAY OF CHAR): CompareResults; (* Returns less, equal, or greater, according as stringVal1 is lexically less than, equal to, or greater than stringVal2. *) Equal PROCEDURE Equal (stringVal1, stringVal2: ARRAY OF CHAR): BOOLEAN; (* Returns Strings.Compare(stringVal1, stringVal2) = Strings.equal *) FindNext PROCEDURE FindNext (pattern, stringToSearch: ARRAY OF CHAR; startIndex: CARDINAL; VAR patternFound: BOOLEAN; VAR posOfPattern: CARDINAL); (* Looks forward for next occurrence of pattern in stringToSearch, starting the search at position startIndex. If startIndex < LENGTH(stringToSearch) and pattern is found, patternFound is returned as TRUE, and posOfPattern contains the start position in stringToSearch of pattern. Otherwise patternFound is returned as FALSE, and posOfPattern is unchanged. *) FindPrev PROCEDURE FindPrev (pattern, stringToSearch: ARRAY OF CHAR; startIndex: CARDINAL; VAR patternFound: BOOLEAN; VAR posOfPattern: CARDINAL); (* Looks backward for the previous occurrence of pattern in stringToSearch and returns the position of the first character of the pattern if found. The search for the pattern begins at startIndex. If pattern is found, patternFound is returned as TRUE, and posOfPattern contains the start position in stringToSearch of pattern in the range [0..startIndex]. Otherwise patternFound is returned as FALSE, and posOfPattern is unchanged. *) FindDiff PROCEDURE FindDiff (stringVal1, stringVal2: ARRAY OF CHAR; VAR differenceFound: BOOLEAN; VAR posOfDifference: CARDINAL); (* Compares the string values in stringVal1 and stringVal2 for differences. If they are equal, differenceFound is returned as FALSE, and TRUE otherwise. If differenceFound is TRUE, posOfDifference is set to the position of the first difference; otherwise posOfDifference is unchanged. *) Capitalize PROCEDURE Capitalize (VAR stringVar: ARRAY OF CHAR); (* Applies the function CAP to each character of the string value in stringVar. *) END Strings.