From b93c6e0ae6d628ed2767606c1b2d49b4646356b0 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Champin Date: Wed, 8 Apr 2020 15:58:53 +0200 Subject: [PATCH] fix BlankNode::as_str Before that patch, it would return a string padded with null characters, which is not a valid BLANK_NODE_IDENTIFIER in RDF syntaxes. --- lib/src/model/blank_node.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/src/model/blank_node.rs b/lib/src/model/blank_node.rs index df806bc1..bfa1e78e 100644 --- a/lib/src/model/blank_node.rs +++ b/lib/src/model/blank_node.rs @@ -36,7 +36,8 @@ impl BlankNode { /// Returns the underlying ID of this blank node pub fn as_str(&self) -> &str { - str::from_utf8(&self.str).unwrap() + let len = self.str.iter().position(|x| x == &0).unwrap_or(32); + str::from_utf8(&self.str[..len]).unwrap() } /// Returns the internal ID of this blank node @@ -63,3 +64,20 @@ impl<'a> From<&'a BlankNode> for rio::BlankNode<'a> { rio::BlankNode { id: node.as_str() } } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn as_str_partial() { + let b = BlankNode::new_from_unique_id(0x42); + assert_eq!(b.as_str(), "42"); + } + + #[test] + fn as_str_full() { + let b = BlankNode::new_from_unique_id(0x77776666555544443333222211110000); + assert_eq!(b.as_str(), "77776666555544443333222211110000"); + } +}