Skip to content

Commit 83cd990

Browse files
committed
run
1 parent f904ddd commit 83cd990

File tree

9 files changed

+213
-32
lines changed

9 files changed

+213
-32
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Cysharp.Text
6+
{
7+
internal static class ExceptionUtil
8+
{
9+
internal static void ThrowArgumentException(string paramName)
10+
{
11+
throw new ArgumentException("Can't format argument.", paramName);
12+
}
13+
14+
internal static void ThrowFormatException()
15+
{
16+
throw new FormatException("Index (zero based) must be greater than or equal to zero and less than the size of the argument list.");
17+
}
18+
}
19+
}

src/ZString.Unity/Assets/Scripts/ZString/ExceptionUtil.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Buffers;
3+
using System.Collections.Generic;
4+
5+
namespace Cysharp.Text
6+
{
7+
internal static class PreparedFormatHelper
8+
{
9+
internal static FormatSegment[] Parse(string format, bool withStandardFormat)
10+
{
11+
var list = new List<FormatSegment>();
12+
13+
var copyFrom = 0;
14+
for (int i = 0; i < format.Length; i++)
15+
{
16+
if (format[i] == '{')
17+
{
18+
// escape.
19+
if (i == format.Length - 1)
20+
{
21+
throw new FormatException("invalid format");
22+
}
23+
24+
if (i != format.Length && format[i + 1] == '{')
25+
{
26+
var size = i - copyFrom;
27+
if (size != 0)
28+
{
29+
list.Add(new FormatSegment(copyFrom, size, false, 0, null, withStandardFormat));
30+
}
31+
i = i + 1; // skip escaped '{'
32+
copyFrom = i;
33+
continue;
34+
}
35+
else
36+
{
37+
var size = i - copyFrom;
38+
if (size != 0)
39+
{
40+
list.Add(new FormatSegment(copyFrom, size, false, 0, null, withStandardFormat));
41+
}
42+
}
43+
44+
// try to find range
45+
var indexParse = FormatParser.Parse(format.AsSpan(i));
46+
list.Add(new FormatSegment(0, 0, true, indexParse.Index, indexParse.FormatString.ToString(), withStandardFormat));
47+
48+
copyFrom = i + indexParse.LastIndex + 1;
49+
i = i + indexParse.LastIndex;
50+
}
51+
else if (format[i] == '}')
52+
{
53+
if (i != format.Length && format[i + 1] == '}')
54+
{
55+
var size = i - copyFrom;
56+
if (size != 0)
57+
{
58+
list.Add(new FormatSegment(copyFrom, size, false, 0, null, withStandardFormat));
59+
}
60+
i = i + 1; // skip escaped '}'
61+
copyFrom = i;
62+
continue;
63+
}
64+
}
65+
}
66+
67+
{
68+
// final string
69+
var copyLength = format.Length - copyFrom;
70+
if (copyLength > 0)
71+
{
72+
list.Add(new FormatSegment(copyFrom, copyLength, false, 0, null, withStandardFormat));
73+
}
74+
}
75+
76+
return list.ToArray();
77+
}
78+
}
79+
80+
81+
internal readonly struct FormatSegment
82+
{
83+
public readonly int Offset;
84+
public readonly int Count;
85+
public readonly bool IsFormatArgument;
86+
public readonly int FormatIndex;
87+
public readonly string FormatString;
88+
89+
// Utf8
90+
public readonly StandardFormat StandardFormat;
91+
92+
public FormatSegment(int offset, int count, bool isFormatArgument, int formatIndex, string formatString, bool utf8)
93+
{
94+
Offset = offset;
95+
Count = count;
96+
IsFormatArgument = isFormatArgument;
97+
FormatIndex = formatIndex;
98+
FormatString = formatString;
99+
if (utf8)
100+
{
101+
StandardFormat = (formatString != null) ? StandardFormat.Parse(formatString) : default;
102+
}
103+
else
104+
{
105+
StandardFormat = default;
106+
}
107+
}
108+
}
109+
}

src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ZString.Unity/Assets/Scripts/ZString/ZString.Prepare.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ZString.Unity/Assets/Scripts/ZString/ZString.Utf8Format.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ZString/ZString.csproj

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
<ItemGroup>
3434
<None Update="PreparedFormat.tt">
35-
<Generator>TextTemplatingFileGenerator</Generator>
36-
<LastGenOutput>PreparedFormat.cs</LastGenOutput>
35+
<Generator>TextTemplatingFileGenerator</Generator>
36+
<LastGenOutput>PreparedFormat.cs</LastGenOutput>
3737
</None>
3838
<None Update="Unity\TextMeshProExtensions.tt">
3939
<Generator>TextTemplatingFileGenerator</Generator>
@@ -80,20 +80,20 @@
8080
<LastGenOutput>ZString.Format.cs</LastGenOutput>
8181
</None>
8282
<None Update="ZString.Prepare.tt">
83-
<Generator>TextTemplatingFileGenerator</Generator>
84-
<LastGenOutput>ZString.Prepare.cs</LastGenOutput>
83+
<Generator>TextTemplatingFileGenerator</Generator>
84+
<LastGenOutput>ZString.Prepare.cs</LastGenOutput>
8585
</None>
8686
<None Update="ZString.Utf8Format.tt">
87-
<Generator>TextTemplatingFileGenerator</Generator>
88-
<LastGenOutput>ZString.Utf8Format.cs</LastGenOutput>
87+
<Generator>TextTemplatingFileGenerator</Generator>
88+
<LastGenOutput>ZString.Utf8Format.cs</LastGenOutput>
8989
</None>
9090
</ItemGroup>
9191

9292
<ItemGroup>
9393
<Compile Update="PreparedFormat.cs">
94-
<DesignTime>True</DesignTime>
95-
<AutoGen>True</AutoGen>
96-
<DependentUpon>PreparedFormat.tt</DependentUpon>
94+
<DesignTime>True</DesignTime>
95+
<AutoGen>True</AutoGen>
96+
<DependentUpon>PreparedFormat.tt</DependentUpon>
9797
</Compile>
9898
<Compile Update="Utf16\Utf16ValueStringBuilder.AppendFormat.cs">
9999
<DesignTime>True</DesignTime>
@@ -146,15 +146,29 @@
146146
<DependentUpon>ZString.Format.tt</DependentUpon>
147147
</Compile>
148148
<Compile Update="ZString.Prepare.cs">
149-
<DesignTime>True</DesignTime>
150-
<AutoGen>True</AutoGen>
151-
<DependentUpon>ZString.Prepare.tt</DependentUpon>
149+
<DesignTime>True</DesignTime>
150+
<AutoGen>True</AutoGen>
151+
<DependentUpon>ZString.Prepare.tt</DependentUpon>
152152
</Compile>
153153
<Compile Update="ZString.Utf8Format.cs">
154-
<DesignTime>True</DesignTime>
155-
<AutoGen>True</AutoGen>
156-
<DependentUpon>ZString.Utf8Format.tt</DependentUpon>
154+
<DesignTime>True</DesignTime>
155+
<AutoGen>True</AutoGen>
156+
<DependentUpon>ZString.Utf8Format.tt</DependentUpon>
157157
</Compile>
158+
159+
</ItemGroup>
160+
161+
<ItemGroup>
162+
<None Include="PreparedFormat.cs">
163+
<DesignTime>True</DesignTime>
164+
<AutoGen>True</AutoGen>
165+
<DependentUpon>PreparedFormat.tt</DependentUpon>
166+
</None>
167+
<None Include="ZString.Utf8Format.cs">
168+
<DesignTime>True</DesignTime>
169+
<AutoGen>True</AutoGen>
170+
<DependentUpon>ZString.Utf8Format.tt</DependentUpon>
171+
</None>
158172
</ItemGroup>
159173

160174
<ItemGroup>
@@ -168,22 +182,6 @@
168182
<ItemGroup>
169183
<TargetFiles1 Include="$(MSBuildProjectDirectory)\**\*.cs" Exclude="**\bin\**\*.*;**\obj\**\*.*;_InternalVisibleTo.cs" />
170184
</ItemGroup>
171-
<ItemGroup>
172-
<TargetFiles1 Remove="ExceptionUtil.cs" />
173-
<TargetFiles1 Remove="PreparedFormatHelper.cs" />
174-
</ItemGroup>
175-
<ItemGroup>
176-
<None Include="PreparedFormat.cs">
177-
<DesignTime>True</DesignTime>
178-
<AutoGen>True</AutoGen>
179-
<DependentUpon>PreparedFormat.tt</DependentUpon>
180-
</None>
181-
<None Include="ZString.Utf8Format.cs">
182-
<DesignTime>True</DesignTime>
183-
<AutoGen>True</AutoGen>
184-
<DependentUpon>ZString.Utf8Format.tt</DependentUpon>
185-
</None>
186-
</ItemGroup>
187185
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
188186
<Copy SourceFiles="@(TargetFiles1)" DestinationFiles="$(DestinationRoot)\%(RecursiveDir)%(Filename)%(Extension)" SkipUnchangedFiles="true" />
189187
</Target>

tests/ZString.Tests/FormatTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void Test<T0, T1>(string format, T0 t0, T1 t1)
3939

4040
// Direct
4141
{
42-
#if !NETCOREAPP2_1
42+
#if NETCOREAPP3_1
4343
var writer = new ArrayBufferWriter<byte>();
4444
ZString.Utf8Format(writer, format, t0, t1);
4545
var actual = Encoding.UTF8.GetString(writer.WrittenSpan);

0 commit comments

Comments
 (0)