From e4f1a725176e62837afc2f27528495dbdc72b952 Mon Sep 17 00:00:00 2001 From: Furior Date: Wed, 15 May 2024 00:50:05 +0700 Subject: [PATCH] Cringy cringe --- src/error.rs | 6 ++---- src/file.rs | 4 ++-- src/regexp.rs | 46 +++++++++++++++++++++++++++++++++++++-------- tests/dm/regexp.dme | 12 ++++++------ 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/src/error.rs b/src/error.rs index 6caab1f..fc5f54c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,11 +1,9 @@ use std::{ - io, - result, - str::Utf8Error, + io, result, str::Utf8Error }; use thiserror::Error; -pub type Result = result::Result; +pub type BResult = result::Result; #[derive(Error, Debug)] pub enum Error { diff --git a/src/file.rs b/src/file.rs index 43c4b14..e833443 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,4 +1,4 @@ -use crate::error::Result; +use crate::error::BResult; use base64::Engine; use std::{ fs::File, @@ -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 { +fn write(data: &str, path: &str, base64decode: bool) -> BResult { let path: &std::path::Path = path.as_ref(); if let Some(parent) = path.parent() { std::fs::create_dir_all(parent)?; diff --git a/src/regexp.rs b/src/regexp.rs index 74cb62f..6bbb0e8 100644 --- a/src/regexp.rs +++ b/src/regexp.rs @@ -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)) }); @@ -13,21 +15,29 @@ fn init_regex_cache() -> RegexCache { static RE_CACHE: Mutex> = 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 { + { + 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)] @@ -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) + ); + } + } } diff --git a/tests/dm/regexp.dme b/tests/dm/regexp.dme index 435d371..cbc4c3e 100644 --- a/tests/dm/regexp.dme +++ b/tests/dm/regexp.dme @@ -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)