This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #761 from jonfortescue/PrototypeMethods
Added Uri and Decimal support to InvariantParser.
- Loading branch information
Showing
3 changed files
with
429 additions
and
0 deletions.
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
src/System.Text.Primitives/System/Text/InvariantParser_decimal.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
145
src/System.Text.Primitives/System/Text/InvariantParser_uri.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.