Utf8StringSplitterExtensions.cs
943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
namespace UniJSON
{
public static class Utf8StringSplitterExtensions
{
/// <summary>
/// Split integer from start
///
/// "123 " => "123"
/// " 123" => FormatException
///
/// must start +-0123456789
///
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
public static Utf8String SplitInteger(this Utf8String src)
{
var i = 0;
if(src[0]=='+' || src[0] == '-')
{
++i;
}
var j = i;
for(; j<src.ByteLength; ++j)
{
if(src[j]<'0' || src[j]>'9')
{
break;
}
}
if (i == j)
{
throw new FormatException();
}
return src.Subbytes(0, j);
}
}
}