Skip to content

Commit

Permalink
Next attempt. Did first 3 types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel Nobel committed Nov 18, 2022
1 parent 3c5c4d4 commit 39a79f4
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 50 deletions.
15 changes: 14 additions & 1 deletion src/Qowaiv/Date.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
[System.Text.Json.Serialization.JsonConverter(typeof(Json.DateJsonConverter))]
#endif
public readonly partial struct Date : ISerializable, IXmlSerializable, IFormattable, IEquatable<Date>, IComparable, IComparable<Date>
#if NET6_0_OR_GREATER
, ISpanFormattable
#endif
#if NET7_0_OR_GREATER
, IIncrementOperators<Date>, IDecrementOperators<Date>
, IAdditionOperators<Date, TimeSpan, Date>, ISubtractionOperators<Date, TimeSpan, Date>
Expand Down Expand Up @@ -412,7 +415,17 @@ public string ToString(string? format, IFormatProvider? formatProvider)
: m_Value.ToString(format, formatProvider);
}

/// <summary>Gets an XML string representation of the @FullName.</summary>
#if NET6_0_OR_GREATER
/// <inheritdoc />
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
{
format = format.WithDefault("d");
return StringFormatter.TryApplyCustomFormatter(this, destination, out charsWritten, format, provider)
|| m_Value.TryFormat(destination, out charsWritten, format, provider);
}
#endif

/// <summary>Gets an XML string representation of the date.</summary>
[Pure]
private string ToXmlString() => ToString(SerializableFormat, CultureInfo.InvariantCulture);

Expand Down
2 changes: 1 addition & 1 deletion src/Qowaiv/EmailAddressCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public string ToString(string? format, IFormatProvider? formatProvider)
/// s is not in the correct format.
/// </exception>
[Pure]
public static EmailAddressCollection Parse(string? s, IFormatProvider formatProvider)
public static EmailAddressCollection Parse(string? s, IFormatProvider? formatProvider)
{
if (TryParse(s, formatProvider, out EmailAddressCollection val))
{
Expand Down
45 changes: 45 additions & 0 deletions src/Qowaiv/Extensions/System.Span.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#if NET6_0_OR_GREATER
namespace System;

internal static class QowaivSpanExtensions
{
[Pure]
public static bool TryWrite(this Span<char> span, string? value, out int charsWritten)
{
if (value is { Length: > 0 })
{
if (value.TryCopyTo(span))
{
charsWritten = value.Length;
return true;
}
else
{
charsWritten = 0;
return false;
}
}
else
{
charsWritten = 0;
return true;
}
}

[Pure]
public static bool TryWrite(this Span<char> span, char ch, out int charsWritten)
{
if (span.Length != 0)
{
span.Fill(ch);
charsWritten = 1;
return true;
}
else
{
charsWritten = 0;
return false;
}
}
}
#endif
9 changes: 9 additions & 0 deletions src/Qowaiv/Extensions/System.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ public static string ToTitleCase(this string str, IFormatProvider provider)
internal static string WithDefault(this string? str, string @default = "")
=> str is { Length: > 0 } ? str : @default;

#if NET6_0_OR_GREATER
/// <summary>Returns the provided default if <see cref="string.IsNullOrEmpty(string)"/>,
/// otherwise the string value.
/// </summary>
[Pure]
internal static ReadOnlySpan<char> WithDefault(this ReadOnlySpan<char> str, ReadOnlySpan<char> @default)
=> str.IsEmpty ? @default : str;
#endif

[Pure]
internal static string Unify(this string? str) => str.Buffer().Unify();

Expand Down
23 changes: 23 additions & 0 deletions src/Qowaiv/Financial/Amount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace Qowaiv.Financial;
[System.Text.Json.Serialization.JsonConverter(typeof(Json.Financial.AmountJsonConverter))]
#endif
public readonly partial struct Amount : ISerializable, IXmlSerializable, IFormattable, IEquatable<Amount>, IComparable, IComparable<Amount>
#if NET6_0_OR_GREATER
, ISpanFormattable
#endif
#if NET7_0_OR_GREATER
, IIncrementOperators<Amount>, IDecrementOperators<Amount>
, IUnaryPlusOperators<Amount, Amount>, IUnaryNegationOperators<Amount, Amount>
Expand Down Expand Up @@ -393,6 +396,26 @@ namespace Qowaiv.Financial;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => this.DebuggerDisplay("¤{0:0.00########}");

/// <summary>Returns a formatted <see cref="string"/> that represents the current </summary>
/// <param name="format">
/// The format that describes the formatting.
/// </param>
/// <param name="formatProvider">
/// The format provider.
/// </param>
[Pure]
public string ToString(string? format, IFormatProvider? formatProvider)
=> StringFormatter.TryApplyCustomFormatter(format, this, formatProvider, out string formatted)
? formatted
: m_Value.ToString(format, Money.GetNumberFormatInfo(formatProvider));

#if NET6_0_OR_GREATER
/// <inheritdoc />
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
=> StringFormatter.TryApplyCustomFormatter(this, destination, out charsWritten, format, provider)
|| m_Value.TryFormat(destination, out charsWritten, format, Money.GetNumberFormatInfo(provider));
#endif

/// <summary>Gets an XML string representation of the amount.</summary>
[Pure]
private string ToXmlString() => ToString(CultureInfo.InvariantCulture);
Expand Down
47 changes: 0 additions & 47 deletions src/Qowaiv/Financial/Amount.experimental.cs

This file was deleted.

14 changes: 14 additions & 0 deletions src/Qowaiv/Financial/BusinessIdentifierCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ namespace Qowaiv.Financial;
[System.Text.Json.Serialization.JsonConverter(typeof(Json.Financial.BusinessIdentifierCodeJsonConverter))]
#endif
public readonly partial struct BusinessIdentifierCode : ISerializable, IXmlSerializable, IFormattable, IEquatable<BusinessIdentifierCode>, IComparable, IComparable<BusinessIdentifierCode>
#if NET6_0_OR_GREATER
, ISpanFormattable
#endif
{
/// <remarks>
/// http://www.codeproject.com/KB/recipes/bicRegexValidator.aspx
Expand Down Expand Up @@ -96,6 +99,17 @@ public string ToString(string? format, IFormatProvider? formatProvider)
else return m_Value ?? string.Empty;
}

#if NET6_0_OR_GREATER
/// <inheritdoc />
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
{
if (StringFormatter.TryApplyCustomFormatter(this, destination, out charsWritten, format, provider)) return true;
else if (IsUnknown()) return destination.TryWrite('?', out charsWritten);
else return destination.TryWrite(m_Value, out charsWritten);
}

#endif

/// <summary>Gets an XML string representation of the BIC.</summary>
[Pure]
private string ToXmlString() => ToString(CultureInfo.InvariantCulture);
Expand Down
40 changes: 39 additions & 1 deletion src/Qowaiv/Formatting/StringFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Qowaiv.Formatting;
using Microsoft.VisualBasic;
using System.ComponentModel;

namespace Qowaiv.Formatting;

/// <summary>A string formatter class.</summary>
public static class StringFormatter
Expand Down Expand Up @@ -128,6 +131,41 @@ public static bool TryApplyCustomFormatter(string? format, object obj, IFormatPr
}
}

#if NET6_0_OR_GREATER

/// <summary>Tries to apply the custom formatter to format the object.</summary>
/// <param name="obj">
/// The object to format.
/// </param>
/// <param name="destination">
/// The span in which to write this instance's value formatted as a span of characters.
/// </param>
/// <param name="format">
/// The format to apply
/// </param>
/// <param name="charsWritten">
/// When this method returns, contains the number of characters that were written in destination.
/// </param>
/// <param name="provider">
/// The format provider.
/// </param>
/// <returns>
/// True, if the format provider supports custom formatting, otherwise false.
/// </returns>
public static bool TryApplyCustomFormatter(object obj, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
{
if (TryApplyCustomFormatter(format.ToString(), obj, provider, out var formatted))
{
return destination.TryWrite(formatted, out charsWritten);
}
else
{
charsWritten = 0;
return false;
}
}
#endif

/// <summary>Replaces diacritic characters by non diacritic ones.</summary>
/// <param name="str">
/// The string to remove the diacritics from.
Expand Down

0 comments on commit 39a79f4

Please sign in to comment.