[Gambas-devel] Comparing Gambas strings
Tobias Boege
tobias at ...692...
Fri Aug 16 00:07:30 CEST 2013
Hi Benoit,
I'm a little uncertain about the following code:
---
BEGIN_METHOD(Class_Method, GB_STRING a; GB_STRING b)
char *b = GB.NewString(STRING(b), LENGTH(b));
int res = strncmp(STRING(a), b, LENGTH(a) + 1);
END_METHOD
---
I just want to compare (case-unsensitively) the given Gambas strings 'a' and
'b'. As I saw in the sources of STRING_new(), the resulting string 'b' is
always NUL terminated. (I need it GB.NewString'd anyway.)
So this seems correct to me: strncmp() compares the maybe-not-NUL-terminated
string 'a' and 'b' until either string reaches a NUL or the given number of
bytes was searched.
That there is a NUL in 'b' is sure. And for 'a', the NUL is either at
LENGTH(a) + 1 or the string is part of a larger one, right? As if we called
Class.Method(Mid$("String containing 'a'", 3, 3), "string b")
In any case it will *not* produce a segfault if I instruct strncmp() to
examine LENGTH(a) + 1 bytes. Is this correct?
Additionally, it will lead me a correct comparison of the two strings, or
not? I fear that something like this could produce false positives:
Class.Method(Mid$("sample", 1, 2), "sample")
Should I also compare the strings' lengths? Sounds kind of inconvenient
because all I want is to compare ASCII strings case-unsensitively. Maybe I
have overlooked an API or I don't get the right idea just now.
Regards,
Tobi
More information about the Devel
mailing list