-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAnsiStringReplaceJOHIA32Unit12.pas
More file actions
547 lines (528 loc) · 16.4 KB
/
AnsiStringReplaceJOHIA32Unit12.pas
File metadata and controls
547 lines (528 loc) · 16.4 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
unit AnsiStringReplaceJOHIA32Unit12;
interface
{$R-,Q-}
uses
Windows, SysUtils, StrUtils;
{Equivalent of StringReplace for Non Multi Byte Character Sets}
function StringReplace_JOH_IA32_12(const S, OldPattern, NewPattern: AnsiString;
Flags: TReplaceFlags): AnsiString;
function AnsiPosExIC(const SubStr, S: Ansistring; Offset: Integer = 1): Integer;
//Size = 1132 + 256 (Table) + 4 (srCodePage) = 1392 Bytes
implementation
var
AnsiUpcase: packed array[Char] of Char; {Upcase Lookup Table}
srCodePage: UINT; {Active String Replace Windows CodePage}
{Setup Lookup Table for Ansi Uppercase}
procedure InitialiseAnsiUpcase;
var
Ch: Char;
begin
srCodePage := GetACP;
for Ch := #0 to #255 do
AnsiUpcase[Ch] := Ch;
CharUpperBuffA(@AnsiUpcase, 256);
end;
{$IFNDEF Delphi2005Plus}
{$I D7PosEx.inc}
{$ENDIF}
(*
{Non Case Sensitive version of PosEx using AnsiUpcase}
{AnsiUpcase must be initialized before this function is called}
function AnsiPosExIC(const SubStr, S: Ansistring; Offset: Integer = 1): Integer;
asm
@@AnsiPosExIC: //from JOH_IA32_14
push ebx
push esi
push edx {@Str}
mov esi, ecx
mov ecx, [edx-4] {Length(Str) (S<>nil)}
mov ebx, [eax-4] {Length(SubStr) (SubStr<>nil)}
add ecx, edx
sub ecx, ebx {Max Start Pos for a Full Match}
lea edx, [edx+esi-1] {Set Start Position}
cmp edx, ecx
jg @@NotFound {StartPos > Max Start Pos}
sub ebx, 2 {Length(SubStr) - 2}
jc @@SingleChar {Length(SubStr) <= 1}
push edi
push ebp
push ebx {Save Remainder to Check = Length(SubStr) - 2}
push ecx {Save Max Start Position}
mov esi, eax
movzx ebx, [eax] {Search Character = 1st Char of SubStr}
lea edi, AnsiUpcase {Uppercase Lookup Table}
movzx ebx, [edi+ebx] {Convert to Uppercase}
@@Loop: {Loop Comparing 2 Characters per Loop}
movzx eax, [edx] {Get Next Character}
cmp bl, [edi+eax]
jne @@NotChar1
mov ebp, [esp+4] {Remainder to Check}
@@Char1Loop:
movzx eax, [esi+ebp]
movzx ecx, [edx+ebp]
movzx eax, [edi+eax] {Convert to Uppercase}
cmp al, [edi+ecx]
jne @@NotChar1
movzx eax, [esi+ebp+1]
movzx ecx, [edx+ebp+1]
movzx eax, [edi+eax] {Convert to Uppercase}
cmp al, [edi+ecx]
jne @@NotChar1
sub ebp, 2
jge @@Char1Loop
pop ecx
pop eax
pop ebp
pop edi
jmp @@SetPosResult
@@NotChar1:
movzx eax, [edx+1] {Get Next Character}
cmp bl, [edi+eax]
jne @@NotChar2
mov ebp, [esp+4] {Remainder to Check}
@@Char2Loop:
movzx eax, [esi+ebp]
movzx ecx, [edx+ebp+1]
movzx eax, [edi+eax] {Convert to Uppercase}
cmp al, [edi+ecx]
jne @@NotChar2
movzx eax, [esi+ebp+1]
movzx ecx, [edx+ebp+2]
movzx eax, [edi+eax] {Convert to Uppercase}
cmp al, [edi+ecx]
jne @@NotChar2
sub ebp, 2
jge @@Char2Loop
pop ecx
pop eax
pop ebp
pop edi
jmp @@CheckPosResult {Check Match is within String Data}
@@NotChar2:
add edx, 2
cmp edx, [esp] {Compare to Max Start Position}
jle @@Loop {Loop until Start Pos > Max Start Pos}
pop ecx {Dump Start Position}
pop eax {Dump Remainder to Check}
pop ebp
pop edi
jmp @@NotFound
@@SingleChar:
movzx ebx, [eax] {1st Char of SubStr}
lea esi, AnsiUpcase
movzx ebx, [esi+ebx] {Convert to Uppercase}
@@CharLoop:
movzx eax, [edx]
cmp bl, [esi+eax]
je @@SetPosResult
inc edx
cmp edx, ecx
jle @@CharLoop
@@NotFound:
xor eax, eax
pop edx
pop esi
pop ebx
ret
@@CheckPosResult: {Check Match is within String Data}
cmp edx, ecx
jge @@NotFound
inc edx {OK - Adjust Result}
@@SetPosResult: {Set Result Position}
mov eax, edx
inc eax
pop ecx {@Str}
pop esi
pop ebx
sub eax, ecx
ret
end;
*)
(*
function AnsiPosExIC(const SubStr, S: Ansistring; Offset: Integer = 1): Integer;
asm
@@AnsiPosExIC: //wrongly passes validation
push ebx
push esi
push edx {@Str}
mov esi, ecx
mov ecx, [edx-4] {Length(Str) (S<>nil)}
mov ebx, [eax-4] {Length(SubStr) (SubStr<>nil)}
add ecx, edx
sub ecx, ebx {Max Start Pos for a Full Match}
lea edx, [edx+esi-1] {Set Start Position}
cmp edx, ecx
jg @@NotFound {StartPos > Max Start Pos}
sub ebx, 2 {Length(SubStr) - 2}
jc @@SingleChar {Length(SubStr) <= 1}
push edi
push ebp
push ebx {Save Remainder to Check = Length(SubStr) - 2}
push ecx {Save Max Start Position}
mov esi, eax
movzx ebx, [eax] {Search Character = 1st Char of SubStr}
lea edi, AnsiUpcase {Uppercase Lookup Table}
movzx ebx, [edi+ebx] {Convert to Uppercase}
@@Loop: {Loop Comparing 2 Characters per Loop}
movzx eax, [edx] {Get Next Character}
cmp bl, [edi+eax]
jne @@NotChar1
mov ebp, [esp+4] {Remainder to Check}
@@Char1Loop:
movzx eax, [esi+ebp]
movzx ecx, [edx+ebp]
movzx eax, [edi+eax] {Convert to Uppercase}
cmp al, [edi+ecx]
jne @@NotChar1
movzx eax, [esi+ebp+1]
movzx ecx, [edx+ebp+1]
movzx eax, [edi+eax] {Convert to Uppercase}
cmp al, [edi+ecx]
jne @@NotChar1
sub ebp, 2
jge @@Char1Loop
pop ecx
pop eax
pop ebp
pop edi
inc edx
jmp @@SetPosResult
@@NotChar1:
movzx eax, [edx+1] {Get Next Character}
cmp bl, [edi+eax]
jne @@NotChar2
mov ebp, [esp+4] {Remainder to Check}
inc edx
@@Char2Loop:
movzx eax, [esi+ebp]
movzx ecx, [edx+ebp]
movzx eax, [edi+eax] {Convert to Uppercase}
cmp al, [edi+ecx]
jne @@NotChar2
movzx eax, [esi+ebp+1]
movzx ecx, [edx+ebp+1]
movzx eax, [edi+eax] {Convert to Uppercase}
cmp al, [edi+ecx]
jne @@NotChar2
sub ebp, 2
jge @@Char2Loop
pop ecx
pop eax
pop ebp
pop edi
jmp @@CheckPosResult {Check Match is within String Data}
@@NotChar2:
//inc edx
add edx, 2 //should be inc edx
cmp edx, [esp] {Compare to Max Start Position}
jle @@Loop {Loop until Start Pos > Max Start Pos}
pop ecx {Dump Start Position}
pop eax {Dump Remainder to Check}
pop ebp
pop edi
jmp @@NotFound
@@SingleChar:
movzx ebx, [eax] {1st Char of SubStr}
lea esi, AnsiUpcase
movzx ebx, [esi+ebx] {Convert to Uppercase}
@@CharLoop:
movzx eax, [edx]
inc edx
cmp bl, [esi+eax]
je @@SetPosResult
cmp edx, ecx
jle @@CharLoop
@@NotFound:
xor eax, eax
pop edx
pop esi
pop ebx
ret
@@CheckPosResult: {Check Match is within String Data}
cmp edx, ecx
jg @@NotFound
inc edx {OK - Adjust Result}
@@SetPosResult: {Set Result Position}
mov eax, edx
pop ecx {@Str}
pop esi
pop ebx
sub eax, ecx
ret
end;
*)
//original
function AnsiPosExIC(const SubStr, S: Ansistring; Offset: Integer = 1): Integer;
asm
push ebx
push esi
push edx {@Str}
test eax, eax
jz @@NotFound {Exit if SubStr = ''}
test edx, edx
jz @@NotFound {Exit if Str = ''}
mov esi, ecx
mov ecx, [edx-4] {Length(Str)}
mov ebx, [eax-4] {Length(SubStr)}
add ecx, edx
sub ecx, ebx {Max Start Pos for Full Match}
lea edx, [edx+esi-1] {Set Start Position}
cmp edx, ecx
jg @@NotFound {StartPos > Max Start Pos}
cmp ebx, 1 {Length(SubStr)}
jle @@SingleChar {Length(SubStr) <= 1}
push edi
push ebp
lea edi, [ebx-2] {Length(SubStr) - 2}
mov esi, eax
push edi {Save Remainder to Check = Length(SubStr) - 2}
push ecx {Save Max Start Position}
lea edi, AnsiUpcase {Uppercase Lookup Table}
movzx ebx, [eax] {Search Character = 1st Char of SubStr}
movzx ebx, [edi+ebx] {Convert to Uppercase}
@@Loop: {Loop Comparing 2 Characters per Loop}
movzx eax, [edx] {Get Next Character}
movzx eax, [edi+eax] {Convert to Uppercase}
cmp eax, ebx
jne @@NotChar1
mov ebp, [esp+4] {Remainder to Check}
@@Char1Loop:
movzx eax, [esi+ebp]
movzx ecx, [edx+ebp]
movzx eax, [edi+eax] {Convert to Uppercase}
movzx ecx, [edi+ecx] {Convert to Uppercase}
cmp eax, ecx
jne @@NotChar1
movzx eax, [esi+ebp+1]
movzx ecx, [edx+ebp+1]
movzx eax, [edi+eax] {Convert to Uppercase}
movzx ecx, [edi+ecx] {Convert to Uppercase}
cmp eax, ecx
jne @@NotChar1
sub ebp, 2
jnc @@Char1Loop
pop ecx
pop edi
pop ebp
pop edi
jmp @@SetResult
@@NotChar1:
movzx eax, [edx+1] {Get Next Character}
movzx eax, [edi+eax] {Convert to Uppercase}
cmp bl, al
jne @@NotChar2
mov ebp, [esp+4] {Remainder to Check}
@@Char2Loop:
movzx eax, [esi+ebp]
movzx ecx, [edx+ebp+1]
movzx eax, [edi+eax] {Convert to Uppercase}
movzx ecx, [edi+ecx] {Convert to Uppercase}
cmp eax, ecx
jne @@NotChar2
movzx eax, [esi+ebp+1]
movzx ecx, [edx+ebp+2]
movzx eax, [edi+eax] {Convert to Uppercase}
movzx ecx, [edi+ecx] {Convert to Uppercase}
cmp eax, ecx
jne @@NotChar2
sub ebp, 2
jnc @@Char2Loop
pop ecx
pop edi
pop ebp
pop edi
jmp @@CheckResult {Check Match is within String Data}
@@NotChar2:
add edx, 2
cmp edx, [esp] {Compate to Max Start Position}
jle @@Loop {Loop until Start Position > Max Start Position}
pop ecx {Dump Start Position}
pop edi {Dump Remainder to Check}
pop ebp
pop edi
jmp @@NotFound
@@SingleChar:
jl @@NotFound {Needed for Zero-Length Non-NIL Strings}
lea esi, AnsiUpcase
movzx ebx, [eax] {Search Character = 1st Char of SubStr}
movzx ebx, [esi+ebx] {Convert to Uppercase}
@@CharLoop:
movzx eax, [edx]
movzx eax, [esi+eax] {Convert to Uppercase}
cmp eax, ebx
je @@SetResult
movzx eax, [edx+1]
movzx eax, [esi+eax] {Convert to Uppercase}
cmp eax, ebx
je @@CheckResult
add edx, 2
cmp edx, ecx
jle @@CharLoop
@@NotFound:
xor eax, eax
pop edx
pop esi
pop ebx
ret
@@CheckResult: {Check Match is within String Data}
cmp edx, ecx
jge @@NotFound
add edx, 1 {OK - Adjust Result}
@@SetResult: {Set Result Position}
pop ecx {@Str}
pop esi
pop ebx
neg ecx
lea eax, [edx+ecx+1]
end; {AnsiPosExIC}
function StringReplace_JOH_IA32_12(const S, OldPattern, NewPattern: AnsiString;
Flags: TReplaceFlags): AnsiString;
type
TPosEx = function(const SubStr, S: Ansistring; Offset: Integer): Integer;
const
StaticBufferSize = 16;
var
SrcLen, OldLen, NewLen, Found, Count, Start, Match, Matches, BufSize,
Remainder : Integer;
PosExFunction: TPosEx;
StaticBuffer : array[0..StaticBufferSize-1] of Integer;
Buffer : PIntegerArray;
P, PSrc, PRes: PChar;
Ch : Char;
begin
SrcLen := Length(S);
OldLen := Length(OldPattern);
NewLen := Length(NewPattern);
if (OldLen = 0) or (SrcLen < OldLen) then
begin
if SrcLen = 0 then
Result := '' {Needed for Non-Nil Zero Length Strings}
else
Result := S
end
else
begin
if rfIgnoreCase in Flags then
begin
PosExFunction := AnsiPosExIC;
if GetACP <> srCodePage then {Check CodePage}
InitialiseAnsiUpcase; {CodePage Changed - Update Lookup Table}
end
else
PosExFunction := PosEx;
if rfReplaceAll in Flags then
begin
if (OldLen = 1) and (NewLen = 1) then
begin {Single Character Replacement}
Remainder := SrcLen;
SetLength(Result, Remainder);
P := Pointer(Result);
Move(Pointer(S)^, P^, Remainder);
if rfIgnoreCase in Flags then
begin
Ch := AnsiUpcase[OldPattern[1]];
repeat
Dec(Remainder);
if AnsiUpcase[P[Remainder]] = Ch then
P[Remainder] := NewPattern[1];
until Remainder = 0;
end
else
begin
repeat
Dec(Remainder);
if P[Remainder] = OldPattern[1] then
P[Remainder] := NewPattern[1];
until Remainder = 0;
end;
Exit;
end;
Found := PosExFunction(OldPattern, S, 1);
if Found <> 0 then
begin
Buffer := @StaticBuffer;
BufSize := StaticBufferSize;
Matches := 1;
Buffer[0] := Found;
repeat
Inc(Found, OldLen);
Found := PosExFunction(OldPattern, S, Found);
if Found > 0 then
begin
if Matches = BufSize then
begin {Create or Expand Dynamic Buffer}
BufSize := BufSize + (BufSize shr 1); {Grow by 50%}
if Buffer = @StaticBuffer then
begin {Create Dynamic Buffer}
GetMem(Buffer, BufSize * SizeOf(Integer));
Move(StaticBuffer, Buffer^, SizeOf(StaticBuffer));
end
else {Expand Dynamic Buffer}
ReallocMem(Buffer, BufSize * SizeOf(Integer));
end;
Buffer[Matches] := Found;
Inc(Matches);
end
until Found = 0;
SetLength(Result, SrcLen + (Matches * (NewLen - OldLen)));
PSrc := Pointer(S);
PRes := Pointer(Result);
Start := 1;
Match := 0;
repeat
Found := Buffer[Match];
Count := Found - Start;
Start := Found + OldLen;
if Count > 0 then
begin
Move(PSrc^, PRes^, Count);
Inc(PRes, Count);
end;
Inc(PSrc, Count + OldLen);
Move(Pointer(NewPattern)^, PRes^, NewLen);
Inc(PRes, NewLen);
Inc(Match);
until Match = Matches;
Remainder := SrcLen - Start;
if Remainder >= 0 then
Move(PSrc^, PRes^, Remainder + 1);
if BufSize <> StaticBufferSize then
FreeMem(Buffer); {Free Dynamic Buffer if Created}
end
else {No Matches Found}
Result := S
end {ReplaceAll}
else
begin {Replace First Occurance Only}
Found := PosExFunction(OldPattern, S, 1);
if Found <> 0 then
begin {Match Found}
SetLength(Result, SrcLen - OldLen + NewLen);
Dec(Found);
PSrc := Pointer(S);
PRes := Pointer(Result);
if NewLen = OldLen then
begin
Move(PSrc^, PRes^, SrcLen);
Inc(PRes, Found);
Move(Pointer(NewPattern)^, PRes^, NewLen);
end
else
begin
Move(PSrc^, PRes^, Found);
Inc(PRes, Found);
Inc(PSrc, Found + OldLen);
Move(Pointer(NewPattern)^, PRes^, NewLen);
Inc(PRes, NewLen);
Move(PSrc^, PRes^, SrcLen - Found - OldLen);
end;
end
else {No Matches Found}
Result := S
end;
end;
end;
initialization
srCodePage := 0; {Invalidate AnsiUpcase Lookup Table}
InitialiseAnsiUpcase; //test
end.