Skip to content

Commit 09a2b99

Browse files
GearsDatapackslpil
authored andcommitted
Add support for pattern matching on non-utf8 string segments
1 parent e7b1865 commit 09a2b99

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

compiler-core/src/strings.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,34 @@ pub fn to_upper_camel_case(string: &str) -> EcoString {
110110

111111
pascal_case
112112
}
113+
114+
/// Converts a string into its UTF-16 representation in bytes
115+
pub fn string_to_utf16_bytes(string: &str) -> Vec<u8> {
116+
let mut bytes = Vec::with_capacity(string.len() * 2);
117+
118+
for character in string.chars() {
119+
let mut character_buffer = [0, 0];
120+
121+
_ = character.encode_utf16(&mut character_buffer);
122+
123+
bytes.extend(character_buffer[0].to_le_bytes());
124+
125+
if character_buffer[1] != 0 {
126+
bytes.extend(character_buffer[1].to_le_bytes());
127+
}
128+
}
129+
130+
bytes
131+
}
132+
133+
/// Converts a string into its UTF-32 representation in bytes
134+
pub fn string_to_utf32_bytes(string: &str) -> Vec<u8> {
135+
let mut bytes = Vec::with_capacity(string.len() * 4);
136+
137+
for character in string.chars() {
138+
let u32 = character as u32;
139+
bytes.extend(u32.to_le_bytes());
140+
}
141+
142+
bytes
143+
}

0 commit comments

Comments
 (0)