Skip to content

Commit 7178666

Browse files
committed
refactor
Signed-off-by: xxchan <[email protected]>
1 parent 0416fe2 commit 7178666

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/lib.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,34 +346,42 @@ impl Expect {
346346

347347
let literal_start = literal_start + (lit_to_eof.len() - lit_to_eof_trimmed.len());
348348

349-
let literal_len =
350-
locate_end(lit_to_eof_trimmed).expect("Couldn't find closing delimiter for `expect!`.");
349+
let literal_len = locate_end(lit_to_eof_trimmed);
351350
let literal_range = literal_start..literal_start + literal_len;
352351
Location { line_indent, literal_range }
353352
}
354353
}
355354

356355
/// Returns the byte index of the closing delimiter.
357356
///
358-
/// `arg_start_to_eof` is the part after `expect![`, with leading whitespaces trimmed.
359-
fn locate_end(arg_start_to_eof: &str) -> Option<usize> {
360-
match arg_start_to_eof.chars().next()? {
357+
/// `arg_start_to_eof` is the part after `expect![` until the closing delimiter
358+
/// with leading whitespaces trimmed.
359+
/// Note that we can actually assume the syntax is valid according to `macro_rules`.
360+
fn locate_end(arg_start_to_eof: &str) -> usize {
361+
let mut chars = arg_start_to_eof.chars();
362+
match chars.next().expect("after `expect![` there should be a char") {
361363
c if c.is_whitespace() => panic!("skip whitespace before calling `locate_end`"),
362364

363365
// expect![[]]
364366
'[' => {
365367
let str_start_to_eof = arg_start_to_eof[1..].trim_start();
366-
let str_len = find_str_lit_len(str_start_to_eof).unwrap_or(0);
368+
let str_len = if ']' == chars.next().expect("after `expect![[` there should be a char")
369+
{
370+
0
371+
} else {
372+
find_str_lit_len(str_start_to_eof).expect("invalid string literal in `expect![[`")
373+
};
367374
let str_end_to_eof = &str_start_to_eof[str_len..];
368-
let closing_brace_offset = str_end_to_eof.find(']')?;
369-
Some((arg_start_to_eof.len() - str_end_to_eof.len()) + closing_brace_offset + 1)
375+
let closing_brace_offset =
376+
str_end_to_eof.find(']').expect("closing `]` not found after `expect![[`");
377+
(arg_start_to_eof.len() - str_end_to_eof.len()) + closing_brace_offset + 1
370378
}
371379

372380
// expect![] | expect!{} | expect!()
373-
']' | '}' | ')' => Some(0),
381+
']' | '}' | ')' => 0,
374382

375383
// expect!["..."] | expect![r#"..."#]
376-
_ => find_str_lit_len(arg_start_to_eof),
384+
_ => find_str_lit_len(arg_start_to_eof).expect("invalid string literal after `expect![`"),
377385
}
378386
}
379387

@@ -843,7 +851,7 @@ line1
843851
let lit = stringify!($s);
844852
let with_trailer = format!("[{} \t]]\n", lit);
845853
// ^ ^^ ^^ 5 additional chars
846-
assert_eq!(locate_end(&with_trailer), Some(4+lit.len()));
854+
assert_eq!(locate_end(&with_trailer), 4+lit.len());
847855
})*};
848856
}
849857

@@ -856,9 +864,14 @@ line1
856864
);
857865

858866
// Check `expect![[ ]]`
859-
assert_eq!(locate_end("[]]"), Some(2));
867+
assert_eq!(locate_end("[]]"), 2);
860868
// Check `expect![ ]`
861-
assert_eq!(locate_end("]"), Some(0));
869+
assert_eq!(locate_end("]"), 0);
870+
871+
// `locate_end` returns after the closing delimiter.
872+
assert_eq!(locate_end("]abc"), 0);
873+
// This is actually invalid syntax.
874+
assert_eq!(locate_end("]]abc"), 0);
862875
}
863876

864877
#[test]

0 commit comments

Comments
 (0)