Skip to content

Commit 94d6e5f

Browse files
committed
change the take_while function to return a slice result
1 parent 3949260 commit 94d6e5f

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

core/parser/src/lexer/cursor.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,18 @@ impl<R: ReadChar> Cursor<R> {
164164
/// Note that all characters up until the stop character are added to the buffer, including the character right before.
165165
#[allow(clippy::cast_possible_truncation)]
166166
#[inline]
167-
pub(super) fn take_while_ascii_pred<F>(&mut self, buf: &mut [u8], pred: &F) -> io::Result<()>
167+
pub(super) fn take_while_ascii_pred<'a, F>(
168+
&mut self,
169+
buf: &'a mut [u8],
170+
pred: &F,
171+
) -> io::Result<&'a [u8]>
168172
where
169173
F: Fn(char) -> bool,
170174
{
171175
let mut count = 0;
172176
loop {
173177
if !self.next_is_ascii_pred(pred)? {
174-
return Ok(());
178+
return Ok(&buf[..count]);
175179
} else if let Some(byte) = self.next_char()? {
176180
buf[count] = byte as u8;
177181
count += 1;

core/parser/src/lexer/regex.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ impl<R> Tokenizer<R> for RegexLiteral {
116116

117117
let mut flags: [u8; 8] = [0; 8];
118118
let flags_start = cursor.pos();
119-
cursor.take_while_ascii_pred(&mut flags, &char::is_alphabetic)?;
119+
let flags_slice = cursor.take_while_ascii_pred(&mut flags, &char::is_alphabetic)?;
120120

121-
let flags_string = match RegExpFlags::from_bytes(flags) {
121+
// TODO: Change this to if err() then convert flags_slice to str
122+
let flags_string = match RegExpFlags::from_bytes(flags_slice) {
122123
Err(message) => return Err(Error::Syntax(message.into(), flags_start)),
123124
Ok(regex_flags) => regex_flags.to_string(),
124125
};
@@ -193,7 +194,7 @@ bitflags! {
193194
}
194195

195196
impl RegExpFlags {
196-
fn from_bytes(bytes: [u8; 8]) -> Result<Self, String> {
197+
fn from_bytes(bytes: &[u8]) -> Result<Self, String> {
197198
let mut flags = Self::default();
198199
for c in bytes {
199200
let new_flag = match c {
@@ -206,13 +207,18 @@ impl RegExpFlags {
206207
b'd' => Self::HAS_INDICES,
207208
b'v' => Self::UNICODE_SETS,
208209
0x00 => continue,
209-
_ => return Err(format!("invalid regular expression flag {}", char::from(c))),
210+
_ => {
211+
return Err(format!(
212+
"invalid regular expression flag {}",
213+
char::from(c.to_owned())
214+
))
215+
}
210216
};
211217

212218
if flags.contains(new_flag) {
213219
return Err(format!(
214220
"repeated regular expression flag {}",
215-
char::from(c)
221+
char::from(c.to_owned())
216222
));
217223
}
218224
flags.insert(new_flag);

0 commit comments

Comments
 (0)