Skip to content

Commit 7e846f3

Browse files
kornelskiorium
authored andcommitted
Use c"" literals
1 parent baa7151 commit 7e846f3

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

c-api/src/element.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ pub unsafe extern "C" fn lol_html_element_namespace_uri_get(
4949
) -> *const c_char {
5050
let element = to_ref!(element);
5151

52-
match element.namespace_uri() {
53-
"http://www.w3.org/1999/xhtml" => static_c_str!("http://www.w3.org/1999/xhtml"),
54-
"http://www.w3.org/2000/svg" => static_c_str!("http://www.w3.org/2000/svg"),
55-
"http://www.w3.org/1998/Math/MathML" => static_c_str!("http://www.w3.org/1998/Math/MathML"),
56-
_ => unreachable!("Unknown namespace URI"),
57-
}
52+
element.namespace_uri_c_str().as_ptr()
5853
}
5954

6055
/// Returns the iterator over the element attributes.

c-api/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ macro_rules! to_str {
5050
};
5151
}
5252

53-
macro_rules! static_c_str {
54-
($s:expr) => {
55-
concat!($s, "\0").as_ptr() as *const c_char
56-
};
57-
}
58-
5953
macro_rules! unwrap_or_ret {
6054
($expr:expr, $ret_val:expr) => {
6155
match $expr {

src/html/namespace.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ impl Namespace {
2020
MathML => "http://www.w3.org/1998/Math/MathML",
2121
}
2222
}
23+
24+
#[inline]
25+
#[must_use]
26+
pub const fn uri_c_str(self) -> &'static std::ffi::CStr {
27+
use Namespace::{Html, MathML, Svg};
28+
29+
match self {
30+
Html => c"http://www.w3.org/1999/xhtml",
31+
Svg => c"http://www.w3.org/2000/svg",
32+
MathML => c"http://www.w3.org/1998/Math/MathML",
33+
}
34+
}
2335
}

src/rewritable_units/element.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,13 @@ impl<'rewriter, 'input_token, H: HandlerTypes> Element<'rewriter, 'input_token,
737737
pub fn source_location(&self) -> SourceLocation {
738738
self.start_tag.source_location()
739739
}
740+
741+
/// [Self::namespace_uri], but as a `CStr`
742+
#[inline]
743+
#[must_use]
744+
pub fn namespace_uri_c_str(&self) -> &'static std::ffi::CStr {
745+
self.start_tag.namespace_uri_c_str()
746+
}
740747
}
741748

742749
impl_user_data!(Element<'_, '_>);

src/rewritable_units/tokens/start_tag.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ impl<'input_token> StartTag<'input_token> {
263263
pub fn source_location(&self) -> SourceLocation {
264264
self.raw.source_location()
265265
}
266+
267+
/// [Self::namespace_uri], but as a `CStr`
268+
#[inline]
269+
#[must_use]
270+
pub fn namespace_uri_c_str(&self) -> &'static std::ffi::CStr {
271+
self.ns.uri_c_str()
272+
}
266273
}
267274

268275
impl_serialize!(StartTag);

0 commit comments

Comments
 (0)