STDSTRIN.DEF 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. (* ========================================================= *)
  2. (* Preliminary library module for Gardens Point Modula *)
  3. (* in this implementation the string terminator is Ascii.nul *)
  4. (* ========================================================= *)
  5. (* !LIBRARY! *) DEFINITION MODULE StdStrings;
  6. TYPE String1 = ARRAY [0 .. 0] OF CHAR;
  7. (* allows single char variables to be used as strings *)
  8. CONST StringCapacity = 256;
  9. TYPE String = ARRAY [0 .. StringCapacity - 1] OF CHAR;
  10. (* a conveniently sized array for general purposes *)
  11. PROCEDURE Length(stringValue : ARRAY OF CHAR) : CARDINAL;
  12. (* number of character in string, not including nul *)
  13. PROCEDURE CanAssignAll
  14. ( sourceLength : CARDINAL;
  15. VAR destination : ARRAY OF CHAR) : BOOLEAN;
  16. (* Returns sourceLength <= HIGH(destination) + 1 *)
  17. PROCEDURE Assign ( source : ARRAY OF CHAR;
  18. VAR destination : ARRAY OF CHAR);
  19. (* Source is copied to destination. If shorter, string
  20. is nul terminated, if too long it is truncated *)
  21. PROCEDURE CanExtractAll
  22. ( sourceLength : CARDINAL;
  23. startIndex : CARDINAL;
  24. numberToExtract : CARDINAL;
  25. VAR destination : ARRAY OF CHAR) : BOOLEAN;
  26. (* Returns (startIndex + numberToExtract <= sourceLength) AND
  27. (HIGH(destination) + 1 >= numberToExtract) *)
  28. PROCEDURE Extract ( source : ARRAY OF CHAR;
  29. startIndex : CARDINAL;
  30. numberToExtract : CARDINAL;
  31. VAR destination : ARRAY OF CHAR);
  32. (* Extracts numberToExtract characters starting at startIndex.
  33. If source string is too short less will be extracted, even
  34. zero characters if source does not extend past startIndex *)
  35. PROCEDURE CanDeleteAll
  36. ( stringLength : CARDINAL;
  37. startIndex : CARDINAL;
  38. numberToDelete : CARDINAL) : BOOLEAN;
  39. (* Returns (startIndex < stringLength) AND
  40. (startIndex + numberToDelete <= stringLength) *)
  41. PROCEDURE Delete (VAR string : ARRAY OF CHAR;
  42. startIndex : CARDINAL;
  43. numberToDelete : CARDINAL);
  44. (* Deletes numberToDelete characters starting at startIndex.
  45. If string is short then fewer may be deleted, even none *)
  46. PROCEDURE CanInsertAll
  47. ( sourceLength : CARDINAL;
  48. startIndex : CARDINAL;
  49. VAR destination : ARRAY OF CHAR) : BOOLEAN;
  50. (* Returns (startIndex < Length(destination)) AND
  51. (sourceLength + Length(destination) <= HIGH(destination) + 1) *)
  52. PROCEDURE Insert ( source : ARRAY OF CHAR;
  53. startIndex : CARDINAL;
  54. VAR destination : ARRAY OF CHAR);
  55. (* Inserts source string starting at startIndex. Resulting
  56. string is truncated if necessary to fit in destination array *)
  57. PROCEDURE CanReplaceAll
  58. ( sourceLength : CARDINAL;
  59. startIndex : CARDINAL;
  60. VAR destination : ARRAY OF CHAR) : BOOLEAN;
  61. (* Returns (sourceLength + startIndex <= Length(destination)) *)
  62. PROCEDURE Replace ( source : ARRAY OF CHAR;
  63. startIndex : CARDINAL;
  64. VAR destination : ARRAY OF CHAR);
  65. (* Replaces destination characters with those of source, starting
  66. at startIndex. Result string is always the same length as before *)
  67. PROCEDURE CanAppendAll
  68. ( sourceLength : CARDINAL;
  69. VAR destination : ARRAY OF CHAR) : BOOLEAN;
  70. (* Returns Length(destination) + sourceLength <= HIGH(destination) + 1 *)
  71. PROCEDURE Append ( source : ARRAY OF CHAR;
  72. VAR destination : ARRAY OF CHAR);
  73. (* Appends source string to destination. Resulting string is
  74. truncated if necessary, to fit into destination array *)
  75. PROCEDURE Capitalize
  76. (VAR stringVar : ARRAY OF CHAR);
  77. (* Applies the CAP function to each character in string *)
  78. TYPE CompareResult = (less, equal, greater);
  79. PROCEDURE Compare(stringVal1 : ARRAY OF CHAR;
  80. stringVal2 : ARRAY OF CHAR) : CompareResult;
  81. (* Compares according to the underlying collating sequence
  82. of the character set (Ascii in this case). Returns less
  83. if stringVal1 < stringVal2 and so on *)
  84. PROCEDURE FindNext ( pattern : ARRAY OF CHAR;
  85. stringValue : ARRAY OF CHAR;
  86. startIndex : CARDINAL;
  87. VAR patternFound : BOOLEAN;
  88. VAR posOfPattern : CARDINAL);
  89. (* Finds next occurrence of pattern in stringValue,
  90. starting at index greater than or equal to startIndex.
  91. If pattern is not found, posOfPattern is not changed *)
  92. PROCEDURE FindPrev ( pattern : ARRAY OF CHAR;
  93. stringValue : ARRAY OF CHAR;
  94. startIndex : CARDINAL;
  95. VAR patternFound : BOOLEAN;
  96. VAR PosOfPattern : CARDINAL);
  97. (* Finds previous occurrence of pattern in stringValue,
  98. starting at index greater than or equal to startIndex
  99. If pattern is not found, posOfPattern is not changed *)
  100. PROCEDURE FindDiff
  101. ( stringVal1 : ARRAY OF CHAR;
  102. stringVal2 : ARRAY OF CHAR;
  103. VAR differenceFound : BOOLEAN;
  104. VAR posOfDifference : CARDINAL);
  105. (* Finds first character index in which the strings differ *)
  106. END StdStrings.