1 registered members (degenerate_762),
1,114
guests, and 1
spider. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Odd behavior by strmid()
#481321
08/26/20 11:53
08/26/20 11:53
|
Joined: May 2020
Posts: 27 Germany
Morris
OP
Newbie
|
OP
Newbie
Joined: May 2020
Posts: 27
Germany
|
strmid() does not return what I would sensibly expect when running into the end of the string; instead, it returns the last character before the end (going back before the 'first' parameter). This is observed with Zorro 2.25.7. Code in point:
void main() {
string test = "abc\0def";
printf("\ntest='%s'", test); // Prints 'abc' -- as expected
string mid = strmid(test, 0, 2);
printf("\nmid='%s'", mid); // Prints 'ab' -- as expected
string mid = strmid(test, 2, 2);
printf("\nmid='%s'", mid); // Prints 'c' -- as expected
string mid = strmid(test, 3, 2);
printf("\nmid='%s'", mid); // Prints 'c' -- that appears wrong. Should yield an empty string
string mid = strmid(test, 4, 2);
printf("\nmid='%s'", mid); // Prints 'c' -- that definitely appears wrong. Should print an empty string or 'de'
}
The real life case for this involved parsing a configuration file, where running mid on the end of the string should have returned just '\0'. Am I wrong in assuming this might be a bug?
|
|
|
Re: Odd behavior by strmid()
[Re: Morris]
#481332
08/28/20 08:51
08/28/20 08:51
|
Joined: Mar 2019
Posts: 357
danatrader
Senior Member
|
Senior Member
Joined: Mar 2019
Posts: 357
|
string base = strmid(Asset, 0, 3);
This works in my script, but I will verify, cause don't want some odd behaviour to crash my script.
Last edited by danatrader; 08/28/20 08:56.
|
|
|
Re: Odd behavior by strmid()
[Re: Morris]
#481333
08/28/20 09:07
08/28/20 09:07
|
Joined: May 2020
Posts: 27 Germany
Morris
OP
Newbie
|
OP
Newbie
Joined: May 2020
Posts: 27
Germany
|
danatrader, thanks again. The signature for strmid is strmid(str, first, count). So in your example, first (3) matched (not exceeded) the length of str, and count (2) did not. So I don't think this is pertinent to the case in the manual where count exceeds len(str).
> string mid = strmid(test, 3, 2); > 0123 (3 exceeds) but you want two characters, so since 3 exceeds, the complete is returned and the 2 is pulled
By your logic, strmid(test, 3, 1) would return the second character in test ("the 1 is pulled"), but it doesn't; it still returns 'c'.
I would expect strmid to return count characters starting with first (like it says in the manual, by the way). And if count is more than what's remaining after first, the whole remainder is returned (same behavior as, e.g., MID() in Excel). There is no reason, at least that I can see, why strmid would return any character *before* first.
|
|
|
|