Skip to content

Commit 82e4708

Browse files
committed
Debug for LocalNameHash
1 parent 99c4e23 commit 82e4708

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/html/local_name.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::Tag;
22
use crate::base::{Bytes, HasReplacementsError, Range};
33
use encoding_rs::Encoding;
4+
use std::fmt;
45

56
// NOTE: All standard tag names contain only ASCII alpha characters
67
// and digits from 1 to 6 (in numbered header tags, i.e. <h1> - <h6>).
@@ -31,7 +32,7 @@ use encoding_rs::Encoding;
3132
// `EMPTY_HASH` is used as a sentinel value.
3233
//
3334
// Pub only for integration tests
34-
#[derive(Debug, PartialEq, Eq, Copy, Clone, Default, Hash)]
35+
#[derive(PartialEq, Eq, Copy, Clone, Default, Hash)]
3536
pub struct LocalNameHash(u64);
3637

3738
const EMPTY_HASH: u64 = !0;
@@ -83,6 +84,33 @@ impl LocalNameHash {
8384
}
8485
}
8586

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+
86114
impl From<&str> for LocalNameHash {
87115
#[inline]
88116
fn from(string: &str) -> Self {

0 commit comments

Comments
 (0)