Skip to content

Commit 88112b3

Browse files
committed
use DebugHaystack
1 parent 5881c81 commit 88112b3

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/regex/bytes.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::{borrow::Cow, format, string::String, sync::Arc, vec::Vec};
1+
use alloc::{borrow::Cow, string::String, sync::Arc, vec::Vec};
22

33
use regex_automata::{meta, util::captures, Input, PatternID};
44

@@ -1555,29 +1555,17 @@ impl<'h> Match<'h> {
15551555

15561556
impl<'h> core::fmt::Debug for Match<'h> {
15571557
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1558-
let mut fmt = f.debug_struct("Match");
1559-
fmt.field("start", &self.start).field("end", &self.end);
1558+
use regex_automata::util::escape::DebugHaystack;
15601559

1561-
let bytes = self.as_bytes();
1562-
let formatted = bytes_to_string_with_invalid_utf8_escaped(bytes);
1563-
fmt.field("bytes", &formatted);
1560+
let mut fmt = f.debug_struct("Match");
1561+
fmt.field("start", &self.start)
1562+
.field("end", &self.end)
1563+
.field("bytes", &DebugHaystack(&self.as_bytes()));
15641564

15651565
fmt.finish()
15661566
}
15671567
}
15681568

1569-
fn bytes_to_string_with_invalid_utf8_escaped(bytes: &[u8]) -> String {
1570-
let mut result = String::new();
1571-
for &byte in bytes {
1572-
if byte.is_ascii() {
1573-
result.push(byte as char);
1574-
} else {
1575-
result.push_str(&format!("\\x{:02X}", byte));
1576-
}
1577-
}
1578-
result
1579-
}
1580-
15811569
impl<'h> From<Match<'h>> for &'h [u8] {
15821570
fn from(m: Match<'h>) -> &'h [u8] {
15831571
m.as_bytes()
@@ -2674,7 +2662,19 @@ mod tests {
26742662

26752663
assert_eq!(
26762664
debug_str,
2677-
r#"Match { start: 7, end: 13, bytes: "\\xFFworld" }"#
2665+
r#"Match { start: 7, end: 13, bytes: "\xffworld" }"#
2666+
);
2667+
}
2668+
2669+
#[test]
2670+
fn test_non_ascii_utf8() {
2671+
let haystack = "아스키문자는 아닌데 UTF-8 문자열".as_bytes();
2672+
let m = Match::new(haystack, 0, haystack.len());
2673+
let debug_str = format!("{:?}", m);
2674+
2675+
assert_eq!(
2676+
debug_str,
2677+
r#"Match { start: 0, end: 44, bytes: "아스키문자는 아닌데 UTF-8 문자열" }"#
26782678
);
26792679
}
26802680
}

0 commit comments

Comments
 (0)