Skip to content

Commit 06e03c3

Browse files
committedMay 11, 2020
format parser does not handle escape character #16
1 parent 0191cb5 commit 06e03c3

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed
 

‎sandbox/ConsoleApp/Program.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ static void Main(string[] args)
2525

2626
static void Run()
2727
{
28-
var a = new string('a', 10000);
29-
var b = new string('b', 1000000);
28+
TimeSpan span = new TimeSpan(12, 34, 56);
29+
Console.WriteLine($"string.Format: {string.Format(@"{0:h\,h\:mm\:ss}", span)}");
30+
31+
32+
Console.WriteLine($"ZString.Format: {ZString.Format(@"{0:h\,h\:mm\:ss}", span)}");
3033

31-
ZString.Join(',', new string[] { a, b });
3234

33-
3435

3536
}
3637
}

‎src/ZString.Unity/Assets/Scripts/ZString/FormatParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static ParseResult Parse(ReadOnlySpan<char> format)
4242
}
4343
}
4444

45-
if (index == -1 && format[i] == ',' || format[i] == ':')
45+
if (index == -1 && (format[i] == ',' && format[i - 1] != '\\') || (format[i] == ':' && format[i - 1] != '\\'))
4646
{
4747
index = Int32.Parse(format.Slice(1, i - 1));
4848
formatStart = i + 1;

‎src/ZString/FormatParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static ParseResult Parse(ReadOnlySpan<char> format)
4242
}
4343
}
4444

45-
if (index == -1 && format[i] == ',' || format[i] == ':')
45+
if (index == -1 && (format[i] == ',' && format[i - 1] != '\\') || (format[i] == ':' && format[i - 1] != '\\'))
4646
{
4747
index = Int32.Parse(format.Slice(1, i - 1));
4848
formatStart = i + 1;

‎tests/ZString.Tests/FormatTest.cs

+10
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,15 @@ public void FormatArgs()
9696
{
9797
ZString.Format("{0:00000000}-{1:00000000}", 100, 200).Should().Be("00000100-00000200");
9898
}
99+
100+
[Fact]
101+
public void Escape()
102+
{
103+
TimeSpan span = new TimeSpan(12, 34, 56);
104+
var reference = string.Format(@"{0:h\,h\:mm\:ss}", span);
105+
106+
var actual = ZString.Format(@"{0:h\,h\:mm\:ss}", span);
107+
actual.Should().Be(reference);
108+
}
99109
}
100110
}

0 commit comments

Comments
 (0)