troubleshooting Question

Custom find functions

Avatar of chsalvia
chsalvia asked on
C++
13 Comments2 Solutions1489 ViewsLast Modified:
I created two find functions for a class I'm working on, which holds a wchar_t buffer.  (Similar to STL wstring.)  One find function is supposed to be basically the same as STL string.find(), whereas the other is supposed to search only for whole words.  

They both work well, as far as I know.  I've tested them a lot, but I want to run them by some experts to see if there is anything wrong, anything that could potentially be dangerous, any efficiency improvements, etc.  I'd appreciate any suggestions or comments to improve these or point out mistakes:

charbuffer is a member variable of type wchar_t* which holds the string data, and len is another member variable which is equivalent to wcslen(charbuffer)

// BEGIN CODE

size_t find (wchar_t* wstr, size_t pos = 0)
{
      wchar_t* p = wstr;
      size_t wstrlen = wcslen(wstr);
      size_t _wstrlen = wstrlen;
            
      while (pos < len)
      {
            if (*(charbuffer + pos) == *(p)) { p++; _wstrlen--; }
            else { p = wstr; _wstrlen = wstrlen; }
            pos++;
            if (_wstrlen == 0) { return pos-wstrlen; }
      }
      return (size_t) -1;
}

size_t findwhole (wchar_t* wstr, size_t pos = 0)
{
      wchar_t* p = wstr;
      size_t wstrlen = wcslen(wstr);
      size_t _wstrlen = wstrlen;
      
      while (pos < len)
      {
            if (*(charbuffer + pos) == *(p)) { p++; _wstrlen--; }
            else { p = wstr; _wstrlen = wstrlen; }
            pos++;
            if (_wstrlen == 0)
            {
                  if ( (isdelim(*(charbuffer+pos)) || pos == len )
                  && ( isdelim(*(charbuffer+pos-wstrlen-1)) || pos-wstrlen == 0) ) return pos - wstrlen;
                  else _wstrlen = wstrlen;
            }
      }
      return (size_t) -1;
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros