Author Topic: Latest String Class  (Read 9984 times)

0 Members and 1 Guest are viewing this topic.

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 595
  • User-Rate: +11/-8
Re: Latest String Class
« Reply #15 on: February 18, 2013, 10:36:54 AM »
Fred,
  32 or 64 bit?

James

Offline Patrice Terrier

  • ROMs
  • Hero Member
  • *****
  • Posts: 934
  • User-Rate: +62/-1
    • www.zapsolution.com
Re: Latest String Class
« Reply #16 on: February 18, 2013, 11:04:16 AM »
Fred,

The main difference between PowerBASIC INSTR and a C/C++ version, is that PB is 1-based, while C/C++ is 0-based, and return -1 when the searched string couldn't be found, thus anyway we have to pay much attention when translating from one language to another.

Also switching to UNICODE wstring, add an extra level of complexity, especially when dealing with string comparison.

James, (32 or 64-bit?)
I am doing all that hard work with C/C++, only because i need a 64-bit compiler. ;)

...
« Last Edit: February 18, 2013, 11:07:03 AM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Patrice Terrier

  • ROMs
  • Hero Member
  • *****
  • Posts: 934
  • User-Rate: +62/-1
    • www.zapsolution.com
Basic wstring INSTR
« Reply #17 on: February 18, 2013, 11:58:28 AM »
WARNING, (instead of 1-based like in PB), this function is zero-based to match C++ standard.

long INSTR(IN long fromPos, IN wstring sMain, IN wstring sSearch) {
    long nRet = -1; // Not found
    long nLength = sMain.length();
    if (nLength && (sSearch.length())) {
        if (fromPos < 0) {
            nRet = sMain.find_last_of(sSearch, nLength + fromPos + 1); }
        else {
            nRet = sMain.find_first_of(sSearch, fromPos);
        }
    }
    return nRet;
}
« Last Edit: February 18, 2013, 12:01:29 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 595
  • User-Rate: +11/-8
Re: Latest String Class
« Reply #18 on: February 18, 2013, 03:08:59 PM »
Patrice,
  I know what your priorities are :)
 I was wondering if Fred did his testing and timing with 32 or 64 bit. Timing data would mean little to you, I would assume, if it was all 32 bit?

James

Offline Patrice Terrier

  • ROMs
  • Hero Member
  • *****
  • Posts: 934
  • User-Rate: +62/-1
    • www.zapsolution.com
Basic wstring PARSE
« Reply #19 on: February 18, 2013, 04:24:22 PM »
Note: This code must be used altogether with the RTRIM function.

wstring PARSE(IN wstring sMain, IN wstring sDelim, IN long nIndex) {
    wstring sResult = L"";
    long nLength = sDelim.length();
    if (nLength == 0) { sDelim = L","; ++nLength; } // Use comma "," as default delimiter
    sMain = RTRIM(sMain, sDelim); sMain += sDelim;
    if (sMain.length() && nLength) {
        long prev_pos = 0, pos = 0, nCount = 0;
        while( (pos = sMain.find(sDelim, pos)) != std::wstring::npos ) {
            wstring substring(sMain.substr(prev_pos, pos - prev_pos));
            ++nCount;
            if (nCount == nIndex) { sResult = substring; break; }
            prev_pos = ++pos;
        }
    }
    return sResult;
}



James,

Yes, i do not care much of 32-bit timing, my first concern being to convert my PowerBASIC code to C/C++, with minimum changes, in order to maintain both code in parallel.

This is the reason why i need to have first all the basic string manipulation facilities we are accustomed in PB, keeping the same name (and syntax when possible) to ease readability.

« Last Edit: February 18, 2013, 06:26:09 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Latest String Class
« Reply #20 on: February 18, 2013, 05:53:29 PM »
Quote
Fred,
  32 or 64 bit?

Sorry James!  I believe you are asking about those two programs I posted regarding timing/speed?  Those I compiled and tested as 32 bit, as that is what I had been using when I wrote them several years ago.  I never tested them as 64 bit.  Never even thought of it!  I'll have to see if it makes any difference.

The other programs I posted worked as either 32 bit or 64 bit, and I did test them each way.  Do you know off the top of your head what the best conversion is from 32 bit unsigned integer to 64 bit unsigned integer?  I noticed the C strlen() function returns a 64 bit number in 64 bit, and requires casting down to an int if that's where you want to assign it.  I know there vis a size_t, and I think a SIZE_T. 

For my C++ InStr() routines I'm using a 1 offset rather than 0 offset because like Patrice, I always counted that way with Mid, Left, Right, etc. 

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 595
  • User-Rate: +11/-8
Re: Latest String Class
« Reply #21 on: February 18, 2013, 07:00:39 PM »
Fred,
  I think just adding _PTR will handle it in both worlds.

James

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Latest String Class
« Reply #22 on: February 18, 2013, 08:14:47 PM »
Quote
WARNING, (instead of 1-based like in PB), this function is zero-based to match C++ standard.

I believe I just used one based counting in my string class Patrice.  What's the point of doing it if it isn't going to make things easier for you in your work?  Its not as if C++ coders are going to be coming to us begging for our Left$, Right$, Mid$, InStr$ translations of Basic string handling functions!  Just my opinion! :)