aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/json
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe/src/json')
-rw-r--r--crates/shirabe/src/json/json_file.rs18
-rw-r--r--crates/shirabe/src/json/json_manipulator.rs54
2 files changed, 44 insertions, 28 deletions
diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs
index 075e1f64..0ce55e55 100644
--- a/crates/shirabe/src/json/json_file.rs
+++ b/crates/shirabe/src/json/json_file.rs
@@ -14,8 +14,8 @@ use shirabe_external_packages::seld::json_lint::{ParsingException, ParsingExcept
use shirabe_php_shim::{
InvalidArgumentException, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE,
PhpMixed, RuntimeException, UnexpectedValueException, dirname, file_exists, file_get_contents,
- file_put_contents, is_dir, is_file, json_decode, json_encode_ex, mkdir, realpath, str_contains,
- str_ends_with, str_repeat, strlen, strpos, usleep,
+ file_put_contents, is_dir, is_file, json_decode, json_encode_ex, mkdir, php_regex, realpath,
+ str_contains, str_ends_with, str_repeat, strlen, strpos, usleep,
};
#[derive(Debug, Clone)]
@@ -113,7 +113,7 @@ impl JsonFile {
http_downloader: Option<std::rc::Rc<std::cell::RefCell<HttpDownloader>>>,
io: Option<std::rc::Rc<std::cell::RefCell<dyn IOInterface>>>,
) -> anyhow::Result<Self> {
- if http_downloader.is_none() && Preg::is_match(r"{^https?://}i", &path) {
+ if http_downloader.is_none() && Preg::is_match(php_regex!(r"{^https?://}i"), &path) {
return Err(InvalidArgumentException {
message: "http urls require a HttpDownloader instance to be passed".to_string(),
code: 0,
@@ -466,7 +466,7 @@ impl JsonFile {
// Pretty printing and not using default indentation
let indent_owned = options.indent.clone();
return Preg::replace_callback(
- r"#^ {4,}#m",
+ php_regex!(r"#^ {4,}#m"),
move |m: &indexmap::IndexMap<
shirabe_external_packages::composer::pcre::CaptureKey,
String,
@@ -510,7 +510,9 @@ impl JsonFile {
{
let mut count: usize = 0;
let replaced = Preg::replace5(
- r#"{\r?\n<<<<<<< [^\r\n]+\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n(?:\|{7} [^\r\n]+\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n)?=======\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n>>>>>>> [^\r\n]+(\r?\n)}"#,
+ php_regex!(
+ r#"{\r?\n<<<<<<< [^\r\n]+\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n(?:\|{7} [^\r\n]+\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n)?=======\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n>>>>>>> [^\r\n]+(\r?\n)}"#
+ ),
" \"content-hash\": \"VCS merge conflict detected. Please run `composer update --lock`.\",$1",
json,
-1,
@@ -574,7 +576,11 @@ impl JsonFile {
pub fn detect_indenting(json: Option<&str>) -> String {
let mut m: IndexMap<CaptureKey, String> = IndexMap::new();
- if Preg::is_match3(r##"#^([ \t]+)"#m"##, json.unwrap_or(""), Some(&mut m)) {
+ if Preg::is_match3(
+ php_regex!(r##"#^([ \t]+)"#m"##),
+ json.unwrap_or(""),
+ Some(&mut m),
+ ) {
return m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default();
}
diff --git a/crates/shirabe/src/json/json_manipulator.rs b/crates/shirabe/src/json/json_manipulator.rs
index b11178ff..e501940c 100644
--- a/crates/shirabe/src/json/json_manipulator.rs
+++ b/crates/shirabe/src/json/json_manipulator.rs
@@ -8,8 +8,8 @@ use shirabe_external_packages::composer::pcre::{CaptureKey, Preg};
use shirabe_php_shim::{
InvalidArgumentException, LogicException, PhpMixed, addcslashes, array_key_exists, array_keys,
array_reverse, empty, explode, implode, in_array, is_array, is_int, is_numeric, json_decode,
- php_truthy, preg_quote, rtrim, str_contains, str_repeat, str_replace, strlen, strnatcmp,
- strpos, substr, trim, uksort,
+ php_regex, php_truthy, preg_quote, rtrim, str_contains, 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::is_match3("#^\\{(.*)\\}$#s", &contents, None) {
+ if !Preg::is_match3(php_regex!("#^\\{(.*)\\}$#s"), &contents, None) {
return Err(InvalidArgumentException {
message: "The json file must be an object ({})".to_string(),
code: 0,
@@ -115,7 +115,7 @@ impl JsonManipulator {
} else {
let mut groups: IndexMap<CaptureKey, String> = IndexMap::new();
if Preg::is_match3(
- "#^\\s*\\{\\s*\\S+.*?(\\s*\\}\\s*)$#s",
+ php_regex!("#^\\s*\\{\\s*\\S+.*?(\\s*\\}\\s*)$#s"),
&links,
Some(&mut groups),
) {
@@ -125,7 +125,7 @@ impl JsonManipulator {
.unwrap_or_default();
// link missing but non empty links
links = Preg::replace(
- &format!("{{{}$}}", preg_quote(&groups_1, None)),
+ format!("{{{}$}}", preg_quote(&groups_1, None)),
// addcslashes is used to double up backslashes/$ since preg_replace resolves them as back references otherwise, see #1588
&addcslashes(
&format!(
@@ -175,7 +175,7 @@ impl JsonManipulator {
let replacements = ["0-$0", "1-$0", "2-$0", "3-$0", "4-$0"];
let mut result = requirement.to_string();
for (p, r) in patterns.iter().zip(replacements.iter()) {
- result = Preg::replace(p, r, &result);
+ result = Preg::replace(*p, r, &result);
}
result
} else {
@@ -738,7 +738,9 @@ impl JsonManipulator {
} else {
let mut leading_match: IndexMap<String, String> = IndexMap::new();
if Preg::is_match_named(
- "#^\\{(?P<leadingspace>\\s*?)(?P<content>\\S+.*?)?(?P<trailingspace>\\s*)\\}$#s",
+ php_regex!(
+ "#^\\{(?P<leadingspace>\\s*?)(?P<content>\\S+.*?)?(?P<trailingspace>\\s*)\\}$#s"
+ ),
&children,
&mut leading_match,
) {
@@ -762,7 +764,7 @@ impl JsonManipulator {
// child missing but non empty children
if append {
children = Preg::replace(
- &format!("#{}}}$#", whitespace),
+ format!("#{}}}$#", whitespace),
&addcslashes(
&format!(
",{}{}{}{}: {}{}}}",
@@ -780,7 +782,7 @@ impl JsonManipulator {
} else {
whitespace = leading_space.clone();
children = Preg::replace(
- &format!("#^{{{}#", whitespace),
+ format!("#^{{{}#", whitespace),
&addcslashes(
&format!(
"{{{}{}: {},{}{}{}",
@@ -894,7 +896,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::is_match3(&format!("{{\"{}\"\\s*:}}i", key_regex), &children, None) {
+ if Preg::is_match3(format!("{{\"{}\"\\s*:}}i", key_regex), &children, None) {
// 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.
@@ -908,7 +910,7 @@ impl JsonManipulator {
}
let mut count_out: usize = 0;
let cleaned = Preg::replace5(
- &format!("{{,\\s*{}}}i", preg_quote(&best_match, None)),
+ format!("{{,\\s*{}}}i", preg_quote(&best_match, None)),
"",
&children,
-1,
@@ -916,7 +918,7 @@ impl JsonManipulator {
);
if 1 != count_out {
let cleaned2 = Preg::replace5(
- &format!("{{{}\\s*,?\\s*}}i", preg_quote(&best_match, None)),
+ format!("{{{}\\s*,?\\s*}}i", preg_quote(&best_match, None)),
"",
&cleaned,
-1,
@@ -942,7 +944,7 @@ impl JsonManipulator {
// no child data left, $name was the only key in
let mut empty_match: IndexMap<String, String> = IndexMap::new();
if Preg::is_match_named(
- "#^\\{\\s*?(?P<content>\\S+.*?)?(?P<trailingspace>\\s*)\\}$#s",
+ php_regex!("#^\\{\\s*?(?P<content>\\S+.*?)?(?P<trailingspace>\\s*)\\}$#s"),
&children_clean,
&mut empty_match,
) && empty_match.get("content").is_none()
@@ -1039,7 +1041,9 @@ impl JsonManipulator {
let mut leading_match: IndexMap<String, String> = IndexMap::new();
if Preg::is_match_named(
- "#^\\[(?P<leadingspace>\\s*?)(?P<content>\\S+.*?)?(?P<trailingspace>\\s*)\\]$#s",
+ php_regex!(
+ "#^\\[(?P<leadingspace>\\s*?)(?P<content>\\S+.*?)?(?P<trailingspace>\\s*)\\]$#s"
+ ),
&children,
&mut leading_match,
) {
@@ -1067,7 +1071,7 @@ impl JsonManipulator {
// child missing but non empty children
if append {
children = Preg::replace(
- &format!("#{}\\]$#", whitespace),
+ format!("#{}\\]$#", whitespace),
&addcslashes(
&format!(
",{}{}{}]",
@@ -1082,7 +1086,7 @@ impl JsonManipulator {
} else {
whitespace = leading_whitespace.clone();
children = Preg::replace(
- &format!("#^\\[{}#", whitespace),
+ format!("#^\\[{}#", whitespace),
&addcslashes(
&format!(
"[{}{},{}",
@@ -1330,13 +1334,17 @@ impl JsonManipulator {
// append at the end of the file and keep whitespace
let mut tail_match: IndexMap<CaptureKey, String> = IndexMap::new();
- if Preg::is_match3("#[^{\\s](\\s*)\\}$#", &self.contents, Some(&mut tail_match)) {
+ if Preg::is_match3(
+ php_regex!("#[^{\\s](\\s*)\\}$#"),
+ &self.contents,
+ Some(&mut tail_match),
+ ) {
let tail_match_1 = tail_match
.get(&CaptureKey::ByIndex(1))
.cloned()
.unwrap_or_default();
self.contents = Preg::replace(
- &format!("#{}\\}}$#", tail_match_1),
+ format!("#{}\\}}$#", tail_match_1),
&addcslashes(
&format!(
",{}{}{}: {}{}}}",
@@ -1356,7 +1364,7 @@ impl JsonManipulator {
// append at the end of the file
self.contents = Preg::replace(
- "#\\}$#",
+ php_regex!("#\\}$#"),
&addcslashes(
&format!(
"{}{}: {}{}}}",
@@ -1406,15 +1414,17 @@ 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::is_match3("#,\\s*$#", &start, None) && Preg::is_match3("#^\\}$#", &end, None) {
+ if Preg::is_match3(php_regex!("#,\\s*$#"), &start, None)
+ && Preg::is_match3(php_regex!("#^\\}$#"), &end, None)
+ {
start = rtrim(
- &Preg::replace("#,(\\s*)$#", "$1", &start),
+ &Preg::replace(php_regex!("#,(\\s*)$#"), "$1", &start),
Some(&self.indent),
);
}
self.contents = format!("{}{}", start, end);
- if Preg::is_match3("#^\\{\\s*\\}\\s*$#", &self.contents, None) {
+ if Preg::is_match3(php_regex!("#^\\{\\s*\\}\\s*$#"), &self.contents, None) {
self.contents = "{\n}".to_string();
}