Registriert seit: 10. Jun 2002
Ort: Unterhaching
11.412 Beiträge
Delphi 12 Athens
|
Vorkommen eines SubStrings in einem String
5. Jul 2003, 13:13
Die folgende Methode gibt die Anzahl des vollständigen Auftreten eines SubStrings in einem String zurück. (getestet in Delphi 5 - 7)
Delphi-Quellcode:
function CountSubStrings(SubStr, Str: AnsiString): DWORD;
type
StrRec = packed record
allocSiz: Longint;
refCnt: Longint;
length: Longint;
end;
const
skew = sizeof(StrRec);
var
Count, SubLength: Integer;
asm
// Search-String passed?
TEST EAX,EAX
JE @@noWork
// Sub-String passed?
TEST EDX,EDX
JE @@stringEmpty
// Save registers affected
PUSH ECX
PUSH EBX
PUSH EDI
PUSH ESI
// Load Sub-String pointer
MOV ESI,EAX
// Load Search-String pointer
MOV EDI,EDX
// Save Last Position in EBX
MOV EBX,ECX
// Get Search-String Length
MOV ECX,[EDI-skew].StrRec.length
// Get Sub-String Length
MOV EAX,[ESI-skew].StrRec.length
// Adjust
DEC EAX
MOV SubLength,EAX
// Failed if Sub-String Length was zero
JS @@exit
XOR EDX,EDX
MOV Count,EDX
// Load character count to be scanned
SUB ECX,EAX
// Failed if Sub-String was equal or longer than Search-String
JLE @@exit
// Pull first character of Sub-String for SCASB function
MOV AL,[ESI]
// Point to second character for CMPSB function
INC ESI
@@loop:
// Scan for first matching character
REPNE SCASB
// Failed, if none are matching
JNE @@exit
// Save counter
MOV EBX,ECX
// load Sub-String length
MOV ECX,SubLength
PUSH ESI
PUSH EDI
// compare all bytes until one is not equal
REPE CMPSB
// restore counter
POP EDI
POP ESI
// restore counter
MOV ECX,EBX
// all byte were equal, search is completed
JE @@found
// continue search
JMP @@loop
@@found:
// increment found counter
MOV EBX,SubLength
ADD EDI,EBX
INC Count
JMP @@loop
@@stringEmpty:
// return zero - no match
XOR EDX,EDX
JMP @@noWork
@@exit:
// restore registers
POP ESI
POP EDI
POP EBX
POP ECX
MOV EAX,Count
@@noWork:
end;
... ...
Daniel Lizbeth Ich bin nicht zurück, ich tue nur so
|
|
Zitat
|