Skip to content

Commit 9ec8406

Browse files
npmaniaRiey
authored andcommitted
fix: xim-ctext: Fix decode macro to track bytes
In current implementation of decode! macro, length of bytes read returned by encoding_rs::Decoder::decode_to_string is not used, causing early part of japanese string to be repeated. This commit fixes invalid behavior by using previously mentioned length value to prevent decoding certain part of bytes multiple times. Signed-off-by: npmania <[email protected]>
1 parent 5fdae7a commit 9ec8406

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

xim-ctext/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ impl fmt::Display for DecodeError {
100100

101101
macro_rules! decode {
102102
($decoder:expr, $out:expr, $bytes:expr, $last:expr) => {
103+
let mut _current_bytes: &[u8] = $bytes;
103104
loop {
104-
let (ret, _, _) = $decoder.decode_to_string($bytes, $out, $last);
105+
let (ret, nread, _) = $decoder.decode_to_string(_current_bytes, $out, $last);
105106

106107
match ret {
107108
encoding_rs::CoderResult::InputEmpty => break,
@@ -111,6 +112,7 @@ macro_rules! decode {
111112
.max_utf8_buffer_length($bytes.len())
112113
.unwrap_or_default(),
113114
);
115+
_current_bytes = &_current_bytes[nread..];
114116
}
115117
}
116118
}
@@ -271,7 +273,7 @@ mod tests {
271273
}
272274

273275
#[test]
274-
fn iso_2011_jp() {
276+
fn iso_2022_jp() {
275277
const UTF8: &str = "東京";
276278
const COMP: &[u8] = &[27, 36, 40, 66, 69, 108, 53, 126];
277279
assert_eq!(crate::compound_text_to_utf8(COMP).unwrap(), UTF8);
@@ -318,4 +320,13 @@ mod tests {
318320
const COMP: &[u8] = &[0x1b, 0x2d, 0x42, 0xa1, 0xa3, 0xa5, 0xa6, 0xa9, 0xab];
319321
assert_eq!(crate::compound_text_to_utf8(COMP).unwrap(), UTF8);
320322
}
323+
324+
#[test]
325+
fn iso_2022_jp_long() {
326+
const UTF8: &str = "知ってるつもり";
327+
const COMP: &[u8] = &[
328+
27, 36, 40, 66, 67, 78, 36, 67, 36, 70, 36, 107, 36, 68, 36, 98, 36, 106, 27, 40, 66,
329+
];
330+
assert_eq!(crate::compound_text_to_utf8(COMP).unwrap(), UTF8);
331+
}
321332
}

0 commit comments

Comments
 (0)