Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[resolver]
incompatible-rust-versions = "fallback"
3 changes: 2 additions & 1 deletion fluent-pseudo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ include = [
]

[dependencies]
regex = "1"
regex = "1.9"
once_cell = "1.20"
14 changes: 5 additions & 9 deletions fluent-pseudo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use once_cell::sync::Lazy;
use regex::Captures;
use regex::Regex;
use std::borrow::Cow;
Expand All @@ -20,8 +21,8 @@ static FLIPPED_CAPS_MAP: &[char] = &[
'⊥', '∩', 'Ʌ', 'M', 'X', '⅄', 'Z',
];

static mut RE_EXCLUDED: Option<Regex> = None;
static mut RE_AZ: Option<Regex> = None;
static RE_EXCLUDED: Lazy<Regex> = Lazy::new(|| Regex::new(r"&[#\w]+;|<\s*.+?\s*>").unwrap());
static RE_AZ: Lazy<Regex> = Lazy::new(|| Regex::new(r"[a-zA-Z]").unwrap());

pub fn transform_dom(s: &str, flipped: bool, elongate: bool, with_markers: bool) -> Cow<str> {
// Exclude access-keys and other single-char messages
Expand All @@ -30,15 +31,12 @@ pub fn transform_dom(s: &str, flipped: bool, elongate: bool, with_markers: bool)
}

// XML entities (&#x202a;) and XML tags.
let re_excluded =
unsafe { RE_EXCLUDED.get_or_insert_with(|| Regex::new(r"&[#\w]+;|<\s*.+?\s*>").unwrap()) };

let mut result = Cow::from(s);

let mut pos = 0;
let mut diff = 0;

for cap in re_excluded.captures_iter(s) {
for cap in RE_EXCLUDED.captures_iter(s) {
let capture = cap.get(0).unwrap();

let sub_len = capture.start() - pos;
Expand All @@ -65,15 +63,13 @@ pub fn transform_dom(s: &str, flipped: bool, elongate: bool, with_markers: bool)
}

pub fn transform(s: &str, flipped: bool, elongate: bool) -> Cow<str> {
let re_az = unsafe { RE_AZ.get_or_insert_with(|| Regex::new(r"[a-zA-Z]").unwrap()) };

let (small_map, caps_map) = if flipped {
(FLIPPED_SMALL_MAP, FLIPPED_CAPS_MAP)
} else {
(TRANSFORM_SMALL_MAP, TRANSFORM_CAPS_MAP)
};

re_az.replace_all(s, |caps: &Captures| {
RE_AZ.replace_all(s, |caps: &Captures| {
let ch = caps[0].chars().next().unwrap();
let cc = ch as u8;
if (97..=122).contains(&cc) {
Expand Down