Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 54d94be

Browse files
committed
Add support for leading 0's
1 parent b8dabb9 commit 54d94be

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/ServiceStack.Text/StringSegmentExtensions.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,26 @@ public static ulong ParseUnsignedInteger(this StringSegment value, ulong maxValu
221221
throw new FormatException(BadFormat);
222222

223223
ulong result = 0;
224-
int i = 0;
224+
int i = value.Offset;
225+
int end = value.Offset + value.Length;
225226
var state = ParseState.LeadingWhite;
226227

227-
while (i < value.Length)
228+
//skip leading whitespaces
229+
while (i < end && JsonUtils.IsWhiteSpace(value.Buffer[i])) i++;
230+
231+
if (i == end)
232+
throw new FormatException(BadFormat);
233+
234+
//skip leading zeros
235+
while (i < end && value.Buffer[i] == '0')
228236
{
229-
var c = value.GetChar(i++);
237+
state = ParseState.Number;
238+
i++;
239+
}
240+
241+
while (i < end)
242+
{
243+
var c = value.Buffer[i++];
230244

231245
switch (state)
232246
{
@@ -292,9 +306,17 @@ public static long ParseSignedInteger(this StringSegment value, long maxValue, l
292306

293307
//skip leading whitespaces
294308
while (i < end && JsonUtils.IsWhiteSpace(value.Buffer[i])) i++;
309+
295310
if (i == end)
296311
throw new FormatException(BadFormat);
297312

313+
//skip leading zeros
314+
while (i < end && value.Buffer[i] == '0')
315+
{
316+
state = ParseState.Number;
317+
i++;
318+
}
319+
298320
while (i < end)
299321
{
300322
var c = value.Buffer[i++];

tests/ServiceStack.Text.Tests/BasicStringSerializerTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ public void Can_convert_Field_Map_or_List_with_single_invalid_char()
571571
{
572572
foreach (var invalidChar in allCharsUsed)
573573
{
574-
var singleInvalidChar = string.Format("a {0} b", invalidChar);
574+
var singleInvalidChar = $"a {invalidChar} b";
575575

576576
var instance = new ModelWithMapAndList<string>
577577
{
@@ -616,6 +616,14 @@ public void Can_convert_List_Guid()
616616

617617
Assert.That(toModel, Is.EquivalentTo(model));
618618
}
619+
620+
[Test]
621+
public void Can_deserialize_int_with_leading_zeros()
622+
{
623+
Assert.That("01".FromJson<int>(), Is.EqualTo(1));
624+
Assert.That("01".FromJson<long>(), Is.EqualTo(1));
625+
Assert.That("01".FromJson<ulong>(), Is.EqualTo(1));
626+
}
619627
#endif
620628
}
621629
}

tests/ServiceStack.Text.Tests/Support/StringSegmentParse.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ public void Can_parse_int32()
2020
Assert.That(new StringSegment("1").ParseInt32(), Is.EqualTo(1));
2121
Assert.That(new StringSegment(int.MaxValue.ToString()).ParseInt32(), Is.EqualTo(int.MaxValue));
2222
Assert.That(new StringSegment(int.MinValue.ToString()).ParseInt32(), Is.EqualTo(int.MinValue));
23-
Assert.Throws<FormatException>(() => new StringSegment("01").ParseInt32());
2423
Assert.That(new StringSegment("234").ParseInt32(), Is.EqualTo(234));
2524
Assert.That(new StringSegment(" 234 ").ParseInt32(), Is.EqualTo(234));
2625
Assert.That(new StringSegment("234 ").ParseInt32(), Is.EqualTo(234));
2726
Assert.That(new StringSegment(" 234").ParseInt32(), Is.EqualTo(234));
2827
Assert.That(new StringSegment(" -234 ").ParseInt32(), Is.EqualTo(-234));
2928
Assert.Throws<FormatException>(() => new StringSegment("").ParseInt32());
30-
Assert.Throws<FormatException>(() => new StringSegment("01").ParseInt32());
3129
Assert.Throws<FormatException>(() => new StringSegment("-01").ParseInt32());
3230
Assert.Throws<FormatException>(() => new StringSegment(" - 234 ").ParseInt32());
3331
Assert.Throws<FormatException>(() => new StringSegment(" 2.34 ").ParseInt32());

0 commit comments

Comments
 (0)