Skip to content

Commit

Permalink
Add StringBuilder.AppendJoin
Browse files Browse the repository at this point in the history
Fix #24
  • Loading branch information
meziantou committed Aug 12, 2023
1 parent 3717577 commit 61e22fd
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text;

static partial class PolyfillExtensions
{
public static StringBuilder AppendJoin(this StringBuilder target, char separator, params object?[] values)
{
var first = true;
foreach (var value in values)
{
if (!first)
{
target.Append(separator);
}

target.Append(value);
first = false;
}

return target;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text;

static partial class PolyfillExtensions
{
public static StringBuilder AppendJoin(this StringBuilder target, char separator, params string?[] values)
{
var first = true;
foreach (var value in values)
{
if (!first)
{
target.Append(separator);
}

target.Append(value);
first = false;
}

return target;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text;

static partial class PolyfillExtensions
{
public static StringBuilder AppendJoin(this StringBuilder target, string? separator, params object?[] values)
{
var first = true;
foreach (var value in values)
{
if (!first)
{
target.Append(separator);
}

target.Append(value);
first = false;
}

return target;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text;

static partial class PolyfillExtensions
{
public static StringBuilder AppendJoin(this StringBuilder target, string? separator, params string?[] values)
{
var first = true;
foreach (var value in values)
{
if (!first)
{
target.Append(separator);
}

target.Append(value);
first = false;
}

return target;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Text;

static partial class PolyfillExtensions
{
public static StringBuilder AppendJoin<T>(this StringBuilder target, char separator, IEnumerable<T> values)
{
var first = true;
foreach (var value in values)
{
if (!first)
{
target.Append(separator);
}

target.Append(value);
first = false;
}

return target;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Text;

static partial class PolyfillExtensions
{
public static StringBuilder AppendJoin<T>(this StringBuilder target, string? separator, IEnumerable<T> values)
{
var first = true;
foreach (var value in values)
{
if (!first)
{
target.Append(separator);
}

target.Append(value);
first = false;
}

return target;
}
}
3 changes: 3 additions & 0 deletions Meziantou.Polyfill.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
.Replace('}', '_')
.Replace('(', '_')
.Replace(')', '_')
.Replace('[', '_')
.Replace(']', '_')
.Replace(',', '_')
.Replace('.', '_')
.Replace('`', '_'),
Expand Down Expand Up @@ -193,6 +195,7 @@ string GenerateIncludePostCondition(PolyfillData data)
sb.AppendLine("}");

Console.WriteLine(sb.ToString());
Console.WriteLine("Polyfills: " + polyfills.Length);
var path = GetMemberFilePath();
Console.WriteLine(path);
File.WriteAllText(path, sb.ToString());
Expand Down
62 changes: 62 additions & 0 deletions Meziantou.Polyfill.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,66 @@ public void Encoding_GetString()
var str = Encoding.UTF8.GetString((ReadOnlySpan<byte>)Encoding.UTF8.GetBytes("sample").AsSpan());
Assert.Equal("sample", str);
}

[Fact]
public void StringBuilder_AppendJoin_String_ObjectArray()
{
var sb = new StringBuilder();
sb.AppendJoin(", ", 1, 2, 3);
Assert.Equal("1, 2, 3", sb.ToString());
}

[Fact]
public void StringBuilder_AppendJoin_String_StringArray()
{
var sb = new StringBuilder();
sb.AppendJoin(", ", "1", "2", "3");
Assert.Equal("1, 2, 3", sb.ToString());
}

[Fact]
public void StringBuilder_AppendJoin_String_IEnumerable()
{
var sb = new StringBuilder();
sb.AppendJoin(", ", Enumerable());
Assert.Equal("1, 2, 3", sb.ToString());

static IEnumerable<string> Enumerable()
{
yield return "1";
yield return "2";
yield return "3";
}
}

[Fact]
public void StringBuilder_AppendJoin_Char_ObjectArray()
{
var sb = new StringBuilder();
sb.AppendJoin(',', 1, 2, 3);
Assert.Equal("1,2,3", sb.ToString());
}

[Fact]
public void StringBuilder_AppendJoin_Char_StringArray()
{
var sb = new StringBuilder();
sb.AppendJoin(',', "1", "2", "3");
Assert.Equal("1,2,3", sb.ToString());
}

[Fact]
public void StringBuilder_AppendJoin_Char_IEnumerable()
{
var sb = new StringBuilder();
sb.AppendJoin(',', Enumerable());
Assert.Equal("1,2,3", sb.ToString());

static IEnumerable<string> Enumerable()
{
yield return "1";
yield return "2";
yield return "3";
}
}
}
2 changes: 1 addition & 1 deletion Meziantou.Polyfill/Meziantou.Polyfill.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>1.0.25</Version>
<Version>1.0.26</Version>
<TransformOnBuild>true</TransformOnBuild>

<LangVersion>11</LangVersion>
Expand Down

0 comments on commit 61e22fd

Please sign in to comment.