Skip to content

Commit 64e7073

Browse files
author
Oren (electricessence)
committed
Testing and more assertions.
1 parent 4de94d2 commit 64e7073

8 files changed

Lines changed: 366 additions & 136 deletions

File tree

Source/.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[*.cs]
2+
3+
# CA1303: Do not pass literals as localized parameters
4+
dotnet_diagnostic.CA1303.severity = silent
5+
6+
# CA1707: Identifiers should not contain underscores
7+
dotnet_diagnostic.CA1707.severity = silent
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Part of the "Open" set of libraries.
1515
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Text</RepositoryUrl>
1616
<RepositoryType>git</RepositoryType>
1717
<PackageTags>dotnet, dotnetcore, string, text, format, stringbuilder, extensions</PackageTags>
18-
<Version>2.2.0</Version>
18+
<Version>2.3.0</Version>
1919
<PackageReleaseNotes></PackageReleaseNotes>
2020
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2121
<PublishRepositoryUrl>true</PublishRepositoryUrl>
@@ -29,8 +29,10 @@ Part of the "Open" set of libraries.
2929
</ItemGroup>
3030

3131
<ItemGroup>
32+
<None Remove=".editorconfig" />
3233
<None Remove=".git" />
3334
<None Remove=".gitignore" />
35+
<AdditionalFiles Include=".editorconfig" />
3436
<None Include="logo.png">
3537
<Pack>True</Pack>
3638
<PackagePath></PackagePath>
@@ -39,6 +41,10 @@ Part of the "Open" set of libraries.
3941

4042
<ItemGroup>
4143
<PackageReference Include="Open.MemoryExtensions" Version="2.7.0" />
42-
</ItemGroup>
44+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
45+
<PrivateAssets>all</PrivateAssets>
46+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
47+
</PackageReference>
48+
</ItemGroup>
4349

4450
</Project>
Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using System;
1+
/*!
2+
* @author electricessence / https://github.com/electricessence/
3+
* Licensing: MIT
4+
*/
5+
6+
using System;
27
using System.Collections.Generic;
38
using System.Diagnostics.Contracts;
49
using System.Text;
@@ -7,6 +12,14 @@ namespace Open.Text
712
{
813
public static class StringBuilderExtensions
914
{
15+
private const string ParametersMissing = "Parameters missing.";
16+
17+
/// <summary>
18+
/// Adds every entry to a StringBuilder.
19+
/// </summary>
20+
/// <typeparam name="T">The type of the source.</typeparam>
21+
/// <param name="source">The source span.</param>
22+
/// <returns>The resultant StringBuilder.</returns>
1023
public static StringBuilder ToStringBuilder<T>(this in ReadOnlySpan<T> source)
1124
{
1225
var len = source.Length;
@@ -18,15 +31,29 @@ public static StringBuilder ToStringBuilder<T>(this in ReadOnlySpan<T> source)
1831
return sb;
1932
}
2033

34+
/// <summary>
35+
/// Adds every entry to a StringBuilder.
36+
/// </summary>
37+
/// <typeparam name="T">The type of the source.</typeparam>
38+
/// <param name="source">The source span.</param>
39+
/// <returns>The resultant StringBuilder.</returns>
2140
public static StringBuilder ToStringBuilder<T>(this IEnumerable<T> source)
2241
{
42+
if (source is null) throw new NullReferenceException();
2343
var sb = new StringBuilder();
2444
foreach (var s in source)
2545
sb.Append(s);
2646

2747
return sb;
2848
}
2949

50+
/// <summary>
51+
/// Adds every entry to a StringBuilder separated by the specified sequence.
52+
/// </summary>
53+
/// <typeparam name="T">The type of the source.</typeparam>
54+
/// <param name="source">The source span.</param>
55+
/// <param name="separator">The separator sequence.</param>
56+
/// <returns>The resultant StringBuilder.</returns>
3057
public static StringBuilder ToStringBuilder<T>(this in ReadOnlySpan<T> source, string separator)
3158
{
3259
var len = source.Length;
@@ -45,11 +72,17 @@ public static StringBuilder ToStringBuilder<T>(this in ReadOnlySpan<T> source, s
4572
return sb;
4673
}
4774

75+
/// <summary>
76+
/// Adds every entry to a StringBuilder separated by the specified character.
77+
/// </summary>
78+
/// <typeparam name="T">The type of the source.</typeparam>
79+
/// <param name="source">The source span.</param>
80+
/// <param name="separator">The separator character.</param>
81+
/// <returns>The resultant StringBuilder.</returns>
4882
public static StringBuilder ToStringBuilder<T>(this in ReadOnlySpan<T> source, in char separator)
4983
{
5084
var len = source.Length;
51-
if (len < 2)
52-
return ToStringBuilder(source);
85+
if (len < 2) return ToStringBuilder(source);
5386

5487
var sb = new StringBuilder(2 * len - 1);
5588

@@ -63,10 +96,23 @@ public static StringBuilder ToStringBuilder<T>(this in ReadOnlySpan<T> source, i
6396
return sb;
6497
}
6598

99+
/// <summary>
100+
/// Adds every entry to a StringBuilder separated by the specified sequence.
101+
/// </summary>
102+
/// <typeparam name="T">The type of the source.</typeparam>
103+
/// <param name="source">The source enumerable.</param>
104+
/// <param name="separator">The separator sequence.</param>
105+
/// <returns>The resultant StringBuilder.</returns>
66106
public static StringBuilder ToStringBuilder<T>(this IEnumerable<T> source, string separator)
67107
{
68-
var sb = new StringBuilder();
108+
if (source is null) throw new NullReferenceException();
109+
Contract.EndContractBlock();
110+
111+
if (string.IsNullOrEmpty(separator))
112+
return ToStringBuilder(source);
113+
69114
var first = true;
115+
var sb = new StringBuilder();
70116
foreach (var s in source)
71117
{
72118
if (first) first = false;
@@ -76,8 +122,18 @@ public static StringBuilder ToStringBuilder<T>(this IEnumerable<T> source, strin
76122
return sb;
77123
}
78124

125+
/// <summary>
126+
/// Adds every entry to a StringBuilder separated by the specified character.
127+
/// </summary>
128+
/// <typeparam name="T">The type of the source.</typeparam>
129+
/// <param name="source">The source enumerable.</param>
130+
/// <param name="separator">The separator character.</param>
131+
/// <returns>The resultant StringBuilder.</returns>
79132
public static StringBuilder ToStringBuilder<T>(this IEnumerable<T> source, in char separator)
80133
{
134+
if (source is null) throw new NullReferenceException();
135+
Contract.EndContractBlock();
136+
81137
var sb = new StringBuilder();
82138
var first = true;
83139
foreach (var s in source)
@@ -139,7 +195,7 @@ public static StringBuilder AppendWithSeparator<T>(this StringBuilder target, st
139195
if (target is null)
140196
throw new NullReferenceException();
141197
if (values is null || values.Length == 0)
142-
throw new ArgumentException("Parameters missing.");
198+
throw new ArgumentException(ParametersMissing);
143199
Contract.EndContractBlock();
144200

145201
if (!string.IsNullOrEmpty(separator) && target.Length != 0)
@@ -157,7 +213,7 @@ public static StringBuilder AppendWithSeparator<T>(this StringBuilder target, in
157213
if (target is null)
158214
throw new NullReferenceException();
159215
if (values is null || values.Length == 0)
160-
throw new ArgumentException("Parameters missing.");
216+
throw new ArgumentException(ParametersMissing);
161217
Contract.EndContractBlock();
162218

163219
if (target.Length != 0)

0 commit comments

Comments
 (0)