Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Trim().Length as empty check and ToLowerInvariant() in string comparison #1567

Merged
merged 7 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/NHibernate.DomainModel/NHSpecific/NullableInt32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public int CompareTo(object obj)

public static NullableInt32 Parse(string s)
{
if ((s == null) || (s.Trim().Length == 0))
if (string.IsNullOrWhiteSpace(s))
{
return new NullableInt32();
}
Expand All @@ -235,4 +235,4 @@ public static NullableInt32 Parse(string s)

#endregion
}
}
}
1 change: 0 additions & 1 deletion src/NHibernate/Async/Tool/hbm2ddl/SchemaExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public partial class SchemaExport
dialect = Dialect.Dialect.GetDialect(configProperties);

string autoKeyWordsImport = PropertiesHelper.GetString(Environment.Hbm2ddlKeyWords, configProperties, "not-defined");
autoKeyWordsImport = autoKeyWordsImport.ToLowerInvariant();
if (autoKeyWordsImport == Hbm2DDLKeyWords.AutoQuote)
{
await (SchemaMetadataUpdater.UpdateAsync(cfg, dialect, cancellationToken)).ConfigureAwait(false);
Expand Down
11 changes: 5 additions & 6 deletions src/NHibernate/Async/Tool/hbm2ddl/SchemaUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public partial class SchemaUpdate
}

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

for (int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("--"))
if (args[i].StartsWith("--", StringComparison.Ordinal))
{
if (args[i].Equals("--quiet"))
{
script = false;
}
else if (args[i].StartsWith("--properties="))
else if (args[i].StartsWith("--properties=", StringComparison.Ordinal))
{
throw new NotSupportedException("No properties file for .NET, use app.config instead");
//propFile = args[i].Substring( 13 );
}
else if (args[i].StartsWith("--config="))
else if (args[i].StartsWith("--config=", StringComparison.Ordinal))
{
cfg.Configure(args[i].Substring(9));
}
else if (args[i].StartsWith("--text"))
else if (args[i].StartsWith("--text", StringComparison.Ordinal))
{
doUpdate = false;
}
else if (args[i].StartsWith("--naming="))
else if (args[i].StartsWith("--naming=", StringComparison.Ordinal))
{
cfg.SetNamingStrategy(
(INamingStrategy)
Expand Down
8 changes: 4 additions & 4 deletions src/NHibernate/Async/Tool/hbm2ddl/SchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ public partial class SchemaValidator

for (int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("--"))
if (args[i].StartsWith("--", StringComparison.Ordinal))
{
//if (args[i].StartsWith("--properties="))
//{
// propFile = args[i].Substring(13);
//}
//else
if (args[i].StartsWith("--config="))
if (args[i].StartsWith("--config=", StringComparison.Ordinal))
{
cfg.Configure(args[i].Substring(9));
}
else if (args[i].StartsWith("--naming="))
else if (args[i].StartsWith("--naming=", StringComparison.Ordinal))
{
cfg.SetNamingStrategy(
(INamingStrategy)
Expand Down Expand Up @@ -113,4 +113,4 @@ public partial class SchemaValidator
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void Parse(XPathNavigator classCacheElement)
switch (classCacheElement.Name)
{
case "class":
if (classCacheElement.Value.Trim().Length == 0)
if (string.IsNullOrWhiteSpace(classCacheElement.Value))
throw new HibernateConfigException("Invalid class-cache element; the attribute <class> must be assigned with no empty value");
clazz = classCacheElement.Value;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private void Parse(XPathNavigator collectionCacheElement)
switch (collectionCacheElement.Name)
{
case "collection":
if (collectionCacheElement.Value.Trim().Length == 0)
if (string.IsNullOrWhiteSpace(collectionCacheElement.Value))
throw new HibernateConfigException("Invalid collection-cache element; the attribute <collection> must be assigned with no empty value");
collection = collectionCacheElement.Value;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void Parse(XPathNavigator listenerElement)
switch (listenerElement.Name)
{
case "class":
if (listenerElement.Value.Trim().Length == 0)
if (string.IsNullOrWhiteSpace(listenerElement.Value))
throw new HibernateConfigException("Invalid listener element; the attribute <class> must be assigned with no empty value");
clazz = listenerElement.Value;
break;
Expand Down
6 changes: 4 additions & 2 deletions src/NHibernate/Cfg/Hbm2ddlKeyWords.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace NHibernate.Cfg
{
public class Hbm2DDLKeyWords
Expand Down Expand Up @@ -33,7 +35,7 @@ public override bool Equals(object obj)

public bool Equals(string other)
{
return value.Equals(other);
return value.Equals(other, StringComparison.OrdinalIgnoreCase);
}

public bool Equals(Hbm2DDLKeyWords other)
Expand Down Expand Up @@ -82,4 +84,4 @@ public override int GetHashCode()
public static Hbm2DDLKeyWords Keywords = new Hbm2DDLKeyWords("keywords");
public static Hbm2DDLKeyWords AutoQuote = new Hbm2DDLKeyWords("auto-quote");
}
}
}
7 changes: 3 additions & 4 deletions src/NHibernate/Cfg/Mappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Mappings
[Serializable]
public class ColumnNames
{
public readonly IDictionary<string, string> logicalToPhysical = new Dictionary<string, string>();
public readonly IDictionary<string, string> logicalToPhysical = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
public readonly IDictionary<string, string> physicalToLogical = new Dictionary<string, string>();
}

Expand Down Expand Up @@ -536,8 +536,8 @@ public void AddColumnBinding(string logicalName, Column finalColumn, Table table
}

string oldFinalName;
binding.logicalToPhysical.TryGetValue(logicalName.ToLowerInvariant(), out oldFinalName);
binding.logicalToPhysical[logicalName.ToLowerInvariant()] = finalColumn.GetQuotedName();
binding.logicalToPhysical.TryGetValue(logicalName, out oldFinalName);
binding.logicalToPhysical[logicalName] = finalColumn.GetQuotedName();
if (oldFinalName != null &&
!(finalColumn.IsQuoted
? oldFinalName.Equals(finalColumn.GetQuotedName())
Expand Down Expand Up @@ -584,7 +584,6 @@ public string GetLogicalColumnName(string physicalName, Table table)

public string GetPhysicalColumnName(string logicalName, Table table)
{
logicalName = logicalName.ToLowerInvariant();
string finalName = null;
Table currentTable = table;
do
Expand Down
1 change: 0 additions & 1 deletion src/NHibernate/Cfg/SettingsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ public Settings BuildSettings(IDictionary<string, string> properties)
}

string autoKeyWordsImport = PropertiesHelper.GetString(Environment.Hbm2ddlKeyWords, properties, "not-defined");
autoKeyWordsImport = autoKeyWordsImport.ToLowerInvariant();
if (autoKeyWordsImport == Hbm2DDLKeyWords.None)
{
settings.IsKeywordsImportEnabled = false;
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Dialect/Function/ClassicAggregateFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ bool IFunctionGrammar.IsSeparator(string token)

bool IFunctionGrammar.IsKnownArgument(string token)
{
return "distinct".Equals(token.ToLowerInvariant()) ||
"all".Equals(token.ToLowerInvariant());
return "distinct".Equals(token, StringComparison.OrdinalIgnoreCase) ||
"all".Equals(token, StringComparison.OrdinalIgnoreCase);
}

#endregion
Expand Down
5 changes: 4 additions & 1 deletion src/NHibernate/Dialect/Function/SQLFunctionRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ public SQLFunctionRegistry(Dialect dialect, IDictionary<string, ISQLFunction> us
{
this.dialect = dialect;
this.userFunctions = new Dictionary<string, ISQLFunction>(userFunctions,
StringComparer.InvariantCultureIgnoreCase);
StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// Find function by function name ignoring case
/// </summary>
public ISQLFunction FindSQLFunction(string functionName)
{
ISQLFunction result;
Expand Down
8 changes: 4 additions & 4 deletions src/NHibernate/Dialect/MsSqlCeDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,25 +287,25 @@ public override string Qualify(string catalog, string schema, string table)
var tableName = new StringBuilder();
if (!string.IsNullOrEmpty(schema))
{
if (schema.StartsWith(OpenQuote.ToString()))
if (schema.StartsWith(OpenQuote))
{
schema = schema.Substring(1, schema.Length - 1);
quoted = true;
}
if (schema.EndsWith(CloseQuote.ToString()))
if (schema.EndsWith(CloseQuote))
{
schema = schema.Substring(0, schema.Length - 1);
quoted = true;
}
tableName.Append(schema).Append(StringHelper.Underscore);
}

if (table.StartsWith(OpenQuote.ToString()))
if (table.StartsWith(OpenQuote))
{
table = table.Substring(1, table.Length - 1);
quoted = true;
}
if (table.EndsWith(CloseQuote.ToString()))
if (table.EndsWith(CloseQuote))
{
table = table.Substring(0, table.Length - 1);
quoted = true;
Expand Down
14 changes: 7 additions & 7 deletions src/NHibernate/Dialect/SQLiteDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,12 @@ public override string Qualify(string catalog, string schema, string table)

if (!string.IsNullOrEmpty(catalog))
{
if (catalog.StartsWith(OpenQuote.ToString()))
if (catalog.StartsWith(OpenQuote))
{
catalog = catalog.Substring(1, catalog.Length - 1);
quoted = true;
}
if (catalog.EndsWith(CloseQuote.ToString()))
if (catalog.EndsWith(CloseQuote))
{
catalog = catalog.Substring(0, catalog.Length - 1);
quoted = true;
Expand All @@ -291,25 +291,25 @@ public override string Qualify(string catalog, string schema, string table)
}
if (!string.IsNullOrEmpty(schema))
{
if (schema.StartsWith(OpenQuote.ToString()))
if (schema.StartsWith(OpenQuote))
{
schema = schema.Substring(1, schema.Length - 1);
quoted = true;
}
if (schema.EndsWith(CloseQuote.ToString()))
if (schema.EndsWith(CloseQuote))
{
schema = schema.Substring(0, schema.Length - 1);
quoted = true;
}
qualifiedName.Append(schema).Append(StringHelper.Underscore);
}

if (table.StartsWith(OpenQuote.ToString()))
if (table.StartsWith(OpenQuote))
{
table = table.Substring(1, table.Length - 1);
quoted = true;
}
if (table.EndsWith(CloseQuote.ToString()))
if (table.EndsWith(CloseQuote))
{
table = table.Substring(0, table.Length - 1);
quoted = true;
Expand Down Expand Up @@ -420,7 +420,7 @@ protected class SQLiteCastFunction : CastFunction
protected override bool CastingIsRequired(string sqlType)
{
// SQLite doesn't support casting to datetime types. It assumes you want an integer and destroys the date string.
if (sqlType.ToLowerInvariant().Contains("date") || sqlType.ToLowerInvariant().Contains("time"))
if (StringHelper.ContainsCaseInsensitive(sqlType, "date") || StringHelper.ContainsCaseInsensitive(sqlType, "time"))
return false;
return true;
}
Expand Down
19 changes: 10 additions & 9 deletions src/NHibernate/Dialect/Schema/AbstractTableMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using NHibernate.Util;
Expand All @@ -10,9 +11,9 @@ public abstract class AbstractTableMetadata : ITableMetadata
private string catalog;
private string schema;
private string name;
private readonly Dictionary<string, IColumnMetadata> columns = new Dictionary<string, IColumnMetadata>();
private readonly Dictionary<string, IForeignKeyMetadata> foreignKeys = new Dictionary<string, IForeignKeyMetadata>();
private readonly Dictionary<string, IIndexMetadata> indexes = new Dictionary<string, IIndexMetadata>();
private readonly Dictionary<string, IColumnMetadata> columns = new Dictionary<string, IColumnMetadata>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, IForeignKeyMetadata> foreignKeys = new Dictionary<string, IForeignKeyMetadata>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, IIndexMetadata> indexes = new Dictionary<string, IIndexMetadata>(StringComparer.OrdinalIgnoreCase);

public AbstractTableMetadata(DataRow rs, IDataBaseSchema meta, bool extras)
{
Expand Down Expand Up @@ -71,21 +72,21 @@ public override string ToString()
public IColumnMetadata GetColumnMetadata(string columnName)
{
IColumnMetadata result;
columns.TryGetValue(columnName.ToLowerInvariant(), out result);
columns.TryGetValue(columnName, out result);
return result;
}

public IForeignKeyMetadata GetForeignKeyMetadata(string keyName)
{
IForeignKeyMetadata result;
foreignKeys.TryGetValue(keyName.ToLowerInvariant(), out result);
foreignKeys.TryGetValue(keyName, out result);
return result;
}

public IIndexMetadata GetIndexMetadata(string indexName)
{
IIndexMetadata result;
indexes.TryGetValue(indexName.ToLowerInvariant(), out result);
indexes.TryGetValue(indexName, out result);
return result;
}

Expand All @@ -105,7 +106,7 @@ private void AddForeignKey(DataRow rs, IDataBaseSchema meta)
if (info == null)
{
info = GetForeignKeyMetadata(rs);
foreignKeys[info.Name.ToLowerInvariant()] = info;
foreignKeys[info.Name] = info;
}

foreach (DataRow row in meta.GetIndexColumns(catalog, schema, name, fk).Rows)
Expand All @@ -124,7 +125,7 @@ private void AddIndex(DataRow rs, IDataBaseSchema meta)
if (info == null)
{
info = GetIndexMetadata(rs);
indexes[info.Name.ToLowerInvariant()] = info;
indexes[info.Name] = info;
}

foreach (DataRow row in meta.GetIndexColumns(catalog, schema, name, index).Rows)
Expand All @@ -142,7 +143,7 @@ private void AddColumn(DataRow rs)
if (GetColumnMetadata(column) == null)
{
IColumnMetadata info = GetColumnMetadata(rs);
columns[info.Name.ToLowerInvariant()] = info;
columns[info.Name] = info;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Engine/CascadeStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public virtual bool HasOrphanDelete

#region Static helper methods

private static readonly Dictionary<string, CascadeStyle> Styles = new Dictionary<string, CascadeStyle>();
private static readonly Dictionary<string, CascadeStyle> Styles = new Dictionary<string, CascadeStyle>(StringComparer.OrdinalIgnoreCase);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks to me as an undue change. Cascade styles are not case insensitive, even if currently we would not have names conflict by going insensitive.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? Change is based on the following line:

styles[i++] = CascadeStyle.GetCascadeStyle(token.ToLowerInvariant().Trim());

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, so the internal usage of this public class is case insensitive. It is then unlikely it could be a breaking change for anyone to render the class case insensitive, but not strictly impossible either.

Well, keep your change in place.

private static readonly Dictionary<CascadeStyle, string> AliasByStyle = new Dictionary<CascadeStyle, string>();

/// <summary> Factory method for obtaining named cascade styles </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Engine/JoinSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private void AddExtraJoins(JoinFragment joinFragment, string alias, IJoinable jo

public JoinSequence AddCondition(SqlString condition)
{
if (condition.Trim().Length != 0)
if (!condition.IsEmptyOrWhitespace())
{
if (!condition.StartsWithCaseInsensitive(" and "))
conditions.Add(" and ");
Expand Down Expand Up @@ -314,4 +314,4 @@ public interface ISelector
bool IncludeSubclasses(string alias);
}
}
}
}
Loading