File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments