Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #761 from jonfortescue/PrototypeMethods
Browse files Browse the repository at this point in the history
Added Uri and Decimal support to InvariantParser.
  • Loading branch information
joshfree authored Aug 18, 2016
2 parents 9e031a0 + 23f91d1 commit 3bf6143
Show file tree
Hide file tree
Showing 3 changed files with 429 additions and 0 deletions.
175 changes: 175 additions & 0 deletions src/System.Text.Primitives/System/Text/InvariantParser_decimal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using System.Text.Utf8;

namespace System.Text
{
public static partial class InvariantParser
{
public static bool TryParse(byte[] utf8Text, int index, out decimal value, out int bytesConsumed)
{
// Precondition replacement
if (utf8Text.Length < 1 || index < 0 || index >= utf8Text.Length)
{
value = 0;
bytesConsumed = 0;
return false;
}

value = 0.0M;
bytesConsumed = 0;
string decimalString = "";
bool decimalPlace = false, signed = false;

if (utf8Text[index] == '-' || utf8Text[index] == '+')
{
signed = true;
decimalString += (char)utf8Text[index];
index++;
bytesConsumed++;
}

for (int byteIndex = index; byteIndex < utf8Text.Length; byteIndex++)
{
byte nextByte = utf8Text[byteIndex];
byte nextByteVal = (byte)(nextByte - '0');

if (nextByteVal > 9)
{
if (!decimalPlace && nextByte == '.')
{
bytesConsumed++;
decimalPlace = true;
decimalString += (char)nextByte;
}
else if ((decimalPlace && signed && bytesConsumed == 2) || ((signed || decimalPlace) && bytesConsumed == 1))
{
value = 0;
bytesConsumed = 0;
return false;
}
else
{
if (decimal.TryParse(decimalString, out value))
{
return true;
}
else
{
bytesConsumed = 0;
return false;
}
}
}
else
{
bytesConsumed++;
decimalString += (char)nextByte;
}
}

if ((decimalPlace && signed && bytesConsumed == 2) || ((signed || decimalPlace) && bytesConsumed == 1))
{
value = 0;
bytesConsumed = 0;
return false;
}
else
{
if (decimal.TryParse(decimalString, out value))
{
return true;
}
else
{
bytesConsumed = 0;
return false;
}
}
}

public unsafe static bool TryParse(byte* utf8Text, int index, int length, out decimal value, out int bytesConsumed)
{
// Precondition replacement
if (length < 1 || index < 0)
{
value = 0;
bytesConsumed = 0;
return false;
}

value = 0.0M;
bytesConsumed = 0;
string decimalString = "";
bool decimalPlace = false, signed = false;

if (utf8Text[index] == '-' || utf8Text[index] == '+')
{
signed = true;
decimalString += (char)utf8Text[index];
index++;
bytesConsumed++;
}

for (int byteIndex = index; byteIndex < length; byteIndex++)
{
byte nextByte = utf8Text[byteIndex];
byte nextByteVal = (byte)(nextByte - '0');

if (nextByteVal > 9)
{
if (!decimalPlace && nextByte == '.')
{
bytesConsumed++;
decimalPlace = true;
decimalString += (char)nextByte;
}
else if ((decimalPlace && signed && bytesConsumed == 2) || ((signed || decimalPlace) && bytesConsumed == 1))
{
value = 0;
bytesConsumed = 0;
return false;
}
else
{
if (decimal.TryParse(decimalString, out value))
{
return true;
}
else
{
bytesConsumed = 0;
return false;
}
}
}
else
{
bytesConsumed++;
decimalString += (char)nextByte;
}
}

if ((decimalPlace && signed && bytesConsumed == 2) || ((signed || decimalPlace) && bytesConsumed == 1))
{
value = 0;
bytesConsumed = 0;
return false;
}
else
{
if (decimal.TryParse(decimalString, out value))
{
return true;
}
else
{
bytesConsumed = 0;
return false;
}
}
}
}
}
145 changes: 145 additions & 0 deletions src/System.Text.Primitives/System/Text/InvariantParser_uri.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using System.Text.Utf8;

namespace System.Text
{
public static partial class InvariantParser
{
public static bool TryParse(byte[] utf8Text, int index, out Uri value, out int bytesConsumed)
{
// Precondition replacement
if (utf8Text.Length < 1 || index < 0 || index >= utf8Text.Length)
{
value = default(Uri);
bytesConsumed = 0;
return false;
}

StringBuilder sb = new StringBuilder();
bytesConsumed = 0;

for (int byteIndex = index; byteIndex < utf8Text.Length; byteIndex++)
{
byte nextByte = utf8Text[byteIndex];

if (nextByte == '#' || nextByte == '&' || nextByte == '=' || nextByte == '?' || nextByte == '_' || nextByte == '\\' || nextByte == ':' ||
nextByte >= '-' && nextByte <= '9' || nextByte >= 'A' && nextByte <= 'Z' || nextByte >= 'a' && nextByte <= 'z')
{
sb.Append((char)nextByte);
bytesConsumed++;
}
else
{
if (bytesConsumed == 0)
{
value = default(Uri);
return false;
}
else
{
if (Uri.TryCreate(sb.ToString(), UriKind.Absolute, out value))
{
return true;
}
else
{
value = default(Uri);
bytesConsumed = 0;
return false;
}
}
}
}

if (bytesConsumed == 0)
{
value = default(Uri);
return false;
}
else
{
if (Uri.TryCreate(sb.ToString(), UriKind.Absolute, out value))
{
return true;
}
else
{
value = default(Uri);
bytesConsumed = 0;
return false;
}
}
}

public static unsafe bool TryParse(byte* utf8Text, int index, int length, out Uri value, out int bytesConsumed)
{
// Precondition replacement
if (length < 1 || index < 0)
{
value = default(Uri);
bytesConsumed = 0;
return false;
}

StringBuilder sb = new StringBuilder();
bytesConsumed = 0;

for (int byteIndex = index; byteIndex < length; byteIndex++)
{
byte nextByte = utf8Text[byteIndex];

if (nextByte == '#' || nextByte == '&' || nextByte == '=' || nextByte == '?' || nextByte == '_' || nextByte == '\\' || nextByte == ':' ||
nextByte >= '-' && nextByte <= '9' || nextByte >= 'A' && nextByte <= 'Z' || nextByte >= 'a' && nextByte <= 'z')
{
sb.Append((char)nextByte);
bytesConsumed++;
}
else
{
if (bytesConsumed == 0)
{
value = default(Uri);
return false;
}
else
{
if (Uri.TryCreate(sb.ToString(), UriKind.Absolute, out value))
{
return true;
}
else
{
value = default(Uri);
bytesConsumed = 0;
return false;
}
}
}
}



if (bytesConsumed == 0)
{
value = default(Uri);
return false;
}
else
{
if (Uri.TryCreate(sb.ToString(), UriKind.Absolute, out value))
{
return true;
}
else
{
value = default(Uri);
bytesConsumed = 0;
return false;
}
}
}
}
}
Loading

0 comments on commit 3bf6143

Please sign in to comment.