any one know if msvc++ has a string replace function. btw im also trying to make one converted from assembly and is impossable, so i need to know if c++ has a default one.
btw if any one wants to help me convert my assembly string replace to c++ please tell me and ill post assembly code, basicly i want to see if the c++ compiler can optimize my assembly convert. i cant seem to get the binary string of bytes compare to work.
I know a lot oF Vb, expert in C++, and i think in assembly.
MSVC++6.NET
vb6
masm
Windowz Xp
I find my self using this a lot in C++
__asm {
}
ohh man i was tired, i need a multipule one, many chars not just one, example replace "hjfassss","ss","03787u" somtin like that as an example. basicly like the one vb6 has, but vb6 one is 3x faster then my assembly one.
....
std::string::replace
Displaying Topics 1 to 1 of 1
only ref to std::string::replace is in this thread !!!!!
Last edited by Cmdr0Sunburn; Oct 9th, 2002 at 05:54 PM.
I know a lot oF Vb, expert in C++, and i think in assembly.
MSVC++6.NET
vb6
masm
Windowz Xp
I find my self using this a lot in C++
__asm {
}
ive searched google and are not many refrences to it. but when i do use it in c++ , i get
F:\C++\newprojects\strtest\strst.cpp(11) : error C2352: 'std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::replace' : illegal call of non-static member function
c:\program files\microsoft visual studio\vc98\include\xstring(295) : see declaration of 'replace'
I know a lot oF Vb, expert in C++, and i think in assembly.
MSVC++6.NET
vb6
masm
Windowz Xp
I find my self using this a lot in C++
__asm {
}
loool too long, its 15000 chars, ill post atatchment
EDIT:
Code:
StringLen Proc stdcall public,ptrSTRING1:DWORD;duh
push esi
mov esi,ptrSTRING1
xor eax,eax
cmp BYTE PTR [esi+eax],0
jne @F
pop esi
ret
@@:
inc eax
cmp BYTE PTR [esi+eax],0
jne @B
pop esi
ret
StringLen EndP
Last edited by Cmdr0Sunburn; Oct 11th, 2002 at 03:10 PM.
I know a lot oF Vb, expert in C++, and i think in assembly.
MSVC++6.NET
vb6
masm
Windowz Xp
I find my self using this a lot in C++
__asm {
}
Don't know anything about MASM special syntax. Are @@ and @F special jump marks?
So the PStrReplace is the main function?
I assume that lpReplace is the string to search for and PTRSTRING2REPW is the string with which it gets replaced, and PTRbuffer is the destination.
Given a call
PStrReplace("Hi there, er er er", "er", "ah", buffer);
buffer would then be
"Hi thahe, ah ah ah"
right?
Given all these conditions I wrote a C function and compiled it to assembly. It needs some optimizations, but I'm sure you're better at this.
Code:
INCLUDELIB LIBC
INCLUDELIB OLDNAMES
PUBLIC _PStrReplace
EXTRN _memcmp:NEAR
EXTRN _strlen:NEAR
; Function compile flags: /Ods
; COMDAT _PStrReplace
_TEXT SEGMENT
_q$ = -12
_slen$ = -8
_p$ = -4
_szIn$ = 8
_szSearch$ = 12
_szReplace$ = 16
_buffer$ = 20
_PStrReplace PROC NEAR ; COMDAT
; File d:\download\forum\strreplace.c
; Line 5
push ebp
mov ebp, esp
sub esp, 12 ; 0000000cH
; Line 6
mov eax, DWORD PTR _buffer$[ebp]
mov DWORD PTR _p$[ebp], eax
; Line 8
push DWORD PTR _szSearch$[ebp]
call _strlen
pop ecx
mov DWORD PTR _slen$[ebp], eax
$L1064:
; Line 9
mov eax, DWORD PTR _szIn$[ebp]
movsx eax, BYTE PTR [eax]
test eax, eax
je SHORT $L1065
; Line 11
mov ecx, DWORD PTR _szSearch$[ebp]
movsx ecx, BYTE PTR [ecx]
cmp eax, ecx
jne SHORT $L1066
; Line 14
push DWORD PTR _slen$[ebp]
push DWORD PTR _szSearch$[ebp]
push DWORD PTR _szIn$[ebp]
call _memcmp
add esp, 12 ; 0000000cH
test eax, eax
jne SHORT $L1066
; Line 17
mov ebx, DWORD PTR _szReplace$[ebp]
jmp SHORT $L1068
$L1069:
mov eax, DWORD PTR _p$[ebp]
mov cl, BYTE PTR [ebx]
mov BYTE PTR [eax], cl
inc eax
mov DWORD PTR _p$[ebp], eax
inc ebx
$L1068:
movsx eax, BYTE PTR [eax]
test eax, eax
je SHORT $L1070
; Line 18
jmp SHORT $L1069
$L1070:
; Line 19
mov eax, DWORD PTR _szIn$[ebp]
add eax, DWORD PTR _slen$[ebp]
mov DWORD PTR _szIn$[ebp], eax
; Line 20
jmp SHORT $L1064
$L1066:
; Line 23
mov eax, DWORD PTR _p$[ebp]
mov ebx, DWORD PTR _szIn$[ebp]
mov cl, BYTE PTR [ebx]
mov BYTE PTR [eax], cl
inc eax
mov DWORD PTR _p$[ebp], eax
inc ebx
mov DWORD PTR _szIn$[ebp], ebx
; Line 24
jmp SHORT $L1064
$L1065:
; Line 25
mov eax, DWORD PTR _p$[ebp]
and BYTE PTR [eax], 0
; Line 26
leave
ret 0
_PStrReplace ENDP
Especially the lines 17 and 18 need optimizing, but the whole code contains quite a few redundant dereferencings. Sadly my version of VC++7 (Standard Edition) doesn't really optimize. Maybe keda can compile it and post it. I'll ask him.
Just in case you're interested, attached is the source file.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
; Listing generated by Microsoft (R) Optimizing Compiler Version 13.00.9466
TITLE \Documents and Settings\Administrator\Desktop\strreplace.c
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
; COMDAT ??_C@_02OEIPGGFF@ah?$AA@
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
; COMDAT ??_C@_02FDKLDEFC@er?$AA@
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
; COMDAT ??_C@_0BG@LKJNBNEP@Hello?5there?0?5er?5er?5er?$AA@
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
; COMDAT ??_C@_03OFAPEBGM@?$CFs?6?$AA@
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
; COMDAT _PStrReplace
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
; COMDAT _main
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif
INCLUDELIB LIBC
INCLUDELIB OLDNAMES
PUBLIC ??_C@_02OEIPGGFF@ah?$AA@ ; `string'
PUBLIC ??_C@_02FDKLDEFC@er?$AA@ ; `string'
PUBLIC ??_C@_0BG@LKJNBNEP@Hello?5there?0?5er?5er?5er?$AA@ ; `string'
PUBLIC ??_C@_03OFAPEBGM@?$CFs?6?$AA@ ; `string'
EXTRN _printf:NEAR
; COMDAT ??_C@_02OEIPGGFF@ah?$AA@
CONST SEGMENT
??_C@_02OEIPGGFF@ah?$AA@ DB 'ah', 00H ; `string'
CONST ENDS
; COMDAT ??_C@_02FDKLDEFC@er?$AA@
CONST SEGMENT
??_C@_02FDKLDEFC@er?$AA@ DB 'er', 00H ; `string'
CONST ENDS
; COMDAT ??_C@_0BG@LKJNBNEP@Hello?5there?0?5er?5er?5er?$AA@
CONST SEGMENT
??_C@_0BG@LKJNBNEP@Hello?5there?0?5er?5er?5er?$AA@ DB 'Hello there, er er'
DB ' er', 00H ; `string'
CONST ENDS
; COMDAT ??_C@_03OFAPEBGM@?$CFs?6?$AA@
CONST SEGMENT
??_C@_03OFAPEBGM@?$CFs?6?$AA@ DB '%s', 0aH, 00H ; `string'
CONST ENDS
PUBLIC _PStrReplace
; Function compile flags: /Ogty
; File h:\documents and settings\administrator\desktop\strreplace.c
; COMDAT _PStrReplace
_TEXT SEGMENT
_slen$ = -4
_szSearch$ = 8
_szReplace$ = 12
_PStrReplace PROC NEAR ; COMDAT
; _szIn$ = eax
; _buffer$ = edx
; 5 : {
push ecx
push ebx
push esi
push edi
mov edi, DWORD PTR _szSearch$[esp+12]
mov ebx, eax
; 6 : char *p = buffer;
; 7 : const char *q;
; 8 : size_t slen = strlen(szSearch);
mov eax, edi
lea esi, DWORD PTR [eax+1]
; 26 : }
npad 1
; 6 : char *p = buffer;
; 7 : const char *q;
; 8 : size_t slen = strlen(szSearch);
$L1155:
mov cl, BYTE PTR [eax]
inc eax
test cl, cl
jne SHORT $L1155
sub eax, esi
mov DWORD PTR _slen$[esp+16], eax
; 9 : while(*szIn)
mov al, BYTE PTR [ebx]
test al, al
je SHORT $L1157
push ebp
jmp SHORT $L1123
$L1159:
mov edi, DWORD PTR _szSearch$[esp+16]
; 26 : }
npad 6
$L1123:
; 10 : {
; 11 : if(*szIn == *szSearch)
cmp al, BYTE PTR [edi]
jne SHORT $L1158
; 12 : {
; 13 : // scan ahead
; 14 : if(memcmp(szIn, szSearch, slen) == 0)
mov ecx, DWORD PTR _slen$[esp+20]
mov esi, ebx
xor ebp, ebp
repe cmpsb
jne SHORT $L1158
; 15 : {
; 16 : // copy with incrementing p
; 17 : for(q=szReplace;*q;*p++ = *q++)
mov ecx, DWORD PTR _szReplace$[esp+16]
mov al, BYTE PTR [ecx]
test al, al
je SHORT $L1129
; 26 : }
npad 6
$L1127:
; 15 : {
; 16 : // copy with incrementing p
; 17 : for(q=szReplace;*q;*p++ = *q++)
mov BYTE PTR [edx], al
mov al, BYTE PTR [ecx+1]
inc edx
inc ecx
test al, al
jne SHORT $L1127
$L1129:
; 18 : ;
; 19 : szIn += slen;
add ebx, DWORD PTR _slen$[esp+20]
; 20 : continue;
jmp SHORT $L1154
$L1158:
; 21 : }
; 22 : }
; 23 : *p++ = *szIn++;
mov BYTE PTR [edx], al
inc edx
inc ebx
$L1154:
mov al, BYTE PTR [ebx]
test al, al
jne SHORT $L1159
pop ebp
$L1157:
pop edi
pop esi
; 24 : }
; 25 : *p = 0;
mov BYTE PTR [edx], 0
pop ebx
; 26 : }
pop ecx
ret 0
_PStrReplace ENDP
_TEXT ENDS
PUBLIC _main
EXTRN ___security_cookie:DWORD
EXTRN @__security_check_cookie@4:NEAR
; Function compile flags: /Ogty
; COMDAT _main
_TEXT SEGMENT
_buffer$ = -104
__$ArrayPad$ = -4
__$ReturnAddr$ = 4
_main PROC NEAR ; COMDAT
; 29 : {
sub esp, 104 ; 00000068H
mov eax, DWORD PTR ___security_cookie
xor eax, DWORD PTR __$ReturnAddr$[esp+100]
; 30 : char buffer[100];
; 31 : PStrReplace("Hello there, er er er", "er", "ah", buffer);
push OFFSET FLAT:??_C@_02OEIPGGFF@ah?$AA@
mov DWORD PTR __$ArrayPad$[esp+108], eax
push OFFSET FLAT:??_C@_02FDKLDEFC@er?$AA@
lea edx, DWORD PTR _buffer$[esp+112]
mov eax, OFFSET FLAT:??_C@_0BG@LKJNBNEP@Hello?5there?0?5er?5er?5er?$AA@
call _PStrReplace
; 32 : printf("%s\n", buffer);
lea eax, DWORD PTR _buffer$[esp+112]
push eax
push OFFSET FLAT:??_C@_03OFAPEBGM@?$CFs?6?$AA@
call _printf
; 33 : return 0;
; 34 : }
mov ecx, DWORD PTR __$ArrayPad$[esp+120]
xor eax, eax
xor ecx, DWORD PTR __$ReturnAddr$[esp+116]
add esp, 120 ; 00000078H
jmp @__security_check_cookie@4
_main ENDP
_TEXT ENDS
END
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
wow thats remarcably small coding, but my code with out optimizations if 10x that.
btw
jmp @F will jump to the first ocurrence of @@:, forward, same for backwardz
I know a lot oF Vb, expert in C++, and i think in assembly.
MSVC++6.NET
vb6
masm
Windowz Xp
I find my self using this a lot in C++
__asm {
}
i use vb to test things. i estamate my 1.4ghz will spend 54,414,122 clock cycles(hz) give or take 1million
replacing 22,402 e's with 1's
and your c++ Function
1,451,930 clock cycles doing the same thing.
using QueryPerformanceCounter to time the vb function, and the rdtsc on the c++ one coded in.
wow 50x faster
thx all its exactly what i needed. but u can add a buffer overflow checker if you want. but i cant, it would lead to incompatablilties with my current programs.
just so you know, i have a lot of string functions to convert over to c++
Last edited by Cmdr0Sunburn; Oct 12th, 2002 at 06:13 PM.
I know a lot oF Vb, expert in C++, and i think in assembly.
MSVC++6.NET
vb6
masm
Windowz Xp
I find my self using this a lot in C++
__asm {
}
Originally posted by CornedBee Tell me what they do, I'll try to find C solutions.
lol most of them c++ already has, reson i made them is becuase assembly doesnt come with them. but here are a few that can be transed into c++
Code:
;---------------------------;---------------------------;---------------------------
;ascii to dword, eax is value;old stuff
;---------------------------;---------------------------;---------------------------
a2dw Proc stdcall public Uses ecx edi edx esi, String:DWORD
xor ecx, ecx
mov edi, String
Invoke StringLen, edi
.While eax != 0
xor edx, edx
mov dl, byte ptr [edi]
sub dl, "0" ; subtrack each digit with "0" to convert it to hex value
mov esi, eax
dec esi
push eax
mov eax, edx
push ebx
mov ebx, 10
.While esi > 0
mul ebx
dec esi
.EndW
pop ebx
add ecx, eax
pop eax
inc edi
dec eax
.EndW
mov eax, ecx
ret
a2dw EndP
Code:
;---------------------------;---------------------------;---------------------------
;Crc Table from Thomas
;---------------------------;---------------------------;---------------------------
Thomas1crctable proc uses ebx esi edi
cmp CrcAlreadyFilled,1
je @Thomas1crctableEND
mov edi, offset CRCtable - 4
xor ecx, ecx
_ml:
mov eax, ecx
;mov ebx, 0EDB88320h
add edi, 4
mov esi, 8
@@:
shr eax, 1
sbb edx, edx
and edx, 0EDB88320h
xor eax, edx
dec esi
jnz @B
inc ecx
mov [edi], eax
cmp ecx, 256
jb _ml
mov CrcAlreadyFilled,1
@Thomas1crctableEND:
ret
Thomas1crctable endp
;---------------------------;---------------------------;---------------------------
;Crc algo from Thomas
;---------------------------;---------------------------;---------------------------
Thomas2CrC proc uses ebx esi edi buf:DWORD, len:DWORD
cmp CrcAlreadyFilled,1
je @F
call Thomas1crctable
@@:
mov esi, buf
mov edi, offset CRCtable
mov edx, len
shr edx, 1
or ecx, -1
xor eax, eax
@@:
mov al, [esi]
xor al, cl
shr ecx, 8
mov ebx, [edi+4*eax]
xor ecx, ebx
mov al, [esi+1]
xor al, cl
shr ecx, 8
mov ebx, [edi+4*eax]
add esi,2
xor ecx, ebx
dec edx
jnz @B
test len, 1
jz @F
mov al, [esi]
xor al, cl
inc esi
shr ecx, 8
mov ebx, [edi+4*eax]
xor ecx, ebx
@@:
mov eax, ecx
not eax
ret
Thomas2CrC endp
Code:
;---------------------------;---------------------------;---------------------------
; gets an error discription from number
;---------------------------;---------------------------;---------------------------
GetErrDescriptionP Proc Uses ebx edi ErrNum:DWORD
Local hLocal:DWORD
;LOCAL Buffer1[256]:BYTE
mov eax,ErrNum
or eax,eax
jne WeAlreadyKnowIt
Invoke GetLastError
WeAlreadyKnowIt:
mov edi,eax
Invoke FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER or \
FORMAT_MESSAGE_FROM_SYSTEM,
0, ; GetItFromSystem
edi,0, ; ErrNum,Default language
Addr hLocal, ; where to send the address of string from system
0,0 ; any size, no arguments
or eax,eax
mov ebx,Offset UnKnown
je UnKnown
Invoke LocalLock,hLocal
mov ebx,eax
UnKnown:
Invoke wsprintf,Addr Buffer1,Offset ErrMsgTmpl,edi,ebx
Invoke MessageBox,0 ,Addr Buffer1,Addr mberr1,MB_OK + MB_ICONINFORMATION + MB_TOPMOST
cmp ebx,Offset UnKnown
je @F
Invoke LocalFree,hLocal
@@:
Invoke gggge,Addr Buffer1
ret
GetErrDescriptionP EndP
;---------------------------;---------------------------;---------------------------
;end
;---------------------------;---------------------------;---------------------------
I know a lot oF Vb, expert in C++, and i think in assembly.
MSVC++6.NET
vb6
masm
Windowz Xp
I find my self using this a lot in C++
__asm {
}