szInsertString proc uses ebx esi edi lpString1,lpString2,len1,len2,index
mov ebx,index
mov edx,len1
cmp edx,ebx
jc Error
je Append
mov edi,lpString1
neg ebx
lea esi,[edi][edx-3]
lea ecx,[edx][ebx][3]
mov edi,esi
mov edx,ecx
add edi,len2
shr ecx,2
and edx,3
std
rep movsd
add esi,3
add edi,3
mov ecx,edx
rep movsb
cld
mov edi,lpString1
neg ebx
mov esi,lpString2
add edi,ebx
mov ecx,len2
shr ecx,2
rep movsd
mov ecx,len2
and ecx,3
rep movsb
jmp GoodEx
Append:
mov ecx,len2
mov edi,lpString1
inc ecx
mov esi,lpString2
mov edx,ecx
add edi,ebx
shr ecx,2
and edx,3
rep movsd
mov ecx,edx
rep movsb
GoodEx: mov eax,1
ret
Error: xor eax,eax
ret
szInsertString endp

Posted on 2002-01-28 01:33:10 by The Svin
Nice algo, Svin!

Why don't you comment out:
;	mov esi,lpString2

; add edi,ebx
; shr ecx,2
; rep movsd
; mov ecx,len2
; and ecx,3
; rep movsb
; jmp GoodEx
...and jump into the append code? Could even play with it to fall through, but it adds an instruction to the common case (no cost for it?).
szInsertString proc uses ebx esi edi lpString1,lpString2,len1,len2,index

mov ebx,index
mov edx,len1
cmp edx,ebx
jc Error
je Append
mov edi,lpString1
neg ebx
lea esi,[edi][edx-3]
lea ecx,[edx][ebx][3]
mov edi,esi
mov edx,ecx
add edi,len2
shr ecx,2
and edx,3
std
rep movsd
add esi,3
add edi,3
mov ecx,edx
dec len2
rep movsb
cld
neg ebx
Append:
mov ecx,len2
mov edi,lpString1
inc ecx
mov esi,lpString2
add edi,ebx
mov edx,ecx
shr ecx,2
and edx,3 ; carry clear ;)
rep movsd
mov ecx,edx
rep movsb
;GoodEx:
; mov eax,1
; ret
Error: ; carry set
sbb eax,eax ; -1 = Error, 0 = Okay
ret
szInsertString endp
Posted on 2002-01-28 02:06:12 by bitRAKE
Of course!
It shall be done this way. Thanks for the idea.
And I like your solution for ret value with cf
Posted on 2002-01-28 04:51:38 by The Svin
Thanks for the algo, Svin!
What about a "szReplace" function? :grin:
Posted on 2002-01-29 01:12:13 by bazik
bAZiK,

The search and replace code is no big deal but a memory strategy to handle replacements longer than the source is no joy to write. I have thought of a few ways but it involves reallocating memory when a threshold is reached near the top of memory.

Using a buffer may sound attractive but if the text being replaced is much longer than the replacement, there is no automatic way to control the write so it does not write past the end of the buffer.

I personally think its best done in OLE string memory as it can be dynamically reallocated on a needs basis.

Regards,

hutch@movsd.com
Posted on 2002-01-29 01:56:29 by hutch--
bitRake:
Actually it can't be done this way.
Append is for appending new string (when index is pointing to
zero caracter) and it means that we need in the case connect len+1 characters to include 0 to the end.
Posted on 2002-01-31 14:36:58 by The Svin
That is why:
Append: 

mov ecx,len2
mov edi,lpString1
inc ecx ; length + one
Doesn't this include the zero?
Posted on 2002-01-31 14:56:37 by bitRAKE
You are right.
But I don't like dec len2, let's find another way.
Posted on 2002-01-31 15:44:02 by The Svin

But I don't like dec len2, let's find another way.
Same here, ;) I looked a little, but I bet we can find a work around if we try.
Posted on 2002-01-31 16:22:40 by bitRAKE
bitRAKE:
That's the spirit, Man!
Solution 2 clocks faster:
mov ebx,index
mov edx,len1
mov ecx,1 ;noclocks - we brake dependences
cmp edx,ebx
jc Error
je Append
mov edi,lpString1
neg ebx
lea esi, ;????????? ?? ????????? DWORD String1 c 0
lea ecx,[3]
mov edi,esi
mov edx,ecx
add edi,len2
shr ecx,2
and edx,3
std
rep movsd
add esi,3
add edi,3
mov ecx,edx
rep movsb
cld
neg ebx

Append:
add ecx,len2 ;2clocks and uv instead of 3 inc memory
mov edi,lpString1
mov esi,lpString2
mov eax,ecx
add edi,ebx
shr ecx,2
and eax,3
rep movsd
mov ecx,eax
rep movsb
Error:
sbb eax,eax
ret
szInsertString endp
Posted on 2002-01-31 17:01:09 by The Svin
Hello bAZiK,
I've posted a thread with a ReplaceString function.
Hope you like it. :)
Posted on 2002-02-03 18:22:59 by dilau