-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtinystring.pas
171 lines (154 loc) · 4.52 KB
/
dtinystring.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
unit dtinystring;
// tiny string utilities
// - rlyeh, public domain | wtrmrkrlyeh
// Ported to pascal by Doj
// @toadd: pad, left, right, center, triml, trimr, trim, [-1]
{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
interface
type
TArrayOfStrings = array of AnsiString;
function Tokenize(S: AnsiString; Delimiters: PAnsiChar): TArrayOfStrings;
function Split(S: AnsiString; Delimiters: PAnsiChar): TArrayOfStrings;
function LeftOf(const SubString, S: AnsiString): AnsiString;
function RightOf(const SubString, S: AnsiString): AnsiString;
function ReplaceOne(const S, Target, Replacement: AnsiString): AnsiString;
function ReplaceAll(const S, Target, Replacement: AnsiString): AnsiString;
implementation
function Tokenize(S: AnsiString; Delimiters: PAnsiChar): TArrayOfStrings;
var
map: array[AnsiChar] of AnsiChar;
Ch: AnsiChar;
begin
Result := nil; // make compiler happy
for Ch := Low(AnsiChar) to High(AnsiChar) do
map[Ch] := #0;
while delimiters^ <> #0 do begin
Inc(Delimiters);
map[delimiters[-1]] := #1;
end;
SetLength(Result, 1);
for Ch in S do begin
if map[Ch] = #0 then begin
Result[High(Result)] := Result[High(Result)] + Ch;
end else if Length(Result[High(Result)]) > 0 then begin
SetLength(Result, Length(Result) + 1);
end;
end;
while (Length(Result) > 0) and (Length(Result[High(Result)]) <= 0) do
SetLength(Result, Length(Result) - 1);
end;
function Split(S: AnsiString; Delimiters: PAnsiChar): TArrayOfStrings;
var
Buf: AnsiString;
Ch: AnsiChar;
begin
Result := nil; // make compiler happy
Buf := '';
SetLength(Result, 0);
for Ch in S do begin
if Pos(Ch, Delimiters) > 0 then begin
if Length(Buf) > 0 then begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := Buf;
Buf := ''
end;
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := Ch;
end else
Buf := Buf + Ch;
end;
if Length(Buf) > 0 then begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := Buf;
end;
end;
function LeftOf(const SubString, S: AnsiString): AnsiString;
var
Index: SizeInt;
begin
Index := Pos(SubString, S);
if Index > 0 then begin
Exit(Copy(S, 1, Index - 1));
end else
Exit(S);
end;
function RightOf(const SubString, S: AnsiString): AnsiString;
var
Index: SizeInt;
begin
Index := Pos(SubString, S);
if Index > 0 then begin
Exit(Copy(S, Index + Length(SubString), Length(S) - Index - Length(SubString) + 1));
end else
Exit(S);
end;
function ReplaceOne(const S, Target, Replacement: AnsiString): AnsiString;
var
Found: SizeInt;
begin
Found := Pos(Target, S);
if Found > 0 then begin
Exit(Copy(S, 1, Found - 1) +
Replacement +
Copy(S, Found + Length(Target), Length(S) - Found - Length(Target) + 1));
end else
Exit(S);
end;
function ReplaceAt(const S, Replacement: AnsiString; Index, TargetLength: SizeInt): AnsiString;
var
Left, Right: AnsiString;
begin
Left := Copy(S, 1, Index - 1);
Right := Copy(S, Index + TargetLength, Length(S) - Index - TargetLength + 1);
Exit(Left + Replacement + Right);
end;
function Find(const Target, S: AnsiString; Beginning: SizeInt): SizeInt;
var
Cursor, CursorEnd: PAnsiChar;
begin
Cursor := @S[Beginning];
CursorEnd := @S[1] + Length(S);
while Cursor + Length(Target) <= CursorEnd do begin
if CompareByte(Cursor^, Target[1], Length(Target)) = 0 then
Exit(Cursor - @S[1] + 1);
Inc(Cursor);
end;
Exit(0);
end;
function ReplaceAll(const S, Target, Replacement: AnsiString): AnsiString;
var
Found: SizeInt;
begin
Result := S;
Found := Pos(Target, S);
while Found > 0 do begin
Result := ReplaceAt(Result, Replacement, Found, Length(Target));
Found := Find(Target, Result, Found + Length(Replacement) + 1);
end;
end;
// var
// Parts: TArrayOfStrings;
// Part: AnsiString;
//
// begin
// Parts := Tokenize('a/b/c/\d\e,f,g,', '/\,');
// for Part in Parts do
// Write('"', Part, '" ');
// Writeln;
// // "a" "b" "c" "d" "e" "f" "g"
//
// Parts := Split('a/b/c/\d\e,f,g,', '/\,');
// for Part in Parts do
// Write('"', Part, '" ');
// Writeln;
// // "a" "/" "b" "/" "c" "/" "\" "d" "\" "e" "," "f" "," "g" ","
//
// Writeln('"', LeftOf('beginning', 'in the beginning'), '"'); // "in the "
// Writeln('"', RightOf('in the ', 'in the beginning'), '"'); // "beginning"
// Writeln('"', ReplaceOne('0cad0', '0', 'abra'), '"'); // "abracad0"
// Writeln('"', ReplaceAll('0cad0', '0', 'abra'), '"'); // "abracadabra"
// end.
end.