mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +02:00
dont split string inside utf8 char
This commit is contained in:
parent
7bc5ef8759
commit
4ab65a675f
1 changed files with 13 additions and 4 deletions
|
|
@ -300,12 +300,14 @@ where
|
||||||
/// [`ReadError::Utf8Error`]: enum.ReadError.html#variant.Utf8Error
|
/// [`ReadError::Utf8Error`]: enum.ReadError.html#variant.Utf8Error
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn read_string(&mut self, byte_len: Option<usize>) -> Result<String> {
|
pub fn read_string(&mut self, byte_len: Option<usize>) -> Result<String> {
|
||||||
|
let max_length = self.bits_left() / 8;
|
||||||
|
|
||||||
let result = self.buffer.read_string(self.pos, byte_len).map_err(|err| {
|
let result = self.buffer.read_string(self.pos, byte_len).map_err(|err| {
|
||||||
// still advance the stream on malformed utf8
|
// still advance the stream on malformed utf8
|
||||||
if let ReadError::Utf8Error(err) = &err {
|
if let ReadError::Utf8Error(err) = &err {
|
||||||
self.pos += match byte_len {
|
self.pos += match byte_len {
|
||||||
Some(len) => len * 8,
|
Some(len) => len * 8,
|
||||||
None => min((err.as_bytes().len() + 1) * 8, self.bits_left() / 8),
|
None => min((err.as_bytes().len() + 1) * 8, max_length),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
err
|
err
|
||||||
|
|
@ -319,9 +321,16 @@ where
|
||||||
// (but not the top level buffer)
|
// (but not the top level buffer)
|
||||||
// thus we trim the resulting string to make sure it fits in the source stream
|
// thus we trim the resulting string to make sure it fits in the source stream
|
||||||
if read > self.bits_left() {
|
if read > self.bits_left() {
|
||||||
let new_length = self.bits_left() / 8;
|
// find the maximum well-formed utf8 string that fits in max_len
|
||||||
self.pos += new_length * 8;
|
let mut acc = String::new();
|
||||||
return Ok(result[0..new_length].to_string());
|
for c in result.chars() {
|
||||||
|
if acc.len() + c.len_utf8() > max_length {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
acc.push(c);
|
||||||
|
}
|
||||||
|
self.pos += acc.len() * 8;
|
||||||
|
return Ok(acc);
|
||||||
}
|
}
|
||||||
self.pos += read;
|
self.pos += read;
|
||||||
Ok(result)
|
Ok(result)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue