Skip to content

Commit

Permalink
Update unicode-xid-cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
syoyo committed Apr 7, 2024
1 parent f8f9bb8 commit 3240ab0
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/unicode-xid.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,23 @@ constexpr uint32_t kMaxCodepoint = 0x10FFFF;

namespace detail {

// Assume table is sorted by the first key(lower)
// Assume table is sorted by the first key(range_begin)
#include "unicode-xid-table.inc"

}


inline bool is_xid_start(uint32_t codepoint) {
if (codepoint > kMaxCodepoint) {
return false;
}

// first find lower location based on the first key, then test with second key with linear search for (lower <= codepoint <= upper) range check.
// NOTE: second item in query is not used. fill it T::min just in case.
auto it = std::lower_bound(detail::kXID_StartTable.begin(), detail::kXID_StartTable.end(), std::make_pair(int(codepoint), (std::numeric_limits<int>::min)()));

// subtract 1 to get the first entry of possible hit(lower <= codepoint <= upper)
if ((it != detail::kXID_StartTable.begin() && (int(codepoint) < it->second))) {
it--;
}
// first a range(range_begin <= codepoint <= range_end) by comparing the second key(range end).
auto it = std::lower_bound(detail::kXID_StartTable.begin(), detail::kXID_StartTable.end(), int(codepoint), [](const std::pair<int, int> &a, const int b) {
return a.second < b;
});

for (; it != detail::kXID_StartTable.end(); it++) {
if ((int(codepoint) >= it->first) && (int(codepoint) <= it->second)) { // range end is inclusive.
if (it != detail::kXID_StartTable.end()) {
if ((int(codepoint) >= it->first) && (int(codepoint) <= it->second)) { // range end is inclusive.
return true;
}
}
Expand All @@ -52,15 +47,12 @@ inline bool is_xid_continue(uint32_t codepoint) {
return false;
}

auto it = std::lower_bound(detail::kXID_ContinueTable.begin(), detail::kXID_ContinueTable.end(), std::make_pair(int(codepoint), (std::numeric_limits<int>::min)()));

// subtract 1 to get the first entry of possible hit(lower <= codepoint <= upper)
if ((it != detail::kXID_ContinueTable.begin() && (int(codepoint) < it->second))) {
it--;
}
auto it = std::lower_bound(detail::kXID_ContinueTable.begin(), detail::kXID_ContinueTable.end(), int(codepoint), [](const std::pair<int, int> &a, const int b) {
return a.second < b;
});

for (; it != detail::kXID_ContinueTable.end(); it++) {
if ((int(codepoint) >= it->first) && (int(codepoint) <= it->second)) { // range end is inclusive.
if (it != detail::kXID_ContinueTable.end()) {
if ((int(codepoint) >= it->first) && (int(codepoint) <= it->second)) { // range end is inclusive.
return true;
}
}
Expand Down

0 comments on commit 3240ab0

Please sign in to comment.