Skip to content

Commit abdc3ee

Browse files
Add StringSegment extensions and update version
Added `IsNullOrEmpty` and `IsNullOrWhiteSpace` methods to the `TextExtensions` class for convenient null/empty/whitespace checks on `StringSegment`. Improved code formatting in the `removeEmpty` logic for readability. Bumped version in `Open.Text.csproj` from 9.0.0 to 9.0.1 to reflect the changes.
1 parent d04d247 commit abdc3ee

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

Source/Extensions.StringSegment.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,27 @@ static IEnumerable<StringSegment> SplitAsSegmentsCoreOmitEmpty(
181181
}
182182
}
183183

184+
/// <summary>
185+
/// Determines whether the specified <see cref="StringSegment"/> is null or empty.
186+
/// </summary>
187+
/// <remarks>This method is a convenience extension for checking whether a <see cref="StringSegment"/> has no
188+
/// value or contains no characters.</remarks>
189+
/// <param name="segment">The <see cref="StringSegment"/> to check.</param>
190+
/// <returns><see langword="true"/> if the <paramref name="segment"/> is null or its length is zero; otherwise, <see
191+
/// langword="false"/>.</returns>
192+
public static bool IsNullOrEmpty(this StringSegment segment)
193+
=> !segment.HasValue || segment.Length == 0;
194+
195+
/// <summary>
196+
/// Determines whether the specified <see cref="StringSegment"/> is null, empty, or consists only of white-space
197+
/// characters.
198+
/// </summary>
199+
/// <param name="segment">The <see cref="StringSegment"/> to evaluate.</param>
200+
/// <returns><see langword="true"/> if the <paramref name="segment"/> is null, empty, or contains only white-space characters;
201+
/// otherwise, <see langword="false"/>.</returns>
202+
public static bool IsNullOrWhiteSpace(this StringSegment segment)
203+
=> !segment.HasValue || segment.AsSpan().Trim().Length == 0;
204+
184205
/// <summary>
185206
/// Enumerates a string by segments that are separated by the regular expression matches.
186207
/// </summary>
@@ -236,10 +257,10 @@ static IEnumerable<StringSegment> SplitCore(string source, Regex pattern, String
236257
}
237258

238259
int len;
239-
if(removeEmpty)
260+
if (removeEmpty)
240261
{
241262
len = source.Length - nextStart;
242-
if(len==0) yield break;
263+
if (len == 0) yield break;
243264
}
244265
else
245266
{

Source/Open.Text.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Text</RepositoryUrl>
2121
<RepositoryType>git</RepositoryType>
2222
<PackageTags>string, span, enum, readonlyspan, text, format, split, trim, equals, trimmed equals, first, last, preceding, following, stringbuilder, extensions, stringcomparable, spancomparable, stringsegment, splitassegment</PackageTags>
23-
<Version>9.0.0</Version>
23+
<Version>9.0.1</Version>
2424
<PackageReleaseNotes></PackageReleaseNotes>
2525
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2626
<PublishRepositoryUrl>true</PublishRepositoryUrl>

0 commit comments

Comments
 (0)