 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
talkerzero Grandmaster Cheater
Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Tue Jun 02, 2009 4:39 pm Post subject: [C] My string functions |
|
|
Any suggestions for improvement?
| Code: | UINT FindString(LPCTSTR lpctszString, LPCTSTR lpctszFind)
{
BOOL bMatch = TRUE;
UINT i = 0;
UINT j = 0;
for(; i < _tcsclen(lpctszString); i++, bMatch = TRUE;)
{
for(j = 0; j < _tcsclen(lpctszFind); j++)
if(lpctszString[i + j] != lpctszFind[j])
bMatch = FALSE;
if(bMatch)
return i;
}
return -1;
} |
| Code: | LPTSTR FilterString(LPCTSTR lpctszString, LPCTSTR lpctszFilter)
{
BOOL bMatch = TRUE;
UINT i = 0;
UINT j = 0;
UINT k = 0;
LPTSTR lptszRet;
for(; i < _tcsclen(lpctszString); bMatch = TRUE;)
{
for(j = 0; j < _tcsclen(lpctszFilter); j++)
if(lpctszString[i + j] != lpctszFilter[j])
bMatch = FALSE;
bMatch ? i += j : lptszRet[k++] = lpctszString[i++];
}
lptszRet[k] = '\0';
return lptszRet;
} |
| Code: | LPTSTR FilterCharacter(LPCTSTR lpctszString, TCHAR tch)
{
UINT i = 0;
UINT j = 0;
LPTSTR lptszRet;
for(; i < _tcsclen(lpctszString); i++)
if(lpctszString[i] != tch)
lptszRet[j++] = lpctszString[i];
lptszRet[j] = '\0';
return lptszRet;
} |
| Code: | LPTSTR FilterSection(LPCTSTR lpctszString, LPCTSTR lpctszBegin, LPCTSTR lpctszEnd)
{
UINT i = 0;
UINT j = 0;
UINT k = 0;
BOOL bMatch = TRUE;
BOOL bFiltering = FALSE;
LPTSTR lptszRet;
for(; i < _tcsclen(lpctszString); i++)
{
for(j = 0; j < _tcsclen((!bFiltering ? lpctszBegin : lpctszEnd)); j++, bMatch = TRUE;)
if(lpctszString[i + j] != (!bFiltering ? lpctszBegin : lpctszEnd)[j])
bMatch = FALSE;
if(bMatch)
{
if(bFiltering)
bFiltering = FALSE;
else
bFiltering = TRUE;
}
else
{
if(!bFiltering)
lptszRet[k++] = lpctszString[i];
}
}
lptszRet[k] = '\0';
return lptszRet;
} |
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Wed Jun 03, 2009 1:29 am Post subject: Re: [C] My string functions |
|
|
| colour0xFFA500 wrote: | Any suggestions for improvement?
| Code: | UINT FindString(LPCTSTR lpctszString, LPCTSTR lpctszFind)
{
BOOL bMatch = TRUE;
UINT i = 0;
UINT j = 0;
for(; i < _tcsclen(lpctszString); i++, bMatch = TRUE;)
{
for(j = 0; j < _tcsclen(lpctszFind); j++)
if(lpctszString[i + j] != lpctszFind[j])
bMatch = FALSE;
if(bMatch)
return i;
}
return -1;
} |
| after 'bMatch = FALSE;' add a 'break;' because once it finds a non-matching character, there's no need to search the rest of the string
| colour0xFFA500 wrote: |
| Code: | LPTSTR FilterString(LPCTSTR lpctszString, LPCTSTR lpctszFilter)
{
BOOL bMatch = TRUE;
UINT i = 0;
UINT j = 0;
UINT k = 0;
LPTSTR lptszRet;
for(; i < _tcsclen(lpctszString); bMatch = TRUE;)
{
for(j = 0; j < _tcsclen(lpctszFilter); j++)
if(lpctszString[i + j] != lpctszFilter[j])
bMatch = FALSE;
bMatch ? i += j : lptszRet[k++] = lpctszString[i++];
}
lptszRet[k] = '\0';
return lptszRet;
} |
| Code: | LPTSTR FilterCharacter(LPCTSTR lpctszString, TCHAR tch)
{
UINT i = 0;
UINT j = 0;
LPTSTR lptszRet;
for(; i < _tcsclen(lpctszString); i++)
if(lpctszString[i] != tch)
lptszRet[j++] = lpctszString[i];
lptszRet[j] = '\0';
return lptszRet;
} |
| Code: | LPTSTR FilterSection(LPCTSTR lpctszString, LPCTSTR lpctszBegin, LPCTSTR lpctszEnd)
{
UINT i = 0;
UINT j = 0;
UINT k = 0;
BOOL bMatch = TRUE;
BOOL bFiltering = FALSE;
LPTSTR lptszRet;
for(; i < _tcsclen(lpctszString); i++)
{
for(j = 0; j < _tcsclen((!bFiltering ? lpctszBegin : lpctszEnd)); j++, bMatch = TRUE;)
if(lpctszString[i + j] != (!bFiltering ? lpctszBegin : lpctszEnd)[j])
bMatch = FALSE;
if(bMatch)
{
if(bFiltering)
bFiltering = FALSE;
else
bFiltering = TRUE;
}
else
{
if(!bFiltering)
lptszRet[k++] = lpctszString[i];
}
}
lptszRet[k] = '\0';
return lptszRet;
} |
|
lptszRet isn't assigned any value in the above three functions. You can't just create a string like that. You can either do: 'lptszRet = new tchar[256]' (where 256 is the lenght of the new string) but then you'll have to deallocate it as well. You could also do 'TCHAR lptszRet[256]' so it will be on the stack. Then after your operation has completed, you could copy the contents of lptszRet to one of the input strings for example.
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Wed Jun 03, 2009 8:33 pm Post subject: |
|
|
Unsigned integer's range is 0 ~ +4,294,967,295. <_< Why are you returning -1?
I think you should use __fastcall instead of __cdecl.
|
|
| Back to top |
|
 |
talkerzero Grandmaster Cheater
Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Fri Jun 05, 2009 4:34 pm Post subject: |
|
|
| : wrote: | Unsigned integer's range is 0 ~ +4,294,967,295. <_< Why are you returning -1?
I think you should use __fastcall instead of __cdecl. |
Returning -1 is the same as returning 4294967295, and it's just so much easier to remember. And the length of a string is never going to exceed 4294967295.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|