-
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
5 changed files
with
71 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#define rustutils_regex_replace(text, re, re_params, replacement) CALL_LIB(RUST_UTILS, "regex_replace")(text, re, re_params, replacement) |
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 |
---|---|---|
@@ -1,19 +1,56 @@ | ||
use regex::Regex; | ||
use regex_cache::RegexCache; | ||
use std::sync::Mutex; | ||
|
||
const CACHE_SIZE: usize = 128; | ||
|
||
fn init_regex_cache() -> RegexCache { | ||
RegexCache::new(CACHE_SIZE) | ||
} | ||
|
||
static RE_CACHE: Mutex<Option<RegexCache>> = Mutex::new(None); | ||
|
||
|
||
fn compile_regex(pattern: &str) -> regex::Regex { | ||
let mut cache = RE_CACHE.lock().unwrap(); | ||
if let Some(ref mut cache) = *cache { | ||
cache.compile(pattern).unwrap().clone() | ||
} else { | ||
*cache = Some(init_regex_cache()); | ||
init_regex_cache().compile(pattern).unwrap().clone() | ||
} | ||
} | ||
|
||
byond_fn!(fn regex_replace (text, re, re_params, replacement) { | ||
Some(regexp_replace(text, re, re_params, replacement)) | ||
}); | ||
|
||
fn regexp_replace(text: &str, re: &str, re_params: &str, replacement: &str) -> String { | ||
let pattern = format!(r"(?{}){}", re_params, re); | ||
let re = compile_regex(pattern.as_str()); | ||
|
||
re.replace_all(text, replacement).to_string() | ||
} | ||
|
||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn regexp_test() { | ||
let pattern = r"(?i)\bго+л\b"; | ||
let pattern = r"\bго+л\b"; | ||
|
||
let re = Regex::new(pattern).unwrap(); | ||
let pattern_flags = "i"; | ||
|
||
let input = "Сука Гооооооооооооооол в гооооландские ворота."; | ||
|
||
let output = re.replace_all(input, "поподание мячем"); | ||
let expected_output = "Сука попадание мячем в гооооландские ворота."; | ||
|
||
let replacement = "попадание мячем"; | ||
|
||
assert_eq!(output, "Сука поподание мячем в гооооландские ворота."); | ||
for _ in 1..2 { | ||
assert_eq!(expected_output, regexp_replace(input, pattern, pattern_flags, replacement)); | ||
} | ||
} | ||
} |
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,19 @@ | ||
#include "common.dm" | ||
|
||
var/input = "Сука Гооооооооооооооол в гооооландские ворота." | ||
var/pattern = @"\bго+л\b" | ||
var/pattern_flags = "i" | ||
var/replacement = "попадание мячем" | ||
|
||
var/expected_output = "Сука попадание мячем в гооооландские ворота." | ||
|
||
/test/proc/test_regexp_replace() | ||
var/output = rustutils_regex_replace(input, pattern, pattern_flags, replacement) | ||
ASSERT(output == expected_output) | ||
|
||
/test/proc/test_regex_time() | ||
var/start = world.timeofday | ||
for(var/i in 1 to 1000) | ||
var/output = rustutils_regex_replace(input, pattern, pattern_flags, replacement) | ||
var/end = world.timeofday | ||
ASSERT(end - start < 10) |