Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
Chibicc
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
邓家琪
Chibicc
Commits
a8ceed4b
Commit
a8ceed4b
authored
4 years ago
by
Rui Ueyama
Browse files
Options
Downloads
Patches
Plain Diff
Add UTF-32 string literal
parent
7f39f986
Branches
Branches containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
test/unicode.c
+11
-0
11 additions, 0 deletions
test/unicode.c
tokenize.c
+29
-0
29 additions, 0 deletions
tokenize.c
with
40 additions
and
0 deletions
test/unicode.c
+
11
−
0
View file @
a8ceed4b
...
...
@@ -41,6 +41,17 @@ int main() {
ASSERT
(
u'b'
,
u"βb"
[
1
]);
ASSERT
(
0
,
u"βb"
[
2
]);
ASSERT
(
4
,
sizeof
(
U""
));
ASSERT
(
20
,
sizeof
(
U"
\xff
zzz"
));
ASSERT
(
0
,
memcmp
(
U""
,
"
\0\0\0\0
"
,
4
));
ASSERT
(
0
,
memcmp
(
U"abc"
,
"a
\0\0\0
b
\0\0\0
c
\0\0\0\0\0\0\0
"
,
16
));
ASSERT
(
0
,
memcmp
(
U"日本語"
,
"
\345
e
\0\0
,g
\0\0\236\212\0\0\0\0\0\0
"
,
16
));
ASSERT
(
0
,
memcmp
(
U"🍣"
,
"c
\363\001\0\0\0\0\0
"
,
8
));
ASSERT
(
u'β'
,
U"βb"
[
0
]);
ASSERT
(
u'b'
,
U"βb"
[
1
]);
ASSERT
(
0
,
U"βb"
[
2
]);
ASSERT
(
1
,
U"
\xffff
ffff"
[
0
]
>>
31
);
printf
(
"OK
\n
"
);
return
0
;
}
This diff is collapsed.
Click to expand it.
tokenize.c
+
29
−
0
View file @
a8ceed4b
...
...
@@ -257,6 +257,28 @@ static Token *read_utf16_string_literal(Token *cur, char *start) {
return
tok
;
}
// Read a UTF-8-encoded string literal and transcode it in UTF-32.
//
// UTF-32 is a fixed-width encoding for Unicode. Each code point is
// encoded in 4 bytes.
static
Token
*
read_utf32_string_literal
(
Token
*
cur
,
char
*
start
,
Type
*
ty
)
{
char
*
end
=
string_literal_end
(
start
+
1
);
uint32_t
*
buf
=
calloc
(
4
,
end
-
start
);
int
len
=
0
;
for
(
char
*
p
=
start
+
1
;
p
<
end
;)
{
if
(
*
p
==
'\\'
)
buf
[
len
++
]
=
read_escaped_char
(
&
p
,
p
+
1
);
else
buf
[
len
++
]
=
decode_utf8
(
&
p
,
p
);
}
Token
*
tok
=
new_token
(
TK_STR
,
cur
,
start
,
end
-
start
+
1
);
tok
->
ty
=
array_of
(
ty
,
len
+
1
);
tok
->
str
=
(
char
*
)
buf
;
return
tok
;
}
static
Token
*
read_char_literal
(
Token
*
cur
,
char
*
start
,
Type
*
ty
)
{
char
*
p
=
start
+
1
;
if
(
*
p
==
'\0'
)
...
...
@@ -497,6 +519,13 @@ Token *tokenize(File *file) {
continue
;
}
// UTF-32 string literal
if
(
startswith
(
p
,
"U
\"
"
))
{
cur
=
read_utf32_string_literal
(
cur
,
p
+
1
,
ty_uint
);
p
+=
cur
->
len
+
1
;
continue
;
}
// Character literal
if
(
*
p
==
'\''
)
{
cur
=
read_char_literal
(
cur
,
p
,
ty_int
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment