Skip to content

Commit dffb95c

Browse files
Joy-lesslinkdotnet
authored andcommitted
Improve Concat helpers
1 parent 00b9a62 commit dffb95c

File tree

2 files changed

+69
-46
lines changed

2 files changed

+69
-46
lines changed

src/LinkDotNet.StringBuilder/ValueStringBuilder.Concat.Helper.cs

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ namespace LinkDotNet.StringBuilder;
55
public ref partial struct ValueStringBuilder
66
{
77
/// <summary>
8-
/// Concatenates multiple objects together.
8+
/// Concatenates the string representation of multiple objects together.
99
/// </summary>
10-
/// <param name="values">Values to be concatenated together.</param>
11-
/// <typeparam name="T">Any given type, which can be translated to <see cref="string"/>.</typeparam>
12-
/// <returns>Concatenated string or an empty string if <paramref name="values"/> is empty.</returns>
10+
/// <param name="values">Values to be concatenated.</param>
11+
/// <typeparam name="T">Any type for <paramref name="values"/> that can be converted to <see cref="string"/>.</typeparam>
12+
/// <returns>The concatenated string, or an empty string if <paramref name="values"/> is empty.</returns>
1313
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1414
public static string Concat<T>(params scoped ReadOnlySpan<T> values)
1515
{
16-
if (values.Length == 0)
16+
if (values.IsEmpty)
1717
{
1818
return string.Empty;
1919
}
@@ -25,13 +25,28 @@ public static string Concat<T>(params scoped ReadOnlySpan<T> values)
2525
}
2626

2727
/// <summary>
28-
/// Concatenates two different types together.
28+
/// Creates the string representation of an object.
2929
/// </summary>
30-
/// <typeparam name="T1">Typeparameter of <paramref name="arg1"/>.</typeparam>
31-
/// <typeparam name="T2">Typeparameter of <paramref name="arg2"/>.</typeparam>
32-
/// <param name="arg1">First argument.</param>
33-
/// <param name="arg2">Second argument.</param>
34-
/// <returns>String representation of the concateneted result.</returns>
30+
/// <typeparam name="T1">Any type for <paramref name="arg1"/> that can be converted to <see cref="string"/>.</typeparam>
31+
/// <param name="arg1">First value to be concatenated.</param>
32+
/// <returns>The string representation of the object.</returns>
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
public static string Concat<T1>(T1 arg1)
35+
{
36+
using var sb = new ValueStringBuilder(stackalloc char[128]);
37+
sb.AppendInternal(arg1);
38+
39+
return sb.ToString();
40+
}
41+
42+
/// <summary>
43+
/// Concatenates the string representation of two objects together.
44+
/// </summary>
45+
/// <typeparam name="T1">Any type for <paramref name="arg1"/> that can be converted to <see cref="string"/>.</typeparam>
46+
/// <typeparam name="T2">Any type for <paramref name="arg2"/> that can be converted to <see cref="string"/>.</typeparam>
47+
/// <param name="arg1">First value to be concatenated.</param>
48+
/// <param name="arg2">Second value to be concatenated.</param>
49+
/// <returns>The concatenated string.</returns>
3550
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3651
public static string Concat<T1, T2>(T1 arg1, T2 arg2)
3752
{
@@ -43,15 +58,15 @@ public static string Concat<T1, T2>(T1 arg1, T2 arg2)
4358
}
4459

4560
/// <summary>
46-
/// Concatenates two different types together.
61+
/// Concatenates the string representation of three objects together.
4762
/// </summary>
48-
/// <typeparam name="T1">Typeparameter of <paramref name="arg1"/>.</typeparam>
49-
/// <typeparam name="T2">Typeparameter of <paramref name="arg2"/>.</typeparam>
50-
/// <typeparam name="T3">Typeparameter of <paramref name="arg3"/>.</typeparam>
51-
/// <param name="arg1">First argument.</param>
52-
/// <param name="arg2">Second argument.</param>
53-
/// <param name="arg3">Third argument.</param>
54-
/// <returns>String representation of the concateneted result.</returns>
63+
/// <typeparam name="T1">Any type for <paramref name="arg1"/> that can be converted to <see cref="string"/>.</typeparam>
64+
/// <typeparam name="T2">Any type for <paramref name="arg2"/> that can be converted to <see cref="string"/>.</typeparam>
65+
/// <typeparam name="T3">Any type for <paramref name="arg3"/> that can be converted to <see cref="string"/>.</typeparam>
66+
/// <param name="arg1">First value to be concatenated.</param>
67+
/// <param name="arg2">Second value to be concatenated.</param>
68+
/// <param name="arg3">Third value to be concatenated.</param>
69+
/// <returns>The concatenated string.</returns>
5570
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5671
public static string Concat<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3)
5772
{
@@ -64,17 +79,17 @@ public static string Concat<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3)
6479
}
6580

6681
/// <summary>
67-
/// Concatenates two different types together.
82+
/// Concatenates the string representation of four objects together.
6883
/// </summary>
69-
/// <typeparam name="T1">Typeparameter of <paramref name="arg1"/>.</typeparam>
70-
/// <typeparam name="T2">Typeparameter of <paramref name="arg2"/>.</typeparam>
71-
/// <typeparam name="T3">Typeparameter of <paramref name="arg3"/>.</typeparam>
72-
/// <typeparam name="T4">Typeparameter of <paramref name="arg4"/>.</typeparam>
73-
/// <param name="arg1">First argument.</param>
74-
/// <param name="arg2">Second argument.</param>
75-
/// <param name="arg3">Third argument.</param>
76-
/// <param name="arg4">Fourth argument.</param>
77-
/// <returns>String representation of the concateneted result.</returns>
84+
/// <typeparam name="T1">Any type for <paramref name="arg1"/> that can be converted to <see cref="string"/>.</typeparam>
85+
/// <typeparam name="T2">Any type for <paramref name="arg2"/> that can be converted to <see cref="string"/>.</typeparam>
86+
/// <typeparam name="T3">Any type for <paramref name="arg3"/> that can be converted to <see cref="string"/>.</typeparam>
87+
/// <typeparam name="T4">Any type for <paramref name="arg4"/> that can be converted to <see cref="string"/>.</typeparam>
88+
/// <param name="arg1">First value to be concatenated.</param>
89+
/// <param name="arg2">Second value to be concatenated.</param>
90+
/// <param name="arg3">Third value to be concatenated.</param>
91+
/// <param name="arg4">Fourth value to be concatenated.</param>
92+
/// <returns>The concatenated string.</returns>
7893
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7994
public static string Concat<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
8095
{
@@ -88,19 +103,19 @@ public static string Concat<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
88103
}
89104

90105
/// <summary>
91-
/// Concatenates two different types together.
106+
/// Concatenates the string representation of five objects together.
92107
/// </summary>
93-
/// <typeparam name="T1">Typeparameter of <paramref name="arg1"/>.</typeparam>
94-
/// <typeparam name="T2">Typeparameter of <paramref name="arg2"/>.</typeparam>
95-
/// <typeparam name="T3">Typeparameter of <paramref name="arg3"/>.</typeparam>
96-
/// <typeparam name="T4">Typeparameter of <paramref name="arg4"/>.</typeparam>
97-
/// <typeparam name="T5">Typeparameter of <paramref name="arg5"/>.</typeparam>
98-
/// <param name="arg1">First argument.</param>
99-
/// <param name="arg2">Second argument.</param>
100-
/// <param name="arg3">Third argument.</param>
101-
/// <param name="arg4">Fourth argument.</param>
102-
/// <param name="arg5">Fifth argument.</param>
103-
/// <returns>String representation of the concateneted result.</returns>
108+
/// <typeparam name="T1">Any type for <paramref name="arg1"/> that can be converted to <see cref="string"/>.</typeparam>
109+
/// <typeparam name="T2">Any type for <paramref name="arg2"/> that can be converted to <see cref="string"/>.</typeparam>
110+
/// <typeparam name="T3">Any type for <paramref name="arg3"/> that can be converted to <see cref="string"/>.</typeparam>
111+
/// <typeparam name="T4">Any type for <paramref name="arg4"/> that can be converted to <see cref="string"/>.</typeparam>
112+
/// <typeparam name="T5">Any type for <paramref name="arg5"/> that can be converted to <see cref="string"/>.</typeparam>
113+
/// <param name="arg1">First value to be concatenated.</param>
114+
/// <param name="arg2">Second value to be concatenated.</param>
115+
/// <param name="arg3">Third value to be concatenated.</param>
116+
/// <param name="arg4">Fourth value to be concatenated.</param>
117+
/// <param name="arg5">Fifth value to be concatenated.</param>
118+
/// <returns>The concatenated string.</returns>
104119
[MethodImpl(MethodImplOptions.AggressiveInlining)]
105120
public static string Concat<T1, T2, T3, T4, T5>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
106121
{

tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace LinkDotNet.StringBuilder.UnitTests;
1+
namespace LinkDotNet.StringBuilder.UnitTests;
22

33
public class ValueStringBuilderTests
44
{
@@ -363,31 +363,39 @@ public void ShouldConcatStringsTogether()
363363
}
364364

365365
[Fact]
366-
public void ConcatDifferentTypesWithTwoArguments()
366+
public void ShouldConcatOneObjectTogether()
367+
{
368+
var result = ValueStringBuilder.Concat(1);
369+
370+
result.ShouldBe("1");
371+
}
372+
373+
[Fact]
374+
public void ShouldConcatTwoObjectsTogether()
367375
{
368376
var result = ValueStringBuilder.Concat("Test", 1);
369377

370378
result.ShouldBe("Test1");
371379
}
372380

373381
[Fact]
374-
public void ConcatDifferentTypesWithThreeArguments()
382+
public void ShouldConcatThreeObjectsTogether()
375383
{
376384
var result = ValueStringBuilder.Concat("Test", 1, 2);
377385

378386
result.ShouldBe("Test12");
379387
}
380388

381389
[Fact]
382-
public void ConcatDifferentTypesWithFourArguments()
390+
public void ShouldConcatFourObjectsTogether()
383391
{
384392
var result = ValueStringBuilder.Concat("Test", 1, 2, 3);
385393

386394
result.ShouldBe("Test123");
387395
}
388396

389397
[Fact]
390-
public void ConcatDifferentTypesWithFiveArguments()
398+
public void ShouldConcatFiveObjectsTogether()
391399
{
392400
var result = ValueStringBuilder.Concat("Test", 1, 2, 3, 4);
393401

0 commit comments

Comments
 (0)