diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-18 01:57:02 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-18 01:57:02 +0900 |
| commit | fed0a6e7ac361af9b963c1f62411b1a85478230c (patch) | |
| tree | 5cde64a24845c761890fbcbe05e0d702f1ec8df7 /crates/shirabe/src/json/json_manipulator.rs | |
| parent | e093b2be1c333e67c96aebb0a5291bea9ae3d6db (diff) | |
| download | php-shirabe-fed0a6e7ac361af9b963c1f62411b1a85478230c.tar.gz php-shirabe-fed0a6e7ac361af9b963c1f62411b1a85478230c.tar.zst php-shirabe-fed0a6e7ac361af9b963c1f62411b1a85478230c.zip | |
refactor(preg): add preg_is_match for existence-only call sites
The capture groups were discarded at 162 of the preg_match call sites,
which only tested the Option. They now call preg_is_match, which lets the
regex engine skip capture tracking.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/json/json_manipulator.rs')
| -rw-r--r-- | crates/shirabe/src/json/json_manipulator.rs | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/crates/shirabe/src/json/json_manipulator.rs b/crates/shirabe/src/json/json_manipulator.rs index e224e68e..b580b0ec 100644 --- a/crates/shirabe/src/json/json_manipulator.rs +++ b/crates/shirabe/src/json/json_manipulator.rs @@ -7,9 +7,9 @@ use indexmap::IndexMap; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PhpMixed, addcslashes, array_key_exists, array_keys, array_reverse, empty, explode, implode, in_array_loose, is_array, is_int, is_numeric, - json_decode_assoc, json_decode_obj, php_regex, php_truthy, preg_match, preg_quote, - preg_replace, preg_replace2, rtrim, str_repeat, str_replace, strlen, strnatcmp, strpos, substr, - trim, uksort, + json_decode_assoc, json_decode_obj, php_regex, php_truthy, preg_is_match, preg_match, + preg_quote, preg_replace, preg_replace2, rtrim, str_repeat, str_replace, strlen, strnatcmp, + strpos, substr, trim, uksort, }; #[derive(Debug)] @@ -35,7 +35,7 @@ impl JsonManipulator { if contents.is_empty() { contents = "{}".to_string(); } - if preg_match(php_regex!("#^\\{(.*)\\}$#s"), &contents).is_none() { + if !preg_is_match(php_regex!("#^\\{(.*)\\}$#s"), &contents) { return Err(InvalidArgumentException::new( "The json file must be an object ({})".to_string(), ) @@ -890,7 +890,7 @@ impl JsonManipulator { // try and find a match for the subkey let key_regex = str_replace("/", "\\\\?/", &preg_quote(&name_owned, None)); let mut children_clean: Option<String> = None; - if preg_match(format!("{{\"{}\"\\s*:}}i", key_regex), &children).is_some() { + if preg_is_match(format!("{{\"{}\"\\s*:}}i", key_regex), &children) { // find best match for the value of "name". The PHP pattern `"name"\s*:\s*(?&json)` is // not anchored, so it can match the key at several nesting levels; collect every such // occurrence and keep the longest, reproducing PHP's behaviour. @@ -1390,8 +1390,8 @@ impl JsonManipulator { // check that we are not leaving a dangling comma on the previous line if the last line was removed let mut start = self.contents[..m.key_pos].to_string(); let end = self.contents[e..].to_string(); - if preg_match(php_regex!("#,\\s*$#"), &start).is_some() - && preg_match(php_regex!("#^\\}$#"), &end).is_some() + if preg_is_match(php_regex!("#,\\s*$#"), &start) + && preg_is_match(php_regex!("#^\\}$#"), &end) { start = rtrim( &preg_replace(php_regex!("#,(\\s*)$#"), "$1", &start), @@ -1400,7 +1400,7 @@ impl JsonManipulator { } self.contents = format!("{}{}", start, end); - if preg_match(php_regex!("#^\\{\\s*\\}\\s*$#"), &self.contents).is_some() { + if preg_is_match(php_regex!("#^\\{\\s*\\}\\s*$#"), &self.contents) { self.contents = "{\n}".to_string(); } |
