![]() |
Vorkommen eines SubStrings in einem String
Die folgende Methode gibt die Anzahl des vollständigen Auftreten eines SubStrings in einem String zurück. (getestet in Delphi 5 - 7)
Delphi-Quellcode:
...:cat:...
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; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:04 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz