Skip to content

Commit e06c04e

Browse files
authored
Merge pull request #40 from nblumhardt/replace-function
Add support for `Replace()` with string arguments (no regex support)
2 parents e84c89b + 6baac19 commit e06c04e

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/Seq.Syntax/Expressions/Operators.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static class Operators
4242
public const string OpLastIndexOf = "LastIndexOf";
4343
public const string OpLength = "Length";
4444
public const string OpNow = "Now";
45+
public const string OpReplace = "Replace";
4546
public const string OpRound = "Round";
4647
public const string OpStartsWith = "StartsWith";
4748
public const string OpSubstring = "Substring";

src/Seq.Syntax/Expressions/Runtime/RuntimeOperators.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,16 @@ public static LogEventPropertyValue _Internal_IsNotNull(LogEventPropertyValue? v
464464

465465
return new ScalarValue(str.Substring((int)si, (int)len));
466466
}
467+
468+
public static LogEventPropertyValue? Replace(StringComparison sc, LogEventPropertyValue? @string, LogEventPropertyValue? substring, LogEventPropertyValue? replacement)
469+
{
470+
if (!Coerce.String(@string, out var str) ||
471+
!Coerce.String(substring, out var sub) ||
472+
!Coerce.String(replacement, out var rep))
473+
return null;
474+
475+
return new ScalarValue(str.Replace(sub, rep, sc));
476+
}
467477

468478
public static LogEventPropertyValue? Concat(LogEventPropertyValue? string0, LogEventPropertyValue? string1)
469479
{

test/Seq.Syntax.Tests/Cases/expression-evaluation-cases.asv

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,10 @@ uriencode(' ') ⇶ '%20'
302302

303303
tostring(@l, 'u3') ⇶ 'INF'
304304
tostring(@Elapsed) ⇶ '00:10:00'
305+
306+
// Replace
307+
Replace('xYX', 'x', 'z') ⇶ 'zYX'
308+
Replace('xYX', 'x', 'z') ci ⇶ 'zYz'
309+
Replace('mess', 'ss', 'an') ⇶ 'mean'
310+
Replace('mess', 's', 'an') ⇶ 'meanan'
311+
Replace('xyz', 'x', '$0') ⇶ '$0yz'

0 commit comments

Comments
 (0)