Skip to content

Commit

Permalink
update RemesPath s_sub to allow string-replace
Browse files Browse the repository at this point in the history
  • Loading branch information
molsonkiko committed Feb 27, 2023
1 parent 76ebd58 commit a28eeeb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

1. Hopefully eliminated crash bug that sometimes seems to happen because a file that had a tree viewer associated with it was renamed. This bug is really unpredictable, so it may not be gone.
2. Changed [RemesPath `s_sub` function](/docs/RemesPath.md#vectorized-functions) so that it either does regex-replace or simple string-replace, depending on the type of the second parameter.

## [4.10.0] - 2023-02-15

Expand Down
14 changes: 11 additions & 3 deletions JsonToolsNppPlugin/JSONTools/RemesPathFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1377,8 +1377,16 @@ public static JNode StrSlice(List<JNode> args)
}

/// <summary>
/// Replaces all instances of string to_replace with string repl in the string value
/// of JNode node
/// first arg: a string<br></br>
/// second arg: a string or regex to be replaced<br></br>
/// third arg: a string<br></br>
/// Returns:<br></br>
/// * if arg 2 is a string, a new string with all instances of arg 2 in arg 1 replaced with arg 3<br></br>
/// * if arg 2 is a regex, a new string with all matches to that regex in arg 1 replaced with arg 3<br></br>
/// EXAMPLES:<br></br>
/// * StrSub("abbbbc", Regex("b+"), "z") -> "azc"<br></br>
/// * StrSub("123 123", "1", "z") -> "z23 z23"<br></br>
/// <i>NOTE: prior to JsonTools 4.10.1, it didn't matter whether arg 2 was a string or regex; it always treated it as a regex.</i>
/// </summary>
/// <returns>new JNode of type = Dtype.STR with all replacements made</returns>
public static JNode StrSub(List<JNode> args)
Expand All @@ -1389,7 +1397,7 @@ public static JNode StrSub(List<JNode> args)
string val = (string)node.value;
if (to_replace.type == Dtype.STR)
{
return new JNode(Regex.Replace(val, (string)to_replace.value, (string)repl.value), Dtype.STR, 0);
return new JNode(val.Replace((string)to_replace.value, (string)repl.value), Dtype.STR, 0);
}
return new JNode(((JRegex)to_replace).regex.Replace(val, (string)repl.value), Dtype.STR, 0);
}
Expand Down
4 changes: 2 additions & 2 deletions JsonToolsNppPlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("4.10.0.1")]
[assembly: AssemblyFileVersion("4.10.0.1")]
[assembly: AssemblyVersion("4.10.0.2")]
[assembly: AssemblyFileVersion("4.10.0.2")]
4 changes: 3 additions & 1 deletion JsonToolsNppPlugin/Tests/RemesPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ public static void Test()
new Query_DesiredResult("flatten(@.foo)[:4]", "[0, 1, 2, 3.0]"),
new Query_DesiredResult("flatten(@.guzo, 2)", "[1, 2, 3]"),
new Query_DesiredResult("min_by(@.foo, 1)", "[0, 1, 2]"),
new Query_DesiredResult("s_sub(@.bar.b, g`a(\\`?)`, `$1z`)", "[\"`zg\", \"bzh\"]"),
new Query_DesiredResult("s_sub(@.bar.b, g`a(\\`?)`, `$1z`)", "[\"`zg\", \"bzh\"]"), // regex to_replace
new Query_DesiredResult("s_sub(j`[\"12.0\", \"1.5\", \"2\"]`, `.`, ``)", "[\"120\", \"15\", \"2\"]"), // string to_replace with special char
new Query_DesiredResult("s_sub(j`[\"12.0\", \"1.5\", \"2\"]`, g`.`, ``)", "[\"\", \"\", \"\"]"), // regex to_replace with special char
new Query_DesiredResult("isna(@.foo[0])", "[false, false, false]"),
new Query_DesiredResult("s_slice(@.bar.b, 2)", "[\"g\", \"h\"]"),
new Query_DesiredResult("s_slice(@.bar.b, ::2)", "[\"ag\", \"bh\"]"),
Expand Down
15 changes: 15 additions & 0 deletions docs/RemesPath.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,11 @@ Returns true if x is the floating-point Not-A-Number (represented in some JSON b

Recall that `NaN` is *NOT* in the original JSON specification.

---
`isnull(x: anything) -> bool`

Returns true if x is null, else false.

----
`log(x: number, n: number = e) -> number`

Expand Down Expand Up @@ -608,8 +613,18 @@ Strips the whitespace off both ends of x.

Replaces all instances of string/regex `to_replace` in `x` with `replacement`.

If `to_replace` is a string, replaces all instances of `to_replace` with `replacement`. *NOTE: This is a new behavior in [JsonTools 4.10.1](/CHANGELOG.md#4101-unreleased---2023-mm-dd). Prior to that, this function treated `to_replace` as a regex no matter what.*

If `to_replace` is a regex, replaces all matches to the `to_replace` pattern with `replacement`.

See the [C# regular expressions reference on substitutions](https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference#substitutions).

__Examples:__

* ``s_sub(abbbbbc, g`b+`, z)`` returns `azc`.
* ``s_sub(abbbbbc, `b+`, z)`` returns `abbbbbc`, because `b+` is not being matched as a regex. *Prior to version 4.10.1, this would return the same thing as ``s_sub(abbbbbc, g`b+`, z)``.*
* ``s_sub(abbbbbc, b, z)`` returns `azzzzzc`, because every instance of `b` is replaced by `z`.

----
`s_upper(x: string) -> string`

Expand Down

0 comments on commit a28eeeb

Please sign in to comment.