-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't assume that char_traits::compare returns +/-1 (#225)
- Loading branch information
Showing
1 changed file
with
2 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aa741ba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you have the same problem on the next lines as well? (729, 730)
aa741ba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Unlike
char_traits::compare
, the size comparison always returns +/-1: https://github.com/cppformat/cppformat/blob/477962884e36ae490f2414df44dbc60abcba1ff2/format.h#L313aa741ba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok... OTOH
you could do:
instead of the branching/comparisons. On my box that is 2 vs ~7 instructions.
aa741ba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My suggestion above would not work correctly with very large strings though, but is what libstdc++ does
in essence. To handle huge strings you would have to add the comparions again.
aa741ba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this is not portable because
size_t
can be larger thanint
. It is possible to check this at compile time and usesize_ - other.size_
when possible, but it's probably an overkill.aa741ba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Another option is to return a signed counterpart of
size_t
formcompare
instead ofint
, but again, I don't think it's worth the trouble.aa741ba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right.
However I still thing your tests should not test for -1,1 explicit. IMHO better to do you did and compare
againt < > 0 in all cases.
aa741ba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. Removed the +/-1 assumption in d6d019a. Thanks for the feedback.