public void Format(StringBuffer buffer, int index, StringView format) { switch (index) { case 0: buffer.AppendGeneric(t0, format); break; } }
static void CustomFormat(StringBuffer buffer, Blah blah, StringView format) { if (format == "yes") buffer.Append("World!"); else buffer.Append("(Goodbye)"); }
public static void FormatDouble(StringBuffer formatter, double value, StringView specifier, CachedCulture culture) { int digits; int precision = DoublePrecision; var fmt = ParseFormatSpecifier(specifier, out digits); // ANDing with 0xFFDF has the effect of uppercasing the character switch (fmt & 0xFFDF) { case 'G': if (digits > 15) { precision = 17; } break; case 'E': if (digits > 14) { precision = 17; } break; } var number = new Number(); var buffer = stackalloc char[MaxFloatingDigits + 1]; number.Digits = buffer; DoubleToNumber(value, precision, ref number); if (number.Scale == ScaleNaN) { formatter.Append(culture.NaN); return; } if (number.Scale == ScaleInf) { if (number.Sign > 0) { formatter.Append(culture.NegativeInfinity); } else { formatter.Append(culture.PositiveInfinity); } return; } if (fmt != 0) { NumberToString(formatter, ref number, fmt, digits, culture); } else { NumberToCustomFormatString(formatter, ref number, specifier, culture); } }
private static unsafe bool IsStandardShortDateFormat(StringView format) { if (format.Length == 1 && format.Data[0] == _standardFormatShort) { return(true); } return(format == _standardFormat); }
//[MethodImpl(MethodImplOptions.AggressiveInlining)] public void Format(StringBuffer buffer, int index, StringView format) { switch (index) { case 0: buffer.AppendGeneric <T0>(t0, format); break; case 1: buffer.AppendGeneric <T1>(t1, format); break; case 2: buffer.AppendGeneric <T2>(t2, format); break; } }
internal static char ParseFormatSpecifier(StringView specifier, out int digits) { if (specifier.IsEmpty) { digits = -1; return('G'); } char *curr = specifier.Data; char first = *curr++; if ((first >= 'A' && first <= 'Z') || (first >= 'a' && first <= 'z')) { if (specifier.Length == 1) { digits = -1; return(first); } var n = 0; var end = specifier.Data + specifier.Length; while (curr != end) { var c = *curr++; if (c >= '0' && c <= '9') { n = n * 10 + c - '0'; if (n >= 10) { break; } } else { digits = -1; return((char)0); } } if (curr == end) { digits = n; return(first); } } digits = -1; return((char)0); }
public static unsafe void Format(StringBuffer formatter, Guid value, StringView format) { if (format.Length > 1) { throw new FormatException(string.Format(SR.UnknownFormatSpecifier, format)); } var guidProxy = *(GuidProxy *)&value; var formatChar = format.IsEmpty ? 'D' : format.Data[0]; var guidChars = stackalloc char[68]; var offset = 0; switch (formatChar) { case 'D': case 'd': offset = GuidProxyToChars(guidChars, offset, guidProxy, true, false); break; case 'N': case 'n': offset = GuidProxyToChars(guidChars, offset, guidProxy, false, false); break; case 'B': case 'b': guidChars[offset++] = '{'; offset = GuidProxyToChars(guidChars, offset, guidProxy, true, false); guidChars[offset++] = '}'; break; case 'P': case 'p': guidChars[offset++] = '('; offset = GuidProxyToChars(guidChars, offset, guidProxy, true, false); guidChars[offset++] = ')'; break; case 'X': case 'x': guidChars[offset++] = '{'; offset = GuidProxyToChars(guidChars, offset, guidProxy, false, true); guidChars[offset++] = '}'; break; default: throw new FormatException(string.Format(SR.UnknownFormatSpecifier, format)); } formatter.Append(guidChars, offset); }
public static void FormatInt16(StringBuffer formatter, short value, StringView specifier, CachedCulture culture) { if (value < 0 && !specifier.IsEmpty) { // if we're negative and doing a hex format, mask out the bits for the conversion char c = specifier.Data[0]; if (c == 'X' || c == 'x') { FormatUInt32(formatter, (uint)(value & 0xFFFF), specifier, culture); } } FormatInt32(formatter, value, specifier, culture); }
public static void FormatDecimal(StringBuffer formatter, uint* value, StringView specifier, CachedCulture culture) { int digits; var fmt = ParseFormatSpecifier(specifier, out digits); var number = new Number(); var buffer = stackalloc char[MaxNumberDigits + 1]; number.Digits = buffer; DecimalToNumber(value, ref number); if (fmt != 0) NumberToString(formatter, ref number, fmt, digits, culture, isDecimal: true); else NumberToCustomFormatString(formatter, ref number, specifier, culture); }
public void Format(StringBuffer buffer, int index, StringView format) { switch (index) { case 0: buffer.AppendGeneric(t0, format); break; case 1: buffer.AppendGeneric(t1, format); break; case 2: buffer.AppendGeneric(t2, format); break; case 3: buffer.AppendGeneric(t3, format); break; case 4: buffer.AppendGeneric(t4, format); break; case 5: buffer.AppendGeneric(t5, format); break; } }
public static void FormatUInt32(StringBuffer formatter, uint value, StringView specifier, CachedCulture culture) { int digits; var fmt = ParseFormatSpecifier(specifier, out digits); // ANDing with 0xFFDF has the effect of uppercasing the character switch (fmt & 0xFFDF) { case 'G': if (digits > 0) { goto default; } else { goto case 'D'; } case 'D': UInt32ToDecStr(formatter, value, digits); break; case 'X': // fmt-('X'-'A'+1) gives us the base hex character in either // uppercase or lowercase, depending on the casing of fmt Int32ToHexStr(formatter, value, fmt - ('X' - 'A' + 10), digits); break; default: var number = new Number(); var buffer = stackalloc char[MaxNumberDigits + 1]; number.Digits = buffer; UInt32ToNumber(value, ref number); if (fmt != 0) { NumberToString(formatter, ref number, fmt, digits, culture); } else { NumberToCustomFormatString(formatter, ref number, specifier, culture); } break; } }
private static unsafe TimeSpanFormat ParseTimeSpanFormat(StringView format) { if (format.Length != 1) { return(TimeSpanFormat.Constant); } switch (format.Data[0]) { case 'g': return(TimeSpanFormat.GeneralShort); case 'G': return(TimeSpanFormat.GeneralLong); default: return(TimeSpanFormat.Constant); } }
public static void FormatDecimal(StringBuffer formatter, uint *value, StringView specifier, CachedCulture culture) { int digits; var fmt = ParseFormatSpecifier(specifier, out digits); var number = new Number(); var buffer = stackalloc char[MaxNumberDigits + 1]; number.Digits = buffer; DecimalToNumber(value, ref number); if (fmt != 0) { NumberToString(formatter, ref number, fmt, digits, culture, isDecimal: true); } else { NumberToCustomFormatString(formatter, ref number, specifier, culture); } }
public static void FormatDouble(StringBuffer formatter, double value, StringView specifier, CachedCulture culture) { int digits; int precision = DoublePrecision; var fmt = ParseFormatSpecifier(specifier, out digits); // ANDing with 0xFFDF has the effect of uppercasing the character switch (fmt & 0xFFDF) { case 'G': if (digits > 15) precision = 17; break; case 'E': if (digits > 14) precision = 17; break; } var number = new Number(); var buffer = stackalloc char[MaxFloatingDigits + 1]; number.Digits = buffer; DoubleToNumber(value, precision, ref number); if (number.Scale == ScaleNaN) { formatter.Append(culture.NaN); return; } if (number.Scale == ScaleInf) { if (number.Sign > 0) formatter.Append(culture.NegativeInfinity); else formatter.Append(culture.PositiveInfinity); return; } if (fmt != 0) NumberToString(formatter, ref number, fmt, digits, culture); else NumberToCustomFormatString(formatter, ref number, specifier, culture); }
//[MethodImpl(MethodImplOptions.AggressiveInlining)] public void Format(StringBuffer buffer, int index, StringView format) { switch (index) { case 0: buffer.AppendGeneric <T0>(t0, format); break; case 1: buffer.AppendGeneric <T1>(t1, format); break; case 2: buffer.AppendGeneric <T2>(t2, format); break; case 3: buffer.AppendGeneric <T3>(t3, format); break; case 4: buffer.AppendGeneric <T4>(t4, format); break; case 5: buffer.AppendGeneric <T5>(t5, format); break; case 6: buffer.AppendGeneric <T6>(t6, format); break; case 7: buffer.AppendGeneric <T7>(t7, format); break; } }
static char ParseFormatSpecifier(StringView specifier, out int digits) { if (specifier.IsEmpty) { digits = -1; return('G'); } char *curr = specifier.Data; char first = *curr++; if ((first >= 'A' && first <= 'Z') || (first >= 'a' && first <= 'z')) { int n = -1; char c = *curr++; if (c >= '0' && c <= '9') { n = c - '0'; c = *curr++; while (c >= '0' && c <= '9') { n = n * 10 + c - '0'; c = *curr++; if (n >= 10) { break; } } } if (c == 0) { digits = n; return(first); } } digits = -1; return((char)0); }
public static unsafe void Format(StringBuffer formatter, DateTime dateTime, StringView format) { const int tempCharsLength = 4; var tempChars = stackalloc char[tempCharsLength]; if (IsStandardShortDateFormat(format)) { var(year, month, day) = dateTime; formatter.EnsureCapacity(10); AppendNumber(formatter, year, 4, tempChars, tempCharsLength); formatter.Append('-'); AppendNumber(formatter, month, 2, tempChars, tempCharsLength); formatter.Append('-'); AppendNumber(formatter, day, 2, tempChars, tempCharsLength); } else { var(year, month, day, hour, minute, second, millisecond) = dateTime; formatter.EnsureCapacity(23); AppendNumber(formatter, year, 4, tempChars, tempCharsLength); formatter.Append('-'); AppendNumber(formatter, month, 2, tempChars, tempCharsLength); formatter.Append('-'); AppendNumber(formatter, day, 2, tempChars, tempCharsLength); formatter.Append(' '); AppendNumber(formatter, hour, 2, tempChars, tempCharsLength); formatter.Append(':'); AppendNumber(formatter, minute, 2, tempChars, tempCharsLength); formatter.Append(':'); AppendNumber(formatter, second, 2, tempChars, tempCharsLength); formatter.Append('.'); AppendNumber(formatter, millisecond, 3, tempChars, tempCharsLength); } }
static char ParseFormatSpecifier(StringView specifier, out int digits) { if (specifier.IsEmpty) { digits = -1; return 'G'; } char* curr = specifier.Data; char first = *curr++; if ((first >= 'A' && first <= 'Z') || (first >= 'a' && first <= 'z')) { int n = -1; char c = *curr++; if (c >= '0' && c <= '9') { n = c - '0'; c = *curr++; while (c >= '0' && c <= '9') { n = n * 10 + c - '0'; c = *curr++; if (n >= 10) break; } } if (c == 0) { digits = n; return first; } } digits = -1; return (char)0; }
/// <summary> /// Appends the specified value as a string to the current buffer. /// </summary> /// <param name="value">The value to append.</param> /// <param name="format">A format specifier indicating how to convert <paramref name="value"/> to a string.</param> public void Append(ushort value, StringView format) { // widening here is fine Numeric.FormatUInt32(this, value, format, culture); }
/// <summary> /// Appends the specified value as a string to the current buffer. /// </summary> /// <param name="value">The value to append.</param> /// <param name="format">A format specifier indicating how to convert <paramref name="value"/> to a string.</param> public void Append(uint value, StringView format) { Numeric.FormatUInt32(this, value, format, culture); }
/// <summary> /// Appends the specified value as a string to the current buffer. /// </summary> /// <param name="value">The value to append.</param> /// <param name="format">A format specifier indicating how to convert <paramref name="value"/> to a string.</param> public void Append(ulong value, StringView format) { Numeric.FormatUInt64(this, value, format, culture); }
public static unsafe void Format(StringBuffer formatter, TimeSpan timeSpan, StringView format) { const int tempCharsLength = 7; var tempChars = stackalloc char[tempCharsLength]; formatter.EnsureCapacity(20); if (timeSpan.Ticks < 0) { formatter.Append('-'); timeSpan = new TimeSpan(-timeSpan.Ticks); } var(days, hours, minutes, seconds, ticks) = timeSpan; switch (ParseTimeSpanFormat(format)) { case TimeSpanFormat.Constant: if (days > 0) { formatter.Append(days, StringView.Empty); formatter.Append('.'); } AppendNumber(formatter, hours, 2, tempChars, tempCharsLength); formatter.Append(':'); AppendNumber(formatter, minutes, 2, tempChars, tempCharsLength); formatter.Append(':'); AppendNumber(formatter, seconds, 2, tempChars, tempCharsLength); if (ticks != 0) { formatter.Append('.'); AppendNumber(formatter, ticks, 7, tempChars, tempCharsLength); } break; case TimeSpanFormat.GeneralShort: if (days > 0) { formatter.Append(days, StringView.Empty); formatter.Append(':'); } formatter.Append(hours, StringView.Empty); formatter.Append(':'); AppendNumber(formatter, minutes, 2, tempChars, tempCharsLength); formatter.Append(':'); AppendNumber(formatter, seconds, 2, tempChars, tempCharsLength); if (ticks != 0) { formatter.Append('.'); AppendNumber(formatter, ticks, 7, tempChars, tempCharsLength); formatter.TrimEnd('0'); } break; case TimeSpanFormat.GeneralLong: formatter.Append(days, StringView.Empty); formatter.Append(':'); AppendNumber(formatter, hours, 2, tempChars, tempCharsLength); formatter.Append(':'); AppendNumber(formatter, minutes, 2, tempChars, tempCharsLength); formatter.Append(':'); AppendNumber(formatter, seconds, 2, tempChars, tempCharsLength); formatter.Append('.'); AppendNumber(formatter, ticks, 7, tempChars, tempCharsLength); break; } }
/// <summary> /// Appends the specified value as a string to the current buffer. /// </summary> /// <param name="value">The value to append.</param> /// <param name="format">A format specifier indicating how to convert <paramref name="value"/> to a string.</param> public void Append(float value, StringView format) { Numeric.FormatSingle(this, value, format, culture); }
public static void FormatInt16(StringBuffer formatter, short value, StringView specifier, CachedCulture culture) { if (value < 0 && !specifier.IsEmpty) { // if we're negative and doing a hex format, mask out the bits for the conversion char c = specifier.Data[0]; if (c == 'X' || c == 'x') FormatUInt32(formatter, (uint)(value & 0xFFFF), specifier, culture); } FormatInt32(formatter, value, specifier, culture); }
/// <summary> /// Appends the specified value as a string to the current buffer. /// </summary> /// <param name="value">The value to append.</param> /// <param name="format">A format specifier indicating how to convert <paramref name="value"/> to a string.</param> public void Append(double value, StringView format) { Numeric.FormatDouble(this, value, format, culture); }
/// <summary> /// Appends the specified value as a string to the current buffer. /// </summary> /// <param name="value">The value to append.</param> /// <param name="format">A format specifier indicating how to convert <paramref name="value"/> to a string.</param> public void Append(short value, StringView format) { Numeric.FormatInt16(this, value, format, culture); }
static void NumberToCustomFormatString(StringBuffer formatter, ref Number number, StringView specifier, CachedCulture culture) { }
/// <summary> /// Appends the specified value as a string to the current buffer. /// </summary> /// <param name="value">The value to append.</param> /// <param name="format">A format specifier indicating how to convert <paramref name="value"/> to a string.</param> public void Append(decimal value, StringView format) { Numeric.FormatDecimal(this, (uint *)&value, format, culture); }
public static void FormatUInt64(StringBuffer formatter, ulong value, StringView specifier, CachedCulture culture) { int digits; var fmt = ParseFormatSpecifier(specifier, out digits); // ANDing with 0xFFDF has the effect of uppercasing the character switch (fmt & 0xFFDF) { case 'G': if (digits > 0) goto default; else goto case 'D'; case 'D': UInt64ToDecStr(formatter, value, digits); break; case 'X': // fmt-('X'-'A'+1) gives us the base hex character in either // uppercase or lowercase, depending on the casing of fmt Int64ToHexStr(formatter, value, fmt - ('X' - 'A' + 10), digits); break; default: var number = new Number(); var buffer = stackalloc char[MaxNumberDigits + 1]; number.Digits = buffer; UInt64ToNumber(value, ref number); if (fmt != 0) NumberToString(formatter, ref number, fmt, digits, culture); else NumberToCustomFormatString(formatter, ref number, specifier, culture); break; } }
/// <summary> /// Appends the specified value as a string to the current buffer. /// </summary> /// <param name="value">The value to append.</param> /// <param name="format">A format specifier indicating how to convert <paramref name="value"/> to a string.</param> public void Append(sbyte value, StringView format) { Numeric.FormatSByte(this, value, format, culture); }