private static void Pad(ref StackBuffer buffer, int digits, bool negSign) { // add preceding zeros while (digits-- > 0) { buffer.Append('0'); } // add sign if (negSign) { buffer.Append('-'); } // reverse it into the correct order buffer.Reverse(); }
/// <summary> /// EvalArg /// Entry point for evaluating the generic arguments /// </summary> private static void EvalArg <T>(ref StackBuffer buffer, StackBuffer format, T arg) where T : IConvertible { switch (arg?.GetTypeCode()) { case TypeCode.Int16: { Integer(ref buffer, format, arg.GetTypeCode(), arg.ToInt16(NumberFormatInfo.CurrentInfo), true); break; } case TypeCode.Int32: { Integer(ref buffer, format, arg.GetTypeCode(), arg.ToInt32(NumberFormatInfo.CurrentInfo), true); break; } case TypeCode.Int64: { Long(ref buffer, format, arg.GetTypeCode(), arg.ToInt64(NumberFormatInfo.CurrentInfo), true); break; } case TypeCode.UInt16: { Integer(ref buffer, format, arg.GetTypeCode(), arg.ToUInt16(NumberFormatInfo.CurrentInfo), false); break; } case TypeCode.UInt32: { Integer(ref buffer, format, arg.GetTypeCode(), arg.ToUInt32(NumberFormatInfo.CurrentInfo), false); break; } case TypeCode.UInt64: { ULong(ref buffer, format, arg.GetTypeCode(), arg.ToUInt64(NumberFormatInfo.CurrentInfo), false); break; } case TypeCode.Single: { FloatingPoint(ref buffer, format, arg.ToSingle(NumberFormatInfo.CurrentInfo)); break; } case TypeCode.Double: { FloatingPoint(ref buffer, format, arg.ToDouble(NumberFormatInfo.CurrentInfo)); break; } case TypeCode.String: { buffer.Append(arg.ToString()); break; } } }
/// <summary> /// EvalULong /// Converts 64bit signed and unsigned ints to ascii /// </summary> private static void EvalULong(ref StackBuffer buffer, ulong longVal, int digits, long baseVal, bool negSign) { // add the characters in reverse order do { // Lookup from static char array, to cover hex values too buffer.Append(asciiDigits[longVal % (ulong)baseVal]); longVal /= (ulong)baseVal; digits--; } while (longVal != 0); Pad(ref buffer, digits, negSign); }
/// <summary> /// EvalInt /// Converts 16,32bit signed and unsigned ints to ascii /// </summary> private static void EvalInt(ref StackBuffer buffer, ulong intVal, int digits, ulong baseVal, bool negSign) { // add the characters in reverse order do { // Lookup from static char array, to cover hex values too buffer.Append(asciiDigits[intVal % baseVal]); intVal /= baseVal; digits--; } while (intVal != 0); // add preceding zeros while (digits-- > 0) { buffer.Append('0'); } // add sign if (negSign) { buffer.Append('-'); } // reverse it into the correct order buffer.Reverse(); }
public static void Convert(ref StackBuffer buffer, double f, int precision) { if (f > ulong.MaxValue) { buffer.Append("Out-of-range"); return; } ulong intPart; ulong baseVal = 10; // baseVal has to be the same type as the integer part // check precision bounds if (precision > MAX_PRECISION) { precision = MAX_PRECISION; } // sign stuff if (f < 0) { f = -f; buffer.Append('-'); } if (precision < 0) // negative precision == automatic precision guess { if (f < 1.0) { precision = 6; } else if (f < 10.0) { precision = 5; } else if (f < 100.0) { precision = 4; } else if (f < 1000.0) { precision = 3; } else if (f < 10000.0) { precision = 2; } else if (f < 100000.0) { precision = 1; } else { precision = 0; } } // round value according the precision if (precision > 0) { f += rounders[precision]; } // integer part... intPart = (ulong)Math.Truncate(f); f -= intPart; if (intPart == 0) { buffer.Append('0'); } else { var tempBuffer = new StackBuffer(stackalloc char[MAX_PRECISION]); // convert (reverse order) while (intPart != 0) { tempBuffer.Append((char)('0' + intPart % baseVal)); intPart /= baseVal; } tempBuffer.Reverse(); for (var i = 0; i < tempBuffer.Count; i++) { buffer.Append(tempBuffer[i]); } } // decimal part if (precision != 0) { // place decimal point buffer.Append('.'); // convert while (precision-- != 0) { f *= 10.0; var c = (char)f; buffer.Append((char)('0' + c)); f -= c; } } }