Skip to content

Commit

Permalink
Replaced ToUpper with ToUpperInvariant for internal funcions and meth…
Browse files Browse the repository at this point in the history
…ods (fix for issue #1995)

Removed usage of Compare(char, char) method on SqlLike string extension and replaced it for Compare(string, string)
  • Loading branch information
kcsombrio committed Dec 30, 2021
1 parent d12ee99 commit a930df1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
8 changes: 4 additions & 4 deletions LiteDB/Document/Expression/BsonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,14 @@ internal static void SetParameters(BsonExpression expr, BsonDocument parameters)
/// </summary>
private static Dictionary<string, MethodInfo> _methods =
typeof(BsonExpressionMethods).GetMethods(BindingFlags.Public | BindingFlags.Static)
.ToDictionary(m => m.Name.ToUpper() + "~" + m.GetParameters().Where(p => p.ParameterType != typeof(Collation)).Count());
.ToDictionary(m => m.Name.ToUpperInvariant() + "~" + m.GetParameters().Where(p => p.ParameterType != typeof(Collation)).Count());

/// <summary>
/// Get expression method with same name and same parameter - return null if not found
/// </summary>
internal static MethodInfo GetMethod(string name, int parameterCount)
{
var key = name.ToUpper() + "~" + parameterCount;
var key = name.ToUpperInvariant() + "~" + parameterCount;

return _methods.GetOrDefault(key);
}
Expand All @@ -440,15 +440,15 @@ internal static MethodInfo GetMethod(string name, int parameterCount)
/// </summary>
private static Dictionary<string, MethodInfo> _functions =
typeof(BsonExpressionFunctions).GetMethods(BindingFlags.Public | BindingFlags.Static)
.ToDictionary(m => m.Name.ToUpper() + "~" + m.GetParameters()
.ToDictionary(m => m.Name.ToUpperInvariant() + "~" + m.GetParameters()
.Skip(5).Count());

/// <summary>
/// Get expression function with same name and same parameter - return null if not found
/// </summary>
internal static MethodInfo GetFunction(string name, int parameterCount = 0)
{
var key = name.ToUpper() + "~" + parameterCount;
var key = name.ToUpperInvariant() + "~" + parameterCount;

return _functions.GetOrDefault(key);
}
Expand Down
12 changes: 6 additions & 6 deletions LiteDB/Document/Expression/Parser/BsonExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static BsonExpression ParseFullExpression(Tokenizer tokenizer, Expression
}

values.Add(expr);
ops.Add(op.ToUpper());
ops.Add(op.ToUpperInvariant());
}

var order = 0;
Expand Down Expand Up @@ -888,7 +888,7 @@ private static BsonExpression TryParseMethodCall(Tokenizer tokenizer, Expression
var useSource = false;
var fields = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

src.Append(token.Value.ToUpper() + "(");
src.Append(token.Value.ToUpperInvariant() + "(");

// method call with no parameters
if (tokenizer.LookAhead().Type == TokenType.CloseParenthesis)
Expand Down Expand Up @@ -926,7 +926,7 @@ private static BsonExpression TryParseMethodCall(Tokenizer tokenizer, Expression

var method = BsonExpression.GetMethod(token.Value, pars.Count);

if (method == null) throw LiteException.UnexpectedToken($"Method '{token.Value.ToUpper()}' does not exist or contains invalid parameters", token);
if (method == null) throw LiteException.UnexpectedToken($"Method '{token.Value.ToUpperInvariant()}' does not exist or contains invalid parameters", token);

// test if method are decorated with "Variable" (immutable = false)
if (method.GetCustomAttribute<VolatileAttribute>() != null)
Expand Down Expand Up @@ -1170,7 +1170,7 @@ private static BsonExpression TryParseFunction(Tokenizer tokenizer, ExpressionCo
if (tokenizer.Current.Type != TokenType.Word) return null;
if (tokenizer.LookAhead().Type != TokenType.OpenParenthesis) return null;

var token = tokenizer.Current.Value.ToUpper();
var token = tokenizer.Current.Value.ToUpperInvariant();

switch (token)
{
Expand Down Expand Up @@ -1385,7 +1385,7 @@ private static string ReadOperant(Tokenizer tokenizer)

if (token.Is("ALL") || token.Is("ANY"))
{
var key = token.Value.ToUpper();
var key = token.Value.ToUpperInvariant();

tokenizer.ReadToken(); // consume operant

Expand Down Expand Up @@ -1474,7 +1474,7 @@ internal static BsonExpression CreateLogicExpression(BsonExpressionType type, Bs
Expression = Expression.New(ctor, expr),
Left = left,
Right = right,
Source = left.Source + " " + (type.ToString().ToUpper()) + " " + right.Source
Source = left.Source + " " + (type.ToString().ToUpperInvariant()) + " " + right.Source
};

return result;
Expand Down
9 changes: 0 additions & 9 deletions LiteDB/Utils/Collation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ public int Compare(string left, string right)
return result < 0 ? -1 : result > 0 ? +1 : 0;
}

/// <summary>
/// Compare 2 chars values using current culture/compare options
/// </summary>
public int Compare(char left, char right)
{
//TODO implementar o compare corretamente
return char.ToUpper(left) == char.ToUpper(right) ? 0 : 1;
}

public int Compare(BsonValue left, BsonValue rigth)
{
return left.CompareTo(rigth, this);
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Utils/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static bool SqlLike(this string str, string pattern, Collation collation)

if (isWildCardOn)
{
if (char.ToUpper(c) == char.ToUpper(p))
if (collation.Compare(c.ToString(), p.ToString()) == 0)
{
isWildCardOn = false;
patternIndex++;
Expand Down Expand Up @@ -140,7 +140,7 @@ public static bool SqlLike(this string str, string pattern, Collation collation)
}
else
{
if (collation.Compare(c, p) == 0)
if (collation.Compare(c.ToString(), p.ToString()) == 0)
{
patternIndex++;
}
Expand Down

0 comments on commit a930df1

Please sign in to comment.