Repository navigation
Description
Submitted by hendrikweisser on 2010-04-13 15:14:29
There is a compatibility problem between methods of the string classes in OpenMS and in the C++ standard library. Basically, the String::substr methods accept negative indices, but the special value string::npos (see [http://www.cplusplus.com/reference/string/string/npos/]) gets mapped to -1, which leads to unexpected results.
Basically:
string x = "abc";
x.substr(0, x.find("d")) == "abc"; // true
String y = "abc";
y.substr(0, y.find("d")) == "ab"; // true, but unexpected
As a workaround, I've changed the signature of String::substr to use Int64 instead of SignedSize (not committed yet!). This works on my 32 bit machine (string::npos is no longer mapped to -1), but may not work for 64 bit (if size_t uses 64 bit, it won't work).
Commented by cbielow on 2010-04-15 10:04:52:
- Why use Int64 and not Size?
- Most important question: What are the side-effects of disabling support for negative indices?! Isn't this used extensively in OpenMS already?
- Why shouldn't it work on 64bit?
Commented by hendrikweisser on 2010-04-15 14:17:03:
- That would mean disallowing negative indices.
- Good question. I don't know.
- The basic problem is that going from an unsigned to a signed type, the maximum value will overflow to -1. string::npos is defined as the maximum of size_t. On a 32 bit system, where size_t is 32 bit, we can solve this by using a 64 bit signed type, which is large enough to avoid the overflow. However, on a 64 bit system, size_t is already 64 bit long (I've just checked this), so we would need an even larger signed data type.
Commented by cbielow on 2010-04-15 15:15:27:
1 & 2: I guess I assumed Int64 to be unsigned. Naturally, its not.
- So that means all tests did still pass?
To solve this I just tried the following:
add a second implementation of substr as
String substr(SignedSize start, Size n) const;
and rewrite accordingly.
At least the MS compiler will prefer this one, when given an unsigned type (tested with Size on 64 bit). For UInt (which is 32bit on 64bit windows) one will get a "ambiguous function call", but thats something that the programmer can resolve.
Commented by hendrikweisser on 2010-04-15 16:46:02:
String substr(SignedSize start, Size n) const;
Sounds good. I've added the function and it seems to work. I'll check it in.
Commented by hendrikweisser on 2010-04-16 21:53:37:
Fix described above was committed in rev. 6912, but reverted in rev. 6914. It caused lots of "ambiguous overload" errors on a 64 bit system (I had tested only on my 32 bit machine).
I propose to rename the substr(SignedSize, SignedSize) method, maybe to "signedSubstr" or "flexibleSubstr", and to inherit the default substr(size_t, size_t) from std::string.
Commented by cbielow on 2010-04-17 15:46:44:
Replying to [comment:5 hendrikweisser]:
Fix described above was committed in rev. 6912, but reverted in rev. 6914. It caused lots of "ambiguous overload" errors on a 64 bit system (I had tested only on my 32 bit machine).
How many where there?
I propose to rename the substr(SignedSize, SignedSize) method, maybe to "signedSubstr" or "flexibleSubstr", and to inherit the default substr(size_t, size_t) from std::string.
Doing so will be at least as much work as fixing the ambiguouity errors, because one has to look at every single occurence of substr() and decide what was intended.
Commented by hendrikweisser on 2010-04-17 23:02:22:
How many where there?
I think the error occurs for every instance that wasn't explicitly qualified - so, probably all of them. I don't know how many there are in total, because compilation stops at the first target with errors. After I've fixed one case in XMLHandler, there were so many in the next class that I thought: "Nope, I'm not going to do this."
Doing so will be at least as much work as fixing the ambiguouity errors, because one has to look at every single occurence of substr() and decide what was intended.
I don't think so. Since it's worked so far, you can just replace every occurrence where the arguments aren't clearly both positive with the new name (i.e. old implementation). And the regexp for doing this would be simpler than one that adds the type qualifiers. ;-)
More importantly, I think this solution (separating two related but different methods by name) would be cleaner and it wouldn't require anything special if you just want a normal "substr" call.
Commented by cbielow on 2010-04-19 18:02:56:
Replying to [comment:7 hendrikweisser]:
How many where there?
I think the error occurs for every instance that wasn't explicitly qualified - so, probably all of them. I don't know how many there are in total, because compilation stops at the first target with errors. After I've fixed one case in XMLHandler, there were so many in the next class that I thought: "Nope, I'm not going to do this."
On win64 I'm getting something like 20 ambigouity errors, but most of them look like this:
it->substr(0,-1)
I agree that resolving the ambigouity with
it->substr(0,(SignedSize) -1)
is bad style, but in this case a simple
it->prefix(it->size()-1)
will do as well.
Especially I could not find a single instance of substr() where it is not clear from the code what sign the length parameter will have. Thus the need for substr() to support both seems really useless (and adds to slower code, due to many checks inside the function).
So one clean solution seems to be:
- write a new function which clips n chars from the end of a string (I could not find an instance where start>0 and n<0) or just use prefix()
- fix occurences where negative indices are used (e.g. using the ambigouity function)
- remove our substring(pos,n) overload
Doing so will be at least as much work as fixing the ambiguouity errors, because one has to look at every single occurence of substr() and decide what was intended.
I don't think so. Since it's worked so far, you can just replace every occurrence where the arguments aren't clearly both positive with the new name (i.e. old implementation). And the regexp for doing this would be simpler than one that adds the type qualifiers. ;-)
More importantly, I think this solution (separating two related but different methods by name) would be cleaner and it wouldn't require anything special if you just want a normal "substr" call.
Commented by hendrikweisser on 2010-04-20 14:49:12:
- write a new function which clips n chars from the end of a string (I could not find an instance where start>0 and n<0) or just use prefix()
We could change "prefix" to also accept a negative length - there is already an overload for Int that currently throws an exception if the argument is less than zero.
For completeness, the same could be done for "suffix". Since "prefix" and "suffix" aren't std::string methods, there would be no compatibility issues.
I only wonder why the current overloads of "prefix" and "suffix" don't produce the ambiguity error?
Commented by aiche on 2010-04-21 17:00:24:
I started to implement a fix like chris proposed it
write a new function which clips n chars from the end of a string (I could not find an instance where start>0 and n<0) or just use prefix()(done, method is named crop)fix occurences where negative indices are used (e.g. using the ambigouity function)(done)- remove our substring(pos,n) overload
The last point ist problematic. If we remove the overload the stl method returns a std::string so chaining is broken
Example:
String a = "abc def";
a.substr(3).trim(); // fails since trim is a OpenMS String method, but substr() returns a stl string
Commented by aiche on 2010-04-21 17:01:54:
a bit more readable
Example Code
String a = "abc def";
a.substr(3).trim();
The call of trim fails since trim is a OpenMS String method, but substr() returns a stl string
Commented by hendrikweisser on 2010-04-21 17:17:26:
Just wrap the std::string::substr method in a String::substr method (same arguments, but returns a String). Shouldn't that solve the problem?
Commented by aiche on 2010-04-21 17:21:00:
This should lead to ambiguity errors since it is an overload with same parameter set but different return values. But I will check ..
Commented by hendrikweisser on 2010-04-21 17:29:34:
No, it shouldn't, because you're overriding the method, not overloading.
Commented by aiche on 2010-04-22 18:27:39:
Fixed in [6919] ..
following things were done
- write a new function (chop()) which clips n chars from the end of a string (I could not find an instance where start>0 and n<0) or just use prefix()
- fix occurences where negative indices are used (e.g. using the ambigouity function) (done)
- wrap the STL substr() method to return an OpenMS String object
Activity
ci(macOS): default visibility on openms_thermo_bridge target (try #9392)
[FEATURE] Add Thermo RAW file reading support via openms-thermo-bridge (