-
Notifications
You must be signed in to change notification settings - Fork 193
/
parser.rs
156 lines (140 loc) · 5.11 KB
/
parser.rs
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
// Copyright 2013-2014 The Rust Project Developers.
// Copyright 2018 The Uuid Project Developers.
//
// See the COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use crate::{error::*, std::str};
/// Check if the length matches any of the given criteria lengths.
fn len_matches_any(len: usize, crits: &[usize]) -> bool {
for crit in crits {
if len == *crit {
return true;
}
}
false
}
// Accumulated length of each hyphenated group in hex digits.
const ACC_GROUP_LENS: [usize; 5] = [8, 12, 16, 20, 32];
// Length of each hyphenated group in hex digits.
pub const GROUP_LENS: [usize; 5] = [8, 4, 4, 4, 12];
const URN_PREFIX: &str = "urn:uuid:";
pub fn parse_str(mut input: &str) -> Result<[u8; 16], Error> {
// Ensure length is valid for any of the supported formats
let len = input.len();
let mut start = 0;
// Check for a URN prefixed UUID
if len == 45 {
if let Some(stripped) = input.strip_prefix(URN_PREFIX) {
input = stripped;
start = URN_PREFIX.len();
}
}
// Check for a Microsoft GUID wrapped in {}
else if len == 38 {
if let Some(stripped) =
input.strip_prefix('{').and_then(|s| s.strip_suffix('}'))
{
input = stripped;
start = 1;
}
}
// In other cases, check for a simple or hyphenated UUID
else if !len_matches_any(len, &[36, 32]) {
return Err(Error::length(ExpectedLength::Any(&[36, 32]), len));
}
// `digit` counts only hexadecimal digits, `i_char` counts all chars.
let mut digit = 0;
let mut group = 0;
let mut acc = 0;
let mut buffer = [0u8; 16];
for (i_char, character) in input.char_indices() {
let chr = character as u8;
if digit as usize >= 32 && group != 4 {
if group == 0 {
return Err(Error::length(ExpectedLength::Any(&[36, 32]), len));
}
return Err(Error::group_count(
ExpectedLength::Any(&[1, 5]),
group + 1,
));
}
if digit % 2 == 0 {
// First digit of the byte.
match chr {
// Calculate upper half.
b'0'..=b'9' => acc = chr - b'0',
b'a'..=b'f' => acc = chr - b'a' + 10,
b'A'..=b'F' => acc = chr - b'A' + 10,
// Found a group delimiter
b'-' => {
if ACC_GROUP_LENS[group] as u8 != digit {
// Calculate how many digits this group consists of
// in the input.
let found = if group > 0 {
digit as usize - ACC_GROUP_LENS[group - 1]
} else {
digit as usize
};
return Err(Error::group_length(
ExpectedLength::Exact(GROUP_LENS[group]),
found,
group,
start,
));
}
// Next group, decrement digit, it is incremented again
// at the bottom.
group += 1;
digit -= 1;
}
_ => {
return Err(Error::character(character, i_char, start));
}
}
} else {
// Second digit of the byte, shift the upper half.
acc *= 16;
match chr {
b'0'..=b'9' => acc += chr - b'0',
b'a'..=b'f' => acc += chr - b'a' + 10,
b'A'..=b'F' => acc += chr - b'A' + 10,
b'-' => {
// The byte isn't complete yet.
let found = if group > 0 {
digit - ACC_GROUP_LENS[group - 1] as u8
} else {
digit
};
return Err(Error::group_length(
ExpectedLength::Exact(GROUP_LENS[group]),
found as usize,
group,
start,
));
}
_ => {
// let found = input[i_char..].chars().next().unwrap();
// let found = char::from(chr);
return Err(Error::character(character, i_char, start));
}
}
buffer[(digit / 2) as usize] = acc;
}
digit += 1;
}
// Now check the last group.
if ACC_GROUP_LENS[4] as u8 != digit {
return Err(Error::group_length(
ExpectedLength::Exact(GROUP_LENS[4]),
digit as usize - ACC_GROUP_LENS[3],
group,
start,
));
}
Ok(buffer)
}