|
1 | 1 | use super::Tag; |
2 | 2 | use crate::base::{Bytes, HasReplacementsError, Range}; |
3 | 3 | use encoding_rs::Encoding; |
| 4 | +use std::fmt; |
4 | 5 |
|
5 | 6 | // NOTE: All standard tag names contain only ASCII alpha characters |
6 | 7 | // and digits from 1 to 6 (in numbered header tags, i.e. <h1> - <h6>). |
@@ -31,7 +32,7 @@ use encoding_rs::Encoding; |
31 | 32 | // `EMPTY_HASH` is used as a sentinel value. |
32 | 33 | // |
33 | 34 | // Pub only for integration tests |
34 | | -#[derive(Debug, PartialEq, Eq, Copy, Clone, Default, Hash)] |
| 35 | +#[derive(PartialEq, Eq, Copy, Clone, Default, Hash)] |
35 | 36 | pub struct LocalNameHash(u64); |
36 | 37 |
|
37 | 38 | const EMPTY_HASH: u64 = !0; |
@@ -83,6 +84,33 @@ impl LocalNameHash { |
83 | 84 | } |
84 | 85 | } |
85 | 86 |
|
| 87 | +impl fmt::Debug for LocalNameHash { |
| 88 | + #[cold] |
| 89 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 90 | + if self.is_empty() { |
| 91 | + return f.write_str("N/A"); |
| 92 | + } |
| 93 | + |
| 94 | + let mut reverse_buf = [0u8; 12]; |
| 95 | + let mut pos = 11; |
| 96 | + let mut h = self.0; |
| 97 | + loop { |
| 98 | + reverse_buf[pos] = match (h & 31) as u8 { |
| 99 | + v @ 6.. => v + (b'a' - 6), |
| 100 | + v => v + b'1', |
| 101 | + }; |
| 102 | + h >>= 5; |
| 103 | + if h == 0 || pos == 0 { |
| 104 | + break; |
| 105 | + } |
| 106 | + pos -= 1; |
| 107 | + } |
| 108 | + std::str::from_utf8(&reverse_buf[pos..]) |
| 109 | + .unwrap_or_default() |
| 110 | + .fmt(f) |
| 111 | + } |
| 112 | +} |
| 113 | + |
86 | 114 | impl From<&str> for LocalNameHash { |
87 | 115 | #[inline] |
88 | 116 | fn from(string: &str) -> Self { |
|
0 commit comments