-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
315 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* Built-in imports */ | ||
use core::{iter::Peekable, num::ParseIntError, str::Chars}; | ||
|
||
/// A single regex flag. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub enum Flag { | ||
/// Regex should be case insensitive. Corresponds to `i`. | ||
CaseInsensitive, | ||
/// Regex should be case sensitive. Corresponds to `I`. | ||
CaseSensitive, | ||
/// Regex is run for every match in a string. Corresponds to `g`. | ||
Global, | ||
/// Regex is run N times in a string. | ||
Numbered(usize), | ||
/// "Greedy swap" flag. Corresponds to `U`. | ||
GreedySwap, | ||
/// Ignore whitespaces. Corresponds to `x`. | ||
IgnoreWhitespaces, | ||
} | ||
|
||
impl Flag { | ||
pub fn list_from_chars( | ||
mut chars: Peekable<Chars>, | ||
) -> Result<Box<[Self]>, Error> { | ||
let mut flags = Vec::new(); | ||
|
||
while let Some(ch) = chars.next() { | ||
if ch.is_numeric() { | ||
let mut num_str = ch.to_string(); | ||
|
||
while let Some(&next_c) = chars.peek() { | ||
if next_c.is_numeric() { | ||
num_str.push(next_c); | ||
chars.next(); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
flags.push(Self::Numbered(num_str.parse::<usize>()?)); | ||
} else { | ||
flags.push(Self::try_from(ch)?); | ||
} | ||
} | ||
|
||
Ok(flags.into_boxed_slice()) | ||
} | ||
} | ||
|
||
#[derive(Debug, thiserror::Error, PartialEq, Eq)] | ||
pub enum Error { | ||
#[error("Unknown sed flag: {0}")] | ||
UnknownFlag(char), | ||
#[error("{0}")] | ||
ParseIntError(#[from] ParseIntError), | ||
} | ||
|
||
impl TryFrom<char> for Flag { | ||
type Error = Error; | ||
|
||
fn try_from(ch: char) -> Result<Self, Self::Error> { | ||
match ch { | ||
'i' => Ok(Self::CaseInsensitive), | ||
'I' => Ok(Self::CaseSensitive), | ||
'g' => Ok(Self::Global), | ||
'U' => Ok(Self::GreedySwap), | ||
'x' => Ok(Self::IgnoreWhitespaces), | ||
_ => Err(Self::Error::UnknownFlag(ch)), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_mixed_flags() { | ||
let chars: Peekable<Chars> = "i12g34U".chars().peekable(); | ||
let flags = Flag::list_from_chars(chars).unwrap(); | ||
assert_eq!( | ||
flags.as_ref(), | ||
vec![ | ||
Flag::CaseInsensitive, | ||
Flag::Numbered(12), | ||
Flag::Global, | ||
Flag::Numbered(34), | ||
Flag::GreedySwap, | ||
] | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_flag() { | ||
let chars: Peekable<Chars> = "iX".chars().peekable(); | ||
let result = Flag::list_from_chars(chars); | ||
assert_eq!(result.unwrap_err(), Error::UnknownFlag('X')); | ||
} | ||
|
||
#[test] | ||
fn test_empty_input() { | ||
let chars = "".chars().peekable(); | ||
let flags = Flag::list_from_chars(chars).unwrap(); | ||
assert!(flags.is_empty()); | ||
} | ||
} |
Oops, something went wrong.