-Code reformat
This commit is contained in:
@@ -27,243 +27,243 @@ using System;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal enum ParserTimeZone
|
||||
{
|
||||
Unspecified,
|
||||
Utc,
|
||||
LocalWestOfUtc,
|
||||
LocalEastOfUtc
|
||||
}
|
||||
|
||||
internal struct DateTimeParser
|
||||
{
|
||||
static DateTimeParser()
|
||||
internal enum ParserTimeZone
|
||||
{
|
||||
Power10 = new[] { -1, 10, 100, 1000, 10000, 100000, 1000000 };
|
||||
|
||||
Lzyyyy = "yyyy".Length;
|
||||
Lzyyyy_ = "yyyy-".Length;
|
||||
Lzyyyy_MM = "yyyy-MM".Length;
|
||||
Lzyyyy_MM_ = "yyyy-MM-".Length;
|
||||
Lzyyyy_MM_dd = "yyyy-MM-dd".Length;
|
||||
Lzyyyy_MM_ddT = "yyyy-MM-ddT".Length;
|
||||
LzHH = "HH".Length;
|
||||
LzHH_ = "HH:".Length;
|
||||
LzHH_mm = "HH:mm".Length;
|
||||
LzHH_mm_ = "HH:mm:".Length;
|
||||
LzHH_mm_ss = "HH:mm:ss".Length;
|
||||
Lz_ = "-".Length;
|
||||
Lz_zz = "-zz".Length;
|
||||
Lz_zz_ = "-zz:".Length;
|
||||
Lz_zz_zz = "-zz:zz".Length;
|
||||
Unspecified,
|
||||
Utc,
|
||||
LocalWestOfUtc,
|
||||
LocalEastOfUtc
|
||||
}
|
||||
|
||||
public int Year;
|
||||
public int Month;
|
||||
public int Day;
|
||||
public int Hour;
|
||||
public int Minute;
|
||||
public int Second;
|
||||
public int Fraction;
|
||||
public int ZoneHour;
|
||||
public int ZoneMinute;
|
||||
public ParserTimeZone Zone;
|
||||
|
||||
private string _text;
|
||||
private int _length;
|
||||
|
||||
private static readonly int[] Power10;
|
||||
|
||||
private static readonly int Lzyyyy;
|
||||
private static readonly int Lzyyyy_;
|
||||
private static readonly int Lzyyyy_MM;
|
||||
private static readonly int Lzyyyy_MM_;
|
||||
private static readonly int Lzyyyy_MM_dd;
|
||||
private static readonly int Lzyyyy_MM_ddT;
|
||||
private static readonly int LzHH;
|
||||
private static readonly int LzHH_;
|
||||
private static readonly int LzHH_mm;
|
||||
private static readonly int LzHH_mm_;
|
||||
private static readonly int LzHH_mm_ss;
|
||||
private static readonly int Lz_;
|
||||
private static readonly int Lz_zz;
|
||||
private static readonly int Lz_zz_;
|
||||
private static readonly int Lz_zz_zz;
|
||||
|
||||
private const short MaxFractionDigits = 7;
|
||||
|
||||
public bool Parse(string text)
|
||||
internal struct DateTimeParser
|
||||
{
|
||||
_text = text;
|
||||
_length = text.Length;
|
||||
|
||||
if (ParseDate(0) && ParseChar(Lzyyyy_MM_dd, 'T') && ParseTimeAndZoneAndWhitespace(Lzyyyy_MM_ddT))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool ParseDate(int start)
|
||||
{
|
||||
return (Parse4Digit(start, out Year)
|
||||
&& 1 <= Year
|
||||
&& ParseChar(start + Lzyyyy, '-')
|
||||
&& Parse2Digit(start + Lzyyyy_, out Month)
|
||||
&& 1 <= Month
|
||||
&& Month <= 12
|
||||
&& ParseChar(start + Lzyyyy_MM, '-')
|
||||
&& Parse2Digit(start + Lzyyyy_MM_, out Day)
|
||||
&& 1 <= Day
|
||||
&& Day <= DateTime.DaysInMonth(Year, Month));
|
||||
}
|
||||
|
||||
private bool ParseTimeAndZoneAndWhitespace(int start)
|
||||
{
|
||||
return (ParseTime(ref start) && ParseZone(start));
|
||||
}
|
||||
|
||||
private bool ParseTime(ref int start)
|
||||
{
|
||||
if (!(Parse2Digit(start, out Hour)
|
||||
&& Hour < 24
|
||||
&& ParseChar(start + LzHH, ':')
|
||||
&& Parse2Digit(start + LzHH_, out Minute)
|
||||
&& Minute < 60
|
||||
&& ParseChar(start + LzHH_mm, ':')
|
||||
&& Parse2Digit(start + LzHH_mm_, out Second)
|
||||
&& Second < 60))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
start += LzHH_mm_ss;
|
||||
if (ParseChar(start, '.'))
|
||||
{
|
||||
Fraction = 0;
|
||||
int numberOfDigits = 0;
|
||||
|
||||
while (++start < _length && numberOfDigits < MaxFractionDigits)
|
||||
static DateTimeParser()
|
||||
{
|
||||
int digit = _text[start] - '0';
|
||||
if (digit < 0 || digit > 9)
|
||||
break;
|
||||
Power10 = new[] { -1, 10, 100, 1000, 10000, 100000, 1000000 };
|
||||
|
||||
Fraction = (Fraction*10) + digit;
|
||||
|
||||
numberOfDigits++;
|
||||
Lzyyyy = "yyyy".Length;
|
||||
Lzyyyy_ = "yyyy-".Length;
|
||||
Lzyyyy_MM = "yyyy-MM".Length;
|
||||
Lzyyyy_MM_ = "yyyy-MM-".Length;
|
||||
Lzyyyy_MM_dd = "yyyy-MM-dd".Length;
|
||||
Lzyyyy_MM_ddT = "yyyy-MM-ddT".Length;
|
||||
LzHH = "HH".Length;
|
||||
LzHH_ = "HH:".Length;
|
||||
LzHH_mm = "HH:mm".Length;
|
||||
LzHH_mm_ = "HH:mm:".Length;
|
||||
LzHH_mm_ss = "HH:mm:ss".Length;
|
||||
Lz_ = "-".Length;
|
||||
Lz_zz = "-zz".Length;
|
||||
Lz_zz_ = "-zz:".Length;
|
||||
Lz_zz_zz = "-zz:zz".Length;
|
||||
}
|
||||
|
||||
if (numberOfDigits < MaxFractionDigits)
|
||||
public int Year;
|
||||
public int Month;
|
||||
public int Day;
|
||||
public int Hour;
|
||||
public int Minute;
|
||||
public int Second;
|
||||
public int Fraction;
|
||||
public int ZoneHour;
|
||||
public int ZoneMinute;
|
||||
public ParserTimeZone Zone;
|
||||
|
||||
private string _text;
|
||||
private int _length;
|
||||
|
||||
private static readonly int[] Power10;
|
||||
|
||||
private static readonly int Lzyyyy;
|
||||
private static readonly int Lzyyyy_;
|
||||
private static readonly int Lzyyyy_MM;
|
||||
private static readonly int Lzyyyy_MM_;
|
||||
private static readonly int Lzyyyy_MM_dd;
|
||||
private static readonly int Lzyyyy_MM_ddT;
|
||||
private static readonly int LzHH;
|
||||
private static readonly int LzHH_;
|
||||
private static readonly int LzHH_mm;
|
||||
private static readonly int LzHH_mm_;
|
||||
private static readonly int LzHH_mm_ss;
|
||||
private static readonly int Lz_;
|
||||
private static readonly int Lz_zz;
|
||||
private static readonly int Lz_zz_;
|
||||
private static readonly int Lz_zz_zz;
|
||||
|
||||
private const short MaxFractionDigits = 7;
|
||||
|
||||
public bool Parse(string text)
|
||||
{
|
||||
if (numberOfDigits == 0)
|
||||
_text = text;
|
||||
_length = text.Length;
|
||||
|
||||
if (ParseDate(0) && ParseChar(Lzyyyy_MM_dd, 'T') && ParseTimeAndZoneAndWhitespace(Lzyyyy_MM_ddT))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
||||
Fraction *= Power10[MaxFractionDigits - numberOfDigits];
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool ParseZone(int start)
|
||||
{
|
||||
if (start < _length)
|
||||
{
|
||||
char ch = _text[start];
|
||||
if (ch == 'Z' || ch == 'z')
|
||||
private bool ParseDate(int start)
|
||||
{
|
||||
Zone = ParserTimeZone.Utc;
|
||||
start++;
|
||||
return (Parse4Digit(start, out Year)
|
||||
&& 1 <= Year
|
||||
&& ParseChar(start + Lzyyyy, '-')
|
||||
&& Parse2Digit(start + Lzyyyy_, out Month)
|
||||
&& 1 <= Month
|
||||
&& Month <= 12
|
||||
&& ParseChar(start + Lzyyyy_MM, '-')
|
||||
&& Parse2Digit(start + Lzyyyy_MM_, out Day)
|
||||
&& 1 <= Day
|
||||
&& Day <= DateTime.DaysInMonth(Year, Month));
|
||||
}
|
||||
else
|
||||
|
||||
private bool ParseTimeAndZoneAndWhitespace(int start)
|
||||
{
|
||||
if (start + 2 < _length
|
||||
&& Parse2Digit(start + Lz_, out ZoneHour)
|
||||
&& ZoneHour <= 99)
|
||||
{
|
||||
switch (ch)
|
||||
return (ParseTime(ref start) && ParseZone(start));
|
||||
}
|
||||
|
||||
private bool ParseTime(ref int start)
|
||||
{
|
||||
if (!(Parse2Digit(start, out Hour)
|
||||
&& Hour < 24
|
||||
&& ParseChar(start + LzHH, ':')
|
||||
&& Parse2Digit(start + LzHH_, out Minute)
|
||||
&& Minute < 60
|
||||
&& ParseChar(start + LzHH_mm, ':')
|
||||
&& Parse2Digit(start + LzHH_mm_, out Second)
|
||||
&& Second < 60))
|
||||
{
|
||||
case '-':
|
||||
Zone = ParserTimeZone.LocalWestOfUtc;
|
||||
start += Lz_zz;
|
||||
break;
|
||||
|
||||
case '+':
|
||||
Zone = ParserTimeZone.LocalEastOfUtc;
|
||||
start += Lz_zz;
|
||||
break;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (start < _length)
|
||||
{
|
||||
if (ParseChar(start, ':'))
|
||||
start += LzHH_mm_ss;
|
||||
if (ParseChar(start, '.'))
|
||||
{
|
||||
start += 1;
|
||||
Fraction = 0;
|
||||
int numberOfDigits = 0;
|
||||
|
||||
if (start + 1 < _length
|
||||
&& Parse2Digit(start, out ZoneMinute)
|
||||
&& ZoneMinute <= 99)
|
||||
{
|
||||
start += 2;
|
||||
}
|
||||
while (++start < _length && numberOfDigits < MaxFractionDigits)
|
||||
{
|
||||
int digit = _text[start] - '0';
|
||||
if (digit < 0 || digit > 9)
|
||||
break;
|
||||
|
||||
Fraction = (Fraction * 10) + digit;
|
||||
|
||||
numberOfDigits++;
|
||||
}
|
||||
|
||||
if (numberOfDigits < MaxFractionDigits)
|
||||
{
|
||||
if (numberOfDigits == 0)
|
||||
return false;
|
||||
|
||||
Fraction *= Power10[MaxFractionDigits - numberOfDigits];
|
||||
}
|
||||
}
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool ParseZone(int start)
|
||||
{
|
||||
if (start < _length)
|
||||
{
|
||||
if (start + 1 < _length
|
||||
&& Parse2Digit(start, out ZoneMinute)
|
||||
&& ZoneMinute <= 99)
|
||||
{
|
||||
start += 2;
|
||||
}
|
||||
char ch = _text[start];
|
||||
if (ch == 'Z' || ch == 'z')
|
||||
{
|
||||
Zone = ParserTimeZone.Utc;
|
||||
start++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (start + 2 < _length
|
||||
&& Parse2Digit(start + Lz_, out ZoneHour)
|
||||
&& ZoneHour <= 99)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '-':
|
||||
Zone = ParserTimeZone.LocalWestOfUtc;
|
||||
start += Lz_zz;
|
||||
break;
|
||||
|
||||
case '+':
|
||||
Zone = ParserTimeZone.LocalEastOfUtc;
|
||||
start += Lz_zz;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (start < _length)
|
||||
{
|
||||
if (ParseChar(start, ':'))
|
||||
{
|
||||
start += 1;
|
||||
|
||||
if (start + 1 < _length
|
||||
&& Parse2Digit(start, out ZoneMinute)
|
||||
&& ZoneMinute <= 99)
|
||||
{
|
||||
start += 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (start + 1 < _length
|
||||
&& Parse2Digit(start, out ZoneMinute)
|
||||
&& ZoneMinute <= 99)
|
||||
{
|
||||
start += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (start == _length);
|
||||
}
|
||||
}
|
||||
|
||||
return (start == _length);
|
||||
}
|
||||
|
||||
private bool Parse4Digit(int start, out int num)
|
||||
{
|
||||
if (start + 3 < _length)
|
||||
{
|
||||
int digit1 = _text[start] - '0';
|
||||
int digit2 = _text[start + 1] - '0';
|
||||
int digit3 = _text[start + 2] - '0';
|
||||
int digit4 = _text[start + 3] - '0';
|
||||
if (0 <= digit1 && digit1 < 10
|
||||
&& 0 <= digit2 && digit2 < 10
|
||||
&& 0 <= digit3 && digit3 < 10
|
||||
&& 0 <= digit4 && digit4 < 10)
|
||||
private bool Parse4Digit(int start, out int num)
|
||||
{
|
||||
num = (((((digit1*10) + digit2)*10) + digit3)*10) + digit4;
|
||||
return true;
|
||||
if (start + 3 < _length)
|
||||
{
|
||||
int digit1 = _text[start] - '0';
|
||||
int digit2 = _text[start + 1] - '0';
|
||||
int digit3 = _text[start + 2] - '0';
|
||||
int digit4 = _text[start + 3] - '0';
|
||||
if (0 <= digit1 && digit1 < 10
|
||||
&& 0 <= digit2 && digit2 < 10
|
||||
&& 0 <= digit3 && digit3 < 10
|
||||
&& 0 <= digit4 && digit4 < 10)
|
||||
{
|
||||
num = (((((digit1 * 10) + digit2) * 10) + digit3) * 10) + digit4;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
num = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
num = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool Parse2Digit(int start, out int num)
|
||||
{
|
||||
if (start + 1 < _length)
|
||||
{
|
||||
int digit1 = _text[start] - '0';
|
||||
int digit2 = _text[start + 1] - '0';
|
||||
if (0 <= digit1 && digit1 < 10
|
||||
&& 0 <= digit2 && digit2 < 10)
|
||||
private bool Parse2Digit(int start, out int num)
|
||||
{
|
||||
num = (digit1*10) + digit2;
|
||||
return true;
|
||||
if (start + 1 < _length)
|
||||
{
|
||||
int digit1 = _text[start] - '0';
|
||||
int digit2 = _text[start + 1] - '0';
|
||||
if (0 <= digit1 && digit1 < 10
|
||||
&& 0 <= digit2 && digit2 < 10)
|
||||
{
|
||||
num = (digit1 * 10) + digit2;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
num = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
num = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool ParseChar(int start, char ch)
|
||||
{
|
||||
return (start < _length && _text[start] == ch);
|
||||
private bool ParseChar(int start, char ch)
|
||||
{
|
||||
return (start < _length && _text[start] == ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user