FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SimpleNoter/utf8_encode.cpp at main · Magnetic-Fox/SimpleNoter · GitHub
Magnetic-Fox
/
SimpleNoter
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SimpleNoter
/
utf8_encode.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
53 lines (52 loc) · 1.38 KB
Breadcrumbs
SimpleNoter
/
utf8_encode.cpp
Copy path
File metadata and controls
53 lines (52 loc) · 1.38 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
#
include
"
utf8_encode.hpp
"
/*
*
* Encode a code point using UTF-8
*
* @author Ondøej Hruška <ondra@ondrovo.com>
* @license MIT
*
* @param out - output buffer (min 5 characters), will be 0-terminated
* @param utf - code point 0-0x10FFFF
* @return number of bytes on success, 0 on failure (also produces U+FFFD, which uses 3 bytes)
*/
int
utf8_encode
(
char
*out,
uint32_t
utf)
{
if
(utf <=
0x7F
) {
//
Plain ASCII
out[
0
] = (
char
) utf;
out[
1
] =
0
;
return
1
;
}
else
if
(utf <=
0x07FF
) {
//
2-byte unicode
out[
0
] = (
char
) (((utf >>
6
) &
0x1F
) |
0xC0
);
out[
1
] = (
char
) (((utf >>
0
) &
0x3F
) |
0x80
);
out[
2
] =
0
;
return
2
;
}
else
if
(utf <=
0xFFFF
) {
//
3-byte unicode
out[
0
] = (
char
) (((utf >>
12
) &
0x0F
) |
0xE0
);
out[
1
] = (
char
) (((utf >>
6
) &
0x3F
) |
0x80
);
out[
2
] = (
char
) (((utf >>
0
) &
0x3F
) |
0x80
);
out[
3
] =
0
;
return
3
;
}
else
if
(utf <=
0x10FFFF
) {
//
4-byte unicode
out[
0
] = (
char
) (((utf >>
18
) &
0x07
) |
0xF0
);
out[
1
] = (
char
) (((utf >>
12
) &
0x3F
) |
0x80
);
out[
2
] = (
char
) (((utf >>
6
) &
0x3F
) |
0x80
);
out[
3
] = (
char
) (((utf >>
0
) &
0x3F
) |
0x80
);
out[
4
] =
0
;
return
4
;
}
else
{
//
error - use replacement character
out[
0
] = (
char
)
0xEF
;
out[
1
] = (
char
)
0xBF
;
out[
2
] = (
char
)
0xBD
;
out[
3
] =
0
;
return
0
;
}
}
Back
|
FazBrowse Home
|
New Git URL