• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Latest String Class

Started by Frederick J. Harris, February 15, 2013, 11:08:54 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

James C. Fuller


Patrice Terrier

#16
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. ;)

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

#17
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;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

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

Patrice Terrier

#19
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.

Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Frederick J. Harris

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. 

James C. Fuller

Fred,
  I think just adding _PTR will handle it in both worlds.

James

Frederick J. Harris

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! :)