Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
identifier.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <array>
5#include <span>
6#include <string>
7
8#include "table_store.hpp"
9#include "id_tables.cpp"
10
11namespace ada::idna {
12constexpr bool is_ascii_letter(char32_t c) noexcept {
13 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
14}
15
16constexpr bool is_ascii_letter_or_digit(char32_t c) noexcept {
17 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
18 (c >= '0' && c <= '9');
19}
20
21bool valid_name_code_point(char32_t code_point, bool first) {
22 // https://tc39.es/ecma262/#prod-IdentifierStart
23
24 // Fast paths (no table needed)
25 if (first && (code_point == U'$' || code_point == U'_' ||
26 is_ascii_letter(code_point))) {
27 return true;
28 }
29 if (!first && (code_point == U'$' || is_ascii_letter_or_digit(code_point))) {
30 return true;
31 }
32
33 // Minimal error handling for invalid code point
34 if (code_point > 0x10FFFF) {
35 return false;
36 }
37 if (code_point >= 0xD800 && code_point <= 0xDFFF) {
38 return false;
39 }
40
41 if (!ensure_tables() || id_start == nullptr || id_continue == nullptr) {
42 return false;
43 }
44
45 // Slow path: binary search through the appropriate Unicode range table.
46 // Each entry is a [low, high] inclusive range.
47 const std::span<const uint32_t[2]> ranges =
48 first ? std::span<const uint32_t[2]>{ada::idna::id_start,
50 : std::span<const uint32_t[2]>{ada::idna::id_continue,
52
53 const auto iter = std::ranges::lower_bound(
54 ranges, code_point, {},
55 [](const auto& range) { return range[1]; }); // project to range-high
56
57 return iter != ranges.end() && code_point >= (*iter)[0];
58}
59} // namespace ada::idna
constexpr size_t id_continue_count
range_pair_ptr id_continue
constexpr bool is_ascii_letter(char32_t c) noexcept
bool ensure_tables() noexcept
bool valid_name_code_point(char32_t code_point, bool first)
range_pair_ptr id_start
constexpr bool is_ascii_letter_or_digit(char32_t c) noexcept
constexpr size_t id_start_count