Skip to content

Commit 1497912

Browse files
authored
Merge pull request #6 from Cysharp/hadashiA/fix-multibyte-alignment
Make sure string alignment calculate as char count
2 parents 8f4b9f1 + 0b3c0e7 commit 1497912

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/Utf8StringInterpolation/Utf8String.AppendFormatted.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Buffers;
22
using System.Buffers.Text;
33
using System.Runtime.CompilerServices;
4+
using System.Text;
45

56
namespace Utf8StringInterpolation;
67

src/Utf8StringInterpolation/Utf8StringWriter.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,19 @@ public void AppendFormatted(string value, int alignment = 0, string? format = nu
179179
var buffer = rentArray.AsSpan();
180180
var bytesWritten = Encoding.UTF8.GetBytes(value.AsSpan(), buffer);
181181

182-
var space = alignment - bytesWritten;
182+
int charCount;
183+
#if NETSTANDARD2_0
184+
unsafe
185+
{
186+
fixed (byte* ptr = &buffer[0])
187+
{
188+
charCount = Encoding.UTF8.GetCharCount(ptr, bytesWritten);
189+
}
190+
}
191+
#else
192+
charCount = Encoding.UTF8.GetCharCount(buffer.Slice(0, bytesWritten));
193+
#endif
194+
var space = alignment - charCount;
183195
if (space > 0)
184196
{
185197
AppendWhitespace(space);
@@ -190,18 +202,30 @@ public void AppendFormatted(string value, int alignment = 0, string? format = nu
190202
destination = destination.Slice(bytesWritten);
191203
currentWritten += bytesWritten;
192204
ArrayPool<byte>.Shared.Return(rentArray);
193-
return;
194205
}
195206
else
196207
{
197208
// add right whitespace
198209
var max = GetStringByteCount(value.AsSpan());
199210
TryGrow(max);
200211
var bytesWritten = Encoding.UTF8.GetBytes(value.AsSpan(), destination);
212+
213+
int charCount;
214+
#if NETSTANDARD2_0
215+
unsafe
216+
{
217+
fixed (byte* ptr = &destination[currentWritten])
218+
{
219+
charCount = Encoding.UTF8.GetCharCount(ptr, bytesWritten);
220+
}
221+
}
222+
#else
223+
charCount = Encoding.UTF8.GetCharCount(destination.Slice(0, bytesWritten));
224+
#endif
201225
destination = destination.Slice(bytesWritten);
202226
currentWritten += bytesWritten;
203227

204-
var space = bytesWritten + alignment;
228+
var space = charCount + alignment;
205229
if (space < 0)
206230
{
207231
AppendWhitespace(-space);

tests/Utf8StringInterpolation.Tests/FormatTest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,12 @@ public void Unicode()
8686
Utf8String.Format($"\u30cf\u30fc\u30c8: {"\u2764"}, \u5bb6\u65cf: {"\uD83D\uDC69\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC67"}(\u7d75\u6587\u5b57)")
8787
.Should().Be($"\u30cf\u30fc\u30c8: {"\u2764"}, \u5bb6\u65cf: {"\uD83D\uDC69\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC67"}(\u7d75\u6587\u5b57)");
8888
}
89+
90+
[Fact]
91+
public void MultibyteStringAlignment()
92+
{
93+
Utf8String.Format($"abc{"あいう",10}").Should().Be($"abc{"あいう",10}");
94+
Utf8String.Format($"def{"えおか",-10}").Should().Be($"def{"えおか",-10}");
95+
}
8996
}
9097
}

0 commit comments

Comments
 (0)