Skip to content

Commit 071698d

Browse files
xtqqczzecakebaker
authored andcommitted
refactor: use slice::get for clarity
1 parent 685203a commit 071698d

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

‎src/uu/expand/src/expand.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,12 @@ fn classify_char(buf: &[u8], byte: usize, utf8: bool) -> (CharType, usize, usize
365365
if utf8 {
366366
let nbytes = char::from(buf[byte]).len_utf8();
367367

368-
if byte + nbytes > buf.len() {
368+
let Some(slice) = buf.get(byte..byte + nbytes) else {
369369
// don't overrun buffer because of invalid UTF-8
370370
return (Other, 1, 1);
371-
}
371+
};
372372

373-
if let Ok(t) = from_utf8(&buf[byte..byte + nbytes]) {
373+
if let Ok(t) = from_utf8(slice) {
374374
match t.chars().next() {
375375
Some('\t') => (Tab, 0, 1),
376376
Some('\x08') => (Backspace, 0, 1),

‎src/uu/unexpand/src/unexpand.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,12 @@ fn next_char_info(uflag: bool, buf: &[u8], byte: usize) -> (CharType, usize, usi
387387
let (ctype, cwidth, nbytes) = if uflag {
388388
let nbytes = char::from(buf[byte]).len_utf8();
389389

390-
if byte + nbytes > buf.len() {
391-
// make sure we don't overrun the buffer because of invalid UTF-8
392-
(CharType::Other, 1, 1)
393-
} else if let Ok(t) = from_utf8(&buf[byte..byte + nbytes]) {
390+
let Some(slice) = buf.get(byte..byte + nbytes) else {
391+
// don't overrun buffer because of invalid UTF-8
392+
return (CharType::Other, 1, 1);
393+
};
394+
395+
if let Ok(t) = from_utf8(slice) {
394396
// Now that we think it's UTF-8, figure out what kind of char it is
395397
match t.chars().next() {
396398
Some(' ') => (CharType::Space, 0, 1),

0 commit comments

Comments
 (0)