-
Notifications
You must be signed in to change notification settings - Fork 97
Add Unicode and escape sequence support #624
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,94 @@ | ||
| #pragma once | ||
|
|
||
| #include <cassert> | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
| #include <string_view> | ||
|
|
||
| namespace CppClingo::Util { | ||
|
|
||
| void quote(std::string_view in, auto out) { | ||
| for (auto c : in) { | ||
| switch (c) { | ||
| case '\n': { | ||
| *out++ = '\\'; | ||
| *out++ = 'n'; | ||
| break; | ||
| } | ||
| case '\t': { | ||
| *out++ = '\\'; | ||
| *out++ = 't'; | ||
| break; | ||
| } | ||
| case '\\': { | ||
| *out++ = '\\'; | ||
| *out++ = '\\'; | ||
| break; | ||
| } | ||
| case '"': { | ||
| *out++ = '\\'; | ||
| *out++ = '"'; | ||
| break; | ||
| } | ||
| default: { | ||
| *out++ = c; | ||
| break; | ||
| namespace Detail { | ||
|
|
||
| // NOLINTBEGIN(readability-magic-numbers) | ||
|
|
||
| inline auto hex_val(char c) -> uint32_t { | ||
| if (c >= '0' && c <= '9') { | ||
| return c - '0'; | ||
| } | ||
| if (c >= 'A' && c <= 'F') { | ||
| return 10 + (c - 'A'); | ||
| } | ||
| if (c >= 'a' && c <= 'f') { | ||
| return 10 + (c - 'a'); | ||
| } | ||
| throw std::invalid_argument("invalid hex character"); | ||
| } | ||
|
|
||
| inline auto encode_utf8(uint32_t cp, auto out) -> void { | ||
|
Member
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. Add an assertion for the accepted input range or throw an exception. |
||
| if (cp > 0x10FFFF) { | ||
| throw std::invalid_argument("invalid unicode code point"); | ||
| } | ||
| if (cp <= 0x7F) { | ||
| *out++ = static_cast<char>(cp); | ||
| } else if (cp <= 0x7FF) { | ||
| *out++ = static_cast<char>(0xC0 | (cp >> 6)); | ||
| *out++ = static_cast<char>(0x80 | (cp & 0x3F)); | ||
| } else if (cp <= 0xFFFF) { | ||
| *out++ = static_cast<char>(0xE0 | (cp >> 12)); | ||
| *out++ = static_cast<char>(0x80 | ((cp >> 6) & 0x3F)); | ||
| *out++ = static_cast<char>(0x80 | (cp & 0x3F)); | ||
| } else { | ||
| *out++ = static_cast<char>(0xF0 | (cp >> 18)); | ||
| *out++ = static_cast<char>(0x80 | ((cp >> 12) & 0x3F)); | ||
| *out++ = static_cast<char>(0x80 | ((cp >> 6) & 0x3F)); | ||
| *out++ = static_cast<char>(0x80 | (cp & 0x3F)); | ||
| } | ||
| } | ||
|
|
||
| inline auto parse_unicode_escape(auto it, auto ie, auto out) -> auto { | ||
| uint32_t cp = 0; | ||
| if (it == ie || *it != '{') { | ||
| throw std::runtime_error("expected '{' at the beginning of unicode escape"); | ||
| } | ||
| size_t count = 0; | ||
| for (++it; it != ie; ++it, ++count) { | ||
| if (*it == '}') { | ||
| if (count == 0) { | ||
| throw std::runtime_error("expected at least one hex digit in unicode escape"); | ||
| } | ||
| encode_utf8(cp, out); | ||
| return it; | ||
| } | ||
| if (count >= 6) { | ||
| throw std::runtime_error("too many hex digits in unicode escape"); | ||
| } | ||
| cp = (cp << 4) | hex_val(*it); | ||
| } | ||
| throw std::runtime_error("expected '}' at the end of unicode escape"); | ||
| } | ||
|
|
||
| // NOLINTEND(readability-magic-numbers) | ||
|
|
||
| } // namespace Detail | ||
|
|
||
| void unquote(std::string_view in, auto out, bool fstring = false) { | ||
| auto escape = '\0'; | ||
| for (auto c : in) { | ||
| // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) | ||
| for (auto it = in.begin(), ie = in.end(); it != ie; ++it) { | ||
| char c = *it; | ||
| if (escape == '{' || escape == '}') { | ||
| if (c == escape) { | ||
| *out++ = escape; | ||
| } else { | ||
| assert(false); | ||
| throw std::runtime_error("expected brace"); | ||
| } | ||
| escape = '\0'; | ||
| } else if (escape == '\\') { | ||
| switch (c) { | ||
| case 'u': { | ||
| // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) | ||
| it = Detail::parse_unicode_escape(it + 1, ie, out); | ||
| break; | ||
| } | ||
| case 'n': { | ||
| *out++ = '\n'; | ||
| break; | ||
|
|
@@ -55,6 +97,10 @@ void unquote(std::string_view in, auto out, bool fstring = false) { | |
| *out++ = '\t'; | ||
| break; | ||
| } | ||
| case 'r': { | ||
| *out++ = '\r'; | ||
| break; | ||
| } | ||
| case '\\': { | ||
| *out++ = '\\'; | ||
| break; | ||
|
|
@@ -64,8 +110,7 @@ void unquote(std::string_view in, auto out, bool fstring = false) { | |
| break; | ||
| } | ||
| default: { | ||
| assert(false); | ||
| break; | ||
| throw std::runtime_error("invalid escape sequence"); | ||
| } | ||
| } | ||
| escape = '\0'; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 would typically turn the condition into an assertion or exception and simply return; or throw an exception at the end.