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
use crate::lexical::lexical_form;

use std::borrow::{Borrow, ToOwned};
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};

lexical_form! {
	/// Base 64 string.
	///
	/// See: <https://www.w3.org/TR/xmlschema-2/#base64Binary>
	ty: Base64Binary,

	/// Owned base 64 string.
	///
	/// See: <https://www.w3.org/TR/xmlschema-2/#base64Binary>
	buffer: Base64BinaryBuf,

	/// Creates a new base 64 string from a string.
	///
	/// If the input string is ot a [valid XSD base 64 string](https://www.w3.org/TR/xmlschema-2/#base64Binary),
	/// an [`InvalidBase64Binary`] error is returned.
	new,

	/// Creates a new base 64 string from a string without checking it.
	///
	/// # Safety
	///
	/// The input string must be a [valid XSD base 64 string](https://www.w3.org/TR/xmlschema-2/#base64Binary).
	new_unchecked,

	value: crate::Base64BinaryBuf,
	error: InvalidBase64,
	as_ref: as_base_64_binary,
	parent_forms: {}
}

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

	#[inline(always)]
	pub fn value(&self) -> crate::Base64BinaryBuf {
		crate::Base64BinaryBuf::decode(self.as_bytes()).unwrap()
	}
}

impl PartialEq for Base64Binary {
	fn eq(&self, other: &Self) -> bool {
		self.as_canonical_str() == other.as_canonical_str()
	}
}

impl Eq for Base64Binary {}

impl Hash for Base64Binary {
	fn hash<H: Hasher>(&self, h: &mut H) {
		self.as_canonical_str().hash(h)
	}
}

impl Ord for Base64Binary {
	fn cmp(&self, other: &Self) -> Ordering {
		self.as_canonical_str().cmp(other.as_canonical_str())
	}
}

impl PartialOrd for Base64Binary {
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
		Some(self.cmp(other))
	}
}

impl Default for Base64BinaryBuf {
	fn default() -> Self {
		unsafe { Self::new_unchecked(Vec::new()) }
	}
}

impl PartialOrd for Base64BinaryBuf {
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
		Some(self.cmp(other))
	}
}

impl Ord for Base64BinaryBuf {
	fn cmp(&self, other: &Self) -> Ordering {
		self.as_base_64_binary().cmp(other.as_base_64_binary())
	}
}

fn check_bytes(s: &[u8]) -> bool {
	check(s.iter().copied())
}

fn check<C: Iterator<Item = u8>>(mut chars: C) -> bool {
	enum State {
		Data,
		Padding,
	}

	let mut state = State::Data;

	loop {
		state = match state {
			State::Data => match chars.next() {
				Some(b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'+' | b'/') => State::Data,
				Some(b'=') => State::Padding,
				None => break true,
				_ => break false,
			},
			State::Padding => match chars.next() {
				Some(b'=') => State::Padding,
				None => break true,
				_ => break false,
			},
		}
	}
}