Skip to content

Commit f8b1bc4

Browse files
bahusoidfredericDelaporte
authored andcommitted
Avoid Trim().Length as empty check and ToLowerInvariant() in string comparison (nhibernate#1567)
* Trim().Length replaced with IsNullOrWhiteSpace * StartsWith and Equals ordinal where applicable * Get rid of ToLowerInvariant calls where applicable * Hbm2DDLKeyWords ignore case comparision
1 parent 3a9b700 commit f8b1bc4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+195
-150
lines changed

src/NHibernate.DomainModel/NHSpecific/NullableInt32.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public int CompareTo(object obj)
214214

215215
public static NullableInt32 Parse(string s)
216216
{
217-
if ((s == null) || (s.Trim().Length == 0))
217+
if (string.IsNullOrWhiteSpace(s))
218218
{
219219
return new NullableInt32();
220220
}
@@ -235,4 +235,4 @@ public static NullableInt32 Parse(string s)
235235

236236
#endregion
237237
}
238-
}
238+
}

src/NHibernate/Async/Tool/hbm2ddl/SchemaExport.cs

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public partial class SchemaExport
3737
dialect = Dialect.Dialect.GetDialect(configProperties);
3838

3939
string autoKeyWordsImport = PropertiesHelper.GetString(Environment.Hbm2ddlKeyWords, configProperties, "not-defined");
40-
autoKeyWordsImport = autoKeyWordsImport.ToLowerInvariant();
4140
if (autoKeyWordsImport == Hbm2DDLKeyWords.AutoQuote)
4241
{
4342
await (SchemaMetadataUpdater.UpdateAsync(cfg, dialect, cancellationToken)).ConfigureAwait(false);

src/NHibernate/Async/Tool/hbm2ddl/SchemaUpdate.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public partial class SchemaUpdate
3232
}
3333

3434
string autoKeyWordsImport = PropertiesHelper.GetString(Environment.Hbm2ddlKeyWords, configuration.Properties, "not-defined");
35-
autoKeyWordsImport = autoKeyWordsImport.ToLowerInvariant();
3635
if (autoKeyWordsImport == Hbm2DDLKeyWords.AutoQuote)
3736
{
3837
await (SchemaMetadataUpdater.UpdateAsync(configuration, dialect, cancellationToken)).ConfigureAwait(false);
@@ -56,26 +55,26 @@ public partial class SchemaUpdate
5655

5756
for (int i = 0; i < args.Length; i++)
5857
{
59-
if (args[i].StartsWith("--"))
58+
if (args[i].StartsWith("--", StringComparison.Ordinal))
6059
{
6160
if (args[i].Equals("--quiet"))
6261
{
6362
script = false;
6463
}
65-
else if (args[i].StartsWith("--properties="))
64+
else if (args[i].StartsWith("--properties=", StringComparison.Ordinal))
6665
{
6766
throw new NotSupportedException("No properties file for .NET, use app.config instead");
6867
//propFile = args[i].Substring( 13 );
6968
}
70-
else if (args[i].StartsWith("--config="))
69+
else if (args[i].StartsWith("--config=", StringComparison.Ordinal))
7170
{
7271
cfg.Configure(args[i].Substring(9));
7372
}
74-
else if (args[i].StartsWith("--text"))
73+
else if (args[i].StartsWith("--text", StringComparison.Ordinal))
7574
{
7675
doUpdate = false;
7776
}
78-
else if (args[i].StartsWith("--naming="))
77+
else if (args[i].StartsWith("--naming=", StringComparison.Ordinal))
7978
{
8079
cfg.SetNamingStrategy(
8180
(INamingStrategy)

src/NHibernate/Async/Tool/hbm2ddl/SchemaValidator.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ public partial class SchemaValidator
3333

3434
for (int i = 0; i < args.Length; i++)
3535
{
36-
if (args[i].StartsWith("--"))
36+
if (args[i].StartsWith("--", StringComparison.Ordinal))
3737
{
3838
//if (args[i].StartsWith("--properties="))
3939
//{
4040
// propFile = args[i].Substring(13);
4141
//}
4242
//else
43-
if (args[i].StartsWith("--config="))
43+
if (args[i].StartsWith("--config=", StringComparison.Ordinal))
4444
{
4545
cfg.Configure(args[i].Substring(9));
4646
}
47-
else if (args[i].StartsWith("--naming="))
47+
else if (args[i].StartsWith("--naming=", StringComparison.Ordinal))
4848
{
4949
cfg.SetNamingStrategy(
5050
(INamingStrategy)
@@ -113,4 +113,4 @@ public partial class SchemaValidator
113113
}
114114
}
115115
}
116-
}
116+
}

src/NHibernate/Cfg/ConfigurationSchema/ClassCacheConfiguration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private void Parse(XPathNavigator classCacheElement)
8989
switch (classCacheElement.Name)
9090
{
9191
case "class":
92-
if (classCacheElement.Value.Trim().Length == 0)
92+
if (string.IsNullOrWhiteSpace(classCacheElement.Value))
9393
throw new HibernateConfigException("Invalid class-cache element; the attribute <class> must be assigned with no empty value");
9494
clazz = classCacheElement.Value;
9595
break;

src/NHibernate/Cfg/ConfigurationSchema/CollectionCacheConfiguration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private void Parse(XPathNavigator collectionCacheElement)
4949
switch (collectionCacheElement.Name)
5050
{
5151
case "collection":
52-
if (collectionCacheElement.Value.Trim().Length == 0)
52+
if (string.IsNullOrWhiteSpace(collectionCacheElement.Value))
5353
throw new HibernateConfigException("Invalid collection-cache element; the attribute <collection> must be assigned with no empty value");
5454
collection = collectionCacheElement.Value;
5555
break;

src/NHibernate/Cfg/ConfigurationSchema/ListenerConfiguration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void Parse(XPathNavigator listenerElement)
5353
switch (listenerElement.Name)
5454
{
5555
case "class":
56-
if (listenerElement.Value.Trim().Length == 0)
56+
if (string.IsNullOrWhiteSpace(listenerElement.Value))
5757
throw new HibernateConfigException("Invalid listener element; the attribute <class> must be assigned with no empty value");
5858
clazz = listenerElement.Value;
5959
break;

src/NHibernate/Cfg/Hbm2ddlKeyWords.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
13
namespace NHibernate.Cfg
24
{
35
public class Hbm2DDLKeyWords
@@ -33,7 +35,7 @@ public override bool Equals(object obj)
3335

3436
public bool Equals(string other)
3537
{
36-
return value.Equals(other);
38+
return value.Equals(other, StringComparison.OrdinalIgnoreCase);
3739
}
3840

3941
public bool Equals(Hbm2DDLKeyWords other)
@@ -82,4 +84,4 @@ public override int GetHashCode()
8284
public static Hbm2DDLKeyWords Keywords = new Hbm2DDLKeyWords("keywords");
8385
public static Hbm2DDLKeyWords AutoQuote = new Hbm2DDLKeyWords("auto-quote");
8486
}
85-
}
87+
}

src/NHibernate/Cfg/Mappings.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Mappings
1919
[Serializable]
2020
public class ColumnNames
2121
{
22-
public readonly IDictionary<string, string> logicalToPhysical = new Dictionary<string, string>();
22+
public readonly IDictionary<string, string> logicalToPhysical = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
2323
public readonly IDictionary<string, string> physicalToLogical = new Dictionary<string, string>();
2424
}
2525

@@ -536,8 +536,8 @@ public void AddColumnBinding(string logicalName, Column finalColumn, Table table
536536
}
537537

538538
string oldFinalName;
539-
binding.logicalToPhysical.TryGetValue(logicalName.ToLowerInvariant(), out oldFinalName);
540-
binding.logicalToPhysical[logicalName.ToLowerInvariant()] = finalColumn.GetQuotedName();
539+
binding.logicalToPhysical.TryGetValue(logicalName, out oldFinalName);
540+
binding.logicalToPhysical[logicalName] = finalColumn.GetQuotedName();
541541
if (oldFinalName != null &&
542542
!(finalColumn.IsQuoted
543543
? oldFinalName.Equals(finalColumn.GetQuotedName())
@@ -584,7 +584,6 @@ public string GetLogicalColumnName(string physicalName, Table table)
584584

585585
public string GetPhysicalColumnName(string logicalName, Table table)
586586
{
587-
logicalName = logicalName.ToLowerInvariant();
588587
string finalName = null;
589588
Table currentTable = table;
590589
do

src/NHibernate/Cfg/SettingsFactory.cs

-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ public Settings BuildSettings(IDictionary<string, string> properties)
167167
}
168168

169169
string autoKeyWordsImport = PropertiesHelper.GetString(Environment.Hbm2ddlKeyWords, properties, "not-defined");
170-
autoKeyWordsImport = autoKeyWordsImport.ToLowerInvariant();
171170
if (autoKeyWordsImport == Hbm2DDLKeyWords.None)
172171
{
173172
settings.IsKeywordsImportEnabled = false;

src/NHibernate/Dialect/Function/ClassicAggregateFunction.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ bool IFunctionGrammar.IsSeparator(string token)
106106

107107
bool IFunctionGrammar.IsKnownArgument(string token)
108108
{
109-
return "distinct".Equals(token.ToLowerInvariant()) ||
110-
"all".Equals(token.ToLowerInvariant());
109+
return "distinct".Equals(token, StringComparison.OrdinalIgnoreCase) ||
110+
"all".Equals(token, StringComparison.OrdinalIgnoreCase);
111111
}
112112

113113
#endregion

src/NHibernate/Dialect/Function/SQLFunctionRegistry.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ public SQLFunctionRegistry(Dialect dialect, IDictionary<string, ISQLFunction> us
1212
{
1313
this.dialect = dialect;
1414
this.userFunctions = new Dictionary<string, ISQLFunction>(userFunctions,
15-
StringComparer.InvariantCultureIgnoreCase);
15+
StringComparer.OrdinalIgnoreCase);
1616
}
1717

18+
/// <summary>
19+
/// Find function by function name ignoring case
20+
/// </summary>
1821
public ISQLFunction FindSQLFunction(string functionName)
1922
{
2023
ISQLFunction result;

src/NHibernate/Dialect/MsSqlCeDialect.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -287,25 +287,25 @@ public override string Qualify(string catalog, string schema, string table)
287287
var tableName = new StringBuilder();
288288
if (!string.IsNullOrEmpty(schema))
289289
{
290-
if (schema.StartsWith(OpenQuote.ToString()))
290+
if (schema.StartsWith(OpenQuote))
291291
{
292292
schema = schema.Substring(1, schema.Length - 1);
293293
quoted = true;
294294
}
295-
if (schema.EndsWith(CloseQuote.ToString()))
295+
if (schema.EndsWith(CloseQuote))
296296
{
297297
schema = schema.Substring(0, schema.Length - 1);
298298
quoted = true;
299299
}
300300
tableName.Append(schema).Append(StringHelper.Underscore);
301301
}
302302

303-
if (table.StartsWith(OpenQuote.ToString()))
303+
if (table.StartsWith(OpenQuote))
304304
{
305305
table = table.Substring(1, table.Length - 1);
306306
quoted = true;
307307
}
308-
if (table.EndsWith(CloseQuote.ToString()))
308+
if (table.EndsWith(CloseQuote))
309309
{
310310
table = table.Substring(0, table.Length - 1);
311311
quoted = true;

src/NHibernate/Dialect/SQLiteDialect.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,12 @@ public override string Qualify(string catalog, string schema, string table)
277277

278278
if (!string.IsNullOrEmpty(catalog))
279279
{
280-
if (catalog.StartsWith(OpenQuote.ToString()))
280+
if (catalog.StartsWith(OpenQuote))
281281
{
282282
catalog = catalog.Substring(1, catalog.Length - 1);
283283
quoted = true;
284284
}
285-
if (catalog.EndsWith(CloseQuote.ToString()))
285+
if (catalog.EndsWith(CloseQuote))
286286
{
287287
catalog = catalog.Substring(0, catalog.Length - 1);
288288
quoted = true;
@@ -291,25 +291,25 @@ public override string Qualify(string catalog, string schema, string table)
291291
}
292292
if (!string.IsNullOrEmpty(schema))
293293
{
294-
if (schema.StartsWith(OpenQuote.ToString()))
294+
if (schema.StartsWith(OpenQuote))
295295
{
296296
schema = schema.Substring(1, schema.Length - 1);
297297
quoted = true;
298298
}
299-
if (schema.EndsWith(CloseQuote.ToString()))
299+
if (schema.EndsWith(CloseQuote))
300300
{
301301
schema = schema.Substring(0, schema.Length - 1);
302302
quoted = true;
303303
}
304304
qualifiedName.Append(schema).Append(StringHelper.Underscore);
305305
}
306306

307-
if (table.StartsWith(OpenQuote.ToString()))
307+
if (table.StartsWith(OpenQuote))
308308
{
309309
table = table.Substring(1, table.Length - 1);
310310
quoted = true;
311311
}
312-
if (table.EndsWith(CloseQuote.ToString()))
312+
if (table.EndsWith(CloseQuote))
313313
{
314314
table = table.Substring(0, table.Length - 1);
315315
quoted = true;
@@ -420,7 +420,7 @@ protected class SQLiteCastFunction : CastFunction
420420
protected override bool CastingIsRequired(string sqlType)
421421
{
422422
// SQLite doesn't support casting to datetime types. It assumes you want an integer and destroys the date string.
423-
if (sqlType.ToLowerInvariant().Contains("date") || sqlType.ToLowerInvariant().Contains("time"))
423+
if (StringHelper.ContainsCaseInsensitive(sqlType, "date") || StringHelper.ContainsCaseInsensitive(sqlType, "time"))
424424
return false;
425425
return true;
426426
}

src/NHibernate/Dialect/Schema/AbstractTableMetadata.cs

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Data;
34
using NHibernate.Util;
@@ -10,9 +11,9 @@ public abstract class AbstractTableMetadata : ITableMetadata
1011
private string catalog;
1112
private string schema;
1213
private string name;
13-
private readonly Dictionary<string, IColumnMetadata> columns = new Dictionary<string, IColumnMetadata>();
14-
private readonly Dictionary<string, IForeignKeyMetadata> foreignKeys = new Dictionary<string, IForeignKeyMetadata>();
15-
private readonly Dictionary<string, IIndexMetadata> indexes = new Dictionary<string, IIndexMetadata>();
14+
private readonly Dictionary<string, IColumnMetadata> columns = new Dictionary<string, IColumnMetadata>(StringComparer.OrdinalIgnoreCase);
15+
private readonly Dictionary<string, IForeignKeyMetadata> foreignKeys = new Dictionary<string, IForeignKeyMetadata>(StringComparer.OrdinalIgnoreCase);
16+
private readonly Dictionary<string, IIndexMetadata> indexes = new Dictionary<string, IIndexMetadata>(StringComparer.OrdinalIgnoreCase);
1617

1718
public AbstractTableMetadata(DataRow rs, IDataBaseSchema meta, bool extras)
1819
{
@@ -71,21 +72,21 @@ public override string ToString()
7172
public IColumnMetadata GetColumnMetadata(string columnName)
7273
{
7374
IColumnMetadata result;
74-
columns.TryGetValue(columnName.ToLowerInvariant(), out result);
75+
columns.TryGetValue(columnName, out result);
7576
return result;
7677
}
7778

7879
public IForeignKeyMetadata GetForeignKeyMetadata(string keyName)
7980
{
8081
IForeignKeyMetadata result;
81-
foreignKeys.TryGetValue(keyName.ToLowerInvariant(), out result);
82+
foreignKeys.TryGetValue(keyName, out result);
8283
return result;
8384
}
8485

8586
public IIndexMetadata GetIndexMetadata(string indexName)
8687
{
8788
IIndexMetadata result;
88-
indexes.TryGetValue(indexName.ToLowerInvariant(), out result);
89+
indexes.TryGetValue(indexName, out result);
8990
return result;
9091
}
9192

@@ -105,7 +106,7 @@ private void AddForeignKey(DataRow rs, IDataBaseSchema meta)
105106
if (info == null)
106107
{
107108
info = GetForeignKeyMetadata(rs);
108-
foreignKeys[info.Name.ToLowerInvariant()] = info;
109+
foreignKeys[info.Name] = info;
109110
}
110111

111112
foreach (DataRow row in meta.GetIndexColumns(catalog, schema, name, fk).Rows)
@@ -124,7 +125,7 @@ private void AddIndex(DataRow rs, IDataBaseSchema meta)
124125
if (info == null)
125126
{
126127
info = GetIndexMetadata(rs);
127-
indexes[info.Name.ToLowerInvariant()] = info;
128+
indexes[info.Name] = info;
128129
}
129130

130131
foreach (DataRow row in meta.GetIndexColumns(catalog, schema, name, index).Rows)
@@ -142,7 +143,7 @@ private void AddColumn(DataRow rs)
142143
if (GetColumnMetadata(column) == null)
143144
{
144145
IColumnMetadata info = GetColumnMetadata(rs);
145-
columns[info.Name.ToLowerInvariant()] = info;
146+
columns[info.Name] = info;
146147
}
147148
}
148149

src/NHibernate/Engine/CascadeStyle.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public virtual bool HasOrphanDelete
7878

7979
#region Static helper methods
8080

81-
private static readonly Dictionary<string, CascadeStyle> Styles = new Dictionary<string, CascadeStyle>();
81+
private static readonly Dictionary<string, CascadeStyle> Styles = new Dictionary<string, CascadeStyle>(StringComparer.OrdinalIgnoreCase);
8282
private static readonly Dictionary<CascadeStyle, string> AliasByStyle = new Dictionary<CascadeStyle, string>();
8383

8484
/// <summary> Factory method for obtaining named cascade styles </summary>

src/NHibernate/Engine/JoinSequence.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ private void AddExtraJoins(JoinFragment joinFragment, string alias, IJoinable jo
248248

249249
public JoinSequence AddCondition(SqlString condition)
250250
{
251-
if (condition.Trim().Length != 0)
251+
if (!condition.IsEmptyOrWhitespace())
252252
{
253253
if (!condition.StartsWithCaseInsensitive(" and "))
254254
conditions.Add(" and ");
@@ -314,4 +314,4 @@ public interface ISelector
314314
bool IncludeSubclasses(string alias);
315315
}
316316
}
317-
}
317+
}

0 commit comments

Comments
 (0)