Adem Guest
|
Posted: Thu Apr 26, 2007 7:56 am Post subject: Some assembly required again :) --Wide PosEx |
|
|
This is from fastCode project. It's fast.
Could it also be turned into using WideStrings, please.
function PosEx_EWC_IA32_2_b(const ASearchString, AText: String; AOffset:
Integer = 1): Integer;
//Pattern = edi
//Source = esi
//SourceOffset/LoopVar = ecx
//PatternLength = edx
//Result = eax
asm
test eax, eax
jz @NotFound
test edx, edx
jz @NotFound
cmp ecx, 0
jle @NotFound
push esi
push edi
mov edi,eax
sub ecx, 1 //Make AOffset relative to zero
lea esi,[edx+ecx] //Load esi with starting addr of source String
mov eax,[edx-$04] //Get String Source Length
push eax
mov edx,[edi-$04] //Get String Pattern Length
test eax, eax
jz @NotFoundPop
test edx, edx
jz @NotFoundPop
//Calculate Number of Loops to execute
sub eax,ecx // Subtract the AOffset from the Length of the String
sub eax,edx // Backoff the Length of the Pattern
lea ecx,[eax+1] // Add 1 and move the result to the LoopVar
test ecx,ecx //If Pattern is longer than the remaining Source
jle @NotFoundPop // Pattern was not found.
mov al,[edi] //Get Pattern Character
jmp @BeginLoop
@NextSourceChar:
lea esi, [esi+1] //Advance to next Source character
@BeginLoop:
cmp al,[esi] //Compare to Source Character
jnz @NextSource //If no match, Break loop
@BeginPattern:
push edx //Save Pattern Length
@PatternLoop:
sub edx, 1 //Make Pattern Count an AOffset/Dec Loop Counter
jle @Found //If end of Pattern then must be found
mov ah, [edi+edx] //Get Pattern TNanoXMLChar
cmp ah, [esi+edx] //Compare to Source TNanoXMLChar
je @PatternLoop //if Pattern TNanoXMLChar Matches
pop edx //Restore Pattern Count if not found
@NextSource:
sub ecx, 1
jnz @NextSourceChar
@NotFoundPop:
pop eax
pop edi
pop esi
@NotFound:
xor eax, eax
jmp @Done
@Found:
pop edx
pop eax //Length of Source String
pop edi
pop esi
sub eax, ecx //Subtract Number of Iterations Left
sub eax, edx //Add the Length of the Pattern
lea eax,[eax+2]
@Done:
end; |
|