source: trunk/minix/lib/ack/libm2/Strings.mod@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 4.3 KB
Line 
1(*
2 (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 See the copyright notice in the ACK home directory, in the file "Copyright".
4*)
5
6(*$R-*)
7IMPLEMENTATION MODULE Strings;
8(*
9 Module: String manipulations
10 Author: Ceriel J.H. Jacobs
11 Version: $Header: /cvsup/minix/src/lib/ack/libm2/Strings.mod,v 1.1 2005/10/10 15:27:46 beng Exp $
12*)
13
14 PROCEDURE Assign(source: ARRAY OF CHAR; VAR dest: ARRAY OF CHAR);
15 (* Assign string source to dest
16 *)
17 VAR i: CARDINAL;
18 max: CARDINAL;
19 BEGIN
20 max := HIGH(source);
21 IF HIGH(dest) < max THEN max := HIGH(dest); END;
22 i := 0;
23 WHILE (i <= max) AND (source[i] # 0C) DO
24 dest[i] := source[i];
25 INC(i);
26 END;
27 IF i < HIGH(dest) THEN dest[i] := 0C; END;
28 END Assign;
29
30 PROCEDURE Insert(substr: ARRAY OF CHAR; VAR str: ARRAY OF CHAR; inx: CARDINAL);
31 (* Insert the string substr into str, starting at str[inx].
32 If inx is equal to or greater than Length(str) then substr is appended
33 to the end of str.
34 *)
35 VAR sublen, length, i: CARDINAL;
36 BEGIN
37 sublen := Length(substr);
38 IF sublen = 0 THEN RETURN; END;
39 length := Length(str);
40 IF inx > length THEN inx := length; END;
41 i := length;
42 IF i + sublen - 1 > HIGH(str) THEN i := HIGH(str); END;
43 WHILE i > inx DO
44 str[i+sublen-1] := str[i-1];
45 DEC(i);
46 END;
47 FOR i := 0 TO sublen - 1 DO
48 IF i + inx <= HIGH(str) THEN
49 str[i + inx] := substr[i];
50 ELSE
51 RETURN;
52 END;
53 END;
54 IF length + sublen <= HIGH(str) THEN
55 str[length + sublen] := 0C;
56 END;
57 END Insert;
58
59 PROCEDURE Delete(VAR str: ARRAY OF CHAR; inx, len: CARDINAL);
60 (* Delete len characters from str, starting at str[inx].
61 If inx >= Length(str) then nothing happens.
62 If there are not len characters to delete, characters to the end of the
63 string are deleted.
64 *)
65 VAR length: CARDINAL;
66 BEGIN
67 IF len = 0 THEN RETURN; END;
68 length := Length(str);
69 IF inx >= length THEN RETURN; END;
70 WHILE inx + len < length DO
71 str[inx] := str[inx + len];
72 INC(inx);
73 END;
74 str[inx] := 0C;
75 END Delete;
76
77 PROCEDURE Pos(substr, str: ARRAY OF CHAR): CARDINAL;
78 (* Return the index into str of the first occurrence of substr.
79 Pos returns a value greater than HIGH(str) of no occurrence is found.
80 *)
81 VAR i, j, max, subl: CARDINAL;
82 BEGIN
83 max := Length(str);
84 subl := Length(substr);
85 IF subl > max THEN RETURN HIGH(str) + 1; END;
86 IF subl = 0 THEN RETURN 0; END;
87 max := max - subl;
88 FOR i := 0 TO max DO
89 j := 0;
90 WHILE (j <= subl-1) AND (str[i+j] = substr[j]) DO
91 INC(j);
92 END;
93 IF j = subl THEN RETURN i; END;
94 END;
95 RETURN HIGH(str) + 1;
96 END Pos;
97
98 PROCEDURE Copy(str: ARRAY OF CHAR;
99 inx, len: CARDINAL;
100 VAR result: ARRAY OF CHAR);
101 (* Copy at most len characters from str into result, starting at str[inx].
102 *)
103 VAR i: CARDINAL;
104 BEGIN
105 IF Length(str) <= inx THEN RETURN END;
106 i := 0;
107 LOOP
108 IF i > HIGH(result) THEN RETURN; END;
109 IF len = 0 THEN EXIT; END;
110 IF inx > HIGH(str) THEN EXIT; END;
111 result[i] := str[inx];
112 INC(i); INC(inx); DEC(len);
113 END;
114 IF i <= HIGH(result) THEN result[i] := 0C; END;
115 END Copy;
116
117 PROCEDURE Concat(s1, s2: ARRAY OF CHAR; VAR result: ARRAY OF CHAR);
118 (* Concatenate two strings.
119 *)
120 VAR i, j: CARDINAL;
121 BEGIN
122 i := 0;
123 WHILE (i <= HIGH(s1)) AND (s1[i] # 0C) DO
124 IF i > HIGH(result) THEN RETURN END;
125 result[i] := s1[i];
126 INC(i);
127 END;
128 j := 0;
129 WHILE (j <= HIGH(s2)) AND (s2[j] # 0C) DO
130 IF i > HIGH(result) THEN RETURN END;
131 result[i] := s2[j];
132 INC(i);
133 INC(j);
134 END;
135 IF i <= HIGH(result) THEN result[i] := 0C; END;
136 END Concat;
137
138 PROCEDURE Length(str: ARRAY OF CHAR): CARDINAL;
139 (* Return number of characters in str.
140 *)
141 VAR i: CARDINAL;
142 BEGIN
143 i := 0;
144 WHILE (i <= HIGH(str)) DO
145 IF str[i] = 0C THEN RETURN i; END;
146 INC(i);
147 END;
148 RETURN i;
149 END Length;
150
151 PROCEDURE CompareStr(s1, s2: ARRAY OF CHAR): INTEGER;
152 (* Compare two strings, return -1 if s1 < s2, 0 if s1 = s2, and 1 if s1 > s2.
153 *)
154 VAR i: CARDINAL;
155 max: CARDINAL;
156 BEGIN
157 max := HIGH(s1);
158 IF HIGH(s2) < max THEN max := HIGH(s2); END;
159 i := 0;
160 WHILE (i <= max) DO
161 IF s1[i] < s2[i] THEN RETURN -1; END;
162 IF s1[i] > s2[i] THEN RETURN 1; END;
163 IF s1[i] = 0C THEN RETURN 0; END;
164 INC(i);
165 END;
166 IF (i <= HIGH(s1)) AND (s1[i] # 0C) THEN RETURN 1; END;
167 IF (i <= HIGH(s2)) AND (s2[i] # 0C) THEN RETURN -1; END;
168 RETURN 0;
169 END CompareStr;
170
171END Strings.
Note: See TracBrowser for help on using the repository browser.