Debian Codegraph @

utils/m-tx/mtx-src/utility.pas

raw
1 unit utility; { DPL 2004-03-22 }
2 { uninitialized variable correction in function grep; RDT 2022-07-27 }
3
4 { Utilities, mainly aids to parsing }
5
6 interface
7
8 function equalsIgnoreCase(s1, s2: string): boolean;
9 function startsWithIgnoreCase(s1, s2: string): boolean;
10 function endsWith(s1, s2: string): boolean;
11 function startsWithBracedWord (P: string): boolean;
12 function GetNextWord (var s: string; Delim, Term: char): string;
13 function NextWord (s: string; Delim, Term: char): string;
14 function wordCount (s: string): integer;
15 function plural (n: integer): string;
16 function curtail (var s: string; c: char): integer;
17 { Remove last character if it equals c and return its position;
18 otherwise return 0 }
19 function toString(n: integer): string;
20 procedure trim(var s: string);
21 function digit(c: char): integer;
22 function match(source, pattern: string): boolean;
23 function translate(source, pattern, target: string): string;
24 procedure grep(var source, pattern, target: string);
25 { See Implementation for what this currently does. }
26
27 implementation uses strings;
28
29 const blank = ' '; dummy = #0;
30
31 function wordCount (s: string): integer;
32 var i, n: integer;
33 begin if length(s)=0 then
34 begin wordCount:=0; exit; end;
35 if s[1]=blank then n:=0 else n:=1;
36 for i:=1 to length(s)-1 do
37 if (s[i]=blank) and (s[i+1]<>blank) then inc(n);
38 wordCount:=n;
39 end;
40
41 function GetNextWord (var s: string; Delim, Term: char): string;
42 { A delimiter is a character that separates words, but forms no part
43 of them. A terminator is a character that appears at the end of
44 a word. }
45 var n, start, last: integer;
46 begin last:=length(s); n:=1;
47 while (n<=last) and (s[n] = Delim) do inc(n);
48 start:=n;
49 while (n<=last) and not (s[n] in [Delim,Term]) do inc(n);
50 if (n<=last) and (s[n] = Term) then inc(n);
51 GetNextWord:=substr(s,start,n-start);
52 while (n<=last) and (s[n] = Delim) do inc(n);
53 predelete(s,n-1);
54 end;
55
56 function NextWord (s: string; Delim, Term: char): string;
57 begin NextWord:=GetNextWord(s,Delim,Term); end;
58
59 function plural (n: integer): string;
60 begin if n=1 then plural:='' else plural:='s'; end;
61
62 function curtail (var s: string; c: char): integer;
63 var l: integer;
64 begin l:=length(s); curtail:=0;
65 if s[l]=c then begin shorten(s,l-1); curtail:=l; end;
66 end;
67
68 function toString(n: integer): string;
69 var s: string;
70 begin str(n,s); toString:=s; end;
71
72 function digit(c: char): integer;
73 begin digit:=ord(c)-ord('0'); end;
74
75 function equalsIgnoreCase(s1, s2: string): boolean;
76 begin toUpper(s1); toUpper(s2); equalsIgnoreCase:=s1=s2;
77 end;
78
79 function startsWithIgnoreCase(s1, s2: string): boolean;
80 begin toUpper(s1); toUpper(s2); startsWithIgnoreCase:=startsWith(s1,s2);
81 end;
82
83 function startsWithBracedWord (P: string): boolean;
84 var w: string;
85 begin w := getNextWord(P,blank,dummy);
86 startsWithBracedWord := (w[1]='{') and (w[length(w)]='}');
87 end;
88
89 procedure trim(var s: string);
90 var k: integer;
91 begin k:=posnot(blank,s);
92 if k>1 then predelete(s,k-1) else if k=0 then s:='';
93 end;
94
95 function endsWith(s1, s2: string): boolean;
96 var l1, l2: integer;
97 begin l1:=length(s1); l2:=length(s2);
98 if l1<l2 then begin endsWith:=false; exit end;
99 predelete(s1,l1-l2); endsWith:=s1=s2
100 end;
101
102 {--- Match/Replace package --- }
103
104 { Search and replace. Stops when pattern no longer matches source.
105 Pattern wildcards:
106 ? Any single character
107 * Any string
108 # An unsigned integer
109 ## A signed integer
110 ### A signed number maybe with a decimal part
111 Pattern metacharacters:
112 \x where x is any character, stands for that character
113 Target wildcards
114 \0 to \9 Value of corresponding source wildcard
115 Target metacharacters
116 \ When not followed by 0..9 or \, stands for itself
117 \\ Backslash
118 }
119 procedure grep(var source, pattern, target: string);
120 var p1, p2: array[0..9] of integer;
121 i, j, p, s, t, index, reg: integer;
122 product: string;
123 trigger, matching: boolean;
124 procedure remember(s1, s2: integer);
125 begin if index>9 then halt(9999);
126 p1[index] := s1; p2[index] := s2; s := s2+1; inc(index)
127 end;
128 procedure matchnum;
129 var allowsign, allowpoint, quit: boolean;
130 s0: integer;
131 begin allowsign := false; allowpoint := false; matching := false; s0 := s;
132 if p<length(pattern) then if pattern[p+1]='#' then
133 begin inc(p); allowsign := true;
134 if p<length(pattern) then if pattern[p+1]='#' then
135 begin inc(p); allowpoint := true end
136 end;
137 if allowsign and ((source[s]='-') or (source[s]='+')) then
138 begin inc(s); if s>length(source) then exit end;
139 quit := false;
140 while (not quit) and (s<=length(source)) do
141 begin if source[s]='.' then if not allowpoint then quit := true else
142 begin inc(s); allowpoint := false end;
143 if (source[i]>='0') and (source[i]<='9') then
144 begin inc(s); matching := true end
145 else quit := true
146 end;
147 if matching then remember(s0,s-1)
148 end;
149 procedure matchmeta;
150 begin if p<length(pattern) then inc(p);
151 if source[s]=pattern[p] then begin inc(s); inc(p) end
152 else matching := false
153 end;
154 procedure subgrep;
155 begin matching := true;
156 if pattern[p]='*' then begin remember(s,length(source)); inc(p) end
157 else if pattern[p]='?' then begin remember(s,s); inc(p) end
158 else if pattern[p]='#' then matchnum
159 else if pattern[p]='\' then matchmeta
160 else if source[s]=pattern[p] then begin inc(s); inc(p) end
161 else matching := false
162 end;
163 begin
164 index := 0; s := 1; p := 1;
165 for i:=0 to 9 do begin p1[i]:=1; p2[i]:=0 end;
166 matching := true; { added by RDT 2022-07-27 }
167 while matching and (p<=length(pattern)) and (s<=length(source)) do subgrep;
168 product := ''; trigger := false;
169 for t:=1 to length(target) do if trigger then
170 begin reg := digit(target[t]); if (reg>=0) and (reg<=9) then
171 for j:=p1[reg] to p2[reg] do product := product + source[j]
172 else if target[t]='\' then product := product + '\'
173 else product := product + '\' + target[t];
174 trigger := false
175 end
176 else if (target[t]='\') and (t<length(target)) then trigger := true
177 else product := product + target[t];
178 source := substr(source,s,length(source));
179 pattern := substr(pattern,p,length(pattern));
180 target := product
181 end;
182
183
184 { "match" tests whether the source matches the pattern exactly }
185 function match(source, pattern: string): boolean;
186 const target: string = '';
187 begin grep(source, pattern, target);
188 match := (source='') and (pattern='')
189 end;
190
191 { "translate" replaces the pattern by the target in the source. }
192 function translate(source, pattern, target: string): string;
193 begin grep(source, pattern, target); translate := target;
194 end;
195
196 end.