Ada
4.1.1
Fast spec-compliant URL parser
Toggle main menu visibility
Loading...
Searching...
No Matches
identifier.cpp
Go to the documentation of this file.
1
#include "
ada/idna/identifier.h
"
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
11
namespace
ada::idna
{
12
constexpr
bool
is_ascii_letter
(
char32_t
c)
noexcept
{
13
return
(c >=
'A'
&& c <=
'Z'
) || (c >=
'a'
&& c <=
'z'
);
14
}
15
16
constexpr
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
21
bool
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
,
49
ada::idna::id_start_count
}
50
: std::span<const uint32_t[2]>{
ada::idna::id_continue
,
51
ada::idna::id_continue_count
};
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
id_tables.cpp
identifier.h
ada::idna
Definition
identifier.h:7
ada::idna::id_continue_count
constexpr size_t id_continue_count
Definition
ada_idna.cpp:4900
ada::idna::id_continue
range_pair_ptr id_continue
Definition
ada_idna.cpp:4888
ada::idna::is_ascii_letter
constexpr bool is_ascii_letter(char32_t c) noexcept
Definition
ada_idna.cpp:6963
ada::idna::ensure_tables
bool ensure_tables() noexcept
Definition
ada_idna.cpp:4956
ada::idna::valid_name_code_point
bool valid_name_code_point(char32_t code_point, bool first)
Definition
ada_idna.cpp:6972
ada::idna::id_start
range_pair_ptr id_start
Definition
ada_idna.cpp:4889
ada::idna::joining_type::U
@ U
Definition
ada_idna.cpp:6070
ada::idna::is_ascii_letter_or_digit
constexpr bool is_ascii_letter_or_digit(char32_t c) noexcept
Definition
ada_idna.cpp:6967
ada::idna::id_start_count
constexpr size_t id_start_count
Definition
ada_idna.cpp:4901
table_store.hpp