-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added char pattern support to
str_split
and str_replace
.
Replaced both internal `*Pattern` types used by `str_split` and `str_replace` with a shared `Pattern` enum. Added tests to ensure that both macros handle char patterns correctly.
- Loading branch information
1 parent
57e8893
commit fc07aa6
Showing
10 changed files
with
208 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use super::AsciiByte; | ||
|
||
pub(crate) struct PatternCtor<T>(pub(crate) T); | ||
|
||
impl PatternCtor<u8> { | ||
pub(crate) const fn conv(self) -> Pattern { | ||
Pattern::AsciiByte(AsciiByte::new(self.0)) | ||
} | ||
} | ||
|
||
impl PatternCtor<&'static str> { | ||
pub(crate) const fn conv(self) -> Pattern { | ||
if let [b @ 0..=127] = *self.0.as_bytes() { | ||
Pattern::AsciiByte(AsciiByte::new(b)) | ||
} else { | ||
Pattern::Str(self.0) | ||
} | ||
} | ||
} | ||
|
||
impl PatternCtor<char> { | ||
pub(crate) const fn conv(self) -> Pattern { | ||
let code = self.0 as u32; | ||
if let c @ 0..=127 = code { | ||
Pattern::AsciiByte(AsciiByte::new(c as u8)) | ||
} else { | ||
Pattern::Char(crate::char_encoding::char_to_display(self.0)) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone)] | ||
pub(crate) enum Pattern { | ||
AsciiByte(AsciiByte), | ||
Str(&'static str), | ||
Char(crate::char_encoding::FmtChar), | ||
} | ||
|
||
pub(crate) enum PatternNorm<'a> { | ||
AsciiByte(AsciiByte), | ||
Str(&'a str), | ||
} | ||
|
||
impl Pattern { | ||
pub(crate) const fn normalize(&self) -> PatternNorm<'_> { | ||
match self { | ||
Pattern::AsciiByte(ab) => PatternNorm::AsciiByte(*ab), | ||
Pattern::Str(str) => PatternNorm::Str(*str), | ||
Pattern::Char(char) => PatternNorm::Str(char.as_str()), | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.