Skip to content

Commit d84fbf4

Browse files
author
Oren (electricessence)
committed
Resharper inspection fixes.
1 parent 98952af commit d84fbf4

2 files changed

Lines changed: 50 additions & 31 deletions

File tree

Open.Text.csproj

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ Part of the "Open" set of libraries.</Description>
1515
<RepositoryType>git</RepositoryType>
1616
<RepositoryUrl>https://github.com/electricessence/Open.Text</RepositoryUrl>
1717
<PackageTags>dotnet, dotnetcore, string, text, format, stringbuilder, extensions</PackageTags>
18-
<Version>1.1.0</Version>
19-
<PackageReleaseNotes>Updated to .NET Standard for compatability.</PackageReleaseNotes>
18+
<Version>1.2.0</Version>
19+
<PackageReleaseNotes>Implemented updated for C# 7.2 features.</PackageReleaseNotes>
20+
<AssemblyVersion>1.2.0.0</AssemblyVersion>
21+
<FileVersion>1.2.0.0</FileVersion>
22+
</PropertyGroup>
23+
24+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
25+
<LangVersion>latest</LangVersion>
26+
</PropertyGroup>
27+
28+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
29+
<LangVersion>latest</LangVersion>
2030
</PropertyGroup>
2131

2232
<ItemGroup>
@@ -26,4 +36,8 @@ Part of the "Open" set of libraries.</Description>
2636
<None Remove="README.md" />
2737
</ItemGroup>
2838

39+
<ItemGroup>
40+
<PackageReference Include="System.Memory" Version="4.5.1" />
41+
</ItemGroup>
42+
2943
</Project>

Text.cs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public static string BeforeFirst(this string source, string search, StringCompar
3434
throw new NullReferenceException();
3535
Contract.EndContractBlock();
3636

37-
if (String.IsNullOrEmpty(search))
37+
if (string.IsNullOrEmpty(search))
3838
return null;
3939

40-
int i = source.IndexOf(search, comparisonType);
40+
var i = source.IndexOf(search, comparisonType);
4141
return i == -1 ? null : source.Substring(0, i);
4242
}
4343

@@ -54,10 +54,10 @@ public static string AfterFirst(this string source, string search, StringCompari
5454
throw new NullReferenceException();
5555
Contract.EndContractBlock();
5656

57-
if (String.IsNullOrEmpty(search))
57+
if (string.IsNullOrEmpty(search))
5858
return null;
5959

60-
int i = source.IndexOf(search, comparisonType);
60+
var i = source.IndexOf(search, comparisonType);
6161
return i == -1 ? null : source.Substring(i + search.Length);
6262
}
6363

@@ -74,10 +74,10 @@ public static string BeforeLast(this string source, string search, StringCompari
7474
throw new NullReferenceException();
7575
Contract.EndContractBlock();
7676

77-
if (String.IsNullOrEmpty(search))
77+
if (string.IsNullOrEmpty(search))
7878
return null;
7979

80-
int i = source.LastIndexOf(search, comparisonType);
80+
var i = source.LastIndexOf(search, comparisonType);
8181
return i == -1 ? null : source.Substring(0, i);
8282
}
8383

@@ -94,10 +94,10 @@ public static string AfterLast(this string source, string search, StringComparis
9494
throw new NullReferenceException();
9595
Contract.EndContractBlock();
9696

97-
if (String.IsNullOrEmpty(search))
97+
if (string.IsNullOrEmpty(search))
9898
return null;
9999

100-
int i = source.LastIndexOf(search, comparisonType);
100+
var i = source.LastIndexOf(search, comparisonType);
101101
return i == -1 ? null : source.Substring(i + search.Length);
102102
}
103103

@@ -124,12 +124,12 @@ public static bool IsAnyNullOrWhiteSpace(params string[] values)
124124
if (values == null || values.Length == 0)
125125
return false;
126126

127-
return values.Any(v => String.IsNullOrWhiteSpace(v));
127+
return values.Any(string.IsNullOrWhiteSpace);
128128
}
129129

130130
public static string AssertIsNotNullOrWhiteSpace(this string source)
131131
{
132-
if (String.IsNullOrWhiteSpace(source))
132+
if (string.IsNullOrWhiteSpace(source))
133133
throw new ArgumentException();
134134
Contract.EndContractBlock();
135135

@@ -141,55 +141,60 @@ public static string AssertIsNotNullOrWhiteSpace(this string source)
141141
/// </summary>
142142
public static bool IsNullOrWhiteSpace(this string source)
143143
{
144-
return String.IsNullOrWhiteSpace(source);
144+
return string.IsNullOrWhiteSpace(source);
145145
}
146146

147147
/// <summary>
148148
/// Shortcut for returning a null addValue if the source string is null, white space or empty.
149149
/// </summary>
150+
/// <param name="value">The value to be trimmed.</param>
150151
/// <param name="trim">True will trim whitespace from valid response.</param>
151152
public static string ToNullIfWhiteSpace(this string value, bool trim = false)
152153
{
153-
return String.IsNullOrWhiteSpace(value) ? null : (trim) ? value.Trim() : value;
154+
return string.IsNullOrWhiteSpace(value) ? null : (trim ? value.Trim() : value);
154155
}
155156

156157
/// <summary>
157158
/// Shortcut for returning a formatted string if non-null, non-whitespace action exists.
158159
/// </summary>
159-
/// <param name="trim">True will trim whitespace from valid response.</param>
160+
/// <param name="value">The value to be formatted.</param>
161+
/// <param name="format">The format string.</param>
160162
public static string ToFormat(this string value, string format = null)
161163
{
162-
return String.IsNullOrWhiteSpace(value) ? String.Empty : (format == null ? value : String.Format(format, value));
164+
return string.IsNullOrWhiteSpace(value) ? string.Empty : (format == null ? value : string.Format(format, value));
163165
}
164166

165167
/// <summary>
166168
/// Shortcut for returning a formatted string if non-null, non-whitespace action exists.
167169
/// </summary>
168-
/// <param name="trim">True will trim whitespace from valid response.</param>
170+
/// <param name="value">The value to be formatted.</param>
171+
/// <param name="format">The format string.</param>
169172
public static string ToFormat(this int? value, string format = null)
170173
{
171174
if (format == null) format = "{0}";
172-
return value == null ? String.Empty : String.Format(format, value.Value);
175+
return value == null ? string.Empty : string.Format(format, value.Value);
173176
}
174177

175178
/// <summary>
176179
/// Shortcut for returning a formatted string if non-null, non-whitespace action exists.
177180
/// </summary>
178-
/// <param name="trim">True will trim whitespace from valid response.</param>
181+
/// <param name="value">The value to be formatted.</param>
182+
/// <param name="format">The format string.</param>
179183
public static string ToFormat(this short? value, string format = null)
180184
{
181185
if (format == null) format = "{0}";
182-
return value == null ? String.Empty : String.Format(format, value.Value);
186+
return value == null ? string.Empty : string.Format(format, value.Value);
183187
}
184188

185189

186190
/// <summary>
187191
/// Returns true if only contains alphanumeric characters. Regex: (^\w+$).
188192
/// </summary>
189-
/// <param name="trim">Trims white space from source string before validation.</param>
193+
/// <param name="source">The value to be formatted.</param>
194+
/// <param name="trim">Will be trimmed if true.</param>
190195
public static bool IsAlphaNumeric(this string source, bool trim = false)
191196
{
192-
return !String.IsNullOrWhiteSpace(source) && VALID_ALPHA_NUMERIC_ONLY.IsMatch(trim ? source.Trim() : source);
197+
return !string.IsNullOrWhiteSpace(source) && VALID_ALPHA_NUMERIC_ONLY.IsMatch(trim ? source.Trim() : source);
193198
}
194199

195200
#region Regex helper methods.
@@ -226,7 +231,7 @@ public static StringBuilder AppendAll<T>(this StringBuilder target, IEnumerable<
226231

227232
if (values != null)
228233
{
229-
if (String.IsNullOrEmpty(separator))
234+
if (string.IsNullOrEmpty(separator))
230235
{
231236
foreach (var value in values)
232237
target.Append(value);
@@ -267,7 +272,7 @@ public static StringBuilder AppendWithSeparator<T>(this StringBuilder target, st
267272
throw new ArgumentException("Parameters missing.");
268273
Contract.EndContractBlock();
269274

270-
if (!String.IsNullOrEmpty(separator) && target.Length != 0)
275+
if (!string.IsNullOrEmpty(separator) && target.Length != 0)
271276
target.Append(separator);
272277

273278
target.AppendAll(values);
@@ -309,7 +314,7 @@ public static void AppendWithSeparator<T>(this StringBuilder target, IDictionary
309314
throw new ArgumentNullException(nameof(keyValueSeparator));
310315
Contract.EndContractBlock();
311316

312-
if (source.TryGetValue(key, out T result))
317+
if (source.TryGetValue(key, out var result))
313318
target
314319
.AppendWithSeparator(itemSeparator, key)
315320
.Append(keyValueSeparator)
@@ -373,7 +378,7 @@ public static string ToByteString(this double bytes)
373378
if (Math.Abs(bytes) < BYTE_RED)
374379
return bytes.ToString("0") + " bytes";
375380

376-
foreach (string s in _byte_labels)
381+
foreach (var s in _byte_labels)
377382
{
378383
bytes /= BYTE_RED;
379384
if (Math.Abs(bytes) < BYTE_RED)
@@ -407,7 +412,7 @@ public static string ToMetricString(this double number)
407412
if (Math.Abs(number) < 1000)
408413
return number.ToString("0.0");
409414

410-
foreach (string s in _number_labels)
415+
foreach (var s in _number_labels)
411416
{
412417
number /= 1000;
413418
if (Math.Abs(number) < 1000)
@@ -457,10 +462,10 @@ public static string TrimStart(this string source, string pattern)
457462
if (pattern == null)
458463
return source.TrimStart();
459464

460-
if (pattern == String.Empty)
465+
if (pattern == string.Empty)
461466
return source;
462467

463-
return source.IndexOf(pattern) == 0
468+
return source.IndexOf(pattern, StringComparison.Ordinal) == 0
464469
? source.Substring(pattern.Length)
465470
: source;
466471
}
@@ -474,11 +479,11 @@ public static string TrimEnd(this string source, string pattern)
474479
if (pattern == null)
475480
return source.TrimEnd();
476481

477-
if (pattern == String.Empty)
482+
if (pattern == string.Empty)
478483
return source;
479484

480485
var expectedIndex = source.Length - pattern.Length;
481-
var result = source.IndexOf(pattern);
486+
var result = source.IndexOf(pattern, StringComparison.Ordinal);
482487
return result >= 0 && result == expectedIndex
483488
? source.Substring(0, expectedIndex)
484489
: source;

0 commit comments

Comments
 (0)