コード例 #1
0
ファイル: String8Tests.cs プロジェクト: sharwell/elfie-arriba
        public void String8_FromNumber_Double()
        {
            // TODO: XForm doesn't write to base and exponent notation yet; add tests when it does.

            byte[] buffer = new byte[21];
            Assert.AreEqual("0", String8.FromNumber(0.0, buffer).ToString());
            Assert.AreEqual("1000", String8.FromNumber(1000.0, buffer).ToString());
            Assert.AreEqual("1.5", String8.FromNumber(1.5, buffer).ToString());
            Assert.AreEqual("-1.5", String8.FromNumber(-1.5, buffer).ToString());
            Assert.AreEqual("0.666666666666666", String8.FromNumber((double)2 / (double)3, buffer).ToString());
            Assert.AreEqual("0.75", String8.FromNumber((double)3 / (double)4, buffer).ToString());
            Assert.AreEqual("0.0075", String8.FromNumber(0.0075, buffer).ToString());
            Assert.AreEqual(int.MaxValue.ToString(), String8.FromNumber((double)int.MaxValue, buffer).ToString());
        }
コード例 #2
0
        public NegatedTryConvert TryGetNegatedTryConvert(Type sourceType, Type targetType, object defaultValue)
        {
            // Build a converter for the set of types
            if (targetType == typeof(String8))
            {
                if (sourceType == typeof(string))
                {
                    return(new StringToString8Converter(defaultValue).StringToString8);
                }
                if (sourceType == typeof(DateTime))
                {
                    return(new ToString8Converter <DateTime>(defaultValue, 20, String8.FromDateTime).Convert);
                }
                if (sourceType == typeof(bool))
                {
                    return(new ToString8Converter <bool>(defaultValue, 0, (value, buffer, index) => String8.FromBoolean(value)).Convert);
                }

                if (sourceType == typeof(sbyte))
                {
                    return(new ToString8Converter <sbyte>(defaultValue, 4, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(byte))
                {
                    return(new ToString8Converter <byte>(defaultValue, 3, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(short))
                {
                    return(new ToString8Converter <short>(defaultValue, 6, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(ushort))
                {
                    return(new ToString8Converter <ushort>(defaultValue, 5, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(int))
                {
                    return(new ToString8Converter <int>(defaultValue, 11, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(uint))
                {
                    return(new ToString8Converter <uint>(defaultValue, 10, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(long))
                {
                    return(new ToString8Converter <long>(defaultValue, 21, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(ulong))
                {
                    return(new ToString8Converter <ulong>(defaultValue, 20, (value, buffer, index) => String8.FromNumber(value, false, buffer, index)).Convert);
                }
            }
            else if (sourceType == typeof(String8))
            {
                if (targetType == typeof(int))
                {
                    return(new FromString8Converter <int>(defaultValue, (String8 value, out int result) => value.TryToInteger(out result)).Convert);
                }
                else if (targetType == typeof(uint))
                {
                    return(new FromString8Converter <uint>(defaultValue, (String8 value, out uint result) => value.TryToUInt(out result)).Convert);
                }
                else if (targetType == typeof(DateTime))
                {
                    return(new FromString8Converter <DateTime>(defaultValue, (String8 value, out DateTime result) => value.TryToDateTime(out result)).Convert);
                }
                else if (targetType == typeof(bool))
                {
                    return(new FromString8Converter <bool>(defaultValue, (String8 value, out bool result) => value.TryToBoolean(out result)).Convert);
                }
                else if (targetType == typeof(long))
                {
                    return(new FromString8Converter <long>(defaultValue, (String8 value, out long result) => value.TryToLong(out result)).Convert);
                }
                else if (targetType == typeof(ulong))
                {
                    return(new FromString8Converter <ulong>(defaultValue, (String8 value, out ulong result) => value.TryToULong(out result)).Convert);
                }
                else if (targetType == typeof(ushort))
                {
                    return(new FromString8Converter <ushort>(defaultValue, (String8 value, out ushort result) => value.TryToUShort(out result)).Convert);
                }
                else if (targetType == typeof(short))
                {
                    return(new FromString8Converter <short>(defaultValue, (String8 value, out short result) => value.TryToShort(out result)).Convert);
                }
                else if (targetType == typeof(byte))
                {
                    return(new FromString8Converter <byte>(defaultValue, (String8 value, out byte result) => value.TryToByte(out result)).Convert);
                }
                else if (targetType == typeof(sbyte))
                {
                    return(new FromString8Converter <sbyte>(defaultValue, (String8 value, out sbyte result) => value.TryToSByte(out result)).Convert);
                }
            }

            return(null);
        }
コード例 #3
0
 public void Write(double value)
 {
     Write(String8.FromNumber(value, _convertBuffer, 0));
 }
コード例 #4
0
        public NegatedTryConvert TryGetNegatedTryConvert(Type sourceType, Type targetType, object defaultValue)
        {
            // Build a converter for the set of types
            if (targetType == typeof(String8))
            {
                if (sourceType == typeof(string))
                {
                    return(new StringToString8Converter(defaultValue).StringToString8);
                }
                if (sourceType == typeof(DateTime))
                {
                    return(new ToString8Converter <DateTime>(defaultValue, 20, String8.FromDateTime).Convert);
                }
                if (sourceType == typeof(bool))
                {
                    return(new ToString8Converter <bool>(defaultValue, 0, (value, buffer, index) => String8.FromBoolean(value)).Convert);
                }
                if (sourceType == typeof(TimeSpan))
                {
                    return(new ToString8Converter <TimeSpan>(defaultValue, 21, String8.FromTimeSpan).Convert);
                }

                if (sourceType == typeof(sbyte))
                {
                    return(new ToString8Converter <sbyte>(defaultValue, 4, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(byte))
                {
                    return(new ToString8Converter <byte>(defaultValue, 3, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(short))
                {
                    return(new ToString8Converter <short>(defaultValue, 6, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(ushort))
                {
                    return(new ToString8Converter <ushort>(defaultValue, 5, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(int))
                {
                    return(new ToString8Converter <int>(defaultValue, 11, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(uint))
                {
                    return(new ToString8Converter <uint>(defaultValue, 10, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(long))
                {
                    return(new ToString8Converter <long>(defaultValue, 21, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(ulong))
                {
                    return(new ToString8Converter <ulong>(defaultValue, 20, (value, buffer, index) => String8.FromNumber(value, false, buffer, index)).Convert);
                }

                if (sourceType == typeof(float))
                {
                    return(new ToString8Converter <float>(defaultValue, 21, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
                if (sourceType == typeof(double))
                {
                    return(new ToString8Converter <double>(defaultValue, 21, (value, buffer, index) => String8.FromNumber(value, buffer, index)).Convert);
                }
            }
            else if (sourceType == typeof(String8))
            {
                if (targetType == typeof(int))
                {
                    return(new FromString8Converter <int>(defaultValue, (String8 value, out int result) => value.TryToInteger(out result)).Convert);
                }
                else if (targetType == typeof(uint))
                {
                    return(new FromString8Converter <uint>(defaultValue, (String8 value, out uint result) => value.TryToUInt(out result)).Convert);
                }
                else if (targetType == typeof(DateTime))
                {
                    return(new FromString8Converter <DateTime>(defaultValue, (String8 value, out DateTime result) => value.TryToDateTime(out result)).Convert);
                }
                else if (targetType == typeof(TimeSpan))
                {
                    // Support TimeSpan conversions from .NET Format (DDD.HH:MM:SS.mmm) and 'friendly' format (24h, 7d)
                    return(new FromString8Converter <TimeSpan>(defaultValue, (String8 value, out TimeSpan result) => value.TryToTimeSpanFriendly(out result)).Convert);
                }
                else if (targetType == typeof(bool))
                {
                    return(new FromString8Converter <bool>(defaultValue, (String8 value, out bool result) => value.TryToBoolean(out result)).Convert);
                }
                else if (targetType == typeof(long))
                {
                    return(new FromString8Converter <long>(defaultValue, (String8 value, out long result) => value.TryToLong(out result)).Convert);
                }
                else if (targetType == typeof(ulong))
                {
                    return(new FromString8Converter <ulong>(defaultValue, (String8 value, out ulong result) => value.TryToULong(out result)).Convert);
                }
                else if (targetType == typeof(ushort))
                {
                    return(new FromString8Converter <ushort>(defaultValue, (String8 value, out ushort result) => value.TryToUShort(out result)).Convert);
                }
                else if (targetType == typeof(short))
                {
                    return(new FromString8Converter <short>(defaultValue, (String8 value, out short result) => value.TryToShort(out result)).Convert);
                }
                else if (targetType == typeof(byte))
                {
                    return(new FromString8Converter <byte>(defaultValue, (String8 value, out byte result) => value.TryToByte(out result)).Convert);
                }
                else if (targetType == typeof(sbyte))
                {
                    return(new FromString8Converter <sbyte>(defaultValue, (String8 value, out sbyte result) => value.TryToSByte(out result)).Convert);
                }
                else if (targetType == typeof(double))
                {
                    return(new FromString8Converter <double>(defaultValue, (String8 value, out double result) => value.TryToDouble(out result)).Convert);
                }
                else if (targetType == typeof(float))
                {
                    return(new FromString8Converter <float>(defaultValue, (String8 value, out float result) => value.TryToFloat(out result)).Convert);
                }
            }

            return(null);
        }
コード例 #5
0
 public void Write(double value)
 {
     // Numbers are written without quotes and never need escaping. "ColumnName": -1234
     WriteColumnSeparator();
     String8.FromNumber(value, _typeConversionBuffer).WriteTo(_stream);
 }
コード例 #6
0
 /// <summary>
 ///  Write a double to the current row.
 ///  The value is converted without allocations.
 /// </summary>
 /// <param name="value">Value to write</param>
 public void Write(double value)
 {
     Write(String8.FromNumber(value, _typeConversionBuffer));
 }