Skip to content

Commit 882aafc

Browse files
committed
-manual
1 parent 886919e commit 882aafc

File tree

10 files changed

+49
-109
lines changed

10 files changed

+49
-109
lines changed

components/locale_core/src/extensions/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,12 @@ impl ExtensionType {
9696
}
9797
}
9898

99-
pub(crate) const fn try_from_utf8_manual_slice(
100-
code_units: &[u8],
101-
start: usize,
102-
end: usize,
103-
) -> Result<Self, ParseError> {
104-
if end - start != 1 {
99+
pub(crate) const fn try_from_utf8(code_units: &[u8]) -> Result<Self, ParseError> {
100+
let &[first] = code_units else {
105101
return Err(ParseError::InvalidExtension);
106-
}
107-
#[allow(clippy::indexing_slicing)]
108-
Self::try_from_byte(code_units[start])
102+
};
103+
104+
Self::try_from_byte(first)
109105
}
110106
}
111107

components/locale_core/src/extensions/unicode/keywords.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl Keywords {
330330
}
331331
current_keyword = Some(Key::try_from_utf8(subtag)?);
332332
} else if current_keyword.is_some() {
333-
match Value::parse_subtag(subtag) {
333+
match Value::parse_subtag_from_utf8(subtag) {
334334
Ok(Some(t)) => current_value.push(t),
335335
Ok(None) => {}
336336
Err(_) => break,

components/locale_core/src/extensions/unicode/value.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,8 @@ impl Value {
255255
Self(input)
256256
}
257257

258-
pub(crate) fn parse_subtag(t: &[u8]) -> Result<Option<Subtag>, ParseError> {
259-
Self::parse_subtag_from_utf8_manual_slice(t, 0, t.len())
260-
}
261-
262-
pub(crate) const fn parse_subtag_from_utf8_manual_slice(
263-
code_units: &[u8],
264-
start: usize,
265-
end: usize,
266-
) -> Result<Option<Subtag>, ParseError> {
267-
match Subtag::try_from_utf8_manual_slice(code_units, start, end) {
258+
pub(crate) const fn parse_subtag_from_utf8(t: &[u8]) -> Result<Option<Subtag>, ParseError> {
259+
match Subtag::try_from_utf8(t) {
268260
Ok(TRUE_VALUE) => Ok(None),
269261
Ok(s) => Ok(Some(s)),
270262
Err(_) => Err(ParseError::InvalidSubtag),

components/locale_core/src/helpers.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,15 @@ macro_rules! impl_tinystr_subtag {
4242
}
4343

4444
/// See [`Self::try_from_str`]
45-
#[inline]
46-
pub const fn try_from_utf8(code_units: &[u8]) -> Result<Self, crate::parser::errors::ParseError> {
47-
Self::try_from_utf8_manual_slice(code_units, 0, code_units.len())
48-
}
49-
50-
/// Equivalent to [`try_from_utf8(bytes[start..end])`](Self::try_from_utf8),
51-
/// but callable in a `const` context (which range indexing is not).
52-
pub const fn try_from_utf8_manual_slice(
45+
pub const fn try_from_utf8(
5346
code_units: &[u8],
54-
start: usize,
55-
end: usize,
5647
) -> Result<Self, crate::parser::errors::ParseError> {
57-
let slen = end - start;
58-
59-
#[allow(clippy::double_comparisons)] // if len_start == len_end
60-
if slen < $len_start || slen > $len_end {
48+
#[allow(clippy::double_comparisons)] // if code_units.len() === 0
49+
if code_units.len() < $len_start || code_units.len() > $len_end {
6150
return Err(crate::parser::errors::ParseError::$error);
6251
}
6352

64-
match tinystr::TinyAsciiStr::try_from_utf8_manual_slice(code_units, start, end) {
53+
match tinystr::TinyAsciiStr::try_from_utf8(code_units) {
6554
Ok($tinystr_ident) if $validate => Ok(Self($normalize)),
6655
_ => Err(crate::parser::errors::ParseError::$error),
6756
}

components/locale_core/src/parser/langid.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub const fn parse_locale_with_single_variant_single_keyword_unicode_extension_f
129129

130130
if let (i, Some(subtag)) = iter.next_const() {
131131
iter = i;
132-
match subtags::Language::try_from_utf8_manual_slice(iter.slice, 0, subtag.len()) {
132+
match subtags::Language::try_from_utf8(subtag) {
133133
Ok(l) => language = l,
134134
Err(e) => return Err(e),
135135
}
@@ -145,17 +145,13 @@ pub const fn parse_locale_with_single_variant_single_keyword_unicode_extension_f
145145
}
146146

147147
if matches!(position, ParserPosition::Script) {
148-
if let Ok(s) = subtags::Script::try_from_utf8_manual_slice(subtag, 0, subtag.len()) {
148+
if let Ok(s) = subtags::Script::try_from_utf8(subtag) {
149149
script = Some(s);
150150
position = ParserPosition::Region;
151-
} else if let Ok(r) =
152-
subtags::Region::try_from_utf8_manual_slice(subtag, 0, subtag.len())
153-
{
151+
} else if let Ok(r) = subtags::Region::try_from_utf8(subtag) {
154152
region = Some(r);
155153
position = ParserPosition::Variant;
156-
} else if let Ok(v) =
157-
subtags::Variant::try_from_utf8_manual_slice(subtag, 0, subtag.len())
158-
{
154+
} else if let Ok(v) = subtags::Variant::try_from_utf8(subtag) {
159155
// We cannot handle multiple variants in a const context
160156
debug_assert!(variant.is_none());
161157
variant = Some(v);
@@ -166,12 +162,10 @@ pub const fn parse_locale_with_single_variant_single_keyword_unicode_extension_f
166162
return Err(ParseError::InvalidSubtag);
167163
}
168164
} else if matches!(position, ParserPosition::Region) {
169-
if let Ok(s) = subtags::Region::try_from_utf8_manual_slice(subtag, 0, subtag.len()) {
165+
if let Ok(s) = subtags::Region::try_from_utf8(subtag) {
170166
region = Some(s);
171167
position = ParserPosition::Variant;
172-
} else if let Ok(v) =
173-
subtags::Variant::try_from_utf8_manual_slice(subtag, 0, subtag.len())
174-
{
168+
} else if let Ok(v) = subtags::Variant::try_from_utf8(subtag) {
175169
// We cannot handle multiple variants in a const context
176170
debug_assert!(variant.is_none());
177171
variant = Some(v);
@@ -181,8 +175,7 @@ pub const fn parse_locale_with_single_variant_single_keyword_unicode_extension_f
181175
} else {
182176
return Err(ParseError::InvalidSubtag);
183177
}
184-
} else if let Ok(v) = subtags::Variant::try_from_utf8_manual_slice(subtag, 0, subtag.len())
185-
{
178+
} else if let Ok(v) = subtags::Variant::try_from_utf8(subtag) {
186179
debug_assert!(matches!(position, ParserPosition::Variant));
187180
if variant.is_some() {
188181
// We cannot handle multiple variants in a const context
@@ -200,11 +193,11 @@ pub const fn parse_locale_with_single_variant_single_keyword_unicode_extension_f
200193

201194
if matches!(mode, ParserMode::Locale) {
202195
if let Some(subtag) = iter.peek() {
203-
match ExtensionType::try_from_utf8_manual_slice(subtag, 0, subtag.len()) {
196+
match ExtensionType::try_from_utf8(subtag) {
204197
Ok(ExtensionType::Unicode) => {
205198
iter = iter.next_const().0;
206199
if let Some(peek) = iter.peek() {
207-
if Attribute::try_from_utf8_manual_slice(peek, 0, peek.len()).is_ok() {
200+
if Attribute::try_from_utf8(peek).is_ok() {
208201
// We cannot handle Attributes in a const context
209202
return Err(ParseError::InvalidSubtag);
210203
}
@@ -219,12 +212,12 @@ pub const fn parse_locale_with_single_variant_single_keyword_unicode_extension_f
219212
// We cannot handle more than one Key in a const context
220213
return Err(ParseError::InvalidSubtag);
221214
}
222-
match Key::try_from_utf8_manual_slice(peek, 0, peek.len()) {
215+
match Key::try_from_utf8(peek) {
223216
Ok(k) => key = Some(k),
224217
Err(e) => return Err(e),
225218
};
226219
} else if key.is_some() {
227-
match Value::parse_subtag_from_utf8_manual_slice(peek, 0, peek.len()) {
220+
match Value::parse_subtag_from_utf8(peek) {
228221
Ok(Some(t)) => {
229222
if current_type.is_some() {
230223
// We cannot handle more than one type in a const context

components/locale_core/src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const fn get_current_subtag(slice: &[u8], idx: usize) -> (usize, usize) {
3232
// If it's a separator, set the start to idx+1 and advance the idx to the next char.
3333
(idx + 1, idx + 1)
3434
} else {
35-
// If it's idx=0, start is 0 and end is set to 1
35+
// If it's idx=0, end is set to 1
3636
debug_assert!(idx == 0);
3737
(0, 1)
3838
};

provider/core/src/marker.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,9 @@ impl DataMarkerPath {
423423
i += 1;
424424
}
425425

426-
match Self::validate_path_manual_slice(tagged, start, end) {
426+
match Self::validate_path(tagged.as_bytes().split_at(end).0.split_at(start).1) {
427427
Ok(()) => (),
428-
Err(e) => return Err(e),
428+
Err((expected, index)) => return Err((expected, start + index)),
429429
};
430430

431431
let hash = DataMarkerPathHash(
@@ -440,13 +440,7 @@ impl DataMarkerPath {
440440
Ok(Self { tagged, hash })
441441
}
442442

443-
const fn validate_path_manual_slice(
444-
path: &'static str,
445-
start: usize,
446-
end: usize,
447-
) -> Result<(), (&'static str, usize)> {
448-
debug_assert!(start <= end);
449-
debug_assert!(end <= path.len());
443+
const fn validate_path(path: &'static [u8]) -> Result<(), (&'static str, usize)> {
450444
// Regex: [a-zA-Z0-9_][a-zA-Z0-9_/]*@[0-9]+
451445
enum State {
452446
Empty,
@@ -455,12 +449,12 @@ impl DataMarkerPath {
455449
Version,
456450
}
457451
use State::*;
458-
let mut i = start;
452+
let mut i = 0;
459453
let mut state = Empty;
460454
loop {
461-
let byte = if i < end {
462-
#[allow(clippy::indexing_slicing)] // protected by debug assertion
463-
Some(path.as_bytes()[i])
455+
let byte = if i < path.len() {
456+
#[allow(clippy::indexing_slicing)] // iterator not const
457+
Some(path[i])
464458
} else {
465459
None
466460
};

utils/tinystr/src/ascii.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<const N: usize> TinyAsciiStr<N> {
2525
/// `code_units` may contain at most `N` non-null ASCII code points.
2626
#[inline]
2727
pub const fn try_from_utf8(code_units: &[u8]) -> Result<Self, TinyStrError> {
28-
Self::try_from_utf8_inner(code_units, 0, code_units.len(), false)
28+
Self::try_from_utf8_inner(code_units, false)
2929
}
3030

3131
/// Creates a `TinyAsciiStr<N>` from the given UTF-16 slice.
@@ -126,49 +126,27 @@ impl<const N: usize> TinyAsciiStr<N> {
126126
/// assert!(matches!(TinyAsciiStr::<3>::try_from_raw(*b"\0A\0"), Err(_)));
127127
/// ```
128128
pub const fn try_from_raw(raw: [u8; N]) -> Result<Self, TinyStrError> {
129-
Self::try_from_utf8_inner(&raw, 0, N, true)
130-
}
131-
132-
/// Equivalent to [`try_from_utf8(bytes[start..end])`](Self::try_from_utf8),
133-
/// but callable in a `const` context (which range indexing is not).
134-
#[inline]
135-
pub const fn try_from_utf8_manual_slice(
136-
code_units: &[u8],
137-
start: usize,
138-
end: usize,
139-
) -> Result<Self, TinyStrError> {
140-
Self::try_from_utf8_inner(code_units, start, end, false)
141-
}
142-
143-
/// Equivalent to [`try_from_utf16(bytes[start..end])`](Self::try_from_utf16),
144-
/// but callable in a `const` context (which range indexing is not).
145-
#[inline]
146-
pub const fn try_from_utf16_manual_slice(
147-
code_units: &[u16],
148-
start: usize,
149-
end: usize,
150-
) -> Result<Self, TinyStrError> {
151-
Self::try_from_utf16_inner(code_units, start, end, false)
129+
Self::try_from_utf8_inner(&raw, true)
152130
}
153131

154132
pub(crate) const fn try_from_utf8_inner(
155133
code_units: &[u8],
156-
start: usize,
157-
end: usize,
158134
allow_trailing_null: bool,
159135
) -> Result<Self, TinyStrError> {
160-
let len = end - start;
161-
if len > N {
162-
return Err(TinyStrError::TooLarge { max: N, len });
136+
if code_units.len() > N {
137+
return Err(TinyStrError::TooLarge {
138+
max: N,
139+
len: code_units.len(),
140+
});
163141
}
164142

165143
let mut out = [0; N];
166144
let mut i = 0;
167145
let mut found_null = false;
168146
// Indexing is protected by TinyStrError::TooLarge
169147
#[allow(clippy::indexing_slicing)]
170-
while i < len {
171-
let b = code_units[start + i];
148+
while i < code_units.len() {
149+
let b = code_units[i];
172150

173151
if b == 0 {
174152
found_null = true;

utils/tinystr/src/ule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ unsafe impl<const N: usize> ULE for TinyAsciiStr<N> {
2424
}
2525
// Validate the bytes
2626
for chunk in bytes.chunks_exact(N) {
27-
let _ = TinyAsciiStr::<N>::try_from_utf8_inner(chunk, 0, N, true)
27+
let _ = TinyAsciiStr::<N>::try_from_utf8_inner(chunk, true)
2828
.map_err(|_| ZeroVecError::parse::<Self>())?;
2929
}
3030
Ok(())

utils/zerotrie/src/builder/konst/store.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ impl<'a, T> ConstSlice<'a, T> {
2929
}
3030
}
3131

32-
/// Creates a [`ConstSlice`] with the given start and limit.
33-
pub const fn from_manual_slice(full_slice: &'a [T], start: usize, limit: usize) -> Self {
34-
ConstSlice {
35-
full_slice,
36-
start,
37-
limit,
38-
}
39-
}
40-
4132
/// Returns the length of the [`ConstSlice`].
4233
pub const fn len(&self) -> usize {
4334
self.limit - self.start
@@ -147,7 +138,14 @@ impl<const N: usize, T> ConstArrayBuilder<N, T> {
147138

148139
/// Returns the initialized elements as a [`ConstSlice`].
149140
pub const fn as_const_slice(&self) -> ConstSlice<T> {
150-
ConstSlice::from_manual_slice(&self.full_array, self.start, self.limit)
141+
ConstSlice::from_slice(
142+
&self
143+
.full_array
144+
.split_at(self.limit)
145+
.0
146+
.split_at(self.start)
147+
.1,
148+
)
151149
}
152150

153151
/// Non-const function that returns a slice of the initialized elements.

0 commit comments

Comments
 (0)