Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens committed Mar 8, 2024
1 parent 12778d7 commit eac7702
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ public static DateTime FromIso8601(string input)

if (DateTime.TryParseExact(input, "yyyy-MM-ddTHH:mm:ss.FFFK", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var dateTime))
{
// If not in UTC and has offset information
if (!isUtc && input.Length > 19)
{
string offset = input.Substring(19);
string offset = input[19..];
TimeSpan timeOffset = TimeSpan.Parse(offset);
dateTime = dateTime.Add(timeOffset);
}
Expand Down Expand Up @@ -63,7 +62,6 @@ public static DateTime FromASPNetAjax(string ajax)

long ticks = Convert.ToInt64(parts[1]);

// Create a Utc DateTime based on the tick count
return new DateTime(ticks, DateTimeKind.Utc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ protected enum Token
/// <returns>The parsed JSON string, or null if parsing fails.</returns>
protected static string? ParseString(char[] json, ref int index, ref bool success)
{
var s = new StringBuilder();
var result = new StringBuilder();

success = true;

AdvanceIndexPastWhitespace(json, ref index);

char c;
char character;
bool complete = false;

index++;
Expand All @@ -232,53 +232,53 @@ protected enum Token
break;
}

c = json[index++];
if (c == '"')
character = json[index++];
if (character == '"')
{
complete = true;
break;
}
else if (c == '\\')
else if (character == '\\')
{
if (index == json.Length)
{
break;
}

c = json[index++];
if (c == '"')
character = json[index++];
if (character == '"')
{
s.Append('"');
result.Append('"');
}
else if (c == '\\')
else if (character == '\\')
{
s.Append('\\');
result.Append('\\');
}
else if (c == '/')
else if (character == '/')
{
s.Append('/');
result.Append('/');
}
else if (c == 'b')
else if (character == 'b')
{
s.Append('\b');
result.Append('\b');
}
else if (c == 'f')
else if (character == 'f')
{
s.Append('\f');
result.Append('\f');
}
else if (c == 'n')
else if (character == 'n')
{
s.Append('\n');
result.Append('\n');
}
else if (c == 'r')
else if (character == 'r')
{
s.Append('\r');
result.Append('\r');
}
else if (c == 't')
else if (character == 't')
{
s.Append('\t');
result.Append('\t');
}
else if (c == 'u')
else if (character == 'u')
{
int remainingLength = json.Length - index;
if (remainingLength >= 4)
Expand All @@ -290,7 +290,7 @@ protected enum Token
}

// convert the integer codepoint to a unicode char and add to string
s.Append(UInt32Converters.ConvertUnicodeToAsciiString((int)codePoint));
result.Append(UInt32Converters.ConvertUnicodeToAsciiString((int)codePoint));

// skip 4 chars
index += 4;
Expand All @@ -303,7 +303,7 @@ protected enum Token
}
else
{
s.Append(c);
result.Append(character);
}
}

Expand All @@ -313,7 +313,7 @@ protected enum Token
return null;
}

return s.ToString();
return result.ToString();
}

/// <summary>
Expand Down Expand Up @@ -350,7 +350,7 @@ protected enum Token
else if (value.StartsWith("0x", StringComparison.OrdinalIgnoreCase) || value.IndexOfAny("abcdefABCDEF".ToCharArray()) >= 0)
{
// Parse as a hexadecimal number
if (long.TryParse(value.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out long hexValue))
if (long.TryParse(value[2..], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out long hexValue))
{
return hexValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ public static bool TryParse(string input, bool parseHex, out ulong result, out b
bool isOverflow = false;
result = 0;

// Skip leading white space.
int length = input.Length;
int currentIndex = 0;
while (currentIndex < length && char.IsWhiteSpace(input[currentIndex]))
{
currentIndex++;
}

// Check for leading sign information.
NumberFormatInfo nfi = CultureInfo.CurrentUICulture.NumberFormat;
string posSign = nfi.PositiveSign;
string negSign = nfi.NegativeSign;
sign = false;

while (currentIndex < length)
{
character = input[currentIndex];
Expand All @@ -63,7 +62,6 @@ public static bool TryParse(string input, bool parseHex, out ulong result, out b
}
}

// If the string is empty
if (currentIndex >= length)
{
return false;
Expand All @@ -72,7 +70,7 @@ public static bool TryParse(string input, bool parseHex, out ulong result, out b
uint low = 0;
uint high = 0;
uint digit;
ulong tempa, tempb;
ulong lowPart, highPart;

// Parse the value based on the selected format
do
Expand All @@ -84,30 +82,29 @@ public static bool TryParse(string input, bool parseHex, out ulong result, out b
(uint)(char.IsDigit(character) ? character - '0' : char.ToUpper(character) - 'A' + 10) :
(uint)(character - '0');

// Combine the digit with the result, and check for overflow.
if (!isOverflow)
{
tempa = ((ulong)low) * ((ulong)(parseHex ? 16 : 10));
tempb = ((ulong)high) * ((ulong)(parseHex ? 16 : 10));
tempb += (tempa >> 32);
lowPart = low * ((ulong)(parseHex ? 16 : 10));
highPart = high * ((ulong)(parseHex ? 16 : 10));
highPart += lowPart >> 32;

if (tempb > ((ulong)0xFFFFFFFF))
if (highPart > 0xFFFFFFFF)
{
isOverflow = true;
}
else
{
tempa = (tempa & 0xFFFFFFFF) + ((ulong)digit);
tempb += (tempa >> 32);
lowPart = (lowPart & 0xFFFFFFFF) + digit;
highPart += (lowPart >> 32);

if (tempb > ((ulong)0xFFFFFFFF))
if (highPart > 0xFFFFFFFF)
{
isOverflow = true;
}
else
{
low = unchecked((uint)tempa);
high = unchecked((uint)tempb);
low = unchecked((uint)lowPart);
high = unchecked((uint)highPart);
}
}
}
Expand All @@ -125,9 +122,13 @@ public static bool TryParse(string input, bool parseHex, out ulong result, out b
{
character = input[currentIndex];
if (char.IsWhiteSpace(character))
{
++currentIndex;
}
else
{
break;
}
} while (currentIndex < length);

if (currentIndex < length)
Expand All @@ -136,8 +137,7 @@ public static bool TryParse(string input, bool parseHex, out ulong result, out b
}
}

// Return the results to the caller.
result = (((ulong)high) << 32) | ((ulong)low);
result = (((ulong)high) << 32) | low;
return !isOverflow;
}
}

0 comments on commit eac7702

Please sign in to comment.