FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rust-cssparser/src/unicode_range.rs at rm-parserinput · servo/rust-cssparser · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
servo
/
rust-cssparser
Public
Notifications
You must be signed in to change notification settings
Fork
152
Star
869
Code
Issues
21
Pull requests
5
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rust-cssparser
/
src
/
unicode_range.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
177 lines (161 loc) · 5.43 KB
Breadcrumbs
rust-cssparser
/
src
/
unicode_range.rs
Copy path
File metadata and controls
177 lines (161 loc) · 5.43 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! https://drafts.csswg.org/css-syntax/#urange
use
crate
::
tokenizer
::
Token
;
use
crate
::
{
BasicParseError
,
Parser
,
ToCss
}
;
use
std
::
char
;
use
std
::
fmt
;
/// One contiguous range of code points.
///
/// Can not be empty. Can represent a single code point when start == end.
#
[
derive
(
PartialEq
,
Eq
,
Clone
,
Hash
)
]
#
[
repr
(
C
)
]
pub
struct
UnicodeRange
{
/// Inclusive start of the range. In [0, end].
pub
start
:
u32
,
/// Inclusive end of the range. In [0, 0x10FFFF].
pub
end
:
u32
,
}
impl
UnicodeRange
{
/// https://drafts.csswg.org/css-syntax/#urange-syntax
pub
fn
parse
(
input
:
&
mut
Parser
)
->
Result
<
Self
,
BasicParseError
>
{
// <urange> =
// u '+' <ident-token> '?'* |
// u <dimension-token> '?'* |
// u <number-token> '?'* |
// u <number-token> <dimension-token> |
// u <number-token> <number-token> |
// u '+' '?'+
input
.
expect_ident_matching
(
"u"
)
?
;
let
after_u = input
.
position
(
)
;
parse_tokens
(
input
)
?
;
// This deviates from the spec in case there are CSS comments
// between tokens in the middle of one <unicode-range>,
// but oh well…
let
concatenated_tokens = input
.
slice_from
(
after_u
)
;
let
range =
match
parse_concatenated
(
concatenated_tokens
.
as_bytes
(
)
)
{
Ok
(
range
)
=> range
,
Err
(
(
)
)
=>
return
Err
(
BasicParseError
::
unexpected_token
(
)
)
,
}
;
if
range
.
end
> char
::
MAX
as
u32
|| range
.
start
> range
.
end
{
Err
(
BasicParseError
::
unexpected_token
(
)
)
}
else
{
Ok
(
range
)
}
}
}
fn
parse_tokens
(
input
:
&
mut
Parser
)
->
Result
<
(
)
,
BasicParseError
>
{
match
*
input
.
next_including_whitespace
(
)
?
{
Token
::
Delim
(
'+'
)
=>
{
match
*
input
.
next_including_whitespace
(
)
?
{
Token
::
Ident
(
_
)
=>
{
}
Token
::
Delim
(
'?'
)
=>
{
}
_ =>
return
Err
(
BasicParseError
::
unexpected_token
(
)
)
,
}
parse_question_marks
(
input
)
}
Token
::
Dimension
{
..
}
=>
parse_question_marks
(
input
)
,
Token
::
Number
{
..
}
=>
{
let
after_number = input
.
state
(
)
;
match
input
.
next_including_whitespace
(
)
{
Ok
(
&
Token
::
Delim
(
'?'
)
)
=>
parse_question_marks
(
input
)
,
Ok
(
&
Token
::
Dimension
{
..
}
)
=>
{
}
Ok
(
&
Token
::
Number
{
..
}
)
=>
{
}
_ => input
.
reset
(
&
after_number
)
,
}
}
_ =>
return
Err
(
BasicParseError
::
unexpected_token
(
)
)
,
}
Ok
(
(
)
)
}
/// Consume as many '?' as possible
fn
parse_question_marks
(
input
:
&
mut
Parser
)
{
loop
{
let
start = input
.
state
(
)
;
match
input
.
next_including_whitespace
(
)
{
Ok
(
&
Token
::
Delim
(
'?'
)
)
=>
{
}
_ =>
{
input
.
reset
(
&
start
)
;
return
;
}
}
}
}
fn
parse_concatenated
(
text
:
&
[
u8
]
)
->
Result
<
UnicodeRange
,
(
)
>
{
let
mut
text =
match
text
.
split_first
(
)
{
Some
(
(
&
b'+'
,
text
)
)
=> text
,
_ =>
return
Err
(
(
)
)
,
}
;
let
(
first_hex_value
,
hex_digit_count
)
=
consume_hex
(
&
mut
text
,
6
)
?
;
let
question_marks =
consume_question_marks
(
&
mut
text
)
;
let
consumed = hex_digit_count + question_marks
;
if
consumed ==
0
|| consumed >
6
{
return
Err
(
(
)
)
;
}
if
question_marks >
0
{
if
text
.
is_empty
(
)
{
return
Ok
(
UnicodeRange
{
start
:
first_hex_value <<
(
question_marks
*
4
)
,
end
:
(
(
first_hex_value +
1
)
<<
(
question_marks
*
4
)
)
-
1
,
}
)
;
}
}
else
if
text
.
is_empty
(
)
{
return
Ok
(
UnicodeRange
{
start
:
first_hex_value
,
end
:
first_hex_value
,
}
)
;
}
else
if
let
Some
(
(
&
b'-'
,
mut
text
)
)
= text
.
split_first
(
)
{
let
(
second_hex_value
,
hex_digit_count
)
=
consume_hex
(
&
mut
text
,
6
)
?
;
if
hex_digit_count >
0
&& hex_digit_count <=
6
&& text
.
is_empty
(
)
{
return
Ok
(
UnicodeRange
{
start
:
first_hex_value
,
end
:
second_hex_value
,
}
)
;
}
}
Err
(
(
)
)
}
// Consume hex digits, but return an error if more than digit_limit are found.
fn
consume_hex
(
text
:
&
mut
&
[
u8
]
,
digit_limit
:
usize
)
->
Result
<
(
u32
,
usize
)
,
(
)
>
{
let
mut
value =
0
;
let
mut
digits =
0
;
while
let
Some
(
(
&
byte
,
rest
)
)
= text
.
split_first
(
)
{
if
let
Some
(
digit_value
)
=
(
byte
as
char
)
.
to_digit
(
16
)
{
if
digits == digit_limit
{
return
Err
(
(
)
)
;
}
value = value
*
0x10
+ digit_value
;
digits +=
1
;
*
text = rest
;
}
else
{
break
;
}
}
Ok
(
(
value
,
digits
)
)
}
fn
consume_question_marks
(
text
:
&
mut
&
[
u8
]
)
->
usize
{
let
mut
question_marks =
0
;
while
let
Some
(
(
&
b'?'
,
rest
)
)
= text
.
split_first
(
)
{
question_marks +=
1
;
*
text = rest
}
question_marks
}
impl
fmt
::
Debug
for
UnicodeRange
{
fn
fmt
(
&
self
,
formatter
:
&
mut
fmt
::
Formatter
)
-> fmt
::
Result
{
self
.
to_css
(
formatter
)
}
}
impl
ToCss
for
UnicodeRange
{
fn
to_css
<
W
>
(
&
self
,
dest
:
&
mut
W
)
-> fmt
::
Result
where
W
:
fmt
::
Write
,
{
write
!
(
dest
,
"U+{:X}"
,
self
.
start
)
?
;
if
self
.
end
!=
self
.
start
{
write
!
(
dest
,
"-{:X}"
,
self
.
end
)
?
;
}
Ok
(
(
)
)
}
}
Back
|
FazBrowse Home
|
New Git URL