Skip to content

Commit d9f54fe

Browse files
committed
refactor: Don't write char for char
1 parent e9ee3ba commit d9f54fe

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
### Changed
10+
- Refactored `AppendFormat` to be faster especially for longer text.
11+
912
## [1.11] - 2023-01-01
1013

1114
### Added

src/LinkDotNet.StringBuilder/ValueStringBuilder.AppendFormat.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void AppendFormat<T>(
1919
T arg)
2020
{
2121
var formatIndex = 0;
22+
var start = 0;
2223
while (formatIndex < format.Length)
2324
{
2425
var c = format[formatIndex];
@@ -31,19 +32,29 @@ public void AppendFormat<T>(
3132
return;
3233
}
3334

35+
if (start != formatIndex)
36+
{
37+
Append(format[start..formatIndex]);
38+
}
39+
3440
var placeholder = format.Slice(formatIndex, endIndex + 2);
3541

3642
GetValidArgumentIndex(placeholder, 0);
3743

3844
AppendInternal(arg);
3945
formatIndex += endIndex + 2;
46+
start = formatIndex;
4047
}
4148
else
4249
{
43-
Append(c);
4450
formatIndex++;
4551
}
4652
}
53+
54+
if (start != formatIndex)
55+
{
56+
Append(format[start..formatIndex]);
57+
}
4758
}
4859

4960
/// <summary>
@@ -64,6 +75,7 @@ public void AppendFormat<T1, T2>(
6475
T2 arg2)
6576
{
6677
var formatIndex = 0;
78+
var start = 0;
6779
while (formatIndex < format.Length)
6880
{
6981
var c = format[formatIndex];
@@ -76,6 +88,11 @@ public void AppendFormat<T1, T2>(
7688
return;
7789
}
7890

91+
if (start != formatIndex)
92+
{
93+
Append(format[start..formatIndex]);
94+
}
95+
7996
var placeholder = format.Slice(formatIndex, endIndex + 2);
8097

8198
var index = GetValidArgumentIndex(placeholder, 1);
@@ -91,13 +108,18 @@ public void AppendFormat<T1, T2>(
91108
}
92109

93110
formatIndex += endIndex + 2;
111+
start = formatIndex;
94112
}
95113
else
96114
{
97-
Append(c);
98115
formatIndex++;
99116
}
100117
}
118+
119+
if (start != formatIndex)
120+
{
121+
Append(format[start..formatIndex]);
122+
}
101123
}
102124

103125
/// <summary>
@@ -121,6 +143,7 @@ public void AppendFormat<T1, T2, T3>(
121143
T3 arg3)
122144
{
123145
var formatIndex = 0;
146+
var start = 0;
124147
while (formatIndex < format.Length)
125148
{
126149
var c = format[formatIndex];
@@ -133,6 +156,11 @@ public void AppendFormat<T1, T2, T3>(
133156
return;
134157
}
135158

159+
if (start != formatIndex)
160+
{
161+
Append(format[start..formatIndex]);
162+
}
163+
136164
var placeholder = format.Slice(formatIndex, endIndex + 2);
137165

138166
var index = GetValidArgumentIndex(placeholder, 2);
@@ -151,13 +179,18 @@ public void AppendFormat<T1, T2, T3>(
151179
}
152180

153181
formatIndex += endIndex + 2;
182+
start = formatIndex;
154183
}
155184
else
156185
{
157-
Append(c);
158186
formatIndex++;
159187
}
160188
}
189+
190+
if (start != formatIndex)
191+
{
192+
Append(format[start..formatIndex]);
193+
}
161194
}
162195

163196
private static int GetValidArgumentIndex(ReadOnlySpan<char> placeholder, int allowedRange)

0 commit comments

Comments
 (0)