Skip to content

Commit 0652c61

Browse files
author
rstam
committed
Replaced StartsWith, EndsWith and Contains for strings with much faster equivalents. Thanks to [email protected] who suggested these changes in a Google Groups discussion.
1 parent 0d89120 commit 0652c61

File tree

13 files changed

+30
-29
lines changed

13 files changed

+30
-29
lines changed

Bson/IO/BsonWriter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -607,14 +607,14 @@ protected void CheckElementName(string name)
607607
{
608608
if (_checkUpdateDocument)
609609
{
610-
_checkElementNames = !name.StartsWith("$");
610+
_checkElementNames = (name[0] != '$');
611611
_checkUpdateDocument = false;
612612
return;
613613
}
614614

615615
if (_checkElementNames)
616616
{
617-
if (name.StartsWith("$"))
617+
if (name[0] == '$')
618618
{
619619
// a few element names starting with $ have to be allowed for historical reasons
620620
switch (name)
@@ -631,7 +631,7 @@ protected void CheckElementName(string name)
631631
throw new BsonSerializationException(message);
632632
}
633633
}
634-
if (name.Contains('.'))
634+
if (name.IndexOf('.') != -1)
635635
{
636636
var message = string.Format("Element name '{0}' is not valid because it contains a '.'.", name);
637637
throw new BsonSerializationException(message);
@@ -673,10 +673,10 @@ protected void ThrowInvalidState(string methodName, params BsonWriterState[] val
673673
string message;
674674
if (_state == BsonWriterState.Initial || _state == BsonWriterState.ScopeDocument || _state == BsonWriterState.Done)
675675
{
676-
if (!methodName.StartsWith("End") && methodName != "WriteName")
676+
if (!methodName.StartsWith("End", StringComparison.Ordinal) && methodName != "WriteName")
677677
{
678678
var typeName = methodName.Substring(5);
679-
if (typeName.StartsWith("Start"))
679+
if (typeName.StartsWith("Start", StringComparison.Ordinal))
680680
{
681681
typeName = typeName.Substring(5);
682682
}

Bson/IO/JsonReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ private DateTime ParseJavaScriptDateTimeString(string dateTimeString)
10661066
var secondsString = match.Groups["seconds"].Value;
10671067
int seconds;
10681068
double milliseconds;
1069-
if (secondsString.Contains("."))
1069+
if (secondsString.IndexOf('.') != -1)
10701070
{
10711071
var timeSpan = TimeSpan.FromSeconds(double.Parse(secondsString));
10721072
seconds = timeSpan.Seconds;

Bson/ObjectModel/BsonRegularExpression.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,19 +296,19 @@ public override int GetHashCode()
296296
public Regex ToRegex()
297297
{
298298
var options = RegexOptions.None;
299-
if (_options.Contains("i"))
299+
if (_options.IndexOf('i') != -1)
300300
{
301301
options |= RegexOptions.IgnoreCase;
302302
}
303-
if (_options.Contains("m"))
303+
if (_options.IndexOf('m') != -1)
304304
{
305305
options |= RegexOptions.Multiline;
306306
}
307-
if (_options.Contains("x"))
307+
if (_options.IndexOf('x') != -1)
308308
{
309309
options |= RegexOptions.IgnorePatternWhitespace;
310310
}
311-
if (_options.Contains("s"))
311+
if (_options.IndexOf('s') != -1)
312312
{
313313
options |= RegexOptions.Singleline;
314314
}

Bson/Serialization/BsonClassMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public static string GetTypeNameDiscriminator(Type type)
276276
foreach (var genericType in type.GetGenericArguments())
277277
{
278278
var genericTypeName = GetTypeNameDiscriminator(genericType);
279-
if (genericTypeName.Contains(','))
279+
if (genericTypeName.IndexOf(',') != -1)
280280
{
281281
genericTypeName = "[" + genericTypeName + "]";
282282
}

Bson/Serialization/Serializers/DictionaryGenericSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public override void Serialize(
232232
foreach (object key in dictionary.Keys)
233233
{
234234
var name = key as string; // check for null and type string at the same time
235-
if (name == null || name.StartsWith("$") || name.Contains("."))
235+
if (name == null || name[0] == '$' || name.IndexOf('.') != -1)
236236
{
237237
representation = DictionaryRepresentation.ArrayOfArrays;
238238
break;

Bson/Serialization/Serializers/DictionarySerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public override void Serialize(
240240
foreach (object key in dictionary.Keys)
241241
{
242242
var name = key as string; // check for null and type string at the same time
243-
if (name == null || name.StartsWith("$") || name.Contains("."))
243+
if (name == null || name[0] == '$' || name.IndexOf('.') != -1)
244244
{
245245
representation = DictionaryRepresentation.ArrayOfArrays;
246246
break;

BsonUnitTests/Jira/CSharp310Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static void InitializeSerialization()
5656
var conventions = new ConventionProfile();
5757
conventions.SetDefaultValueConvention(new EmptyGuidDefaultValueConvention());
5858
conventions.SetSerializeDefaultValueConvention(new NeverSerializeDefaultValueConvention());
59-
BsonClassMap.RegisterConventions(conventions, type => type.FullName.StartsWith("MongoDB.BsonUnitTests.Jira.CSharp310Tests"));
59+
BsonClassMap.RegisterConventions(conventions, type => type.FullName.StartsWith("MongoDB.BsonUnitTests.Jira.CSharp310Tests", StringComparison.Ordinal));
6060
}
6161

6262
[Test]

Driver/Core/MongoCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ internal static void ValidateCollectionName(string name)
15921592
throw new ArgumentNullException("name");
15931593
}
15941594
if (name == "" ||
1595-
name.Contains('\0') ||
1595+
name.IndexOf('\0') != -1 ||
15961596
Encoding.UTF8.GetBytes(name).Length > 121)
15971597
{
15981598
throw new ArgumentException("Invalid collection name", "name");

Driver/Core/MongoCredentials.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class MongoCredentials : IEquatable<MongoCredentials>
4040
public MongoCredentials(string username, string password)
4141
{
4242
ValidatePassword(password);
43-
if (username.EndsWith("(admin)"))
43+
if (username.EndsWith("(admin)", StringComparison.Ordinal))
4444
{
4545
_username = username.Substring(0, username.Length - 7);
4646
_password = password;

Driver/Core/MongoDatabase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static MongoDatabase Create(MongoUrl url)
143143
/// </returns>
144144
public static MongoDatabase Create(string connectionString)
145145
{
146-
if (connectionString.StartsWith("mongodb://"))
146+
if (connectionString.StartsWith("mongodb://", StringComparison.Ordinal))
147147
{
148148
MongoUrl url = MongoUrl.Create(connectionString);
149149
return Create(url);
@@ -649,8 +649,8 @@ public virtual IEnumerable<string> GetCollectionNames()
649649
foreach (var @namespace in namespaces.FindAll())
650650
{
651651
string collectionName = @namespace["name"].AsString;
652-
if (!collectionName.StartsWith(prefix)) { continue; }
653-
if (collectionName.Contains('$')) { continue; }
652+
if (!collectionName.StartsWith(prefix, StringComparison.Ordinal)) { continue; }
653+
if (collectionName.IndexOf('$') != -1) { continue; }
654654
collectionNames.Add(collectionName.Substring(prefix.Length));
655655
}
656656
collectionNames.Sort();

0 commit comments

Comments
 (0)