Skip to content

Commit

Permalink
Cringy cringe
Browse files Browse the repository at this point in the history
  • Loading branch information
Furrior committed May 14, 2024
1 parent abe9702 commit e4f1a72
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
6 changes: 2 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::{
io,
result,
str::Utf8Error,
io, result, str::Utf8Error
};
use thiserror::Error;

pub type Result<T> = result::Result<T, Error>;
pub type BResult<T> = result::Result<T, Error>;

#[derive(Error, Debug)]
pub enum Error {
Expand Down
4 changes: 2 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::Result;
use crate::error::BResult;
use base64::Engine;
use std::{
fs::File,
Expand All @@ -12,7 +12,7 @@ byond_fn!(fn file_write(data, path, ...rest) {
write(data, path, should_decode_b64).err()
});

fn write(data: &str, path: &str, base64decode: bool) -> Result<usize> {
fn write(data: &str, path: &str, base64decode: bool) -> BResult<usize> {
let path: &std::path::Path = path.as_ref();
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
Expand Down
46 changes: 38 additions & 8 deletions src/regexp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use regex_cache::RegexCache;
use std::sync::Mutex;



byond_fn!(fn regex_replace (text, re, re_params, replacement) {
Some(regexp_replace(text, re, re_params, replacement))
});
Expand All @@ -13,21 +15,29 @@ fn init_regex_cache() -> RegexCache {

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()
fn compile_regex(pattern: &str) -> Result<regex::Regex, regex_cache::Error> {
{
let mut cache = RE_CACHE.lock().unwrap();
if let Some(ref mut cache) = *cache {
match cache.compile(pattern) {
Ok(re) => return Ok(re.clone()),
Err(error) => return Err(error),
}
} else {
*cache = Some(init_regex_cache());
}
}
compile_regex(pattern)
}

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()
match re {
Ok(re) => re.replace_all(text, replacement).to_string(),
Err(_) => String::from("Words replace."), // words replace, words replace...
}
}

#[cfg(test)]
Expand All @@ -53,4 +63,24 @@ mod tests {
);
}
}

#[test]
fn error_test() {
let pattern = r"\bго+л\b";

let pattern_flags = "igu";

let input = "Сука Гооооооооооооооол в гооооландские ворота.";

let expected_output = "Words replace.";

let replacement = "попадание мячем";

for _ in 1..2 {
assert_eq!(
expected_output,
regexp_replace(input, pattern, pattern_flags, replacement)
);
}
}
}
12 changes: 6 additions & 6 deletions tests/dm/regexp.dme
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
var/input = "Сука Гооооооооооооооол в гооооландские ворота."
var/pattern = @"\bго+л\b"
var/pattern_flags = "i"
var/incorrect_pattern_flags = "igu"
var/replacement = "попадание мячем"

var/expected_output = "Сука попадание мячем в гооооландские ворота."
var/expected_error = "Words replace."

/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)
/test/proc/crash()
var/output = rustutils_regex_replace(input, pattern, incorrect_pattern_flags, replacement)
world.log << output
ASSERT(output == expected_error)

0 comments on commit e4f1a72

Please sign in to comment.