Skip to content

Commit 1038f64

Browse files
Cleanup with added comparison operators and improved docs.
1 parent a43dbfd commit 1038f64

14 files changed

Lines changed: 180 additions & 136 deletions

Benchmarks/EnumAttributeTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BenchmarkDotNet.Attributes;
2-
using System.Linq.Expressions;
32

43
namespace Open.Text.Benchmarks;
54

Benchmarks/EnumToStringTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using BenchmarkDotNet.Attributes;
2-
using System;
32
using System.Collections.Concurrent;
4-
using System.Collections.Generic;
5-
using System.Linq;
63

74
namespace Open.Text.Benchmarks;
85

@@ -54,6 +51,6 @@ public void EnumValueGetName()
5451
public void GetCachedName()
5552
{
5653
foreach (var g in Values)
57-
_ = _reg.GetOrAdd(g, k=>k.ToString());
54+
_ = _reg.GetOrAdd(g, k => k.ToString());
5855
}
5956
}

Benchmarks/Greek.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using System;
2-
3-
namespace Open.Text.Benchmarks;
1+
namespace Open.Text.Benchmarks;
42

53
[AttributeUsage(AttributeTargets.Field)]
6-
public class LetterAttribute : Attribute {
4+
public class LetterAttribute : Attribute
5+
{
76
public LetterAttribute(char upper, char lower)
87
{
98
Upper = upper;
@@ -19,25 +18,25 @@ public bool EqualsLetter(char letter)
1918

2019
public enum Greek
2120
{
22-
[Letter('Α','α')]
21+
[Letter('Α', 'α')]
2322
Alpha,
2423
[Letter('Β', 'β')]
2524
Beta,
2625
[Letter('b', 'b')]
2726
Beta2,
28-
[Letter('Κ','κ')]
27+
[Letter('Κ', 'κ')]
2928
Kappa,
30-
[Letter('Δ','δ')]
29+
[Letter('Δ', 'δ')]
3130
Delta,
32-
[Letter('Ε','ε')]
31+
[Letter('Ε', 'ε')]
3332
Epsilon,
34-
[Letter('Γ','γ')]
33+
[Letter('Γ', 'γ')]
3534
Gamma,
36-
[Letter('Ω','ω')]
35+
[Letter('Ω', 'ω')]
3736
Omega,
38-
[Letter('Φ','φ')]
37+
[Letter('Φ', 'φ')]
3938
Phi,
40-
[Letter('Θ','θ')]
39+
[Letter('Θ', 'θ')]
4140
Theta,
4241
None
4342
}

Source/EnumValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using System.Diagnostics.CodeAnalysis;
77
using System.Linq;
88
using System.Linq.Expressions;
9-
using static System.Linq.Expressions.Expression;
109
using System.Runtime.CompilerServices;
1110
using System.Threading;
11+
using static System.Linq.Expressions.Expression;
1212

1313
namespace Open.Text;
1414

Source/Extensions.Split.cs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static ReadOnlyMemory<char> FirstSplitMemory(string source, int start, int i, in
5959
/// Finds the first instance of a character and returns the set of characters up to that character.
6060
/// </summary>
6161
/// <param name="source">The source characters to look through.</param>
62-
/// <param name="splitCharacter">The charcter to find.</param>
62+
/// <param name="splitCharacter">The character to find.</param>
6363
/// <param name="nextIndex">The next possible index following the current one.</param>
6464
/// <param name="startIndex">The index to start the split.</param>
6565
/// <returns>The portion of the source up to and excluding the sequence searched for.</returns>
@@ -170,7 +170,7 @@ public static ReadOnlySpan<char> FirstSplit(this ReadOnlySpan<char> source,
170170
/// Enumerates a string by segments that are separated by the split character.
171171
/// </summary>
172172
/// <param name="source">The source characters to look through.</param>
173-
/// <param name="splitCharacter">The charcter to find.</param>
173+
/// <param name="splitCharacter">The character to find.</param>
174174
/// <param name="options">Can specify to omit empty entries.</param>
175175
/// <returns>An enumerable of the segments.</returns>
176176
public static IEnumerable<string> SplitToEnumerable(this string source,
@@ -387,29 +387,29 @@ public static IReadOnlyList<string> Split(this ReadOnlySpan<char> source,
387387
return Array.Empty<string>();
388388

389389
case StringSplitOptions.RemoveEmptyEntries:
390-
{
391-
Debug.Assert(!source.IsEmpty);
392-
var list = new List<string>();
393-
394-
loop:
395-
var result = source.FirstSplit(splitCharacter, out var nextIndex);
396-
if (!result.IsEmpty) list.Add(result.ToString());
397-
if (nextIndex == -1) return list;
398-
source = source.Slice(nextIndex);
399-
goto loop;
400-
}
390+
{
391+
Debug.Assert(!source.IsEmpty);
392+
var list = new List<string>();
393+
394+
loop:
395+
var result = source.FirstSplit(splitCharacter, out var nextIndex);
396+
if (!result.IsEmpty) list.Add(result.ToString());
397+
if (nextIndex == -1) return list;
398+
source = source.Slice(nextIndex);
399+
goto loop;
400+
}
401401

402402
default:
403-
{
404-
Debug.Assert(!source.IsEmpty);
405-
var list = new List<string>();
406-
loop:
407-
var result = source.FirstSplit(splitCharacter, out var nextIndex);
408-
list.Add(result.IsEmpty ? string.Empty : result.ToString());
409-
if (nextIndex == -1) return list;
410-
source = source.Slice(nextIndex);
411-
goto loop;
412-
}
403+
{
404+
Debug.Assert(!source.IsEmpty);
405+
var list = new List<string>();
406+
loop:
407+
var result = source.FirstSplit(splitCharacter, out var nextIndex);
408+
list.Add(result.IsEmpty ? string.Empty : result.ToString());
409+
if (nextIndex == -1) return list;
410+
source = source.Slice(nextIndex);
411+
goto loop;
412+
}
413413
}
414414
}
415415

@@ -419,7 +419,7 @@ public static IReadOnlyList<string> Split(this ReadOnlySpan<char> source,
419419
/// <param name="source">The source string to split up.</param>
420420
/// <param name="splitSequence">The sequence to split by.</param>
421421
/// <param name="options">Can specify to omit empty entries.</param>
422-
/// <param name="comparisonType">The optional comparsion type.</param>
422+
/// <param name="comparisonType">The optional comparison type.</param>
423423
/// <returns>The resultant list of string segments.</returns>
424424
public static IReadOnlyList<string> Split(this ReadOnlySpan<char> source,
425425
ReadOnlySpan<char> splitSequence,
@@ -435,29 +435,29 @@ public static IReadOnlyList<string> Split(this ReadOnlySpan<char> source,
435435
return Array.Empty<string>();
436436

437437
case StringSplitOptions.RemoveEmptyEntries:
438-
{
439-
Debug.Assert(!source.IsEmpty);
440-
var list = new List<string>();
441-
442-
loop:
443-
var result = source.FirstSplit(splitSequence, out var nextIndex, comparisonType);
444-
if (!result.IsEmpty) list.Add(result.ToString());
445-
if (nextIndex == -1) return list;
446-
source = source.Slice(nextIndex);
447-
goto loop;
448-
}
438+
{
439+
Debug.Assert(!source.IsEmpty);
440+
var list = new List<string>();
441+
442+
loop:
443+
var result = source.FirstSplit(splitSequence, out var nextIndex, comparisonType);
444+
if (!result.IsEmpty) list.Add(result.ToString());
445+
if (nextIndex == -1) return list;
446+
source = source.Slice(nextIndex);
447+
goto loop;
448+
}
449449

450450
default:
451-
{
452-
Debug.Assert(!source.IsEmpty);
453-
var list = new List<string>();
454-
loop:
455-
var result = source.FirstSplit(splitSequence, out var nextIndex, comparisonType);
456-
list.Add(result.IsEmpty ? string.Empty : result.ToString());
457-
if (nextIndex == -1) return list;
458-
source = source.Slice(nextIndex);
459-
goto loop;
460-
}
451+
{
452+
Debug.Assert(!source.IsEmpty);
453+
var list = new List<string>();
454+
loop:
455+
var result = source.FirstSplit(splitSequence, out var nextIndex, comparisonType);
456+
list.Add(result.IsEmpty ? string.Empty : result.ToString());
457+
if (nextIndex == -1) return list;
458+
source = source.Slice(nextIndex);
459+
goto loop;
460+
}
461461
}
462462
}
463463
}

Source/Extensions.StringSegment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public static IEnumerable<StringSegment> SplitAsSegments(
391391
? throw new ArgumentNullException(nameof(source))
392392
: pattern is null
393393
? throw new ArgumentNullException(nameof(pattern))
394-
: source.Length==0
394+
: source.Length == 0
395395
? options == StringSplitOptions.RemoveEmptyEntries
396396
? Enumerable.Empty<StringSegment>()
397397
: Enumerable.Repeat(StringSegment.Empty, 1)

Source/Extensions._.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public static string ReplaceWhiteSpace(this string source, string replace = " ")
405405
public const string Newline = "\r\n";
406406

407407
/// <summary>
408-
/// Shortcut for WriteLineNoTabs on a TextWriter. Mimimcs similar classes.
408+
/// Shortcut for WriteLineNoTabs on a TextWriter. Mimics similar classes.
409409
/// </summary>
410410
[ExcludeFromCodeCoverage]
411411
public static void WriteLineNoTabs(this TextWriter writer, string? s = null)

Source/Open.Text.csproj

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

Source/SpanComparable.cs

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
namespace Open.Text;
77

88
/// <summary>
9-
/// A StringComparison variable struct for comparing a ReadOnlySpan&lt;char&gt; against other values.
9+
/// A <see cref="StringComparison"/> struct for comparing a span against other values.
1010
/// </summary>
1111
public readonly ref struct SpanComparable
1212
{
1313
/// <summary>
14-
/// Constructs a SpanComparable using the provided string and comparison type.
14+
/// Constructs a <see cref="SpanComparable"/> using the provided string and comparison type.
1515
/// </summary>
1616
public SpanComparable(ReadOnlySpan<char> source, StringComparison type)
1717
{
@@ -100,62 +100,92 @@ public override int GetHashCode()
100100
#pragma warning restore IDE0079 // Remove unnecessary suppression
101101

102102
/// <summary>
103-
/// Compares two SpanComparables for equality.
103+
/// Compares two <see cref="SpanComparable"/>s for equality.
104104
/// </summary>
105105
public static bool operator ==(SpanComparable a, SpanComparable b) => a.Equals(b);
106106

107107
/// <summary>
108-
/// Compares two SpanComparables for inequality.
108+
/// Compares two <see cref="SpanComparable"/>s for inequality.
109109
/// </summary>
110110
public static bool operator !=(SpanComparable a, SpanComparable b) => !a.Equals(b);
111111

112112
/// <summary>
113-
/// Compares a SpanComparable with a string for equality.
113+
/// Compares a <see cref="SpanComparable"/> and a <see cref="StringSegment"/> for equality.
114114
/// </summary>
115-
public static bool operator ==(SpanComparable a, string? b) => a.Equals(b);
115+
public static bool operator ==(SpanComparable comparable, StringSegment segment) => comparable.Equals(segment);
116116

117117
/// <summary>
118-
/// Compares a SpanComparable with a StringSegment for inequality.
118+
/// Compares a <see cref="SpanComparable"/> and a <see cref="StringSegment"/> for inequality.
119119
/// </summary>
120-
public static bool operator !=(SpanComparable a, StringSegment b) => !a.Equals(b);
120+
public static bool operator !=(SpanComparable comparable, StringSegment segment) => !comparable.Equals(segment);
121121

122122
/// <summary>
123-
/// Compares a SpanComparable with a StringSegment for equality.
123+
/// Compares a <see cref="StringSegment"/> and a <see cref="SpanComparable"/> for equality.
124124
/// </summary>
125-
public static bool operator ==(SpanComparable a, StringSegment b) => a.Equals(b);
125+
public static bool operator ==(StringSegment segment, SpanComparable comparable) => comparable.Equals(segment);
126126

127127
/// <summary>
128-
/// Compares a SpanComparable with a string for inequality.
128+
/// Compares a <see cref="StringSegment"/> and a <see cref="SpanComparable"/> for inequality.
129129
/// </summary>
130-
public static bool operator !=(SpanComparable a, string? b) => !a.Equals(b);
130+
public static bool operator !=(StringSegment segment, SpanComparable comparable) => !comparable.Equals(segment);
131131

132132
/// <summary>
133-
/// Compares a SpanComparable with a span for equality.
133+
/// Compares a <see cref="SpanComparable"/> and a <see cref="string"/> for equality.
134134
/// </summary>
135-
public static bool operator ==(SpanComparable a, ReadOnlySpan<char> b) => a.Equals(b);
135+
public static bool operator ==(SpanComparable comparable, string? str) => comparable.Equals(str);
136136

137137
/// <summary>
138-
/// Compares a SpanComparable with a span for inequality.
138+
/// Compares a <see cref="SpanComparable"/> and a <see cref="string"/> for inequality.
139139
/// </summary>
140-
public static bool operator !=(SpanComparable a, ReadOnlySpan<char> b) => !a.Equals(b);
140+
public static bool operator !=(SpanComparable comparable, string? str) => !comparable.Equals(str);
141141

142142
/// <summary>
143-
/// Compares a SpanComparable with a StringComparable for equality.
143+
/// Compares a <see cref="string"/> and a <see cref="SpanComparable"/> for equality.
144144
/// </summary>
145-
public static bool operator ==(SpanComparable a, StringComparable b) => a.Equals(b);
145+
public static bool operator ==(string? str, SpanComparable comparable) => comparable.Equals(str);
146146

147147
/// <summary>
148-
/// Compares a SpanComparable with a StringComparable for inequality.
148+
/// Compares a <see cref="string"/> and a <see cref="SpanComparable"/> for inequality.
149149
/// </summary>
150-
public static bool operator !=(SpanComparable a, StringComparable b) => !a.Equals(b);
150+
public static bool operator !=(string? str, SpanComparable comparable) => !comparable.Equals(str);
151+
152+
/// <summary>
153+
/// Compares a <see cref="SpanComparable"/> with a span for equality.
154+
/// </summary>
155+
public static bool operator ==(SpanComparable comparable, ReadOnlySpan<char> segment) => comparable.Equals(segment);
156+
157+
/// <summary>
158+
/// Compares a <see cref="SpanComparable"/> with a span for inequality.
159+
/// </summary>
160+
public static bool operator !=(SpanComparable comparable, ReadOnlySpan<char> segment) => !comparable.Equals(segment);
161+
162+
/// <summary>
163+
/// Compares a span with a <see cref="SpanComparable"/> for equality.
164+
/// </summary>
165+
public static bool operator ==(ReadOnlySpan<char> segment, SpanComparable comparable) => comparable.Equals(segment);
166+
167+
/// <summary>
168+
/// Compares a span with a <see cref="SpanComparable"/> for inequality.
169+
/// </summary>
170+
public static bool operator !=(ReadOnlySpan<char> segment, SpanComparable comparable) => !comparable.Equals(segment);
171+
172+
/// <summary>
173+
/// Compares a <see cref="SpanComparable"/> with a <see cref="StringComparable"/> for equality.
174+
/// </summary>
175+
public static bool operator ==(SpanComparable span, StringComparable str) => span.Equals(str);
176+
177+
/// <summary>
178+
/// Compares a <see cref="SpanComparable"/> with a <see cref="StringComparable"/> for inequality.
179+
/// </summary>
180+
public static bool operator !=(SpanComparable span, StringComparable str) => !span.Equals(str);
151181
}
152182

153-
/// <summary>Extensions for SpanComparable.</summary>
183+
/// <summary>Extensions for <see cref="SpanComparable"/>.</summary>
154184
[ExcludeFromCodeCoverage]
155185
public static class SpanComparableExtensions
156186
{
157187
/// <summary>
158-
/// Prepares a span for a specific StringComparison operation.
188+
/// Prepares a span for a specific <see cref="StringComparison"/> operation.
159189
/// </summary>
160190
/// <inheritdoc cref="AsCaseInsensitive(ReadOnlySpan{char})"/>
161191
public static SpanComparable AsComparable(this ReadOnlySpan<char> source, StringComparison type)
@@ -195,11 +225,4 @@ public static SpanComparable AsCurrentCulture(this ReadOnlySpan<char> source)
195225
/// <inheritdoc cref="AsCaseInsensitive(ReadOnlySpan{char})"/>
196226
public static SpanComparable AsInvariantCulture(this ReadOnlySpan<char> source)
197227
=> new(source, StringComparison.InvariantCulture);
198-
199-
/// <summary>
200-
/// Prepares a StringSegment for a specific StringComparison operation.
201-
/// </summary>
202-
/// <inheritdoc cref="AsCaseInsensitive(ReadOnlySpan{char})"/>
203-
public static SpanComparable AsComparable(this StringSegment source, StringComparison type)
204-
=> new(source, type);
205228
}

Source/StringBuilderExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ public static StringBuilder TrimStart(this StringBuilder sb)
494494
return sb;
495495
}
496496

497-
498497
/// <summary>
499498
/// Trims the specified characters from the beginning of the <see cref="StringBuilder"/>.
500499
/// </summary>
@@ -521,7 +520,6 @@ public static StringBuilder TrimStart(this StringBuilder sb, ReadOnlySpan<char>
521520
public static StringBuilder Trim(this StringBuilder sb)
522521
=> TrimEnd(sb).TrimStart();
523522

524-
525523
/// <summary>
526524
/// Trims the specified characters from the beginning and end of the <see cref="StringBuilder"/>.
527525
/// </summary>

0 commit comments

Comments
 (0)