-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support optional UTF-8 based contracts.
- Loading branch information
Showing
3 changed files
with
59 additions
and
17 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/Qowaiv.CodeGeneration.SingleValueObjects/Snippets/Utf8.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,40 @@ | ||
#if !NotUtf8 // exec | ||
|
||
#if NET8_0_OR_GREATER | ||
public partial struct @TSvo | ||
: IUtf8SpanParsable<@TSvo> | ||
{ | ||
/// <summary>Converts the <see cref="string"/> to <see cref="@TSvo"/>.</summary> | ||
/// <param name="utf8Text"> | ||
/// An UTF-8 string containing the @FullName to convert. | ||
/// </param> | ||
/// <param name="provider"> | ||
/// The specified format provider. | ||
/// </param> | ||
/// <returns> | ||
/// The parsed @FullName. | ||
/// </returns> | ||
/// <exception cref="FormatException"> | ||
/// <paramref name="utf8Text"/> is not in the correct format. | ||
/// </exception> | ||
[Pure] | ||
public static @TSvo Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider) | ||
=> TryParse(utf8Text, provider) | ||
?? throw Unparsable.ForValue<@TSvo>(utf8Text.ToString(), @FormatExceptionMessage); | ||
|
||
/// <summary>Converts the <see cref="string"/> to <see cref="@TSvo"/>.</summary> | ||
/// <param name="utf8Text"> | ||
/// An UTF-8 string containing the @FullName to convert. | ||
/// </param> | ||
/// <param name="provider"> | ||
/// The specified format provider. | ||
/// </param> | ||
/// <returns> | ||
/// The @FullName if the string was converted successfully, otherwise default. | ||
/// </returns> | ||
[Pure] | ||
public static @TSvo? TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider) | ||
=> TryParse(utf8Text, provider, out var val) ? val : default(@TSvo?); | ||
} | ||
#endif | ||
#endif // exec |
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
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