1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
use std::borrow::{Borrow, ToOwned};
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;

/// Invalid blank node identifier.
///
/// This error is raised by the [`BlankId::new`] and [`BlankIdBuf::new`] functions
/// when the input string is not a valid blank node identifier.
#[derive(Debug)]
pub struct InvalidBlankId<T>(pub T);

/// Blank node identifier.
///
/// A blank node identifier is a string matching
/// the `BLANK_NODE_LABEL` production in the following [EBNF](http://www.w3.org/TR/REC-xml/#sec-notation) grammar:
///
/// ```ebnf
/// [141s] BLANK_NODE_LABEL ::= '_:' (PN_CHARS_U | [0-9]) ((PN_CHARS | '.')* PN_CHARS)?
/// [157s] PN_CHARS_BASE    ::= [A-Z] | [a-z] | [#x00C0-#x00D6] | [#x00D8-#x00F6] | [#x00F8-#x02FF] | [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
/// [158s] PN_CHARS_U       ::= PN_CHARS_BASE | '_' | ':'
/// [160s] PN_CHARS         ::= PN_CHARS_U | '-' | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040]
/// ```
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BlankId(str);

impl BlankId {
	/// Parses a blank node identifier.
	#[inline(always)]
	pub fn new(s: &str) -> Result<&Self, InvalidBlankId<&str>> {
		if check(s.chars()) {
			Ok(unsafe { Self::new_unchecked(s) })
		} else {
			Err(InvalidBlankId(s))
		}
	}

	/// Creates a new blank node identifier from `s` without checking it.
	///
	/// # Safety
	///
	/// The input string `s` must be a valid blank node identifier.
	#[inline(always)]
	pub unsafe fn new_unchecked(s: &str) -> &Self {
		std::mem::transmute(s)
	}

	/// Returns a reference to the underlying string defining the blank node identifier.
	#[inline(always)]
	pub fn as_str(&self) -> &str {
		&self.0
	}

	/// Returns the suffix part (after `_:`) of the blank node identifier.
	#[inline(always)]
	pub fn suffix(&self) -> &str {
		&self.0[2..]
	}
}

impl Deref for BlankId {
	type Target = str;

	#[inline(always)]
	fn deref(&self) -> &str {
		self.as_str()
	}
}

impl AsRef<str> for BlankId {
	fn as_ref(&self) -> &str {
		self.as_str()
	}
}

impl AsRef<[u8]> for BlankId {
	fn as_ref(&self) -> &[u8] {
		self.as_bytes()
	}
}

impl Borrow<str> for BlankId {
	fn borrow(&self) -> &str {
		self.as_str()
	}
}

impl Borrow<[u8]> for BlankId {
	fn borrow(&self) -> &[u8] {
		self.as_bytes()
	}
}

impl ToOwned for BlankId {
	type Owned = BlankIdBuf;

	#[inline(always)]
	fn to_owned(&self) -> BlankIdBuf {
		unsafe { BlankIdBuf::new_unchecked(self.as_str().to_owned()) }
	}
}

impl fmt::Display for BlankId {
	#[inline(always)]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.0.fmt(f)
	}
}

impl fmt::Debug for BlankId {
	#[inline(always)]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.0.fmt(f)
	}
}

impl PartialEq<str> for BlankId {
	#[inline(always)]
	fn eq(&self, other: &str) -> bool {
		self.0 == *other
	}
}

/// Owned blank node identifier.
///
/// A blank node identifier is a string matching
/// the `BLANK_NODE_LABEL` production in the following [EBNF](http://www.w3.org/TR/REC-xml/#sec-notation) grammar:
///
/// ```ebnf
/// [141s] BLANK_NODE_LABEL ::= '_:' (PN_CHARS_U | [0-9]) ((PN_CHARS | '.')* PN_CHARS)?
/// [157s] PN_CHARS_BASE    ::= [A-Z] | [a-z] | [#x00C0-#x00D6] | [#x00D8-#x00F6] | [#x00F8-#x02FF] | [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
/// [158s] PN_CHARS_U       ::= PN_CHARS_BASE | '_' | ':'
/// [160s] PN_CHARS         ::= PN_CHARS_U | '-' | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040]
/// ```
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BlankIdBuf(String);

impl BlankIdBuf {
	/// Parses a blank node identifier.
	#[inline(always)]
	pub fn new(s: String) -> Result<Self, InvalidBlankId<String>> {
		if check(s.chars()) {
			Ok(unsafe { Self::new_unchecked(s) })
		} else {
			Err(InvalidBlankId(s))
		}
	}

	/// Creates a new blank node identifier from `s` without checking it.
	///
	/// # Safety
	///
	/// The input string `s` must be a valid blank node identifier.
	#[inline(always)]
	pub unsafe fn new_unchecked(s: String) -> Self {
		std::mem::transmute(s)
	}

	/// Creates a blank node identifier using the given `u8` as suffix.
	#[inline(always)]
	pub fn from_u8(i: u8) -> Self {
		unsafe { Self::new_unchecked(format!("_:{i}")) }
	}

	/// Creates a blank node identifier using the given `u16` as suffix.
	#[inline(always)]
	pub fn from_u16(i: u16) -> Self {
		unsafe { Self::new_unchecked(format!("_:{i}")) }
	}

	/// Creates a blank node identifier using the given `u32` as suffix.
	#[inline(always)]
	pub fn from_u32(i: u32) -> Self {
		unsafe { Self::new_unchecked(format!("_:{i}")) }
	}

	/// Creates a blank node identifier using the given `u64` as suffix.
	#[inline(always)]
	pub fn from_u64(i: u64) -> Self {
		unsafe { Self::new_unchecked(format!("_:{i}")) }
	}

	/// Creates a blank node identifier using the given suffix.
	#[inline(always)]
	pub fn from_suffix(suffix: &str) -> Result<Self, InvalidBlankId<String>> {
		Self::new(format!("_:{suffix}"))
	}

	/// Returns a reference to this blank id as a `BlankId`.
	#[inline(always)]
	pub fn as_blank_id_ref(&self) -> &BlankId {
		unsafe { BlankId::new_unchecked(&self.0) }
	}
}

impl FromStr for BlankIdBuf {
	type Err = InvalidBlankId<String>;

	fn from_str(s: &str) -> Result<Self, InvalidBlankId<String>> {
		Self::new(s.to_owned())
	}
}

impl Deref for BlankIdBuf {
	type Target = BlankId;

	#[inline(always)]
	fn deref(&self) -> &BlankId {
		self.as_blank_id_ref()
	}
}

impl AsRef<BlankId> for BlankIdBuf {
	#[inline(always)]
	fn as_ref(&self) -> &BlankId {
		self.as_blank_id_ref()
	}
}

impl Borrow<BlankId> for BlankIdBuf {
	#[inline(always)]
	fn borrow(&self) -> &BlankId {
		self.as_blank_id_ref()
	}
}

impl AsRef<str> for BlankIdBuf {
	#[inline(always)]
	fn as_ref(&self) -> &str {
		self.0.as_str()
	}
}

impl Borrow<str> for BlankIdBuf {
	#[inline(always)]
	fn borrow(&self) -> &str {
		self.0.as_str()
	}
}

impl AsRef<[u8]> for BlankIdBuf {
	#[inline(always)]
	fn as_ref(&self) -> &[u8] {
		self.0.as_bytes()
	}
}

impl Borrow<[u8]> for BlankIdBuf {
	#[inline(always)]
	fn borrow(&self) -> &[u8] {
		self.0.as_bytes()
	}
}

impl Borrow<BlankId> for &BlankIdBuf {
	#[inline(always)]
	fn borrow(&self) -> &BlankId {
		self.as_blank_id_ref()
	}
}

impl Borrow<str> for &BlankIdBuf {
	#[inline(always)]
	fn borrow(&self) -> &str {
		self.0.as_str()
	}
}

impl Borrow<[u8]> for &BlankIdBuf {
	#[inline(always)]
	fn borrow(&self) -> &[u8] {
		self.0.as_bytes()
	}
}

impl<'a> From<&'a BlankIdBuf> for &'a BlankId {
	#[inline(always)]
	fn from(b: &'a BlankIdBuf) -> Self {
		b.as_ref()
	}
}

impl fmt::Display for BlankIdBuf {
	#[inline(always)]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.0.fmt(f)
	}
}

impl fmt::Debug for BlankIdBuf {
	#[inline(always)]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.0.fmt(f)
	}
}

impl PartialEq<BlankId> for BlankIdBuf {
	fn eq(&self, other: &BlankId) -> bool {
		self.as_blank_id_ref() == other
	}
}

impl<'a> PartialEq<&'a BlankId> for BlankIdBuf {
	fn eq(&self, other: &&'a BlankId) -> bool {
		self.as_blank_id_ref() == *other
	}
}

impl<'a> PartialEq<BlankIdBuf> for &'a BlankId {
	fn eq(&self, other: &BlankIdBuf) -> bool {
		*self == other.as_blank_id_ref()
	}
}

impl PartialEq<BlankIdBuf> for BlankId {
	fn eq(&self, other: &BlankIdBuf) -> bool {
		self == other.as_blank_id_ref()
	}
}

fn check<C: Iterator<Item = char>>(mut chars: C) -> bool {
	match chars.next() {
		Some('_') => match chars.next() {
			Some(':') => match chars.next() {
				Some(c) if c.is_ascii_digit() || is_pn_char_u(c) => {
					for c in chars {
						if !is_pn_char(c) {
							return false;
						}
					}

					true
				}
				_ => false,
			},
			_ => false,
		},
		_ => false,
	}
}

fn is_pn_char_base(c: char) -> bool {
	matches!(c, 'A'..='Z' | 'a'..='z' | '\u{00c0}'..='\u{00d6}' | '\u{00d8}'..='\u{00f6}' | '\u{00f8}'..='\u{02ff}' | '\u{0370}'..='\u{037d}' | '\u{037f}'..='\u{1fff}' | '\u{200c}'..='\u{200d}' | '\u{2070}'..='\u{218f}' | '\u{2c00}'..='\u{2fef}' | '\u{3001}'..='\u{d7ff}' | '\u{f900}'..='\u{fdcf}' | '\u{fdf0}'..='\u{fffd}' | '\u{10000}'..='\u{effff}')
}

fn is_pn_char_u(c: char) -> bool {
	is_pn_char_base(c) || matches!(c, '_' | ':')
}

fn is_pn_char(c: char) -> bool {
	is_pn_char_u(c)
		|| matches!(c, '-' | '0'..='9' | '\u{00b7}' | '\u{0300}'..='\u{036f}' | '\u{203f}'..='\u{2040}')
}