Skip to content

Commit

Permalink
Use C# 6 features where possible
Browse files Browse the repository at this point in the history
Fix ReSharper warnings
  • Loading branch information
thomaslevesque committed Aug 2, 2015
1 parent a90f244 commit ce4b1b7
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
2 changes: 2 additions & 0 deletions NString.Tests/StringExtensionsTests/IsNullOrEmptyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public void IsNullOrEmpty_Returns_True_If_String_Is_Null()
{
const string s = null;
const bool expected = true;
// ReSharper disable ConditionIsAlwaysTrueOrFalse
bool actual = s.IsNullOrEmpty();
Assert.AreEqual(expected, actual);
// ReSharper restore ConditionIsAlwaysTrueOrFalse
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public void IsNullOrWhiteSpace_Returns_True_If_String_Is_Null()
{
const string s = null;
const bool expected = true;
// ReSharper disable ConditionIsAlwaysTrueOrFalse
bool actual = s.IsNullOrWhiteSpace();
Assert.AreEqual(expected, actual);
// ReSharper restore ConditionIsAlwaysTrueOrFalse
}

[Test]
Expand Down
13 changes: 7 additions & 6 deletions NString.Tests/StringTemplateTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using JetBrains.Annotations;
using NUnit.Framework;
// ReSharper disable InconsistentNaming
using System;
Expand Down Expand Up @@ -99,9 +100,8 @@ public void Test_BracesAreEscaped()
string s = StringTemplate.Format("Hello {{Name}} !", values2);
Assert.AreEqual("Hello {Name} !", s);

string s1, s2;
s1 = String.Format("Hello {{{0}}} !", values1);
s2 = StringTemplate.Format("Hello {{{Name}}} !", values2);
var s1 = String.Format("Hello {{{0}}} !", values1);
var s2 = StringTemplate.Format("Hello {{{Name}}} !", values2);
Assert.AreEqual(s1, s2);

s1 = String.Format("Hello {{{0} !", values1);
Expand All @@ -122,7 +122,7 @@ public void Test_WithFields()
int x = 42;
string text = "Hello world";

string expected = string.Format("{0} {1}", text, x);
string expected = $"{text} {x}";
string actual = StringTemplate.Format("{text} {x}", new FieldValues { x = x, text = text });

Assert.AreEqual(expected, actual);
Expand All @@ -135,7 +135,7 @@ public void Test_HiddenMembers()
BaseValues bValues = values;
bValues.X = 42;

string expected = string.Format("{0} {1}", values.X, values.Y);
string expected = $"{values.X} {values.Y}";
string actual = StringTemplate.Format("{X} {Y}", values);

Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -184,13 +184,14 @@ public void Format_Should_Accept_Alignment_In_PlaceHolder()
Value = 42
};

string expected = string.Format("{0,-10}: {1,10}", x.Name, x.Value);
string expected = $"{x.Name,-10}: {x.Value,10}";
string actual = StringTemplate.Format("{Name,-10}: {Value,10}", x);

Assert.AreEqual(expected, actual);
}

// ReSharper disable NotAccessedField.Local
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
class FieldValues
{
public int x;
Expand Down
2 changes: 1 addition & 1 deletion NString/Internal/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Cache<TKey, TValue>

public TValue GetOrAdd(TKey key, [NotNull] Func<TValue> valueFactory)
{
valueFactory.CheckArgumentNull("valueFactory");
valueFactory.CheckArgumentNull(nameof(valueFactory));

Lazy<TValue> newValue = new Lazy<TValue>(valueFactory);

Expand Down
9 changes: 1 addition & 8 deletions NString/JetBrains/Annotations.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using System;

#pragma warning disable 1591
// ReSharper disable UnusedMember.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable IntroduceOptionalParameters.Global
// ReSharper disable MemberCanBeProtected.Global
// ReSharper disable InconsistentNaming

// ReSharper disable once CheckNamespace
// ReSharper disable All
namespace JetBrains.Annotations
{
/// <summary>
Expand Down
38 changes: 19 additions & 19 deletions NString/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static bool IsNullOrWhiteSpace(this string s)
/// <returns>A string that consists of the members of values delimited by the separator string. If values has no members, the method returns String.Empty.</returns>
public static string Join([NotNull] this IEnumerable<string> values, string separator = null)
{
values.CheckArgumentNull("values");
values.CheckArgumentNull(nameof(values));
return string.Join(separator, values);
}

Expand All @@ -62,7 +62,7 @@ public static string Join([NotNull] this IEnumerable<string> values, string sepa
/// <returns>An enumerable sequence of lines in this string.</returns>
public static IEnumerable<string> GetLines([NotNull] this string s)
{
s.CheckArgumentNull("s");
s.CheckArgumentNull(nameof(s));
return GetLinesIterator(s);
}

Expand All @@ -88,8 +88,8 @@ private static IEnumerable<string> GetLinesIterator(string s)
/// <returns>A string containing a specified number of characters from the left side of a string.</returns>
public static string Left([NotNull] this string s, int count)
{
s.CheckArgumentNull("s");
count.CheckArgumentOutOfRange("count", 0, s.Length, Resources.SubstringCountOutOfRange);
s.CheckArgumentNull(nameof(s));
count.CheckArgumentOutOfRange(nameof(count), 0, s.Length, Resources.SubstringCountOutOfRange);
return s.Substring(0, count);
}

Expand All @@ -103,8 +103,8 @@ public static string Left([NotNull] this string s, int count)
/// <returns>A string containing a specified number of characters from the right side of a string.</returns>
public static string Right([NotNull] this string s, int count)
{
s.CheckArgumentNull("s");
count.CheckArgumentOutOfRange("count", 0, s.Length, Resources.SubstringCountOutOfRange);
s.CheckArgumentNull(nameof(s));
count.CheckArgumentOutOfRange(nameof(count), 0, s.Length, Resources.SubstringCountOutOfRange);
return s.Substring(s.Length - count, count);
}

Expand All @@ -118,8 +118,8 @@ public static string Right([NotNull] this string s, int count)
/// <returns>A string tuncated to the specified number of characters.</returns>
public static string Truncate([NotNull] this string s, int count)
{
s.CheckArgumentNull("s");
count.CheckArgumentOutOfRange("count", 0, int.MaxValue,
s.CheckArgumentNull(nameof(s));
count.CheckArgumentOutOfRange(nameof(count), 0, int.MaxValue,
string.Format(Resources.NumberMustBePositiveOrZero, "count"));
if (count > s.Length)
return s;
Expand All @@ -135,7 +135,7 @@ public static string Truncate([NotNull] this string s, int count)
/// <returns>The capitalized string.</returns>
public static string Capitalize([NotNull] this string s, CultureInfo culture = null)
{
s.CheckArgumentNull("s");
s.CheckArgumentNull(nameof(s));
culture = culture ?? CultureInfo.CurrentCulture;
if (s.Length == 0)
return s;
Expand All @@ -162,8 +162,8 @@ private static char CharAt(this string s, int index)
/// This method is not culture-sensitive.</remarks>
public static bool MatchesWildcard([NotNull] this string s, string pattern, bool caseSensitive = true)
{
s.CheckArgumentNull("s");
pattern.CheckArgumentNull("pattern");
s.CheckArgumentNull(nameof(s));
pattern.CheckArgumentNull(nameof(pattern));
if (!caseSensitive)
{
s = s.ToUpperInvariant();
Expand Down Expand Up @@ -238,8 +238,8 @@ public static string Ellipsis([NotNull] this string s, int maxLength)
/// <returns>The truncated string.</returns>
public static string Ellipsis([NotNull] this string s, int maxLength, string ellipsisString)
{
s.CheckArgumentNull("s");
ellipsisString.CheckArgumentNull("ellipsisString");
s.CheckArgumentNull(nameof(s));
ellipsisString.CheckArgumentNull(nameof(ellipsisString));
if (maxLength < ellipsisString.Length)
throw new ArgumentOutOfRangeException(Resources.MaxLengthCantBeLessThanLengthOfEllipsisString);

Expand All @@ -259,7 +259,7 @@ public static string Ellipsis([NotNull] this string s, int maxLength, string ell
///<returns>true if the s is a valid email address; otherwise, false.</returns>
public static bool IsValidEmail([NotNull] this string s)
{
s.CheckArgumentNull("s");
s.CheckArgumentNull(nameof(s));
return _emailRegex.IsMatch(s);
}

Expand All @@ -274,9 +274,9 @@ public static bool IsValidEmail([NotNull] this string s)
///<returns>true si <c>s</c> contains <c>subString</c>, or if <c>subString</c> is an empty string; otherwise, false.</returns>
public static bool Contains([NotNull] this string s, string subString, StringComparison comparisonType)
{
s.CheckArgumentNull("s");
subString.CheckArgumentNull("subString");
comparisonType.CheckArgumentInEnum("comparisonType");
s.CheckArgumentNull(nameof(s));
subString.CheckArgumentNull(nameof(subString));
comparisonType.CheckArgumentInEnum(nameof(comparisonType));
return s.IndexOf(subString, comparisonType) >= 0;
}

Expand All @@ -291,8 +291,8 @@ public static bool Contains([NotNull] this string s, string subString, StringCom
/// <returns>The string with the replaced character.</returns>
public static string ReplaceAt([NotNull] this string s, int index, char newChar)
{
s.CheckArgumentNull("s");
index.CheckArgumentOutOfRange("index", 0, s.Length - 1);
s.CheckArgumentNull(nameof(s));
index.CheckArgumentOutOfRange(nameof(index), 0, s.Length - 1);

var chars = s.ToCharArray();
chars[index] = newChar;
Expand Down
14 changes: 7 additions & 7 deletions NString/StringTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class StringTemplate

private StringTemplate(string template)
{
template.CheckArgumentNull("template");
template.CheckArgumentNull(nameof(template));
_template = template;
ParseTemplate(out _templateWithIndexes, out _placeholders);
}
Expand All @@ -58,7 +58,7 @@ public static StringTemplate Parse([NotNull] string template)
/// <returns>A StringTemplate using the converted string</returns>
public static implicit operator StringTemplate([NotNull] string s)
{
s.CheckArgumentNull("s");
s.CheckArgumentNull(nameof(s));
return GetTemplate(s);
}

Expand All @@ -82,7 +82,7 @@ public override string ToString()
/// <exception cref="System.Collections.Generic.KeyNotFoundException">throwOnMissingValue is true and no value was found in the dictionary for a placeholder</exception>
public string Format([NotNull] IDictionary<string, object> values, bool throwOnMissingValue = true, IFormatProvider formatProvider = null)
{
values.CheckArgumentNull("values");
values.CheckArgumentNull(nameof(values));

object[] array = new object[_placeholders.Count];
for (int i = 0; i < _placeholders.Count; i++)
Expand All @@ -93,7 +93,7 @@ public string Format([NotNull] IDictionary<string, object> values, bool throwOnM
{
if (throwOnMissingValue)
throw new KeyNotFoundException(string.Format(Resources.TemplateKeyNotFound, key));
value = string.Format("{{{0}}}", key);
value = $"{{{key}}}";
}
array[i] = value;
}
Expand All @@ -112,7 +112,7 @@ public string Format([NotNull] IDictionary<string, object> values, bool throwOnM
/// <exception cref="System.Collections.Generic.KeyNotFoundException">throwOnMissingValue is true and no value was found in the dictionary for a placeholder</exception>
public string Format([NotNull] object values, bool throwOnMissingValue = true, IFormatProvider formatProvider = null)
{
values.CheckArgumentNull("values");
values.CheckArgumentNull(nameof(values));
return Format(MakeDictionary(values), throwOnMissingValue, formatProvider);
}

Expand Down Expand Up @@ -180,7 +180,7 @@ private void ParseTemplate(out string templateWithIndexes, out IList<string> pla
}
int index = tmp.IndexOf(key);
return string.Format("{0}{{{1}{2}{3}}}{4}", open, index, alignment, format, close);
return $"{open}{{{index}{alignment}{format}}}{close}";
};
templateWithIndexes = _regex.Replace(_template, evaluator);
placeholders = tmp.AsReadOnly();
Expand Down Expand Up @@ -224,7 +224,7 @@ private static bool TryGetMemberValue(object obj, string memberName, out object

private static StringTemplate GetTemplate(string template)
{
template.CheckArgumentNull("template");
template.CheckArgumentNull(nameof(template));
return _templateCache.GetOrAdd(template, () => new StringTemplate(template));
}

Expand Down

0 comments on commit ce4b1b7

Please sign in to comment.