Debian Codegraph @

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

raw
1 unit strings; { String handling primitives. }
2 { These should be recoded in C instead of using the p2c code. }
3
4 interface
5
6 procedure scan1 (s: string; p: integer; var n: integer);
7 { Read an integer at position p of s }
8 function startsWith (s1, s2: string): boolean;
9 function pos1(c: char; s: string): integer;
10 function posNot (c: char; var s: string): integer;
11 procedure insertChar (c: char; var s: string; p: integer);
12 function substr (var s: string; start, count: integer): string;
13 procedure getNum(line: string; var k: integer);
14 procedure getTwoNums(var line: string; var k1, k2: integer);
15 procedure toUpper(var s: string);
16 procedure delete1 (var s: string; p: integer);
17 procedure predelete (var s: string; l: integer);
18 procedure shorten (var s: string; new_length: integer);
19 function nextWordBound(s: string; trigger: char; p: integer): integer;
20 { find end of first word starting with trigger after s[p] }
21
22 implementation
23
24 procedure scan1 (s: string; p: integer; var n: integer);
25 begin if length(s)<p then
26 writeln('scan1: string too short');
27 predelete(s,p-1); getNum(s,n);
28 end;
29
30 function pos1(c: char; s: string): integer;
31 begin pos1:= pos(c, s); end;
32
33 procedure delete1 (var s: string; p: integer);
34 begin if length(s)<p then
35 writeln('delete1: string too short');
36 delete(s,p,1);
37 end;
38
39 procedure predelete (var s: string; l: integer);
40 begin if length(s)<l then
41 writeln('predelete: string too short');
42 delete(s,1,l);
43 end;
44
45 procedure shorten (var s: string; new_length: integer);
46 begin if length(s)<new_length then
47 writeln('shorten: string too short');
48 s[0] := char(new_length);
49 end;
50
51 function posNot(c: char; var s: string): integer;
52 var i, l: integer;
53 begin l:=length(s);
54 for i:=1 to l do if s[i]<>c then
55 begin posnot:=i; exit; end;
56 posnot:=0;
57 end;
58
59 procedure strval(line: string; var num, p: integer);
60 begin val(line,num,p); end;
61
62 procedure getNum(line: string; var k: integer);
63 var code: integer;
64 begin strval(line,k,code);
65 if code>0 then strval(copy(line,1,code-1),k,code);
66 end;
67
68 procedure getTwoNums(var line: string; var k1, k2: integer);
69 var num, code, p: integer;
70 begin
71 strval(line,num,p); strval(copy(line,1,p-1),k1,code);
72 strval(copy(line,p+1,255),k2,code);
73 if code>0 then strval(copy(line,1,code-1),k2,code);
74 end;
75
76 procedure toUpper (var s: string);
77 var i: integer;
78 begin
79 for i:=1 to length(s) do s[i]:=upcase(s[i]);
80 end;
81
82 function startsWith (s1, s2: string): boolean;
83 var i, l1, l2: integer;
84 begin l1:=length(s1); l2:=length(s2);
85 startsWith := false; if l1<l2 then exit;
86 for i:=1 to l2 do if s1[i]<>s2[i] then exit;
87 startsWith := true;
88 end;
89
90 procedure insertChar (c: char; var s: string; p: integer);
91 begin if length(s)<p-1 then
92 writeln('insertchar: string too short');
93 insert(c,s,p);
94 end;
95
96 function substr (var s: string; start, count: integer): string;
97 begin if length(s)<start+count-1 then
98 writeln('insertchar: string too short');
99 substr := copy(s,start,count);
100 end;
101
102 function nextWordBound(s: string; trigger: char; p: integer): integer;
103 begin
104 repeat p:=p+1 until (p>length(s)) or (s[p]=trigger);
105 while (p<length(s)) and (s[p+1]<>' ') do p:=p+1;
106 nextWordBound:=p;
107 end;
108
109 end.