Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gregli-msft committed Mar 7, 2025
1 parent 98e67e0 commit 22e0733
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 387 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public override void Visit(IValueVisitor visitor)
visitor.Visit(this);
}

internal StringValue ToLower()
{
return new StringValue(IRContext.NotInSource(FormulaType.String), Value.ToLowerInvariant());
}

public override void ToExpression(StringBuilder sb, FormulaValueSerializerSettings settings)
{
sb.Append($"\"{CharacterUtils.ExcelEscapeString(Value)}\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license.

using System;
using System.Globalization;
using System.Linq;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Core.Utils;
Expand Down Expand Up @@ -720,9 +719,9 @@ private static BooleanValue NotEqualPolymorphic(IRContext irContext, FormulaValu
}

// See in_SS in JScript membershipReplacementFunctions
public static Func<IServiceProvider, IRContext, FormulaValue[], FormulaValue> StringInOperator(bool exact)
public static Func<IRContext, FormulaValue[], FormulaValue> StringInOperator(bool exact)
{
return (services, irContext, args) =>
return (irContext, args) =>
{
var left = args[0];
var right = args[1];
Expand All @@ -738,25 +737,23 @@ public static Func<IServiceProvider, IRContext, FormulaValue[], FormulaValue> St

var leftStr = (StringValue)left;
var rightStr = (StringValue)right;

return new BooleanValue(irContext, services.GetService<CultureInfo>().CompareInfo.IndexOf(rightStr.Value, leftStr.Value, exact ? CompareOptions.Ordinal : CompareOptions.IgnoreCase) >= 0);

return new BooleanValue(irContext, rightStr.Value.IndexOf(leftStr.Value, exact ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase) >= 0);
};
}

// Left is a scalar. Right is a single-column table.
// See in_ST()
public static Func<IServiceProvider, IRContext, FormulaValue[], FormulaValue> InScalarTableOperator(bool exact)
public static Func<IRContext, FormulaValue[], FormulaValue> InScalarTableOperator(bool exact)
{
return (services, irContext, args) =>
return (irContext, args) =>
{
var left = args[0];
var right = args[1];

var cultureInfo = services.GetService<CultureInfo>();

var right = args[1];

if (!exact && left is StringValue strLhs)
{
left = new StringValue(IRContext.NotInSource(FormulaType.String), cultureInfo.TextInfo.ToLower(strLhs.Value));
left = strLhs.ToLower();
}

var source = (TableValue)right;
Expand All @@ -769,7 +766,7 @@ public static Func<IServiceProvider, IRContext, FormulaValue[], FormulaValue> In

if (!exact && rhs is StringValue strRhs)
{
rhs = new StringValue(IRContext.NotInSource(FormulaType.String), cultureInfo.TextInfo.ToLower(strRhs.Value));
rhs = strRhs.ToLower();
}

if (RuntimeHelpers.AreEqual(left, rhs))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,31 +830,31 @@ public static async ValueTask<FormulaValue> SortTable(EvalVisitor runner, EvalVi

if (allNumbers)
{
return SortValueType<NumberValue, double>(pairs, runner, irContext, compareToResultModifier);
return SortValueType<NumberValue, double>(pairs, irContext, compareToResultModifier);
}
else if (allDecimals)
{
return SortValueType<DecimalValue, decimal>(pairs, runner, irContext, compareToResultModifier);
return SortValueType<DecimalValue, decimal>(pairs, irContext, compareToResultModifier);
}
else if (allStrings)
{
return SortValueType<StringValue, string>(pairs, runner, irContext, compareToResultModifier);
return SortValueType<StringValue, string>(pairs, irContext, compareToResultModifier);
}
else if (allBooleans)
{
return SortValueType<BooleanValue, bool>(pairs, runner, irContext, compareToResultModifier);
return SortValueType<BooleanValue, bool>(pairs, irContext, compareToResultModifier);
}
else if (allDatetimes)
{
return SortValueType<DateTimeValue, DateTime>(pairs, runner, irContext, compareToResultModifier);
return SortValueType<DateTimeValue, DateTime>(pairs, irContext, compareToResultModifier);
}
else if (allDates)
{
return SortValueType<DateValue, DateTime>(pairs, runner, irContext, compareToResultModifier);
return SortValueType<DateValue, DateTime>(pairs, irContext, compareToResultModifier);
}
else if (allTimes)
{
return SortValueType<TimeValue, TimeSpan>(pairs, runner, irContext, compareToResultModifier);
return SortValueType<TimeValue, TimeSpan>(pairs, irContext, compareToResultModifier);
}
else if (allOptionSets)
{
Expand Down Expand Up @@ -1281,7 +1281,7 @@ private static FormulaValue DistinctValueType(List<(DValue<RecordValue> row, For
return new InMemoryTableValue(irContext, result);
}

private static FormulaValue SortValueType<TPFxPrimitive, TDotNetPrimitive>(List<(DValue<RecordValue> row, FormulaValue sortValue)> pairs, EvalVisitor runner, IRContext irContext, int compareToResultModifier)
private static FormulaValue SortValueType<TPFxPrimitive, TDotNetPrimitive>(List<(DValue<RecordValue> row, FormulaValue sortValue)> pairs, IRContext irContext, int compareToResultModifier)
where TPFxPrimitive : PrimitiveValue<TDotNetPrimitive>
where TDotNetPrimitive : IComparable<TDotNetPrimitive>
{
Expand All @@ -1297,16 +1297,8 @@ private static FormulaValue SortValueType<TPFxPrimitive, TDotNetPrimitive>(List<
}

var n1 = a.sortValue as TPFxPrimitive;
var n2 = b.sortValue as TPFxPrimitive;
CultureInfo culture;
if (n1.Value is string n1s && n2.Value is string n2s && (culture = runner.GetService<CultureInfo>()) != null)
{
return culture.CompareInfo.Compare(n1s, n2s) * compareToResultModifier;
}
else
{
return n1.Value.CompareTo(n2.Value) * compareToResultModifier;
}
var n2 = b.sortValue as TPFxPrimitive;
return n1.Value.CompareTo(n2.Value) * compareToResultModifier;
});

return new InMemoryTableValue(irContext, pairs.Select(pair => pair.row));
Expand Down

This file was deleted.

Loading

0 comments on commit 22e0733

Please sign in to comment.