Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C] My string functions

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
talkerzero
Grandmaster Cheater
Reputation: 1

Joined: 24 Jul 2008
Posts: 560
Location: California

PostPosted: Tue Jun 02, 2009 4:39 pm    Post subject: [C] My string functions Reply with quote

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
View user's profile Send private message Visit poster's website
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Wed Jun 03, 2009 1:29 am    Post subject: Re: [C] My string functions Reply with quote

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
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Wed Jun 03, 2009 8:33 pm    Post subject: Reply with quote

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
View user's profile Send private message
talkerzero
Grandmaster Cheater
Reputation: 1

Joined: 24 Jul 2008
Posts: 560
Location: California

PostPosted: Fri Jun 05, 2009 4:34 pm    Post subject: Reply with quote

: 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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites