@@ -34,6 +34,7 @@ pub fn utf32_to_str_no_malloc(code u32, mut buf &u8) string {
3434 }
3535}
3636
37+ // Convert utf32 to utf8 into a pre-allocated buffer
3738@[manualfree; unsafe ]
3839pub fn utf32_decode_to_buffer (code u32 , mut buf & u8 ) int {
3940 unsafe {
@@ -67,6 +68,42 @@ pub fn utf32_decode_to_buffer(code u32, mut buf &u8) int {
6768 return 0
6869}
6970
71+ // Convert utf32 to utf8 into a pre-allocated buffer
72+ @[unsafe ]
73+ pub fn push_utf32_decode_to_buffer (code u32 , mut buf []u8 ) {
74+ unsafe {
75+ icode := int (code) // Prevents doing casts everywhere
76+ if icode < = 127 {
77+ buf << u8 (icode) // 0x00 - 0x7F
78+ return
79+ } else if icode < = 2047 {
80+ t_buf := [
81+ u8 (192 | u8 (icode >> 6 )), // 0xC0 - 110xxxxx
82+ u8 (128 | u8 (icode & 63 )), // 0x80 - 0x3F - 10xxxxxx
83+ ]!
84+ buf.push_many (& t_buf[0 ], 2 )
85+ return
86+ } else if icode < = 65535 {
87+ t_buf := [
88+ u8 (224 | u8 (icode >> 12 )), // 0xE0 - 1110xxxx
89+ u8 (128 | (u8 (icode >> 6 ) & 63 )), // 0x80 - 0x3F - 10xxxxxx
90+ u8 (128 | u8 (icode & 63 )), // 0x80 - 0x3F - 10xxxxxx
91+ ]!
92+ buf.push_many (& t_buf[0 ], 3 )
93+ return
94+ } else if icode < = 1114111 {
95+ t_buf := [
96+ u8 (240 | u8 (icode >> 18 )), // 0xF0 - 11110xxx
97+ u8 (128 | (u8 (icode >> 12 ) & 63 )), // 0x80 - 0x3F - 10xxxxxx
98+ u8 (128 | (u8 (icode >> 6 ) & 63 )), // 0x80 - 0x3F - 10xxxxxx
99+ u8 (128 | u8 (icode & 63 )), // 0x80 - 0x3F - 10xxxxxx
100+ ]!
101+ buf.push_many (& t_buf[0 ], 4 )
102+ return
103+ }
104+ }
105+ }
106+
70107// Convert utf8 to utf32
71108// the original implementation did not check for
72109// valid utf8 in the string, and could result in
0 commit comments