Skip to content

Commit 798df97

Browse files
Merge pull request #2 from Open-NET-Libraries/dev
V3 update
2 parents 15c6721 + 551d6f6 commit 798df97

110 files changed

Lines changed: 4756 additions & 875 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
# Open.Text
22

3-
A set of useful classes for working with strings and formatting values.
3+
A set of useful classes for working with strings, spans, and value formatting.
44

55
[![NuGet](https://img.shields.io/nuget/v/Open.Text.svg)](https://www.nuget.org/packages/Open.Text/)
6+
7+
## Features
8+
9+
* v3.x is a major overhaul with much improved methods and expanded tests and coverage.
10+
* Avoids allocation wherever possible.
11+
12+
### String vs Span Equality
13+
14+
Optimized `.Equals(...)` extension methods for comparing spans and strings.
15+
16+
### String & Span Splitting
17+
18+
#### `SplitToEnumerable`
19+
20+
Returns each string segment of the split through an enumerable instead of all at once in an array.
21+
22+
#### `SplitToMemory`
23+
24+
Produces an enumerable where each segment is yielded as a `ReadOnlyMemory<char>`.
25+
26+
### Trimming
27+
28+
#### `TrimStartPattern` & `TrimEndPattern`
29+
30+
Similar to their character trimming counterparts, these methods can trim sequences of characters or regular expression patterns.
31+
32+
### String Segments
33+
34+
Similar to `ArraySegment`, `StringSegment` offers methods for operating on strings without requiring allocation.
35+
36+
Instead of extensions like `string.BeforeFirst(search)`, now you can call `string.First(search).Preceding()`.
37+
38+
### StringBuilder Extensions
39+
40+
* Extensions for adding segments with separators.
41+
* Extensions for adding spans without creating a string first.
42+
* Extensions for converting enumerables to a `StringBuilder`.
43+
44+
### ... And more
45+
46+
Various formatting and `Regex` extensions including `Capture.AsSpan()` for getting a `ReadOnlySpan<char>` instead of allocating a string.
47+

Source/Extensions.Equals.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
3+
namespace Open.Text
4+
{
5+
public static partial class Extensions
6+
{
7+
8+
/// <summary>
9+
/// Optimized equals for comparing as span vs a string.
10+
/// </summary>
11+
/// <param name="source">The source span.</param>
12+
/// <param name="other">The string to compare to.</param>
13+
/// <param name="stringComparison">The string comparison type.</param>
14+
/// <returns>True if the are contents equal.</returns>
15+
public static bool Equals(this in ReadOnlySpan<char> source, string? other, StringComparison stringComparison)
16+
{
17+
if (other is null) return false;
18+
var len = source.Length;
19+
if (len != other.Length) return false;
20+
return len switch
21+
{
22+
0 => true,
23+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
24+
_ => source.Equals(other.AsSpan(), stringComparison),
25+
};
26+
}
27+
28+
/// <summary>
29+
/// Optimized equals for comparing as span vs a string.
30+
/// </summary>
31+
/// <param name="source">The source span.</param>
32+
/// <param name="other">The string to compare to.</param>
33+
/// <param name="stringComparison">The string comparison type.</param>
34+
/// <returns>True if the are contents equal.</returns>
35+
public static bool Equals(this in Span<char> source, string? other, StringComparison stringComparison)
36+
{
37+
if (other is null) return false;
38+
var len = source.Length;
39+
if (len != other.Length) return false;
40+
return len switch
41+
{
42+
0 => true,
43+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
44+
_ => MemoryExtensions.Equals(source, other.AsSpan(), stringComparison)
45+
};
46+
}
47+
48+
/// <summary>
49+
/// Optimized equals for comparing spans.
50+
/// </summary>
51+
/// <param name="source">The source span.</param>
52+
/// <param name="other">The span to compare to.</param>
53+
/// <param name="stringComparison">The string comparison type.</param>
54+
/// <returns>True if the are contents equal.</returns>
55+
public static bool Equals(this in Span<char> source, in Span<char> other, StringComparison stringComparison)
56+
{
57+
var len = source.Length;
58+
if (len != other.Length) return false;
59+
return len switch
60+
{
61+
0 => true,
62+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
63+
_ => MemoryExtensions.Equals(source, other, stringComparison)
64+
};
65+
}
66+
67+
/// <summary>
68+
/// Optimized equals for comparing spans.
69+
/// </summary>
70+
/// <param name="source">The source span.</param>
71+
/// <param name="other">The span to compare to.</param>
72+
/// <param name="stringComparison">The string comparison type.</param>
73+
/// <returns>True if the are contents equal.</returns>
74+
public static bool Equals(this in Span<char> source, in ReadOnlySpan<char> other, StringComparison stringComparison)
75+
{
76+
var len = source.Length;
77+
if (len != other.Length) return false;
78+
return len switch
79+
{
80+
0 => true,
81+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
82+
_ => MemoryExtensions.Equals(source, other, stringComparison)
83+
};
84+
}
85+
86+
/// <summary>
87+
/// Optimized equals for comparing as string to a span.
88+
/// </summary>
89+
/// <param name="source">The source string.</param>
90+
/// <param name="other">The span to compare to.</param>
91+
/// <param name="stringComparison">The string comparison type.</param>
92+
/// <returns>True if the are contents equal.</returns>
93+
public static bool Equals(this string source, in ReadOnlySpan<char> other, StringComparison stringComparison)
94+
{
95+
if (source is null)
96+
throw new ArgumentNullException(nameof(source));
97+
var len = source.Length;
98+
if (len != other.Length) return false;
99+
return len switch
100+
{
101+
0 => true,
102+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
103+
_ => source.AsSpan().Equals(other, stringComparison),
104+
};
105+
}
106+
107+
/// <summary>
108+
/// Optimized equals for comparing as string to a span.
109+
/// </summary>
110+
/// <param name="source">The source string.</param>
111+
/// <param name="other">The span to compare to.</param>
112+
/// <param name="stringComparison">The string comparison type.</param>
113+
/// <returns>True if the are contents equal.</returns>
114+
public static bool Equals(this string source, in Span<char> other, StringComparison stringComparison)
115+
{
116+
if (source is null) throw new ArgumentNullException(nameof(source));
117+
var len = source.Length;
118+
if (len != other.Length) return false;
119+
return len switch
120+
{
121+
0 => true,
122+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
123+
_ => source.AsSpan().Equals(other, stringComparison),
124+
};
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)