Skip to content

Commit 5663358

Browse files
mujaSerial-ATA
andcommitted
fix(text): include terminator length in empty text size
Co-authored-by: Serial <69764315+serial-ata@users.noreply.github.com>
1 parent 10be465 commit 5663358

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5151
* **FileResolver**: Replaced `FileResolver::supported_tag_types()` with `FileResolver::tag_support()` ([PR](https://github.com/Serial-ATA/lofty-rs/pull/566))
5252

5353
### Fixed
54-
- **ID3v2**: Support parsing UTF-16 `COMM`/`USLT` frames with a single BOM ([issue](https://github.com/Serial-ATA/lofty-rs/issues/532)) ([PR](https://github.com/Serial-ATA/lofty-rs/pull/535))
55-
- Some encoders will only write a BOM to the frame's description, rather than to every string in the frame.
56-
This was previously only supported in `SYLT` frames, and has been extended to `COMM` and `USLT`.
54+
- **ID3v2**:
55+
- Support parsing UTF-16 `COMM`/`USLT` frames with a single BOM ([issue](https://github.com/Serial-ATA/lofty-rs/issues/532)) ([PR](https://github.com/Serial-ATA/lofty-rs/pull/535))
56+
- Some encoders will only write a BOM to the frame's description, rather than to every string in the frame.
57+
This was previously only supported in `SYLT` frames, and has been extended to `COMM` and `USLT`.
58+
- Don't error on empty `SYLT` strings ([issue](https://github.com/Serial-ATA/lofty-rs/issues/563)) ([PR](https://github.com/Serial-ATA/lofty-rs/pull/564))
5759
- **Vorbis Comments**: Parse `TRACKNUMBER` with respect to `ParseOptions::implicit_conversions` ([issue](https://github.com/Serial-ATA/lofty-rs/issues/540)) ([PR](https://github.com/Serial-ATA/lofty-rs/issues/542))
5860
- **APE**: Fix disc number removal/writing ([issue](https://github.com/Serial-ATA/lofty-rs/issues/545)) ([PR](https://github.com/Serial-ATA/lofty-rs/pull/546))
5961

lofty/src/util/text.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl TextEncoding {
5252
}
5353
}
5454

55-
#[derive(Eq, PartialEq, Debug)]
55+
#[derive(Eq, PartialEq, Debug, Default)]
5656
pub(crate) struct DecodeTextResult {
5757
pub(crate) content: String,
5858
pub(crate) bytes_read: usize,
@@ -69,12 +69,6 @@ impl DecodeTextResult {
6969
}
7070
}
7171

72-
const EMPTY_DECODED_TEXT: DecodeTextResult = DecodeTextResult {
73-
content: String::new(),
74-
bytes_read: 0,
75-
bom: [0, 0],
76-
};
77-
7872
/// Specify how to decode the provided text
7973
///
8074
/// By default, this will:
@@ -131,7 +125,10 @@ where
131125
let (bytes, terminator_len) = read_to_terminator(reader, options.encoding);
132126

133127
if bytes.is_empty() {
134-
return Ok(EMPTY_DECODED_TEXT);
128+
return Ok(DecodeTextResult {
129+
bytes_read: terminator_len,
130+
..DecodeTextResult::default()
131+
});
135132
}
136133

137134
bytes_read = bytes.len() + terminator_len;
@@ -141,7 +138,7 @@ where
141138
reader.read_to_end(&mut bytes)?;
142139

143140
if bytes.is_empty() {
144-
return Ok(EMPTY_DECODED_TEXT);
141+
return Ok(DecodeTextResult::default());
145142
}
146143

147144
bytes_read = bytes.len();
@@ -402,6 +399,19 @@ mod tests {
402399
)
403400
.unwrap();
404401

402+
let empty_text_fragment = super::decode_text(
403+
&mut Cursor::new(&[
404+
0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
405+
0x00, 0x02,
406+
]),
407+
TextDecodeOptions::new()
408+
.encoding(TextEncoding::UTF8)
409+
.terminated(true),
410+
)
411+
.unwrap();
412+
assert_eq!(empty_text_fragment.content, "");
413+
assert_eq!(empty_text_fragment.bytes_read, 1);
414+
405415
assert_eq!(utf8_decode.content, TEST_STRING.to_string());
406416
}
407417

0 commit comments

Comments
 (0)