From 1abd009ed43d037e1e6c55ee81beaf23ef60bbff Mon Sep 17 00:00:00 2001 From: Greg Lindhorst Date: Thu, 6 Mar 2025 16:25:22 -0800 Subject: [PATCH] Updates --- docs/regular-expressions.md | 2 +- .../Binding/BinderUtils.cs | 22 +++++++++++++++++-- .../ExpressionTestCases/Match_Limited.txt | 13 +++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/docs/regular-expressions.md b/docs/regular-expressions.md index c4b5a6a3ef..9d79b87371 100644 --- a/docs/regular-expressions.md +++ b/docs/regular-expressions.md @@ -28,7 +28,7 @@ As a result, some regular expressions that may work in other environments will b Power Fx supports the following regular expression features, with notes on how Power Fx behavior may differ from other systems. -The regular expression must be a constant and not calculated or stored in a variable. Using the `&` operator and the `Concatenate`, `Char`, and `UniChar` functions with constant arguments is supported. +The regular expression must be a constant and not calculated or stored in a variable. Using the `&` operator, string interpolation `$"{...}"`, and the `Concatenate`, `Char`, and `UniChar` functions with constant arguments is supported. ### Literal characters diff --git a/src/libraries/Microsoft.PowerFx.Core/Binding/BinderUtils.cs b/src/libraries/Microsoft.PowerFx.Core/Binding/BinderUtils.cs index 25d2f0ccbd..7149dbefff 100644 --- a/src/libraries/Microsoft.PowerFx.Core/Binding/BinderUtils.cs +++ b/src/libraries/Microsoft.PowerFx.Core/Binding/BinderUtils.cs @@ -3,7 +3,8 @@ using System.Collections.Generic; using System.Globalization; -using System.Linq; +using System.Linq; +using System.Text; using Microsoft.CodeAnalysis; using Microsoft.PowerFx.Core.App.Controls; using Microsoft.PowerFx.Core.App.ErrorContainers; @@ -1504,7 +1505,24 @@ public static bool TryGetConstantValue(CheckTypesContext context, TexlNode node, Contracts.AssertValue(node); nodeValue = null; switch (node.Kind) - { + { + case NodeKind.StrInterp: + var strInterpNode = node.AsStrInterp(); + StringBuilder strInterpValue = new StringBuilder(); + foreach (var segmentNode in strInterpNode.Children) + { + if (TryGetConstantValue(context, segmentNode, out var segmentValue)) + { + strInterpValue.Append(segmentValue); + } + else + { + break; + } + } + + nodeValue = strInterpValue.ToString(); + return true; case NodeKind.StrLit: nodeValue = node.AsStrLit().Value; return true; diff --git a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/Match_Limited.txt b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/Match_Limited.txt index de59582a9d..fac2574bda 100644 --- a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/Match_Limited.txt +++ b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/Match_Limited.txt @@ -1449,6 +1449,19 @@ Errors: Error 17-31: Regular expression must be a constant value.|Error 0-7: The >> IsMatch( "asdf", "asdf", If( Int(4) > -1, MatchOptions.IgnoreCase, MatchOptions.Contains ) ) Errors: Error 25-90: MatchOptions must be a constant value.|Error 0-7: The function 'IsMatch' has some invalid arguments. +// String interpolation is supported +>> Match( "asdf", $"a{ Char(115) }{ UniChar(100) }f" ) +{FullMatch:"asdf",StartMatch:1} + +>> Match( "asdf", $"a{$"s{$"d{$"f"}"}"}" ) +{FullMatch:"asdf",StartMatch:1} + +>> Match( "asdf", $"a{ Char(115) }" & $"{ UniChar(100) }f" ) +{FullMatch:"asdf",StartMatch:1} + +>> Match( "asdf", "a" & $"{Char(115)}" & $"{UniChar(100)}" & "f" ) +{FullMatch:"asdf",StartMatch:1} + // leading zeros on quant support >> Match( "aaaaaa", "a{00002,}" )