Skip to content

Commit 7eebb96

Browse files
committed
DRY
1 parent f444155 commit 7eebb96

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/util.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,8 @@ impl CharReader {
160160
if let Some(Ok(c)) = char::decode_utf16([u16::from_be_bytes(buf[..2].try_into().unwrap())]).next() {
161161
return Ok(Some(c));
162162
}
163-
} else if pos == 4 { // surrogate
164-
return char::decode_utf16([u16::from_be_bytes(buf[..2].try_into().unwrap()), u16::from_be_bytes(buf[2..4].try_into().unwrap())])
165-
.next().transpose()
166-
.map_err(|e| CharReadError::Io(io::Error::new(io::ErrorKind::InvalidData, e)));
163+
} else if pos == 4 {
164+
return Self::surrogate([u16::from_be_bytes(buf[..2].try_into().unwrap()), u16::from_be_bytes(buf[2..4].try_into().unwrap())]);
167165
}
168166
},
169167
Encoding::Utf16Le => {
@@ -173,10 +171,8 @@ impl CharReader {
173171
if let Some(Ok(c)) = char::decode_utf16([u16::from_le_bytes(buf[..2].try_into().unwrap())]).next() {
174172
return Ok(Some(c));
175173
}
176-
} else if pos == 4 { // surrogate
177-
return char::decode_utf16([u16::from_le_bytes(buf[..2].try_into().unwrap()), u16::from_le_bytes(buf[2..4].try_into().unwrap())])
178-
.next().transpose()
179-
.map_err(|e| CharReadError::Io(io::Error::new(io::ErrorKind::InvalidData, e)));
174+
} else if pos == 4 {
175+
return Self::surrogate([u16::from_le_bytes(buf[..2].try_into().unwrap()), u16::from_le_bytes(buf[2..4].try_into().unwrap())]);
180176
}
181177
},
182178
}
@@ -214,6 +210,11 @@ impl CharReader {
214210
}
215211
None
216212
}
213+
214+
fn surrogate(buf: [u16; 2]) -> Result<Option<char>, CharReadError> {
215+
char::decode_utf16(buf).next().transpose()
216+
.map_err(|e| CharReadError::Io(io::Error::new(io::ErrorKind::InvalidData, e)))
217+
}
217218
}
218219

219220
#[cfg(test)]

0 commit comments

Comments
 (0)