-
Notifications
You must be signed in to change notification settings - Fork 161
make x3 interger parser less dependent on fundamental character types #762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,17 +112,19 @@ namespace boost { namespace spirit { namespace x3 { namespace detail | |
template <typename Char> | ||
inline static bool is_valid(Char ch) | ||
{ | ||
return (ch >= '0' && ch <= (Radix > 10 ? '9' : static_cast<Char>('0' + Radix -1))) | ||
|| (Radix > 10 && ch >= 'a' && ch <= static_cast<Char>('a' + Radix -10 -1)) | ||
|| (Radix > 10 && ch >= 'A' && ch <= static_cast<Char>('A' + Radix -10 -1)); | ||
return (ch >= '0' && ch <= (Radix > 10 ? '9' : static_cast<char>('0' + Radix -1))) | ||
|| (Radix > 10 && ch >= 'a' && ch <= static_cast<char>('a' + Radix -10 -1)) | ||
|| (Radix > 10 && ch >= 'A' && ch <= static_cast<char>('A' + Radix -10 -1)); | ||
} | ||
|
||
template <typename Char> | ||
inline static unsigned digit(Char ch) | ||
{ | ||
return (Radix <= 10 || (ch >= '0' && ch <= '9')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a compile time branch, an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this minor optimization is better than simplicity. But If it is really important that the commit won't change the performance in any branch, we could add this compile time optimization back in. Then we could keep this optimization and don't rely on char_encoding::ascii::tolower.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a IMO the two changes (with Radix constexpr optimization for the 2nd one?) are better implementation than the original code. They provide better semantics and less dependency. If you @Kojoley agree with both changes I could add a simplified char_ascii type and test cases to cover this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does your 'user defined char type' support bitwise operators? Will replacing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For fundamental char types, integral promotion is performed before doing bitwise operations. Basically it is an integer operation and relies on implicit conversion to integer type. For our But I do see there are some performance improvement with
The performance is the same as below with fundamental char types.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think your char type design is just broken. Is there
I don't want to add a code path that literally nobody else is using and also is not tested by the test suite. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We simply don't use
You can take this pull request as a refactoring suggestion. The first change |
||
return ch < 'A' | ||
? ch - '0' | ||
: char_encoding::ascii::tolower(ch) - 'a' + 10; | ||
: ch < 'a' | ||
? ch - 'A' + 10 | ||
: ch - 'a' + 10; | ||
} | ||
}; | ||
|
||
|
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.
I agree that at least for consistency it is better, though the original code is relying without a static assert or a comment on a fact that all these constants always are positive numbers and radix addition to a constant would never overflow or will be caught by the compiler.