| 55 | } |
| 56 | |
| 57 | fn write_fstring(&mut self, string: Option<&str>) -> Result<usize, FStringError> { |
| 58 | if let Some(string) = string { |
| 59 | let is_unicode = string.len() != string.chars().count(); |
| 60 | |
| 61 | if is_unicode { |
| 62 | let utf16 = string.encode_utf16().collect::<Vec<_>>(); |
| 63 | |
| 64 | // this is safe because we know that string is utf16 and therefore can easily be aligned to u8 |
| 65 | // this is also faster than alternatives without unsafe block |
| 66 | let (_, aligned, _) = unsafe { utf16.align_to::<u8>() }; |
| 67 | |
| 68 | self.write_i32::<LE>(-(aligned.len() as i32 / 2) - 1)?; |
| 69 | self.write_all(aligned)?; |
| 70 | |
| 71 | self.write_all(&[0u8; 2])?; |
| 72 | Ok(size_of::<i32>() + aligned.len()) |
| 73 | } else { |
| 74 | self.write_i32::<LE>(string.len() as i32 + 1)?; |
| 75 | let bytes = string.as_bytes(); |
| 76 | self.write_all(bytes)?; |
| 77 | self.write_all(&[0u8; 1])?; |
| 78 | |
| 79 | Ok(size_of::<i32>() + bytes.len() + 1) |
| 80 | } |
| 81 | } else { |
| 82 | self.write_i32::<LE>(0)?; |
| 83 | Ok(size_of::<i32>()) |
| 84 | } |
| 85 | } |
| 86 | } |