|
1 |
| -use alloc::{borrow::Cow, format, string::String, sync::Arc, vec::Vec}; |
| 1 | +use alloc::{borrow::Cow, string::String, sync::Arc, vec::Vec}; |
2 | 2 |
|
3 | 3 | use regex_automata::{meta, util::captures, Input, PatternID};
|
4 | 4 |
|
@@ -1555,29 +1555,17 @@ impl<'h> Match<'h> {
|
1555 | 1555 |
|
1556 | 1556 | impl<'h> core::fmt::Debug for Match<'h> {
|
1557 | 1557 | 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; |
1560 | 1559 |
|
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())); |
1564 | 1564 |
|
1565 | 1565 | fmt.finish()
|
1566 | 1566 | }
|
1567 | 1567 | }
|
1568 | 1568 |
|
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 |
| - |
1581 | 1569 | impl<'h> From<Match<'h>> for &'h [u8] {
|
1582 | 1570 | fn from(m: Match<'h>) -> &'h [u8] {
|
1583 | 1571 | m.as_bytes()
|
@@ -2674,7 +2662,19 @@ mod tests {
|
2674 | 2662 |
|
2675 | 2663 | assert_eq!(
|
2676 | 2664 | 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 문자열" }"# |
2678 | 2678 | );
|
2679 | 2679 | }
|
2680 | 2680 | }
|
0 commit comments