Skip to content

Commit dd26747

Browse files
committed
CSHARP-3975: Move the string extension methods in MongoDBLinqExtensions to StringExtensions.
1 parent bed0524 commit dd26747

File tree

7 files changed

+112
-107
lines changed

7 files changed

+112
-107
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/MongoDBLinqExtensions.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/MongoDBLinqExtensionsMethod.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/StringMethod.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ internal static class StringMethod
3131
private static readonly MethodInfo __indexOfAny;
3232
private static readonly MethodInfo __indexOfAnyWithStartIndex;
3333
private static readonly MethodInfo __indexOfAnyWithStartIndexAndCount;
34+
private static readonly MethodInfo __indexOfBytesWithValue;
35+
private static readonly MethodInfo __indexOfBytesWithValueAndStartIndex;
36+
private static readonly MethodInfo __indexOfBytesWithValueAndStartIndexAndCount;
3437
private static readonly MethodInfo __indexOfWithChar;
3538
private static readonly MethodInfo __indexOfWithCharAndStartIndex;
3639
private static readonly MethodInfo __indexOfWithCharAndStartIndexAndCount;
@@ -50,6 +53,8 @@ internal static class StringMethod
5053
private static readonly MethodInfo __startsWith;
5154
private static readonly MethodInfo __startsWithWithComparisonType;
5255
private static readonly MethodInfo __startsWithWithIgnoreCaseAndCulture;
56+
private static readonly MethodInfo __strLenBytes;
57+
private static readonly MethodInfo __substrBytes;
5358
private static readonly MethodInfo __substring;
5459
private static readonly MethodInfo __substringWithLength;
5560
private static readonly MethodInfo __toLower;
@@ -74,6 +79,9 @@ static StringMethod()
7479
__indexOfAny = ReflectionInfo.Method((string s, char[] anyOf) => s.IndexOfAny(anyOf));
7580
__indexOfAnyWithStartIndex = ReflectionInfo.Method((string s, char[] anyOf, int startIndex) => s.IndexOfAny(anyOf, startIndex));
7681
__indexOfAnyWithStartIndexAndCount = ReflectionInfo.Method((string s, char[] anyOf, int startIndex, int count) => s.IndexOfAny(anyOf, startIndex, count));
82+
__indexOfBytesWithValue = ReflectionInfo.Method((string s, string value) => s.IndexOfBytes(value));
83+
__indexOfBytesWithValueAndStartIndex = ReflectionInfo.Method((string s, string value, int startIndex) => s.IndexOfBytes(value, startIndex));
84+
__indexOfBytesWithValueAndStartIndexAndCount = ReflectionInfo.Method((string s, string value, int startIndex, int count) => s.IndexOfBytes(value, startIndex, count));
7785
__indexOfWithChar = ReflectionInfo.Method((string s, char value) => s.IndexOf(value));
7886
__indexOfWithCharAndStartIndex = ReflectionInfo.Method((string s, char value, int startIndex) => s.IndexOf(value, startIndex));
7987
__indexOfWithCharAndStartIndexAndCount = ReflectionInfo.Method((string s, char value, int startIndex, int count) => s.IndexOf(value, startIndex, count));
@@ -93,6 +101,8 @@ static StringMethod()
93101
__startsWith = ReflectionInfo.Method((string s, string value) => s.StartsWith(value));
94102
__startsWithWithComparisonType = ReflectionInfo.Method((string s, string value, StringComparison comparisonType) => s.StartsWith(value, comparisonType));
95103
__startsWithWithIgnoreCaseAndCulture = ReflectionInfo.Method((string s, string value, bool ignoreCase, CultureInfo culture) => s.StartsWith(value, ignoreCase, culture));
104+
__strLenBytes = ReflectionInfo.Method((string s) => s.StrLenBytes());
105+
__substrBytes = ReflectionInfo.Method((string s, int startIndex, int length) => s.SubstrBytes(startIndex, length));
96106
__substring = ReflectionInfo.Method((string s, int startIndex) => s.Substring(startIndex));
97107
__substringWithLength = ReflectionInfo.Method((string s, int startIndex, int length) => s.Substring(startIndex, length));
98108
__toLower = ReflectionInfo.Method((string s) => s.ToLower());
@@ -116,6 +126,9 @@ static StringMethod()
116126
public static MethodInfo IndexOfAny => __indexOfAny;
117127
public static MethodInfo IndexOfAnyWithStartIndex => __indexOfAnyWithStartIndex;
118128
public static MethodInfo IndexOfAnyWithStartIndexAndCount => __indexOfAnyWithStartIndexAndCount;
129+
public static MethodInfo IndexOfBytesWithValue => __indexOfBytesWithValue;
130+
public static MethodInfo IndexOfBytesWithValueAndStartIndex => __indexOfBytesWithValueAndStartIndex;
131+
public static MethodInfo IndexOfBytesWithValueAndStartIndexAndCount => __indexOfBytesWithValueAndStartIndexAndCount;
119132
public static MethodInfo IndexOfWithChar => __indexOfWithChar;
120133
public static MethodInfo IndexOfWithCharAndStartIndex => __indexOfWithCharAndStartIndex;
121134
public static MethodInfo IndexOfWithCharAndStartIndexAndCount => __indexOfWithCharAndStartIndexAndCount;
@@ -134,6 +147,8 @@ static StringMethod()
134147
public static MethodInfo SplitWithStringsAndOptions => __splitWithStringsAndOptions;
135148
public static MethodInfo StartsWith => __startsWith;
136149
public static MethodInfo StartsWithWithComparisonType => __startsWithWithComparisonType;
150+
public static MethodInfo StrLenBytes => __strLenBytes;
151+
public static MethodInfo SubstrBytes => __substrBytes;
137152
public static MethodInfo StartsWithWithIgnoreCaseAndCulture => __startsWithWithIgnoreCaseAndCulture;
138153
public static MethodInfo Substring => __substring;
139154
public static MethodInfo SubstringWithLength => __substringWithLength;

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/IndexOfMethodToAggregationExpressionTranslator.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,37 @@ internal static class IndexOfMethodToAggregationExpressionTranslator
3030
private static readonly MethodInfo[] __indexOfMethods =
3131
{
3232
StringMethod.IndexOfWithChar,
33+
StringMethod.IndexOfBytesWithValue,
34+
StringMethod.IndexOfBytesWithValueAndStartIndex,
35+
StringMethod.IndexOfBytesWithValueAndStartIndexAndCount,
3336
StringMethod.IndexOfWithCharAndStartIndex,
3437
StringMethod.IndexOfWithCharAndStartIndexAndCount,
3538
StringMethod.IndexOfWithString,
3639
StringMethod.IndexOfWithStringAndStartIndex,
3740
StringMethod.IndexOfWithStringAndStartIndexAndCount,
3841
StringMethod.IndexOfWithStringAndComparisonType,
3942
StringMethod.IndexOfWithStringAndStartIndexAndComparisonType,
40-
StringMethod.IndexOfWithStringAndStartIndexAndCountAndComparisonType,
41-
MongoDBLinqExtensionsMethod.IndexOfBytesWithValue,
42-
MongoDBLinqExtensionsMethod.IndexOfBytesWithValueAndStartIndex,
43-
MongoDBLinqExtensionsMethod.IndexOfBytesWithValueAndStartIndexAndCount
43+
StringMethod.IndexOfWithStringAndStartIndexAndCountAndComparisonType
4444
};
4545

4646
private static readonly MethodInfo[] __indexOfWithStartIndexMethods =
4747
{
48+
StringMethod.IndexOfBytesWithValueAndStartIndex,
49+
StringMethod.IndexOfBytesWithValueAndStartIndexAndCount,
4850
StringMethod.IndexOfWithCharAndStartIndex,
4951
StringMethod.IndexOfWithCharAndStartIndexAndCount,
5052
StringMethod.IndexOfWithStringAndStartIndex,
5153
StringMethod.IndexOfWithStringAndStartIndexAndCount,
5254
StringMethod.IndexOfWithStringAndStartIndexAndComparisonType,
53-
StringMethod.IndexOfWithStringAndStartIndexAndCountAndComparisonType,
54-
MongoDBLinqExtensionsMethod.IndexOfBytesWithValueAndStartIndex,
55-
MongoDBLinqExtensionsMethod.IndexOfBytesWithValueAndStartIndexAndCount
55+
StringMethod.IndexOfWithStringAndStartIndexAndCountAndComparisonType
5656
};
5757

5858
private static readonly MethodInfo[] __indexOfWithCountMethods =
5959
{
60+
StringMethod.IndexOfBytesWithValueAndStartIndexAndCount,
6061
StringMethod.IndexOfWithCharAndStartIndexAndCount,
6162
StringMethod.IndexOfWithStringAndStartIndexAndCount,
62-
StringMethod.IndexOfWithStringAndStartIndexAndCountAndComparisonType,
63-
MongoDBLinqExtensionsMethod.IndexOfBytesWithValueAndStartIndexAndCount
63+
StringMethod.IndexOfWithStringAndStartIndexAndCountAndComparisonType
6464
};
6565

6666
private static readonly MethodInfo[] __indexOfWithStringComparisonMethods =
@@ -72,9 +72,9 @@ internal static class IndexOfMethodToAggregationExpressionTranslator
7272

7373
private static readonly MethodInfo[] __indexOfBytesMethods =
7474
{
75-
MongoDBLinqExtensionsMethod.IndexOfBytesWithValue,
76-
MongoDBLinqExtensionsMethod.IndexOfBytesWithValueAndStartIndex,
77-
MongoDBLinqExtensionsMethod.IndexOfBytesWithValueAndStartIndexAndCount
75+
StringMethod.IndexOfBytesWithValue,
76+
StringMethod.IndexOfBytesWithValueAndStartIndex,
77+
StringMethod.IndexOfBytesWithValueAndStartIndexAndCount
7878
};
7979

8080
public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression)

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/StrLenBytesMethodToAggregationExpressionTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static AggregationExpression Translate(TranslationContext context, Method
2828
var method = expression.Method;
2929
var arguments = expression.Arguments;
3030

31-
if (method.Is(MongoDBLinqExtensionsMethod.StrLenBytes))
31+
if (method.Is(StringMethod.StrLenBytes))
3232
{
3333
var stringExpression = arguments[0];
3434
var stringTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, stringExpression);

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/SubstringMethodToAggregationExpressionTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static AggregationExpression Translate(TranslationContext context, Method
3737
return TranslateHelper(context, expression, stringExpression, startIndexExpression, lengthExpression, AstTernaryOperator.SubstrCP);
3838
}
3939

40-
if (method.Is(MongoDBLinqExtensionsMethod.SubstrBytes))
40+
if (method.Is(StringMethod.SubstrBytes))
4141
{
4242
var stringExpression = arguments[0];
4343
var startIndexExpression = arguments[1];
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
18+
namespace MongoDB.Driver.Linq
19+
{
20+
/// <summary>
21+
/// This static class holds methods that can be used to express MongoDB specific operations in LINQ queries.
22+
/// </summary>
23+
public static class StringExtensions
24+
{
25+
/// <summary>
26+
/// Searches a string for an occurrence of a substring and returns the UTF-8 byte index (zero-based) of the first occurrence.
27+
/// </summary>
28+
/// <param name="s">The string.</param>
29+
/// <param name="value">The value.</param>
30+
/// <returns>The byte index of the first occurrence, or -1 if not found.</returns>
31+
public static int IndexOfBytes(this string s, string value)
32+
{
33+
throw new InvalidOperationException("This String.IndexOfBytes method is only intended to be used in LINQ queries.");
34+
}
35+
36+
/// <summary>
37+
/// Searches a string for an occurrence of a substring and returns the UTF-8 byte index (zero-based) of the first occurrence.
38+
/// </summary>
39+
/// <param name="s">The string.</param>
40+
/// <param name="value">The value.</param>
41+
/// <param name="startIndex">The start index.</param>
42+
/// <returns>The byte index of the first occurrence, or -1 if not found.</returns>
43+
public static int IndexOfBytes(this string s, string value, int startIndex)
44+
{
45+
throw new InvalidOperationException("This String.IndexOfBytes method is only intended to be used in LINQ queries.");
46+
}
47+
48+
/// <summary>
49+
/// Searches a string for an occurrence of a substring and returns the UTF-8 byte index (zero-based) of the first occurrence.
50+
/// </summary>
51+
/// <param name="s">The string.</param>
52+
/// <param name="value">The value.</param>
53+
/// <param name="startIndex">The start index.</param>
54+
/// <param name="count">The count.</param>
55+
/// <returns>The byte index of the first occurrence, or -1 if not found.</returns>
56+
public static int IndexOfBytes(this string s, string value, int startIndex, int count)
57+
{
58+
throw new InvalidOperationException("This String.IndexOfBytes method is only intended to be used in LINQ queries.");
59+
}
60+
61+
/// <summary>
62+
/// Returns the number of UTF-8 encoded bytes in the specified string.
63+
/// </summary>
64+
/// <param name="s">The string.</param>
65+
/// <returns>The number of UTF-8 bytes.</returns>
66+
public static int StrLenBytes(this string s)
67+
{
68+
throw new InvalidOperationException("This String.StrLenBytes method is only intended to be used in LINQ queries.");
69+
}
70+
71+
/// <summary>
72+
/// Returns the number of UTF-8 encoded bytes in the specified string.
73+
/// </summary>
74+
/// <param name="s">The string.</param>
75+
/// <param name="startIndex">The start index.</param>
76+
/// <param name="length">The length.</param>
77+
/// <returns>The number of UTF-8 bytes.</returns>
78+
public static string SubstrBytes(this string s, int startIndex, int length)
79+
{
80+
throw new InvalidOperationException("This String.SubstrBytes method is only intended to be used in LINQ queries.");
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)