Skip to content

Commit 80bc1e9

Browse files
fix resize buffer issue(append large string).
1 parent 642182a commit 80bc1e9

16 files changed

+217
-157
lines changed

sandbox/ConsoleApp/Program.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using Cysharp.Text;
22
using System;
33
using System.Buffers;
4+
using System.Collections.Concurrent;
45
using System.Linq;
56
using System.Text;
67
using System.Text.Formatting;
78
using System.Text.Json;
9+
using System.Threading;
10+
using System.Threading.Tasks;
811

912
namespace ConsoleApp
1013
{
@@ -22,15 +25,13 @@ static void Main(string[] args)
2225

2326
static void Run()
2427
{
25-
using var sb = ZString.CreateUtf8StringBuilder();
26-
IBufferWriter<byte> boxed = sb;
27-
var writer = new Utf8JsonWriter(boxed);
28-
JsonSerializer.Serialize(writer, new { foo = 999 });
28+
var a = new string('a', 10000);
29+
var b = new string('b', 1000000);
2930

30-
using var unboxed = (Utf8ValueStringBuilder)boxed;
31+
ZString.Join(',', new string[] { a, b });
32+
33+
3134

32-
Console.WriteLine(sb.ToString());
33-
Console.WriteLine(unboxed.ToString());
3435
}
3536
}
3637

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Buffers;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Cysharp.Text
7+
{
8+
public interface IResettableBufferWriter<T> : IBufferWriter<T>
9+
{
10+
void Reset();
11+
}
12+
}

src/ZString.Unity/Assets/Scripts/ZString/Utf16/Utf16ValueStringBuilder.SpanFormattableAppend.cs

+30-30
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public void Append(System.Byte value)
1111
{
1212
if(!value.TryFormat(buffer.AsSpan(index), out var written))
1313
{
14-
Grow();
14+
Grow(written);
1515
if(!value.TryFormat(buffer.AsSpan(index), out written))
1616
{
1717
ThrowArgumentException(nameof(value));
@@ -26,7 +26,7 @@ public void Append(System.Byte value, string format)
2626
{
2727
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
2828
{
29-
Grow();
29+
Grow(written);
3030
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
3131
{
3232
ThrowArgumentException(nameof(value));
@@ -56,7 +56,7 @@ public void Append(System.DateTime value)
5656
{
5757
if(!value.TryFormat(buffer.AsSpan(index), out var written))
5858
{
59-
Grow();
59+
Grow(written);
6060
if(!value.TryFormat(buffer.AsSpan(index), out written))
6161
{
6262
ThrowArgumentException(nameof(value));
@@ -71,7 +71,7 @@ public void Append(System.DateTime value, string format)
7171
{
7272
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
7373
{
74-
Grow();
74+
Grow(written);
7575
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
7676
{
7777
ThrowArgumentException(nameof(value));
@@ -101,7 +101,7 @@ public void Append(System.DateTimeOffset value)
101101
{
102102
if(!value.TryFormat(buffer.AsSpan(index), out var written))
103103
{
104-
Grow();
104+
Grow(written);
105105
if(!value.TryFormat(buffer.AsSpan(index), out written))
106106
{
107107
ThrowArgumentException(nameof(value));
@@ -116,7 +116,7 @@ public void Append(System.DateTimeOffset value, string format)
116116
{
117117
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
118118
{
119-
Grow();
119+
Grow(written);
120120
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
121121
{
122122
ThrowArgumentException(nameof(value));
@@ -146,7 +146,7 @@ public void Append(System.Decimal value)
146146
{
147147
if(!value.TryFormat(buffer.AsSpan(index), out var written))
148148
{
149-
Grow();
149+
Grow(written);
150150
if(!value.TryFormat(buffer.AsSpan(index), out written))
151151
{
152152
ThrowArgumentException(nameof(value));
@@ -161,7 +161,7 @@ public void Append(System.Decimal value, string format)
161161
{
162162
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
163163
{
164-
Grow();
164+
Grow(written);
165165
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
166166
{
167167
ThrowArgumentException(nameof(value));
@@ -191,7 +191,7 @@ public void Append(System.Double value)
191191
{
192192
if(!value.TryFormat(buffer.AsSpan(index), out var written))
193193
{
194-
Grow();
194+
Grow(written);
195195
if(!value.TryFormat(buffer.AsSpan(index), out written))
196196
{
197197
ThrowArgumentException(nameof(value));
@@ -206,7 +206,7 @@ public void Append(System.Double value, string format)
206206
{
207207
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
208208
{
209-
Grow();
209+
Grow(written);
210210
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
211211
{
212212
ThrowArgumentException(nameof(value));
@@ -236,7 +236,7 @@ public void Append(System.Int16 value)
236236
{
237237
if(!value.TryFormat(buffer.AsSpan(index), out var written))
238238
{
239-
Grow();
239+
Grow(written);
240240
if(!value.TryFormat(buffer.AsSpan(index), out written))
241241
{
242242
ThrowArgumentException(nameof(value));
@@ -251,7 +251,7 @@ public void Append(System.Int16 value, string format)
251251
{
252252
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
253253
{
254-
Grow();
254+
Grow(written);
255255
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
256256
{
257257
ThrowArgumentException(nameof(value));
@@ -281,7 +281,7 @@ public void Append(System.Int32 value)
281281
{
282282
if(!value.TryFormat(buffer.AsSpan(index), out var written))
283283
{
284-
Grow();
284+
Grow(written);
285285
if(!value.TryFormat(buffer.AsSpan(index), out written))
286286
{
287287
ThrowArgumentException(nameof(value));
@@ -296,7 +296,7 @@ public void Append(System.Int32 value, string format)
296296
{
297297
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
298298
{
299-
Grow();
299+
Grow(written);
300300
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
301301
{
302302
ThrowArgumentException(nameof(value));
@@ -326,7 +326,7 @@ public void Append(System.Int64 value)
326326
{
327327
if(!value.TryFormat(buffer.AsSpan(index), out var written))
328328
{
329-
Grow();
329+
Grow(written);
330330
if(!value.TryFormat(buffer.AsSpan(index), out written))
331331
{
332332
ThrowArgumentException(nameof(value));
@@ -341,7 +341,7 @@ public void Append(System.Int64 value, string format)
341341
{
342342
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
343343
{
344-
Grow();
344+
Grow(written);
345345
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
346346
{
347347
ThrowArgumentException(nameof(value));
@@ -371,7 +371,7 @@ public void Append(System.SByte value)
371371
{
372372
if(!value.TryFormat(buffer.AsSpan(index), out var written))
373373
{
374-
Grow();
374+
Grow(written);
375375
if(!value.TryFormat(buffer.AsSpan(index), out written))
376376
{
377377
ThrowArgumentException(nameof(value));
@@ -386,7 +386,7 @@ public void Append(System.SByte value, string format)
386386
{
387387
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
388388
{
389-
Grow();
389+
Grow(written);
390390
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
391391
{
392392
ThrowArgumentException(nameof(value));
@@ -416,7 +416,7 @@ public void Append(System.Single value)
416416
{
417417
if(!value.TryFormat(buffer.AsSpan(index), out var written))
418418
{
419-
Grow();
419+
Grow(written);
420420
if(!value.TryFormat(buffer.AsSpan(index), out written))
421421
{
422422
ThrowArgumentException(nameof(value));
@@ -431,7 +431,7 @@ public void Append(System.Single value, string format)
431431
{
432432
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
433433
{
434-
Grow();
434+
Grow(written);
435435
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
436436
{
437437
ThrowArgumentException(nameof(value));
@@ -461,7 +461,7 @@ public void Append(System.TimeSpan value)
461461
{
462462
if(!value.TryFormat(buffer.AsSpan(index), out var written))
463463
{
464-
Grow();
464+
Grow(written);
465465
if(!value.TryFormat(buffer.AsSpan(index), out written))
466466
{
467467
ThrowArgumentException(nameof(value));
@@ -476,7 +476,7 @@ public void Append(System.TimeSpan value, string format)
476476
{
477477
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
478478
{
479-
Grow();
479+
Grow(written);
480480
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
481481
{
482482
ThrowArgumentException(nameof(value));
@@ -506,7 +506,7 @@ public void Append(System.UInt16 value)
506506
{
507507
if(!value.TryFormat(buffer.AsSpan(index), out var written))
508508
{
509-
Grow();
509+
Grow(written);
510510
if(!value.TryFormat(buffer.AsSpan(index), out written))
511511
{
512512
ThrowArgumentException(nameof(value));
@@ -521,7 +521,7 @@ public void Append(System.UInt16 value, string format)
521521
{
522522
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
523523
{
524-
Grow();
524+
Grow(written);
525525
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
526526
{
527527
ThrowArgumentException(nameof(value));
@@ -551,7 +551,7 @@ public void Append(System.UInt32 value)
551551
{
552552
if(!value.TryFormat(buffer.AsSpan(index), out var written))
553553
{
554-
Grow();
554+
Grow(written);
555555
if(!value.TryFormat(buffer.AsSpan(index), out written))
556556
{
557557
ThrowArgumentException(nameof(value));
@@ -566,7 +566,7 @@ public void Append(System.UInt32 value, string format)
566566
{
567567
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
568568
{
569-
Grow();
569+
Grow(written);
570570
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
571571
{
572572
ThrowArgumentException(nameof(value));
@@ -596,7 +596,7 @@ public void Append(System.UInt64 value)
596596
{
597597
if(!value.TryFormat(buffer.AsSpan(index), out var written))
598598
{
599-
Grow();
599+
Grow(written);
600600
if(!value.TryFormat(buffer.AsSpan(index), out written))
601601
{
602602
ThrowArgumentException(nameof(value));
@@ -611,7 +611,7 @@ public void Append(System.UInt64 value, string format)
611611
{
612612
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
613613
{
614-
Grow();
614+
Grow(written);
615615
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
616616
{
617617
ThrowArgumentException(nameof(value));
@@ -641,7 +641,7 @@ public void Append(System.Guid value)
641641
{
642642
if(!value.TryFormat(buffer.AsSpan(index), out var written))
643643
{
644-
Grow();
644+
Grow(written);
645645
if(!value.TryFormat(buffer.AsSpan(index), out written))
646646
{
647647
ThrowArgumentException(nameof(value));
@@ -656,7 +656,7 @@ public void Append(System.Guid value, string format)
656656
{
657657
if(!value.TryFormat(buffer.AsSpan(index), out var written, format.AsSpan()))
658658
{
659-
Grow();
659+
Grow(written);
660660
if(!value.TryFormat(buffer.AsSpan(index), out written, format.AsSpan()))
661661
{
662662
ThrowArgumentException(nameof(value));

src/ZString.Unity/Assets/Scripts/ZString/Utf16ValueStringBuilder.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
22
using System.Buffers;
3+
using System.IO;
34
using System.Runtime.CompilerServices;
45

56
namespace Cysharp.Text
67
{
7-
public partial struct Utf16ValueStringBuilder : IDisposable, IBufferWriter<char>
8+
public partial struct Utf16ValueStringBuilder : IDisposable, IBufferWriter<char>, IResettableBufferWriter<char>
89
{
910
public delegate bool TryFormat<T>(T value, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format);
1011

@@ -88,13 +89,14 @@ public void Dispose()
8889

8990
public void TryGrow(int sizeHint)
9091
{
92+
9193
if (buffer.Length < index + sizeHint)
9294
{
9395
Grow(sizeHint);
9496
}
9597
}
9698

97-
public void Grow(int sizeHint = 0)
99+
public void Grow(int sizeHint)
98100
{
99101
var nextSize = buffer.Length * 2;
100102
if (sizeHint != 0)
@@ -178,7 +180,7 @@ public void Append<T>(T value)
178180
{
179181
if (!FormatterCache<T>.TryFormatDelegate(value, buffer.AsSpan(index), out var written, default))
180182
{
181-
Grow();
183+
Grow(written);
182184
if (!FormatterCache<T>.TryFormatDelegate(value, buffer.AsSpan(index), out written, default))
183185
{
184186
ThrowArgumentException(nameof(value));
@@ -246,6 +248,11 @@ public void Advance(int count)
246248
index += count;
247249
}
248250

251+
void IResettableBufferWriter<char>.Reset()
252+
{
253+
index = 0;
254+
}
255+
249256
void ThrowArgumentException(string paramName)
250257
{
251258
throw new ArgumentException("Can't format argument.", paramName);

0 commit comments

Comments
 (0)