Skip to content

Commit d5f1841

Browse files
feat:
+ add InterpolatedStringHandler
1 parent 42ccfc0 commit d5f1841

File tree

4 files changed

+208
-4
lines changed

4 files changed

+208
-4
lines changed

Src/StringBuilderArray.Tests/AppendFixture.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ public void AppendTest(int length)
1919
for (int i = 0; i < 53; i++)
2020
{
2121
sb.Append(i.ToString() + strBuilded);
22+
#if NET6_0_OR_GREATER
23+
sb.Append($"Interpool {i}");
24+
#endif
2225
}
2326

2427
var sbArr = new StringBuilderArray.StringBuilderArray();
2528
for (int i = 0; i < 53; i++)
2629
{
2730
sbArr.Append(i.ToString() + strBuilded);
31+
#if NET6_0_OR_GREATER
32+
sbArr.Append($"Interpool {i}");
33+
#endif
2834
}
2935

3036
#if NET5_0_OR_GREATER
@@ -50,17 +56,26 @@ public void AppendClearTest(int length)
5056
for (int i = 53; i < 70; i++)
5157
{
5258
sb.Append(i.ToString() + strBuilded);
59+
#if NET6_0_OR_GREATER
60+
sb.Append($"Interpool {i}");
61+
#endif
5362
}
5463

5564
var sbArr = new StringBuilderArray.StringBuilderArray();
5665
for (int i = 0; i < 53; i++)
5766
{
5867
sbArr.Append(i.ToString() + strBuilded);
68+
#if NET6_0_OR_GREATER
69+
sbArr.Append($"Interpool {i}");
70+
#endif
5971
}
6072
sbArr.Clear();
6173
for (int i = 53; i < 70; i++)
6274
{
6375
sbArr.Append(i.ToString() + strBuilded);
76+
#if NET6_0_OR_GREATER
77+
sbArr.Append($"Interpool {i}");
78+
#endif
6479
}
6580

6681
#if NET5_0_OR_GREATER
@@ -87,12 +102,19 @@ public void AppendEnumerableTest(int length)
87102
{
88103
var text = i.ToString() + strBuilded;
89104
list.Add(text);
105+
#if NET6_0_OR_GREATER
106+
list.Add("Interpool ");
107+
list.Add(i.ToString());
108+
#endif
90109
}
91110

92111
var sbArr = new StringBuilderArray.StringBuilderArray();
93112
for (int i = 0; i < 53; i++)
94113
{
95114
sbArr.Append(i.ToString() + strBuilded);
115+
#if NET6_0_OR_GREATER
116+
sbArr.Append($"Interpool {i}");
117+
#endif
96118
}
97119

98120
var index = 0;

Src/StringBuilderArray.Tests/AppendLineFixture.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ public void AppendLineTest(int length)
1919
for (int i = 0; i < 53; i++)
2020
{
2121
sb.AppendLine(i.ToString() + strBuilded);
22+
#if NET6_0_OR_GREATER
23+
sb.Append($"Interpool {i}");
24+
#endif
2225
}
2326

2427
var sbArr = new StringBuilderArray.StringBuilderArray();
2528
for (int i = 0; i < 53; i++)
2629
{
2730
sbArr.AppendLine(i.ToString() + strBuilded);
31+
#if NET6_0_OR_GREATER
32+
sbArr.Append($"Interpool {i}");
33+
#endif
2834
}
2935

3036
#if NET5_0_OR_GREATER
@@ -50,17 +56,26 @@ public void AppendLineClearTest(int length)
5056
for (int i = 53; i < 70; i++)
5157
{
5258
sb.AppendLine(i.ToString() + strBuilded);
59+
#if NET6_0_OR_GREATER
60+
sb.Append($"Interpool {i}");
61+
#endif
5362
}
5463

5564
var sbArr = new StringBuilderArray.StringBuilderArray();
5665
for (int i = 0; i < 53; i++)
5766
{
5867
sbArr.AppendLine(i.ToString() + strBuilded);
68+
#if NET6_0_OR_GREATER
69+
sbArr.Append($"Interpool {i}");
70+
#endif
5971
}
6072
sbArr.Clear();
6173
for (int i = 53; i < 70; i++)
6274
{
6375
sbArr.AppendLine(i.ToString() + strBuilded);
76+
#if NET6_0_OR_GREATER
77+
sbArr.Append($"Interpool {i}");
78+
#endif
6479
}
6580

6681
#if NET5_0_OR_GREATER
@@ -88,12 +103,20 @@ public void AppendLineEnumerableTest(int length)
88103
var text = i.ToString() + strBuilded;
89104
list.Add(text);
90105
list.Add(Environment.NewLine);
106+
#if NET6_0_OR_GREATER
107+
list.Add("Interpool ");
108+
list.Add(i.ToString());
109+
list.Add(Environment.NewLine);
110+
#endif
91111
}
92112

93113
var sbArr = new StringBuilderArray.StringBuilderArray();
94114
for (int i = 0; i < 53; i++)
95115
{
96116
sbArr.AppendLine(i.ToString() + strBuilded);
117+
#if NET6_0_OR_GREATER
118+
sbArr.AppendLine($"Interpool {i}");
119+
#endif
97120
}
98121

99122
var index = 0;

Src/StringBuilderArray/StringBuilderArray.cs

Lines changed: 162 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
#if NET6_0_OR_GREATER
5+
using System.ComponentModel;
6+
using System.Diagnostics;
7+
using System.Globalization;
8+
#endif
49
using System.Runtime.CompilerServices;
510

611
namespace StringBuilderArray
@@ -31,7 +36,6 @@ private StringBuilderArray()
3136

3237
public int Length
3338
{
34-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3539
get
3640
{
3741
var result = 0;
@@ -50,7 +54,6 @@ public int Length
5054
}
5155
}
5256

53-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5457
public StringBuilderArray Append(string str)
5558
{
5659
if (_size == _buffer.Length)
@@ -127,7 +130,11 @@ public StringBuilderArray AppendLine(string str)
127130
return Append(Environment.NewLine);
128131
}
129132

130-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
133+
public StringBuilderArray AppendLine()
134+
{
135+
return Append(Environment.NewLine);
136+
}
137+
131138
public StringBuilderArray Append(IList<string> strings)
132139
{
133140
foreach (var str in strings)
@@ -499,5 +506,157 @@ public void Reset()
499506
}
500507

501508
#endregion
509+
510+
#if NET6_0_OR_GREATER
511+
512+
public StringBuilderArray Append([InterpolatedStringHandlerArgument("")] ref StringBuilderArrayStringHandler handler) => this;
513+
514+
public StringBuilderArray Append(IFormatProvider provider, [InterpolatedStringHandlerArgument("", "provider")] ref StringBuilderArrayStringHandler handler) => this;
515+
516+
public StringBuilderArray AppendLine([InterpolatedStringHandlerArgument("")] ref StringBuilderArrayStringHandler handler) => AppendLine();
517+
518+
public StringBuilderArray AppendLine(IFormatProvider provider, [InterpolatedStringHandlerArgument("", "provider")] ref StringBuilderArrayStringHandler handler) => AppendLine();
519+
520+
[EditorBrowsable(EditorBrowsableState.Never)]
521+
[InterpolatedStringHandler]
522+
public struct StringBuilderArrayStringHandler
523+
{
524+
private static readonly string _whitespace = " ";
525+
526+
private readonly StringBuilderArray _stringBuilder;
527+
private readonly IFormatProvider _provider;
528+
private readonly bool _hasCustomFormatter;
529+
530+
public StringBuilderArrayStringHandler(int literalLength, int formattedCount, StringBuilderArray stringBuilder)
531+
{
532+
_stringBuilder = stringBuilder;
533+
_provider = null;
534+
_hasCustomFormatter = false;
535+
}
536+
537+
public StringBuilderArrayStringHandler(int literalLength, int formattedCount, StringBuilderArray stringBuilder, IFormatProvider provider)
538+
{
539+
_stringBuilder = stringBuilder;
540+
_provider = provider;
541+
_hasCustomFormatter = provider is not null && HasCustomFormatter(provider);
542+
}
543+
544+
public void AppendLiteral(string value) => _stringBuilder.Append(value);
545+
546+
#region AppendFormatted
547+
548+
#region AppendFormatted T
549+
public void AppendFormatted<T>(T value)
550+
{
551+
if (_hasCustomFormatter)
552+
{
553+
AppendCustomFormatter(value, format: null);
554+
}
555+
else if (value is IFormattable)
556+
{
557+
_stringBuilder.Append(((IFormattable)value).ToString(format: null, _provider));
558+
}
559+
else if (value is not null)
560+
{
561+
_stringBuilder.Append(value.ToString());
562+
}
563+
}
564+
565+
public void AppendFormatted<T>(T value, string format)
566+
{
567+
if (_hasCustomFormatter)
568+
{
569+
AppendCustomFormatter(value, format);
570+
}
571+
else if (value is IFormattable)
572+
{
573+
_stringBuilder.Append(((IFormattable)value).ToString(format, _provider));
574+
}
575+
else if (value is not null)
576+
{
577+
_stringBuilder.Append(value.ToString());
578+
}
579+
}
580+
581+
public void AppendFormatted<T>(T value, int alignment) => AppendFormatted(value, alignment, format: null);
582+
583+
public void AppendFormatted<T>(T value, int alignment, string format)
584+
{
585+
if (alignment == 0)
586+
{
587+
AppendFormatted(value, format);
588+
}
589+
else if (alignment < 0)
590+
{
591+
int start = _stringBuilder.Length;
592+
AppendFormatted(value, format);
593+
int paddingRequired = -alignment - (_stringBuilder.Length - start);
594+
if (paddingRequired > 0)
595+
{
596+
for (int i = 0; i < paddingRequired; i++)
597+
{
598+
_stringBuilder.Append(_whitespace);
599+
}
600+
}
601+
}
602+
else
603+
{
604+
for (int i = 0; i < alignment; i++)
605+
{
606+
_stringBuilder.Append(_whitespace);
607+
}
608+
AppendFormatted(value, format);
609+
}
610+
}
611+
#endregion
612+
613+
#region AppendFormatted string
614+
public void AppendFormatted(string value)
615+
{
616+
if (!_hasCustomFormatter)
617+
{
618+
_stringBuilder.Append(value);
619+
}
620+
else
621+
{
622+
AppendFormatted<string>(value);
623+
}
624+
}
625+
626+
public void AppendFormatted(string value, int alignment = 0, string format = null) => AppendFormatted<string>(value, alignment, format);
627+
#endregion
628+
629+
#region AppendFormatted object
630+
public void AppendFormatted(object value, int alignment = 0, string format = null) => AppendFormatted<object>(value, alignment, format);
631+
#endregion
632+
633+
#endregion
634+
635+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
636+
private void AppendCustomFormatter<T>(T value, string format)
637+
{
638+
Debug.Assert(_hasCustomFormatter);
639+
Debug.Assert(_provider != null);
640+
641+
ICustomFormatter formatter = (ICustomFormatter)_provider.GetFormat(typeof(ICustomFormatter));
642+
Debug.Assert(formatter != null, "An incorrectly written provider said it implemented ICustomFormatter, and then didn't");
643+
644+
if (formatter is not null)
645+
{
646+
_stringBuilder.Append(formatter.Format(format, value, _provider));
647+
}
648+
}
649+
650+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
651+
internal static bool HasCustomFormatter(IFormatProvider provider)
652+
{
653+
Debug.Assert(provider is not null);
654+
Debug.Assert(provider is not CultureInfo || provider.GetFormat(typeof(ICustomFormatter)) is null, "Expected CultureInfo to not provide a custom formatter");
655+
return
656+
provider.GetType() != typeof(CultureInfo) &&
657+
provider.GetFormat(typeof(ICustomFormatter)) != null;
658+
}
659+
}
660+
#endif
502661
}
503662
}

Src/StringBuilderArray/StringBuilderArray.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66
<PropertyGroup>
77
<PackageId>StringBuilderArray</PackageId>
8-
<PackageVersion>1.3.0.0</PackageVersion>
8+
<PackageVersion>1.3.5.0</PackageVersion>
99
<Authors>Brevnov Vyacheslav Sergeevich</Authors>
1010
<RepositoryUrl>https://github.com/SoftStoneDevelop/StringBuilderArray</RepositoryUrl>
1111
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>

0 commit comments

Comments
 (0)