Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
0012917f
Commit
0012917f
authored
Dec 15, 2019
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a UTF-8 decoder
parent
9e450911
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
0 deletions
+57
-0
include/fmt/format-inl.h
include/fmt/format-inl.h
+57
-0
No files found.
include/fmt/format-inl.h
View file @
0012917f
...
@@ -1240,6 +1240,63 @@ template <> struct formatter<internal::bigint> {
...
@@ -1240,6 +1240,63 @@ template <> struct formatter<internal::bigint> {
}
}
};
};
// A public domain branchless UTF-8 decoder:
// https://github.com/skeeto/branchless-utf8
/* Decode the next character, C, from BUF, reporting errors in E.
*
* Since this is a branchless decoder, four bytes will be read from the
* buffer regardless of the actual length of the next character. This
* means the buffer _must_ have at least three bytes of zero padding
* following the end of the data stream.
*
* Errors are reported in E, which will be non-zero if the parsed
* character was somehow invalid: invalid byte sequence, non-canonical
* encoding, or a surrogate half.
*
* The function returns a pointer to the next character. When an error
* occurs, this pointer will be a guess that depends on the particular
* error, but it will always advance at least one byte.
*/
static
void
*
utf8_decode
(
void
*
buf
,
uint32_t
*
c
,
int
*
e
)
{
static
const
char
lengths
[]
=
{
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
2
,
2
,
2
,
2
,
3
,
3
,
4
,
0
};
static
const
int
masks
[]
=
{
0x00
,
0x7f
,
0x1f
,
0x0f
,
0x07
};
static
const
uint32_t
mins
[]
=
{
4194304
,
0
,
128
,
2048
,
65536
};
static
const
int
shiftc
[]
=
{
0
,
18
,
12
,
6
,
0
};
static
const
int
shifte
[]
=
{
0
,
6
,
4
,
2
,
0
};
auto
s
=
reinterpret_cast
<
unsigned
char
*>
(
buf
);
int
len
=
lengths
[
s
[
0
]
>>
3
];
/* Compute the pointer to the next character early so that the next
* iteration can start working on the next character. Neither Clang
* nor GCC figure out this reordering on their own.
*/
unsigned
char
*
next
=
s
+
len
+
!
len
;
/* Assume a four-byte character and load four bytes. Unused bits are
* shifted out.
*/
*
c
=
(
uint32_t
)(
s
[
0
]
&
masks
[
len
])
<<
18
;
*
c
|=
(
uint32_t
)(
s
[
1
]
&
0x3f
)
<<
12
;
*
c
|=
(
uint32_t
)(
s
[
2
]
&
0x3f
)
<<
6
;
*
c
|=
(
uint32_t
)(
s
[
3
]
&
0x3f
)
<<
0
;
*
c
>>=
shiftc
[
len
];
/* Accumulate the various error conditions. */
*
e
=
(
*
c
<
mins
[
len
])
<<
6
;
// non-canonical encoding
*
e
|=
((
*
c
>>
11
)
==
0x1b
)
<<
7
;
// surrogate half?
*
e
|=
(
*
c
>
0x10FFFF
)
<<
8
;
// out of range?
*
e
|=
(
s
[
1
]
&
0xc0
)
>>
2
;
*
e
|=
(
s
[
2
]
&
0xc0
)
>>
4
;
*
e
|=
(
s
[
3
])
>>
6
;
*
e
^=
0x2a
;
// top two bits of each tail byte correct?
*
e
>>=
shifte
[
len
];
return
next
;
}
#if FMT_USE_WINDOWS_H
#if FMT_USE_WINDOWS_H
FMT_FUNC
internal
::
utf8_to_utf16
::
utf8_to_utf16
(
string_view
s
)
{
FMT_FUNC
internal
::
utf8_to_utf16
::
utf8_to_utf16
(
string_view
s
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment