private static bool TryFormatTimeSpanT(TimeSpan value, Span <byte> buffer, FormattingData formattingData, out int bytesWritten)
        {
            bytesWritten = 0;

            if (value.Ticks < 0)
            {
                if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten))
                {
                    return(false);
                }
            }

            if (!TryWriteInt32(Abs((int)value.TotalHours), buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(Abs(value.Minutes), buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }

            return(true);
        }
        public static bool TryFormat(this DateTime value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (format.IsDefault)
            {
                format.Symbol = 'G';
            }
            Precondition.Require(format.Symbol == 'R' || format.Symbol == 'O' || format.Symbol == 'G');

            switch (format.Symbol)
            {
                case 'R':
                    var utc = value.ToUniversalTime();
                    if (formattingData.IsUtf16)
                    {
                        return TryFormatDateTimeRfc1123(utc, buffer, FormattingData.InvariantUtf16, out bytesWritten);
                    }
                    else
                    {
                        return TryFormatDateTimeRfc1123(utc, buffer, FormattingData.InvariantUtf8, out bytesWritten);
                    }
                case 'O':
                    if (formattingData.IsUtf16)
                    {
                        return TryFormatDateTimeFormatO(value, true, buffer, FormattingData.InvariantUtf16, out bytesWritten);
                    }
                    else
                    {
                        return TryFormatDateTimeFormatO(value, true, buffer, FormattingData.InvariantUtf8, out bytesWritten);
                    }
                case 'G':
                    return TryFormatDateTimeFormagG(value, buffer, formattingData, out bytesWritten);
                default:
                    throw new NotImplementedException();
            }      
        }
        internal static bool TryFormatInt64(long value, byte numberOfBytes, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            Precondition.Require(numberOfBytes <= sizeof(long));

            if (value >= 0)
            {
                return TryFormatUInt64(unchecked((ulong)value), numberOfBytes, buffer, format, formattingData, out bytesWritten);
            }
            else if (format.IsHexadecimal)
            {
                ulong bitMask = GetBitMask(numberOfBytes);
                return TryFormatUInt64(unchecked((ulong)value) & bitMask, numberOfBytes, buffer, format, formattingData, out bytesWritten);
            }
            else
            {
                int minusSignBytes = 0;
                if(!formattingData.TryWriteSymbol(FormattingData.Symbol.MinusSign, buffer, out minusSignBytes))
                {
                    bytesWritten = 0;
                    return false;
                }

                int digitBytes = 0;
                if(!TryFormatUInt64(unchecked((ulong)-value), numberOfBytes, buffer.Slice(minusSignBytes), format, formattingData, out digitBytes))
                {
                    bytesWritten = 0;
                    return false;
                }
                bytesWritten = digitBytes + minusSignBytes;
                return true;
            }
        }
        // TODO: format should be ReadOnlySpan<char>
        internal static bool TryFormatInt64(long value, byte numberOfBytes, Span<byte> buffer, Span<char> format, FormattingData formattingData, out int bytesWritten)
        {
            Precondition.Require(numberOfBytes <= sizeof(long));

            Format.Parsed parsedFormat = Format.Parse(format);
            return TryFormatInt64(value, numberOfBytes, buffer, parsedFormat, formattingData, out bytesWritten);
        }
        internal static bool TryFormatUInt64(ulong value, byte numberOfBytes, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if(format.Symbol == 'g')
            {
                format.Symbol = 'G';
            }

            if (format.IsHexadecimal && formattingData.IsUtf16) {
                return TryFormatHexadecimalInvariantCultureUtf16(value, buffer, format, out bytesWritten);
            }

            if (format.IsHexadecimal && formattingData.IsUtf8) {
                return TryFormatHexadecimalInvariantCultureUtf8(value, buffer, format, out bytesWritten);
            }

            if ((formattingData.IsInvariantUtf16) && (format.Symbol == 'D' || format.Symbol == 'G')) {
                return TryFormatDecimalInvariantCultureUtf16(value, buffer, format, out bytesWritten);
            }

            if ((formattingData.IsInvariantUtf8) && (format.Symbol == 'D' || format.Symbol == 'G')) {
                return TryFormatDecimalInvariantCultureUtf8(value, buffer, format, out bytesWritten);
            }

            return TryFormatDecimal(value, buffer, format, formattingData, out bytesWritten);     
        }
Beispiel #6
0
        // Sets up cultures with digits represented by 1 or 5 'A's (0) through 1 or 5 'J's (9) and the minus sigh represented by an underscore followed by a question mark
        static CustomCultureTests()
        {
            byte[][] utf16digitsAndSymbols = new byte[17][];
            for (ushort digit = 0; digit < 10; digit++)
            {
                char digitChar = (char)(digit + 'A');
                var digitString = new string(digitChar, 5);
                utf16digitsAndSymbols[digit] = GetBytesUtf16(digitString);
            }
            utf16digitsAndSymbols[(ushort)FormattingData.Symbol.DecimalSeparator] = GetBytesUtf16(".");
            utf16digitsAndSymbols[(ushort)FormattingData.Symbol.GroupSeparator] = GetBytesUtf16(",");
            utf16digitsAndSymbols[(ushort)FormattingData.Symbol.MinusSign] = GetBytesUtf16("_?");
            Culture5 = new FormattingData(utf16digitsAndSymbols, FormattingData.Encoding.Utf16);

            utf16digitsAndSymbols = new byte[17][];
            for (ushort digit = 0; digit < 10; digit++)
            {
                char digitChar = (char)(digit + 'A');
                var digitString = new string(digitChar, 1);
                utf16digitsAndSymbols[digit] = GetBytesUtf16(digitString);
            }
            utf16digitsAndSymbols[(ushort)FormattingData.Symbol.DecimalSeparator] = GetBytesUtf16(".");
            utf16digitsAndSymbols[(ushort)FormattingData.Symbol.GroupSeparator] = GetBytesUtf16(",");
            utf16digitsAndSymbols[(ushort)FormattingData.Symbol.MinusSign] = GetBytesUtf16("_?");
            Culture1 = new FormattingData(utf16digitsAndSymbols, FormattingData.Encoding.Utf16);
        }
 public static bool TryFormat(this float value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
 {
     if (format.IsDefault)
     {
         format.Symbol = 'G';
     }
     Precondition.Require(format.Symbol == 'G');
     return FloatFormatter.TryFormatNumber(value, true, buffer, format, formattingData, out bytesWritten);
 }
 static bool TryFormatDateTimeRfc1123(DateTime value, Span <byte> buffer, FormattingData formattingData, out int bytesWritten)
 {
     bytesWritten = 0;
     if (!TryWriteString(s_dayNames[(int)value.DayOfWeek], buffer, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteInt32(value.Day, buffer, D2, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteChar(' ', buffer, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteString(s_monthNames[value.Month - 1], buffer, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteChar(' ', buffer, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteInt32(value.Year, buffer, D4, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteChar(' ', buffer, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteInt32(value.Hour, buffer, D2, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteInt32(value.Minute, buffer, D2, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteInt32(value.Second, buffer, D2, formattingData, ref bytesWritten))
     {
         return(false);
     }
     if (!TryWriteString(" GMT", buffer, formattingData, ref bytesWritten))
     {
         return(false);
     }
     return(true);
 }
        public static bool TryParse(Utf8String utf8Text, FormattingData cultureAndEncodingInfo, Format.Parsed numericFormat, 
            out ulong value, out int bytesConsumed)
        {
            // Precondition replacement
            if (utf8Text.Length < 1)
            {
                value = 0;
                bytesConsumed = 0;
                return false;
            }

            value = 0;
            bytesConsumed = 0;

            if (cultureAndEncodingInfo.IsInvariantUtf8)
            {
                for (int byteIndex = 0; byteIndex < utf8Text.Length; byteIndex++)
                {
                    byte nextByteVal = (byte)((byte)utf8Text[byteIndex] - '0');
                    if (nextByteVal > 9)
                    {
                        if (bytesConsumed == 0)
                        {
                            value = default(ulong);
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else if (value > UInt64.MaxValue / 10) // overflow
                    {
                        value = 0;
                        bytesConsumed = 0;
                        return false;
                    }
                    else if (UInt64.MaxValue - value * 10 < (ulong)(nextByteVal)) // overflow
                    {
                        value = 0;
                        bytesConsumed = 0;
                        return false;
                    }
                    ulong candidate = value * 10 + nextByteVal;

                    value = candidate;
                    bytesConsumed++;
                }

                return true;
            }

            return false;
        }
Beispiel #10
0
        static bool TryWriteString(string s, Span <byte> buffer, FormattingData formattingData, ref int bytesWritten)
        {
            int written;

            if (!s.TryFormat(buffer.Slice(bytesWritten), default(Format.Parsed), formattingData, out written))
            {
                bytesWritten = 0;
                return(false);
            }
            bytesWritten += written;
            return(true);
        }
        public bool TryFormat(Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (!PrimitiveFormatters.TryFormat(_age, buffer, format, formattingData, out bytesWritten)) return false;


            char symbol = _inMonths ? 'm' : 'y';
            int symbolBytes;
            if (!PrimitiveFormatters.TryFormat(symbol, buffer.Slice(bytesWritten), format, formattingData, out symbolBytes)) return false;

            bytesWritten += symbolBytes;
            return true;
        }
        public static bool TryFormat(this char value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (formattingData.IsUtf16)
            {
                if (buffer.Length < 2)
                {
                    bytesWritten = 0;
                    return false;
                }
                buffer[0] = (byte)value;
                buffer[1] = (byte)(value >> 8);
                bytesWritten = 2;
                return true;
            }

            if (buffer.Length < 1)
            {
                bytesWritten = 0;
                return false;
            }

            // fast path for ASCII
            if (value <= 127)
            {
                buffer[0] = (byte)value;
                bytesWritten = 1;
                return true;
            }

            // TODO: This can be directly encoded to SpanByte. There is no conversion between spans yet
            var encoded = new Utf8EncodedCodePoint(value);
            bytesWritten = encoded.Length;
            if (buffer.Length < bytesWritten)
            {
                bytesWritten = 0;
                return false;
            }

            buffer[0] = encoded.Byte0;
            if(bytesWritten > 1)
            {
                buffer[1] = encoded.Byte1;
            }
            if(bytesWritten > 2)
            {
                buffer[2] = encoded.Byte2;
            }
            if(bytesWritten > 3)
            {
                buffer[3] = encoded.Byte3;
            }
            return true;
        }
Beispiel #13
0
        // it might be worth compacting the data into a single byte array.
        // Also, it would be great if we could freeze it.
        static FormattingData()
        {
            var utf16digitsAndSymbols = new byte[][] {
                new byte[] { 48, 0, }, // digit 0
                new byte[] { 49, 0, },
                new byte[] { 50, 0, },
                new byte[] { 51, 0, },
                new byte[] { 52, 0, },
                new byte[] { 53, 0, },
                new byte[] { 54, 0, },
                new byte[] { 55, 0, },
                new byte[] { 56, 0, },
                new byte[] { 57, 0, }, // digit 9
                new byte[] { 46, 0, }, // decimal separator
                new byte[] { 44, 0, }, // group separator
                new byte[] { 73, 0, 110, 0, 102, 0, 105, 0, 110, 0, 105, 0, 116, 0, 121, 0, }, // Infinity
                new byte[] { 45, 0, }, // minus sign 
                new byte[] { 43, 0, }, // plus sign 
                new byte[] { 78, 0, 97, 0, 78, 0, }, // NaN
                new byte[] { 69, 0, }, // E
            };

            s_invariantUtf16 = new FormattingData(utf16digitsAndSymbols, Encoding.Utf16);

            var utf8digitsAndSymbols = new byte[][] {
                new byte[] { 48, },
                new byte[] { 49, },
                new byte[] { 50, },
                new byte[] { 51, },
                new byte[] { 52, },
                new byte[] { 53, },
                new byte[] { 54, },
                new byte[] { 55, },
                new byte[] { 56, },
                new byte[] { 57, }, // digit 9
                new byte[] { 46, }, // decimal separator
                new byte[] { 44, }, // group separator
                new byte[] { 73, 110, 102, 105, 110, 105, 116, 121, },
                new byte[] { 45, }, // minus sign
                new byte[] { 43, }, // plus sign
                new byte[] { 78, 97, 78, }, // NaN
                new byte[] { 69, }, // E
            };

            s_invariantUtf8 = new FormattingData(utf8digitsAndSymbols, Encoding.Utf8);
        }
Beispiel #14
0
        // it might be worth compacting the data into a single byte array.
        // Also, it would be great if we could freeze it.
        static FormattingData()
        {
            var utf16digitsAndSymbols = new byte[][] {
                new byte[] { 48, 0, }, // digit 0
                new byte[] { 49, 0, },
                new byte[] { 50, 0, },
                new byte[] { 51, 0, },
                new byte[] { 52, 0, },
                new byte[] { 53, 0, },
                new byte[] { 54, 0, },
                new byte[] { 55, 0, },
                new byte[] { 56, 0, },
                new byte[] { 57, 0, },                                                         // digit 9
                new byte[] { 46, 0, },                                                         // decimal separator
                new byte[] { 44, 0, },                                                         // group separator
                new byte[] { 73, 0, 110, 0, 102, 0, 105, 0, 110, 0, 105, 0, 116, 0, 121, 0, }, // Infinity
                new byte[] { 45, 0, },                                                         // minus sign
                new byte[] { 43, 0, },                                                         // plus sign
                new byte[] { 78, 0, 97, 0, 78, 0, },                                           // NaN
                new byte[] { 69, 0, },                                                         // E
            };

            s_invariantUtf16 = new FormattingData(utf16digitsAndSymbols, Encoding.Utf16);

            var utf8digitsAndSymbols = new byte[][] {
                new byte[] { 48, },
                new byte[] { 49, },
                new byte[] { 50, },
                new byte[] { 51, },
                new byte[] { 52, },
                new byte[] { 53, },
                new byte[] { 54, },
                new byte[] { 55, },
                new byte[] { 56, },
                new byte[] { 57, },         // digit 9
                new byte[] { 46, },         // decimal separator
                new byte[] { 44, },         // group separator
                new byte[] { 73, 110, 102, 105, 110, 105, 116, 121, },
                new byte[] { 45, },         // minus sign
                new byte[] { 43, },         // plus sign
                new byte[] { 78, 97, 78, }, // NaN
                new byte[] { 69, },         // E
            };

            s_invariantUtf8 = new FormattingData(utf8digitsAndSymbols, Encoding.Utf8);
        }
Beispiel #15
0
        public static bool TryFormatNumber(double value, bool isSingle, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            Precondition.Require(format.Symbol == 'G' || format.Symbol == 'E' || format.Symbol == 'F');

            bytesWritten = 0;
            int written;

            if (Double.IsNaN(value))
            {
                return formattingData.TryWriteSymbol(FormattingData.Symbol.NaN, buffer, out bytesWritten);
            }

            if (Double.IsInfinity(value))
            {
                if (Double.IsNegativeInfinity(value))
                {
                    if (!formattingData.TryWriteSymbol(FormattingData.Symbol.MinusSign, buffer, out written))
                    {
                        bytesWritten = 0;
                        return false;
                    }
                    bytesWritten += written;
                }
                if (!formattingData.TryWriteSymbol(FormattingData.Symbol.InfinitySign, buffer.Slice(bytesWritten), out written))
                {
                    bytesWritten = 0;
                    return false;
                }
                bytesWritten += written;
                return true;
            }

            // TODO: the lines below need to be replaced with properly implemented algorithm
            // the problem is the algorithm is complex, so I am commiting a stub for now
            var hack = value.ToString(format.Symbol.ToString());
            return hack.TryFormat(buffer, default(Format.Parsed), formattingData, out bytesWritten);
        }
		public static unsafe bool TryParse(byte* utf8Text, int index, int length, FormattingData cultureAndEncodingInfo, Format.Parsed numericFormat, 
            out ulong value, out int bytesConsumed)
        {
            // Precondition replacement
            if (length < 1 || index < 0)
            {
                value = default(ulong);
                bytesConsumed = 0;
                return false;
            }

            value = default(ulong);
            bytesConsumed = 0;

            if (cultureAndEncodingInfo.IsInvariantUtf8)
            {
                for (int byteIndex = index; byteIndex < length + index; byteIndex++)
                {
                    byte nextByteVal = (byte)(utf8Text[byteIndex] - '0');
                    if (nextByteVal > 9) // if nextByteVal > 9, we know it is not a digit because any value less than '0' will overflow
										 // to greater than 9 since byte is an unsigned type.
                    {
                        if (bytesConsumed == 0)
                        {
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else if (value > UInt64.MaxValue / 10) // overflow
                    {
                        value = default(ulong);
                        bytesConsumed = 0;
                        return false;
                    }
                    // This next check uses a hardcoded 6 because the max values for unsigned types all end in 5s.
                    else if (value == UInt64.MaxValue / 10 &&  nextByteVal >= 6) // overflow
                    {
                        value = default(ulong);
                        bytesConsumed = 0;
                        return false;
                    }
                    value = (ulong)(value * 10 + nextByteVal); // left shift the value and add the nextByte
                    bytesConsumed++; // increment the number of bytes consumed, then loop
                }
                return true;
            }
            else if (cultureAndEncodingInfo.IsInvariantUtf16)
            {
                for (int byteIndex = index; byteIndex < length + index - 1; byteIndex += 2) // loop through the byte array two bytes at a time for UTF-16
                {
                    byte byteAfterNext = utf8Text[byteIndex + 1];
                    byte nextByteVal = (byte)(utf8Text[byteIndex] - '0');
                    if (nextByteVal > 9 || byteAfterNext != 0) // if the second byte isn't zero, this isn't an ASCII-equivalent code unit and we can quit here
															   // if nextByteVal > 9, we know it is not a digit because any value less than '0' will overflow
															   // to greater than 9 since byte is an unsigned type.
                    {
                        if (bytesConsumed == 0) // check to see if we've processed any digits at all
                        {
                            return false;
                        }
                        else
                        {
                            return true; // otherwise return true
                        }
                    }
                    else if (value > UInt64.MaxValue / 10)
                    {
                        value = default(ulong);
                        bytesConsumed = 0;
                        return false;
                    }
                    // This next check uses a hardcoded 6 because the max values for unsigned types all end in 5s.
                    else if (value == UInt64.MaxValue / 10 &&  nextByteVal >= 6) // overflow
                    {
                        value = default(ulong);
                        bytesConsumed = 0;
                        return false;
                    }

                    value = (ulong)(value * 10 + nextByteVal); // left shift the value and add the nextByte
                    bytesConsumed += 2;
                }
                return true;
            }

            return false;
        }
        static bool TryFormatDateTimeFormatO(DateTimeOffset value, bool isDateTime, Span <byte> buffer, FormattingData formattingData, out int bytesWritten)
        {
            bytesWritten = 0;
            if (!TryWriteInt32(value.Year, buffer, D4, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(value.Month, buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(value.Day, buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar('T', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(value.Hour, buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(value.Minute, buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(value.Second, buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }

            // add optional fractional second only if needed...
            var rounded = new DateTimeOffset(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, TimeSpan.Zero);
            var delta   = value - rounded;

            if (delta.Ticks != 0)
            {
                if (!TryWriteChar('.', buffer, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                var timeFrac = delta.Ticks * FractionalTimeScale / System.TimeSpan.TicksPerSecond;
                if (!TryWriteInt64(timeFrac, buffer, D7, formattingData, ref bytesWritten))
                {
                    return(false);
                }
            }

            if (isDateTime)
            {
                if (!TryWriteChar('Z', buffer, formattingData, ref bytesWritten))
                {
                    return(false);
                }
            }
            else
            {
                if (!TryWriteChar('+', buffer, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                int bytes;
                if (!value.Offset.TryFormat(buffer.Slice(bytesWritten), t, formattingData, out bytes))
                {
                    return(false);
                }
                bytesWritten += bytes;
            }

            return(true);
        }
Beispiel #18
0
 public HttpHeaderBuffer(Span<byte> bytes, FormattingData formattingData)
 {
     _bytes = bytes;
     _formattingData = formattingData;
 }
        private static bool TryFormatTimeSpanG(TimeSpan value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            bytesWritten = 0;

            if (value.Ticks < 0)
            {
                if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten))
                {
                    return(false);
                }
            }

            bool daysWritten = false;

            if (value.Days != 0 || format.Symbol == 'G')
            {
                if (!TryWriteInt32(Abs(value.Days), buffer, default(Format.Parsed), formattingData, ref bytesWritten))
                {
                    return(false);
                }
                daysWritten = true;
                if (format.Symbol == 'c')
                {
                    if (!TryWriteChar('.', buffer, formattingData, ref bytesWritten))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten))
                    {
                        return(false);
                    }
                }
            }

            var hourFormat = default(Format.Parsed);

            if ((daysWritten || format.Symbol == 'c') && format.Symbol != 'g')
            {
                hourFormat = D2;
            }
            if (!TryWriteInt32(Abs(value.Hours), buffer, hourFormat, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(Abs(value.Minutes), buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(Abs(value.Seconds), buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }

            long remainingTicks;

            if (value.Ticks != long.MinValue)
            {
                remainingTicks = Abs(value.Ticks) % TimeSpan.TicksPerSecond;
            }
            else
            {
                remainingTicks = long.MaxValue % TimeSpan.TicksPerSecond;
                remainingTicks = (remainingTicks + 1) % TimeSpan.TicksPerSecond;
            }

            var ticksFormat = D7;

            if (remainingTicks != 0)
            {
                if (!TryWriteChar('.', buffer, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                var fraction = remainingTicks * FractionalTimeScale / TimeSpan.TicksPerSecond;
                if (!TryWriteInt64(fraction, buffer, ticksFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
            }

            return(true);
        }
        // TODO: this whole routine is too slow. It does div and mod twice, which are both costly (especially that some JITs cannot optimize it).
        // It does it twice to avoid reversing the formatted buffer, which can be tricky given it should handle arbitrary cultures.
        // One optimization I thought we could do is to do div/mod once and store digits in a temp buffer (but that would allocate). Modification to the idea would be to store the digits in a local struct
        // Another idea possibly worth tying would be to special case cultures that have constant digit size, and go back to the format + reverse buffer approach.
        private static bool TryFormatDecimal(ulong value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if(format.IsDefault)
            {
                format.Symbol = 'G';
            }
            format.Symbol = Char.ToUpperInvariant(format.Symbol); // TODO: this is costly. I think the transformation should happen in Parse
            Precondition.Require(format.Symbol == 'D' || format.Symbol == 'G' || format.Symbol == 'N');

            // Reverse value on decimal basis, count digits and trailing zeros before the decimal separator
            ulong reversedValueExceptFirst = 0;
            var digitsCount = 1;
            var trailingZerosCount = 0;

            // We reverse the digits in numeric form because reversing encoded digits is hard and/or costly.
            // If value contains 20 digits, its reversed value will not fit into ulong size.
            // So reverse it till last digit (reversedValueExceptFirst will have all the digits except the first one).
            while (value >= 10)
            {
                var digit = value % 10UL;
                value = value / 10UL;

                if (reversedValueExceptFirst == 0 && digit == 0)
                {
                    trailingZerosCount++;
                }
                else
                {
                    reversedValueExceptFirst = reversedValueExceptFirst * 10UL + digit;
                    digitsCount++;
                }
            }

            bytesWritten = 0;
            int digitBytes;
            // If format is D and precision is greater than digitsCount + trailingZerosCount, append leading zeros
            if (format.Symbol == 'D' && format.HasPrecision)
            {
                var leadingZerosCount = format.Precision - digitsCount - trailingZerosCount;
                while (leadingZerosCount-- > 0)
                {
                    if (!formattingData.TryWriteDigitOrSymbol(0, buffer.Slice(bytesWritten), out digitBytes))
                    {
                        bytesWritten = 0;
                        return false;
                    }
                    bytesWritten += digitBytes;
                }
            }

            // Append first digit
            if (!formattingData.TryWriteDigit(value, buffer.Slice(bytesWritten), out digitBytes))
            {
                bytesWritten = 0;
                return false;
            }
            bytesWritten += digitBytes;
            digitsCount--;

            if (format.Symbol == 'N')
            {
                const int GroupSize = 3;

                // Count amount of digits before first group separator. It will be reset to groupSize every time digitsLeftInGroup == zero
                var digitsLeftInGroup = (digitsCount + trailingZerosCount) % GroupSize;
                if (digitsLeftInGroup == 0)
                {
                    if (digitsCount + trailingZerosCount > 0)
                    {
                        // There is a new group immediately after the first digit
                        if (!formattingData.TryWriteSymbol(FormattingData.Symbol.GroupSeparator, buffer.Slice(bytesWritten), out digitBytes))
                        {
                            bytesWritten = 0;
                            return false;
                        }
                        bytesWritten += digitBytes;
                    }
                    digitsLeftInGroup = GroupSize;
                }

                // Append digits
                while (reversedValueExceptFirst > 0)
                {
                    if (digitsLeftInGroup == 0)
                    {
                        if (!formattingData.TryWriteSymbol(FormattingData.Symbol.GroupSeparator, buffer.Slice(bytesWritten), out digitBytes))
                        {
                            bytesWritten = 0;
                            return false;
                        }
                        bytesWritten += digitBytes;
                        digitsLeftInGroup = GroupSize;
                    }

                    var nextDigit = reversedValueExceptFirst % 10UL;
                    reversedValueExceptFirst = reversedValueExceptFirst / 10UL;

                    if (!formattingData.TryWriteDigit(nextDigit, buffer.Slice(bytesWritten), out digitBytes))
                    {
                        bytesWritten = 0;
                        return false;
                    }
                    bytesWritten += digitBytes;
                    digitsLeftInGroup--;
                }

                // Append trailing zeros if any
                while (trailingZerosCount-- > 0)
                {
                    if (digitsLeftInGroup == 0)
                    {
                        if (!formattingData.TryWriteSymbol(FormattingData.Symbol.GroupSeparator, buffer.Slice(bytesWritten), out digitBytes))
                        {
                            bytesWritten = 0;
                            return false;
                        }
                        bytesWritten += digitBytes;
                        digitsLeftInGroup = GroupSize;
                    }

                    if (!formattingData.TryWriteDigitOrSymbol(0, buffer.Slice(bytesWritten), out digitBytes))
                    {
                        bytesWritten = 0;
                        return false;
                    }
                    bytesWritten += digitBytes;
                    digitsLeftInGroup--;
                }
            }
            else
            {
                while (reversedValueExceptFirst > 0)
                {
                    var bufferSlice = buffer.Slice(bytesWritten);
                    var nextDigit = reversedValueExceptFirst % 10UL;
                    reversedValueExceptFirst = reversedValueExceptFirst / 10UL;
                    if (!formattingData.TryWriteDigit(nextDigit, bufferSlice, out digitBytes))
                    {
                        bytesWritten = 0;
                        return false;
                    }
                    bytesWritten += digitBytes;
                }

                // Append trailing zeros if any
                while (trailingZerosCount-- > 0)
                {
                    if (!formattingData.TryWriteDigitOrSymbol(0, buffer.Slice(bytesWritten), out digitBytes))
                    {
                        bytesWritten = 0;
                        return false;
                    }
                    bytesWritten += digitBytes;
                }
            }

            // If format is N and precision is not defined or is greater than zero, append trailing zeros after decimal point
            if (format.Symbol == 'N')
            {
                int trailingZerosAfterDecimalCount = format.HasPrecision ? format.Precision : 2;

                if (trailingZerosAfterDecimalCount > 0)
                {
                    if (!formattingData.TryWriteSymbol(FormattingData.Symbol.DecimalSeparator, buffer.Slice(bytesWritten), out digitBytes))
                    {
                        bytesWritten = 0;
                        return false;
                    }
                    bytesWritten += digitBytes;

                    while (trailingZerosAfterDecimalCount-- > 0)
                    {
                        if (!formattingData.TryWriteDigitOrSymbol(0, buffer.Slice(bytesWritten), out digitBytes))
                        {
                            bytesWritten = 0;
                            return false;
                        }
                        bytesWritten += digitBytes;
                    }
                }
            }

            return true;
        }
        public static unsafe bool TryParse(byte* utf8Text, int index, int length, FormattingData cultureAndEncodingInfo,
            Format.Parsed numericFormat, out bool value, out int bytesConsumed)
        {
            bytesConsumed = 0;
            value = default(bool);

            if (length < 1 || index < 0)
            {
                return false;
            }

            if (cultureAndEncodingInfo.IsInvariantUtf8)
            {
                byte firstByte = utf8Text[index];

                if (firstByte == '1')
                {
                    bytesConsumed = 1;
                    value = true;
                    return true;
                }
                else if (firstByte == '0')
                {
                    bytesConsumed = 1;
                    value = false;
                    return true;
                }
                else if (IsTrue(utf8Text, index, length))
                {
                    bytesConsumed = 4;
                    value = true;
                    return true;
                }
                else if (IsFalse(utf8Text, index, length))
                {
                    bytesConsumed = 5;
                    value = false;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            return false;
        }
 public static bool TryFormat(this float value, Span<byte> buffer, Span<char> format, FormattingData formattingData, out int bytesWritten)
 {
     Format.Parsed parsedFormat = Format.Parse(format);
     return TryFormat(value, buffer, parsedFormat, formattingData, out bytesWritten);
 }
        public static bool TryFormat(this string value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {

            if (formattingData.IsUtf16)
            {
                var valueBytes = value.Length << 1;
                if (valueBytes > buffer.Length)
                {
                    bytesWritten = 0;
                    return false;
                }

                unsafe
                {
                    fixed (char* pCharacters = value)
                    {
                        byte* pBytes = (byte*)pCharacters;
                        buffer.Set(pBytes, valueBytes);
                    }
                }

                bytesWritten = valueBytes;
                return true;
            }

                
            var avaliableBytes = buffer.Length;
            bytesWritten = 0;
            for (int i = 0; i < value.Length; i++)
            {
                var c = value[i];

                var codepoint = (ushort)c;
                if (codepoint <= 0x7f) // this if block just optimizes for ascii
                {
                    if (bytesWritten + 1 > avaliableBytes)
                    {
                        bytesWritten = 0;
                        return false;
                    }
                    buffer[bytesWritten++] = (byte)codepoint;
                }
                else
                {
                    Utf8EncodedCodePoint encoded;
                    if (!char.IsSurrogate(c))
                        encoded = new Utf8EncodedCodePoint(c);
                    else
                    {
                        if (++i >= value.Length)
                            throw new ArgumentException("Invalid surrogate pair.", nameof(value));
                        char lowSurrogate = value[i];
                        encoded = new Utf8EncodedCodePoint(c, lowSurrogate);
                    }
                            

                    if (bytesWritten + encoded.Length > avaliableBytes)
                    {
                        bytesWritten = 0;
                        return false;
                    }

                    buffer[bytesWritten] = encoded.Byte0;
                    if (encoded.Length > 1)
                    {
                        buffer[bytesWritten + 1] = encoded.Byte1;

                        if (encoded.Length > 2)
                        {
                            buffer[bytesWritten + 2] = encoded.Byte2;

                            if (encoded.Length > 3)
                            {
                                buffer[bytesWritten + 3] = encoded.Byte3;
                            }
                        }
                    }

                    bytesWritten += encoded.Length;
                }
            }
            return true;
        }
        public static bool TryFormat(this Utf8String value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (formattingData.IsUtf16) {
                throw new NotImplementedException();
            }

            if(buffer.Length < value.Length) {
                bytesWritten = 0;
                return false;
            }

            buffer.Set(value.Bytes);
            bytesWritten = value.Length;
            return true;
        }
Beispiel #25
0
        internal static bool TryFormatUInt64(ulong value, byte numberOfBytes, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (format.Symbol == 'g')
            {
                format.Symbol = 'G';
            }

            if (format.IsHexadecimal && formattingData.IsUtf16)
            {
                return(TryFormatHexadecimalInvariantCultureUtf16(value, buffer, format, out bytesWritten));
            }

            if (format.IsHexadecimal && formattingData.IsUtf8)
            {
                return(TryFormatHexadecimalInvariantCultureUtf8(value, buffer, format, out bytesWritten));
            }

            if ((formattingData.IsInvariantUtf16) && (format.Symbol == 'D' || format.Symbol == 'G'))
            {
                return(TryFormatDecimalInvariantCultureUtf16(value, buffer, format, out bytesWritten));
            }

            if ((formattingData.IsInvariantUtf8) && (format.Symbol == 'D' || format.Symbol == 'G'))
            {
                return(TryFormatDecimalInvariantCultureUtf8(value, buffer, format, out bytesWritten));
            }

            return(TryFormatDecimal(value, buffer, format, formattingData, out bytesWritten));
        }
Beispiel #26
0
        public static bool TryFormatNumber(double value, bool isSingle, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            Precondition.Require(format.Symbol == 'G' || format.Symbol == 'E' || format.Symbol == 'F');

            bytesWritten = 0;
            int written;

            if (Double.IsNaN(value))
            {
                return(formattingData.TryWriteSymbol(FormattingData.Symbol.NaN, buffer, out bytesWritten));
            }

            if (Double.IsInfinity(value))
            {
                if (Double.IsNegativeInfinity(value))
                {
                    if (!formattingData.TryWriteSymbol(FormattingData.Symbol.MinusSign, buffer, out written))
                    {
                        bytesWritten = 0;
                        return(false);
                    }
                    bytesWritten += written;
                }
                if (!formattingData.TryWriteSymbol(FormattingData.Symbol.InfinitySign, buffer.Slice(bytesWritten), out written))
                {
                    bytesWritten = 0;
                    return(false);
                }
                bytesWritten += written;
                return(true);
            }

            // TODO: the lines below need to be replaced with properly implemented algorithm
            // the problem is the algorithm is complex, so I am commiting a stub for now
            var hack = value.ToString(format.Symbol.ToString());

            return(hack.TryFormat(buffer, default(Format.Parsed), formattingData, out bytesWritten));
        }
Beispiel #27
0
 internal static bool TryFormatUInt64(ulong value, byte numberOfBytes, Span <byte> buffer, Span <char> format, FormattingData formattingData, out int bytesWritten)
 {
     Format.Parsed parsedFormat = Format.Parse(format);
     return(TryFormatUInt64(value, numberOfBytes, buffer, parsedFormat, formattingData, out bytesWritten));
 }
Beispiel #28
0
        // TODO: this whole routine is too slow. It does div and mod twice, which are both costly (especially that some JITs cannot optimize it).
        // It does it twice to avoid reversing the formatted buffer, which can be tricky given it should handle arbitrary cultures.
        // One optimization I thought we could do is to do div/mod once and store digits in a temp buffer (but that would allocate). Modification to the idea would be to store the digits in a local struct
        // Another idea possibly worth tying would be to special case cultures that have constant digit size, and go back to the format + reverse buffer approach.
        private static bool TryFormatDecimal(ulong value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (format.IsDefault)
            {
                format.Symbol = 'G';
            }
            format.Symbol = Char.ToUpperInvariant(format.Symbol); // TODO: this is costly. I think the transformation should happen in Parse
            Precondition.Require(format.Symbol == 'D' || format.Symbol == 'G' || format.Symbol == 'N');

            // Reverse value on decimal basis, count digits and trailing zeros before the decimal separator
            ulong reversedValueExceptFirst = 0;
            var   digitsCount        = 1;
            var   trailingZerosCount = 0;

            // We reverse the digits in numeric form because reversing encoded digits is hard and/or costly.
            // If value contains 20 digits, its reversed value will not fit into ulong size.
            // So reverse it till last digit (reversedValueExceptFirst will have all the digits except the first one).
            while (value >= 10)
            {
                var digit = value % 10UL;
                value = value / 10UL;

                if (reversedValueExceptFirst == 0 && digit == 0)
                {
                    trailingZerosCount++;
                }
                else
                {
                    reversedValueExceptFirst = reversedValueExceptFirst * 10UL + digit;
                    digitsCount++;
                }
            }

            bytesWritten = 0;
            int digitBytes;

            // If format is D and precision is greater than digitsCount + trailingZerosCount, append leading zeros
            if (format.Symbol == 'D' && format.HasPrecision)
            {
                var leadingZerosCount = format.Precision - digitsCount - trailingZerosCount;
                while (leadingZerosCount-- > 0)
                {
                    if (!formattingData.TryWriteDigitOrSymbol(0, buffer.Slice(bytesWritten), out digitBytes))
                    {
                        bytesWritten = 0;
                        return(false);
                    }
                    bytesWritten += digitBytes;
                }
            }

            // Append first digit
            if (!formattingData.TryWriteDigit(value, buffer.Slice(bytesWritten), out digitBytes))
            {
                bytesWritten = 0;
                return(false);
            }
            bytesWritten += digitBytes;
            digitsCount--;

            if (format.Symbol == 'N')
            {
                const int GroupSize = 3;

                // Count amount of digits before first group separator. It will be reset to groupSize every time digitsLeftInGroup == zero
                var digitsLeftInGroup = (digitsCount + trailingZerosCount) % GroupSize;
                if (digitsLeftInGroup == 0)
                {
                    if (digitsCount + trailingZerosCount > 0)
                    {
                        // There is a new group immediately after the first digit
                        if (!formattingData.TryWriteSymbol(FormattingData.Symbol.GroupSeparator, buffer.Slice(bytesWritten), out digitBytes))
                        {
                            bytesWritten = 0;
                            return(false);
                        }
                        bytesWritten += digitBytes;
                    }
                    digitsLeftInGroup = GroupSize;
                }

                // Append digits
                while (reversedValueExceptFirst > 0)
                {
                    if (digitsLeftInGroup == 0)
                    {
                        if (!formattingData.TryWriteSymbol(FormattingData.Symbol.GroupSeparator, buffer.Slice(bytesWritten), out digitBytes))
                        {
                            bytesWritten = 0;
                            return(false);
                        }
                        bytesWritten     += digitBytes;
                        digitsLeftInGroup = GroupSize;
                    }

                    var nextDigit = reversedValueExceptFirst % 10UL;
                    reversedValueExceptFirst = reversedValueExceptFirst / 10UL;

                    if (!formattingData.TryWriteDigit(nextDigit, buffer.Slice(bytesWritten), out digitBytes))
                    {
                        bytesWritten = 0;
                        return(false);
                    }
                    bytesWritten += digitBytes;
                    digitsLeftInGroup--;
                }

                // Append trailing zeros if any
                while (trailingZerosCount-- > 0)
                {
                    if (digitsLeftInGroup == 0)
                    {
                        if (!formattingData.TryWriteSymbol(FormattingData.Symbol.GroupSeparator, buffer.Slice(bytesWritten), out digitBytes))
                        {
                            bytesWritten = 0;
                            return(false);
                        }
                        bytesWritten     += digitBytes;
                        digitsLeftInGroup = GroupSize;
                    }

                    if (!formattingData.TryWriteDigitOrSymbol(0, buffer.Slice(bytesWritten), out digitBytes))
                    {
                        bytesWritten = 0;
                        return(false);
                    }
                    bytesWritten += digitBytes;
                    digitsLeftInGroup--;
                }
            }
            else
            {
                while (reversedValueExceptFirst > 0)
                {
                    var bufferSlice = buffer.Slice(bytesWritten);
                    var nextDigit   = reversedValueExceptFirst % 10UL;
                    reversedValueExceptFirst = reversedValueExceptFirst / 10UL;
                    if (!formattingData.TryWriteDigit(nextDigit, bufferSlice, out digitBytes))
                    {
                        bytesWritten = 0;
                        return(false);
                    }
                    bytesWritten += digitBytes;
                }

                // Append trailing zeros if any
                while (trailingZerosCount-- > 0)
                {
                    if (!formattingData.TryWriteDigitOrSymbol(0, buffer.Slice(bytesWritten), out digitBytes))
                    {
                        bytesWritten = 0;
                        return(false);
                    }
                    bytesWritten += digitBytes;
                }
            }

            // If format is N and precision is not defined or is greater than zero, append trailing zeros after decimal point
            if (format.Symbol == 'N')
            {
                int trailingZerosAfterDecimalCount = format.HasPrecision ? format.Precision : 2;

                if (trailingZerosAfterDecimalCount > 0)
                {
                    if (!formattingData.TryWriteSymbol(FormattingData.Symbol.DecimalSeparator, buffer.Slice(bytesWritten), out digitBytes))
                    {
                        bytesWritten = 0;
                        return(false);
                    }
                    bytesWritten += digitBytes;

                    while (trailingZerosAfterDecimalCount-- > 0)
                    {
                        if (!formattingData.TryWriteDigitOrSymbol(0, buffer.Slice(bytesWritten), out digitBytes))
                        {
                            bytesWritten = 0;
                            return(false);
                        }
                        bytesWritten += digitBytes;
                    }
                }
            }

            return(true);
        }
Beispiel #29
0
        internal static bool TryFormatInt64(long value, byte numberOfBytes, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            Precondition.Require(numberOfBytes <= sizeof(long));

            if (value >= 0)
            {
                return(TryFormatUInt64(unchecked ((ulong)value), numberOfBytes, buffer, format, formattingData, out bytesWritten));
            }
            else if (format.IsHexadecimal)
            {
                ulong bitMask = GetBitMask(numberOfBytes);
                return(TryFormatUInt64(unchecked ((ulong)value) & bitMask, numberOfBytes, buffer, format, formattingData, out bytesWritten));
            }
            else
            {
                int minusSignBytes = 0;
                if (!formattingData.TryWriteSymbol(FormattingData.Symbol.MinusSign, buffer, out minusSignBytes))
                {
                    bytesWritten = 0;
                    return(false);
                }

                int digitBytes = 0;
                if (!TryFormatUInt64(unchecked ((ulong)-value), numberOfBytes, buffer.Slice(minusSignBytes), format, formattingData, out digitBytes))
                {
                    bytesWritten = 0;
                    return(false);
                }
                bytesWritten = digitBytes + minusSignBytes;
                return(true);
            }
        }
Beispiel #30
0
        public static bool TryFormat(this Guid value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (format.IsDefault)
            {
                format.Symbol = 'G';
            }
            Precondition.Require(format.Symbol == 'G' || format.Symbol == 'D' || format.Symbol == 'N' || format.Symbol == 'B' || format.Symbol == 'P');
            bool dash = true;
            char tail = '\0';

            bytesWritten = 0;

            switch (format.Symbol)
            {
            case 'D':
            case 'G':
                break;

            case 'N':
                dash = false;
                break;

            case 'B':
                if (!TryWriteChar('{', buffer, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                tail = '}';
                break;

            case 'P':
                if (!TryWriteChar('(', buffer, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                tail = ')';
                break;

            default:
                Precondition.Require(false);     // how did we get here?
                break;
            }


            var byteFormat = new Format.Parsed('x', 2);

            unsafe
            {
                byte *bytes = (byte *)&value;

                if (!TryWriteByte(bytes[3], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[2], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[1], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[0], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }

                if (dash)
                {
                    if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten))
                    {
                        return(false);
                    }
                }

                if (!TryWriteByte(bytes[5], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[4], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }

                if (dash)
                {
                    if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten))
                    {
                        return(false);
                    }
                }

                if (!TryWriteByte(bytes[7], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[6], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }

                if (dash)
                {
                    if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten))
                    {
                        return(false);
                    }
                }

                if (!TryWriteByte(bytes[8], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[9], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }

                if (dash)
                {
                    if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten))
                    {
                        return(false);
                    }
                }

                if (!TryWriteByte(bytes[10], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[11], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[12], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[13], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[14], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
                if (!TryWriteByte(bytes[15], buffer, byteFormat, formattingData, ref bytesWritten))
                {
                    return(false);
                }
            }

            if (tail != '\0')
            {
                if (!TryWriteChar(tail, buffer, formattingData, ref bytesWritten))
                {
                    return(false);
                }
            }

            return(true);
        }
 public static bool TryFormat(this long value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
 {
     return IntegerFormatter.TryFormatInt64(value, 8, buffer, format, formattingData, out bytesWritten);
 }
        static bool TryFormatDateTimeFormagG(DateTime value, Span <byte> buffer, FormattingData formattingData, out int bytesWritten)
        {
            // for now it only works for invariant culture
            if (!formattingData.IsInvariantUtf16 && !formattingData.IsInvariantUtf8)
            {
                throw new NotImplementedException();
            }

            bytesWritten = 0;
            if (!TryWriteInt32(value.Month, buffer, G, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar('/', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(value.Day, buffer, G, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar('/', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(value.Year, buffer, G, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar(' ', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            var hour = value.Hour;

            if (hour == 0)
            {
                hour = 12;
            }
            if (hour > 12)
            {
                hour = hour - 12;
            }

            if (!TryWriteInt32(hour, buffer, G, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(value.Minute, buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (!TryWriteInt32(value.Second, buffer, D2, formattingData, ref bytesWritten))
            {
                return(false);
            }
            if (!TryWriteChar(' ', buffer, formattingData, ref bytesWritten))
            {
                return(false);
            }

            if (value.Hour > 11)
            {
                TryWriteString("PM", buffer, formattingData, ref bytesWritten);
            }
            else
            {
                TryWriteString("AM", buffer, formattingData, ref bytesWritten);
            }

            return(true);
        }
 public static bool TryFormat(this long value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
 {
     return(IntegerFormatter.TryFormatInt64(value, 8, buffer, format, formattingData, out bytesWritten));
 }
Beispiel #34
0
        // TODO: format should be ReadOnlySpan<char>
        internal static bool TryFormatInt64(long value, byte numberOfBytes, Span <byte> buffer, Span <char> format, FormattingData formattingData, out int bytesWritten)
        {
            Precondition.Require(numberOfBytes <= sizeof(long));

            Format.Parsed parsedFormat = Format.Parse(format);
            return(TryFormatInt64(value, numberOfBytes, buffer, parsedFormat, formattingData, out bytesWritten));
        }
        public static bool TryFormat(this Utf8String value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (formattingData.IsUtf16)
            {
                throw new NotImplementedException();
            }

            if (buffer.Length < value.Length)
            {
                bytesWritten = 0;
                return(false);
            }

            buffer.Set(value.Bytes);
            bytesWritten = value.Length;
            return(true);
        }
        public static bool TryFormat(this DateTime value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (format.IsDefault)
            {
                format.Symbol = 'G';
            }
            Precondition.Require(format.Symbol == 'R' || format.Symbol == 'O' || format.Symbol == 'G');

            switch (format.Symbol)
            {
            case 'R':
                var utc = value.ToUniversalTime();
                if (formattingData.IsUtf16)
                {
                    return(TryFormatDateTimeRfc1123(utc, buffer, FormattingData.InvariantUtf16, out bytesWritten));
                }
                else
                {
                    return(TryFormatDateTimeRfc1123(utc, buffer, FormattingData.InvariantUtf8, out bytesWritten));
                }

            case 'O':
                if (formattingData.IsUtf16)
                {
                    return(TryFormatDateTimeFormatO(value, true, buffer, FormattingData.InvariantUtf16, out bytesWritten));
                }
                else
                {
                    return(TryFormatDateTimeFormatO(value, true, buffer, FormattingData.InvariantUtf8, out bytesWritten));
                }

            case 'G':
                return(TryFormatDateTimeFormagG(value, buffer, formattingData, out bytesWritten));

            default:
                throw new NotImplementedException();
            }
        }
        static bool TryFormatDateTimeFormatO(DateTimeOffset value, bool isDateTime, Span<byte> buffer, FormattingData formattingData, out int bytesWritten)
        {
            bytesWritten = 0;
            if (!TryWriteInt32(value.Year, buffer, D4, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(value.Month, buffer, D2, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(value.Day, buffer, D2, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar('T', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(value.Hour, buffer, D2, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(value.Minute, buffer, D2, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(value.Second, buffer, D2, formattingData, ref bytesWritten)) { return false; }

            // add optional fractional second only if needed...
            var rounded = new DateTimeOffset(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, TimeSpan.Zero);
            var delta = value - rounded;

            if (delta.Ticks != 0)
            {
                if (!TryWriteChar('.', buffer, formattingData, ref bytesWritten)) { return false; }
                var timeFrac = delta.Ticks * FractionalTimeScale / System.TimeSpan.TicksPerSecond;
                if (!TryWriteInt64(timeFrac, buffer, D7, formattingData, ref bytesWritten)) { return false; }
            }

            if (isDateTime)
            {
                if (!TryWriteChar('Z', buffer, formattingData, ref bytesWritten)) { return false; }
            }
            else
            {
                if (!TryWriteChar('+', buffer, formattingData, ref bytesWritten)) { return false; }
                int bytes;
                if (!value.Offset.TryFormat(buffer.Slice(bytesWritten), t, formattingData, out bytes)) { return false; }
                bytesWritten += bytes;
            }

            return true;
        }
        public static bool TryFormat(this TimeSpan value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (format.IsDefault)
            {
                format.Symbol = 'c';
            }
            Precondition.Require(format.Symbol == 'G' || format.Symbol == 't' || format.Symbol == 'c' || format.Symbol == 'g');

            if (format.Symbol != 't')
            {
                return(TryFormatTimeSpanG(value, buffer, format, formattingData, out bytesWritten));
            }

            // else it's format 't' (short time used to print time offsets)
            return(TryFormatTimeSpanT(value, buffer, formattingData, out bytesWritten));
        }
 static bool TryFormatDateTimeRfc1123(DateTime value, Span<byte> buffer, FormattingData formattingData, out int bytesWritten)
 {
     bytesWritten = 0;
     if (!TryWriteString(s_dayNames[(int)value.DayOfWeek], buffer, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteInt32(value.Day, buffer, D2, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteChar(' ', buffer, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteString(s_monthNames[value.Month - 1], buffer, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteChar(' ', buffer, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteInt32(value.Year, buffer, D4, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteChar(' ', buffer, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteInt32(value.Hour, buffer, D2, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteInt32(value.Minute, buffer, D2, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteInt32(value.Second, buffer, D2, formattingData, ref bytesWritten)) { return false; }
     if (!TryWriteString(" GMT", buffer, formattingData, ref bytesWritten)) { return false; }
     return true;
 }
 internal static bool TryFormatUInt64(ulong value, byte numberOfBytes, Span<byte> buffer, Span<char> format, FormattingData formattingData, out int bytesWritten)
 {
     Format.Parsed parsedFormat = Format.Parse(format);
     return TryFormatUInt64(value, numberOfBytes, buffer, parsedFormat, formattingData, out bytesWritten);
 }
        public static bool TryFormat(this TimeSpan value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (format.IsDefault)
            {
                format.Symbol = 'c';
            }
            Precondition.Require(format.Symbol == 'G' || format.Symbol == 't' || format.Symbol == 'c' || format.Symbol == 'g');

            if (format.Symbol != 't')
            {
                return TryFormatTimeSpanG(value, buffer, format, formattingData, out bytesWritten);
            }

            // else it's format 't' (short time used to print time offsets)
            return TryFormatTimeSpanT(value, buffer, formattingData, out bytesWritten);
        }
        public unsafe static bool TryParse(byte* utf8Text, int index, int length, FormattingData cultureAndEncodingInfo,
            Format.Parsed numericFormat, out float value, out int bytesConsumed)
        {
            // Precondition replacement
            if (length < 1 || index < 0)
            {
                value = 0;
                bytesConsumed = 0;
                return false;
            }

            value = 0f;
            bytesConsumed = 0;

            if (cultureAndEncodingInfo.IsInvariantUtf8)
            {
                string floatString = "";
                bool decimalPlace = false, e = false, signed = false, digitLast = false, eLast = false;

                if ((length) >= 3 && utf8Text[index] == 'N' && utf8Text[index + 1] == 'a' && utf8Text[index + 2] == 'N')
                {
                    value = float.NaN;
                    bytesConsumed = 3;
                    return true;
                }
                if (utf8Text[index] == '-' || utf8Text[index] == '+')
                {
                    signed = true;
                    floatString += (char)utf8Text[index];
                    index++;
                    bytesConsumed++;
                }
                if ((length - index) >= 8 && utf8Text[index] == 'I' && utf8Text[index + 1] == 'n' &&
                    utf8Text[index + 2] == 'f' && utf8Text[index + 3] == 'i' && utf8Text[index + 4] == 'n' &&
                    utf8Text[index + 5] == 'i' && utf8Text[index + 6] == 't' && utf8Text[index + 7] == 'y')
                {
                    if (signed && utf8Text[index - 1] == '-')
                    {
                        value = float.NegativeInfinity;
                    }
                    else
                    {
                        value = float.PositiveInfinity;
                    }
                    bytesConsumed += 8;
                    return true;
                }

                for (int byteIndex = index; byteIndex < length; byteIndex++)
                {
                    byte nextByte = utf8Text[byteIndex];
                    byte nextByteVal = (byte)(nextByte - '0');

                    if (nextByteVal > 9)
                    {
                        if (!decimalPlace && nextByte == '.')
                        {
                            if (digitLast)
                            {
                                digitLast = false;
                            }
                            if (eLast)
                            {
                                eLast = false;
                            }
                            bytesConsumed++;
                            decimalPlace = true;
                            floatString += (char)nextByte;
                        }
                        else if (!e && nextByte == 'e' || nextByte == 'E')
                        {
                            e = true;
                            eLast = true;
                            bytesConsumed++;
                            floatString += (char)nextByte;
                        }
                        else if (eLast && nextByte == '+' || nextByte == '-')
                        {
                            eLast = false;
                            bytesConsumed++;
                            floatString += (char)nextByte;
                        }
                        else if ((decimalPlace && signed && bytesConsumed == 2) || ((signed || decimalPlace) && bytesConsumed == 1))
                        {
                            value = 0;
                            bytesConsumed = 0;
                            return false;
                        }
                        else
                        {
                            if (float.TryParse(floatString, out value))
                            {
                                return true;
                            }
                            else
                            {
                                bytesConsumed = 0;
                                return false;
                            }
                        }
                    }
                    else
                    {
                        if (eLast)
                            eLast = false;
                        if (!digitLast)
                            digitLast = true;
                        bytesConsumed++;
                        floatString += (char)nextByte;
                    }
                }

                if ((decimalPlace && signed && bytesConsumed == 2) || ((signed || decimalPlace) && bytesConsumed == 1))
                {
                    value = 0;
                    bytesConsumed = 0;
                    return false;
                }
                else
                {
                    if (float.TryParse(floatString, out value))
                    {
                        return true;
                    }
                    else
                    {
                        bytesConsumed = 0;
                        return false;
                    }
                }
            }
            return false;
        }
        private static bool TryFormatTimeSpanG(TimeSpan value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            bytesWritten = 0;

            if (value.Ticks < 0)
            {
                if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten)) { return false; }
            }

            bool daysWritten = false;
            if (value.Days != 0 || format.Symbol == 'G')
            {
                if (!TryWriteInt32(Abs(value.Days), buffer, default(Format.Parsed), formattingData, ref bytesWritten)) { return false; }
                daysWritten = true;
                if (format.Symbol == 'c')
                {
                    if (!TryWriteChar('.', buffer, formattingData, ref bytesWritten)) { return false; }
                }
                else
                {
                    if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten)) { return false; }
                }
            }

            var hourFormat = default(Format.Parsed);
            if ((daysWritten || format.Symbol == 'c') && format.Symbol != 'g')
            {
                hourFormat = D2;
            }
            if (!TryWriteInt32(Abs(value.Hours), buffer, hourFormat, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(Abs(value.Minutes), buffer, D2, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(Abs(value.Seconds), buffer, D2, formattingData, ref bytesWritten)) { return false; }

            long remainingTicks;
            if (value.Ticks != long.MinValue)
            {
                remainingTicks = Abs(value.Ticks) % TimeSpan.TicksPerSecond;
            }
            else
            {
                remainingTicks = long.MaxValue % TimeSpan.TicksPerSecond;
                remainingTicks = (remainingTicks + 1) % TimeSpan.TicksPerSecond;
            }

            var ticksFormat = D7;
            if (remainingTicks != 0)
            {
                if (!TryWriteChar('.', buffer, formattingData, ref bytesWritten)) { return false; }
                var fraction = remainingTicks * FractionalTimeScale / TimeSpan.TicksPerSecond;
                if (!TryWriteInt64(fraction, buffer, ticksFormat, formattingData, ref bytesWritten)) { return false; }
            }

            return true;
        }
		public static bool TryParse(byte[] utf8Text, int index, FormattingData cultureAndEncodingInfo, Format.Parsed numericFormat,
            out uint value, out int bytesConsumed)
		{
			// Precondition replacement
            if (utf8Text.Length < 1 || index < 0 || index >= utf8Text.Length)
            {
                value = default(uint);
                bytesConsumed = 0;
                return false;
            }

            value = default(uint);
            bytesConsumed = 0;

            if (cultureAndEncodingInfo.IsInvariantUtf8)
            {
                for (int byteIndex = index; byteIndex < utf8Text.Length; byteIndex++) // loop through the byte array
                {
                    byte nextByteVal = (byte)(utf8Text[byteIndex] - '0');
                    if (nextByteVal > 9) // if nextByteVal > 9, we know it is not a digit because any value less than '0' will overflow
										 // to greater than 9 since byte is an unsigned type.
                    {
                        if (bytesConsumed == 0) // check to see if we've processed any digits at all
                        {
                            return false;
                        }
                        else
                        {
                            return true; // otherwise return true
                        }
                    }
                    else if (value > UInt32.MaxValue / 10)
                    {
                        value = default(uint);
                        bytesConsumed = 0;
                        return false;
                    }
                    // This next check uses a hardcoded 6 because the max values for unsigned types all end in 5s.
                    else if (value == UInt32.MaxValue / 10 &&  nextByteVal >= 6) // overflow
                    {
                        value = default(uint);
                        bytesConsumed = 0;
                        return false;
                    }

                    value = (uint)(value * 10 + nextByteVal); // left shift the value and add the nextByte
                    bytesConsumed++;
                }
				return true;
            }
            else if (cultureAndEncodingInfo.IsInvariantUtf16)
            {
                for (int byteIndex = index; byteIndex < utf8Text.Length - 1; byteIndex += 2) // loop through the byte array two bytes at a time for UTF-16
                {
                    byte byteAfterNext = utf8Text[byteIndex + 1];
                    byte nextByteVal = (byte)(utf8Text[byteIndex] - '0');
                    if (nextByteVal > 9 || byteAfterNext != 0)  // if the second byte isn't zero, this isn't an ASCII-equivalent code unit and we can quit here
																// if nextByteVal > 9, we know it is not a digit because any value less than '0' will overflow
																// to greater than 9 since byte is an unsigned type.
                    {
                        if (bytesConsumed == 0) // check to see if we've processed any digits at all
                        {
                            return false;
                        }
                        else
                        {
                            return true; // otherwise return true
                        }
                    }
                    else if (value > UInt32.MaxValue / 10)
                    {
                        value = default(uint);
                        bytesConsumed = 0;
                        return false;
                    }
                    // This next check uses a hardcoded 6 because the max values for unsigned types all end in 5s.
                    else if (value == UInt32.MaxValue / 10 &&  nextByteVal >= 6) // overflow
                    {
                        value = default(uint);
                        bytesConsumed = 0;
                        return false;
                    }

                    value = (uint)(value * 10 + nextByteVal); // left shift the value and add the nextByte
                    bytesConsumed += 2;
                }
                return true;
            }
			else
            {
                int byteIndex = index;
                while (byteIndex < utf8Text.Length)
                {
                    uint result;
					int oldIndex = byteIndex;
                    bool success = cultureAndEncodingInfo.TryParseNextCodingUnit(ref utf8Text, ref byteIndex, out result);

                    if (!success || result > 9)
                    {
                        if (bytesConsumed == 0) // check to see if we've processed any digits at all
                        {
                            return false;
                        }
                        else
                        {
                            return true; // otherwise return true
                        }
                    }
                    else if (value > UInt32.MaxValue / 10)
                    {
                        value = default(uint);
                        bytesConsumed = 0;
                        return false;
                    }
                    // This next check uses a hardcoded 6 because the max values for unsigned types all end in 5s.
                    else if (value == UInt32.MaxValue / 10 && result >= 6) // overflow
                    {
                        value = default(uint);
                        bytesConsumed = 0;
                        return false;
                    }

                    value = (uint)(value * 10 + result); // left shift the value and add the nextByte
                    bytesConsumed += byteIndex - oldIndex;
                }

                return true;
            }
        }
        private static bool TryFormatTimeSpanT(TimeSpan value, Span<byte> buffer, FormattingData formattingData, out int bytesWritten)
        {
            bytesWritten = 0;

            if (value.Ticks < 0)
            {
                if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten)) { return false; }
            }

            if (!TryWriteInt32(Abs((int)value.TotalHours), buffer, D2, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(Abs(value.Minutes), buffer, D2, formattingData, ref bytesWritten)) { return false; }

            return true;
        }
Beispiel #46
0
 public static bool TryFormat(this Guid value, Span <byte> buffer, Span <char> format, FormattingData formattingData, out int bytesWritten)
 {
     Format.Parsed parsedFormat = Format.Parse(format);
     return(TryFormat(value, buffer, parsedFormat, formattingData, out bytesWritten));
 }
        public static bool TryFormat(this string value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (formattingData.IsUtf16)
            {
                var valueBytes = value.Length << 1;
                if (valueBytes > buffer.Length)
                {
                    bytesWritten = 0;
                    return(false);
                }

                unsafe
                {
                    fixed(char *pCharacters = value)
                    {
                        byte *pBytes = (byte *)pCharacters;

                        buffer.Set(pBytes, valueBytes);
                    }
                }

                bytesWritten = valueBytes;
                return(true);
            }


            var avaliableBytes = buffer.Length;

            bytesWritten = 0;
            for (int i = 0; i < value.Length; i++)
            {
                var c = value[i];

                var codepoint = (ushort)c;
                if (codepoint <= 0x7f) // this if block just optimizes for ascii
                {
                    if (bytesWritten + 1 > avaliableBytes)
                    {
                        bytesWritten = 0;
                        return(false);
                    }
                    buffer[bytesWritten++] = (byte)codepoint;
                }
                else
                {
                    Utf8EncodedCodePoint encoded;
                    if (!char.IsSurrogate(c))
                    {
                        encoded = new Utf8EncodedCodePoint(c);
                    }
                    else
                    {
                        if (++i >= value.Length)
                        {
                            throw new ArgumentException("Invalid surrogate pair.", nameof(value));
                        }
                        char lowSurrogate = value[i];
                        encoded = new Utf8EncodedCodePoint(c, lowSurrogate);
                    }


                    if (bytesWritten + encoded.Length > avaliableBytes)
                    {
                        bytesWritten = 0;
                        return(false);
                    }

                    buffer[bytesWritten] = encoded.Byte0;
                    if (encoded.Length > 1)
                    {
                        buffer[bytesWritten + 1] = encoded.Byte1;

                        if (encoded.Length > 2)
                        {
                            buffer[bytesWritten + 2] = encoded.Byte2;

                            if (encoded.Length > 3)
                            {
                                buffer[bytesWritten + 3] = encoded.Byte3;
                            }
                        }
                    }

                    bytesWritten += encoded.Length;
                }
            }
            return(true);
        }
Beispiel #48
0
        static bool TryWriteInt64(long i, Span <byte> buffer, Format.Parsed byteFormat, FormattingData formattingData, ref int bytesWritten)
        {
            int written;

            if (!i.TryFormat(buffer.Slice(bytesWritten), byteFormat, formattingData, out written))
            {
                bytesWritten = 0;
                return(false);
            }
            bytesWritten += written;
            return(true);
        }
		public static bool TryParse(byte[] utf8Text, int index, FormattingData cultureAndEncodingInfo, Format.Parsed numericFormat,
            out int value, out int bytesConsumed)
        {
            // Precondition replacement
            if (utf8Text.Length < 1 || index < 0 || index >= utf8Text.Length)
            {
                value = default(int);
                bytesConsumed = 0;
                return false;
            }

            value = default(int);
            bytesConsumed = 0;
            bool negative = false;
            bool signed = false;

            if (cultureAndEncodingInfo.IsInvariantUtf8)
            {
                if (utf8Text[index] == '-')
                {
                    negative = true;
                    signed = true;
                    index++;
                    bytesConsumed++;
                }
                else if (utf8Text[index] == '+')
                {
                    signed = true;
                    index++;
                    bytesConsumed++;
                }

                for (int byteIndex = index; byteIndex < utf8Text.Length; byteIndex++) // loop through the byte array
                {
                    byte nextByteVal = (byte)(utf8Text[byteIndex] - '0');
                    if (nextByteVal > 9) // if nextByteVal > 9, we know it is not a digit because any value less than '0' will overflow
										 // to greater than 9 since byte is an unsigned type.
                    {
                        if (bytesConsumed == 1 && signed) // if the first character happened to be a '-' or a '+', we reset the byte counter so logic proceeds as normal.
                        {
                            bytesConsumed = 0;
                        }
                        if (bytesConsumed == 0) // check to see if we've processed any digits at all
                        {
                            return false;
                        }
                        else
                        {
                            if (negative) // We check if the value is negative at the very end to save on comp time
                            {
                                value = (int)-value;
								if (value > 0)
								{
									value = 0;
									bytesConsumed = 0;
									return false;
								}
                            }
                            return true; // otherwise return true
                        }
                    }
                    else if (value > Int32.MaxValue / 10) // overflow
                    {
                        value = default(int);
                        bytesConsumed = 0;
                        return false;
                    }
                    // This next check uses a hardcoded 8 because the max values for unsigned types all end in 7s.
					// The min values all end in 8s, which is why that addition exists.
                    else if (value == Int32.MaxValue / 10 &&  nextByteVal >= 8 + (negative ? 1 : 0) ) // overflow
                    {
                        value = default(int);
                        bytesConsumed = 0;
                        return false;
                    }
                    value = (int)(value * 10 + nextByteVal); // parse the current digit to a int and add it to the left-shifted value
                    bytesConsumed++; // increment the number of bytes consumed, then loop
                }

                if (negative) // We check if the value is negative at the very end to save on comp time
                {
                    value = (int)-value;
					if (value > 0)
					{
						value = 0;
						bytesConsumed = 0;
						return false;
					}
				}
                return true;
            }
            else if (cultureAndEncodingInfo.IsInvariantUtf16)
            {
                if (utf8Text[index] == '-' && utf8Text[index + 1] == 0)
                {
                    negative = true;
                    signed = true;
                    index += 2;
                    bytesConsumed += 2;
                }
                else if (utf8Text[index] == '+' && utf8Text[index + 1] == 0)
                {
                    signed = true;
                    index += 2;
                    bytesConsumed += 2;
                }

                for (int byteIndex = index; byteIndex < utf8Text.Length - 1; byteIndex += 2) // loop through the byte array two bytes at a time for UTF-16
                {
                    byte byteAfterNext = utf8Text[byteIndex + 1];
                    byte nextByteVal = (byte)(utf8Text[byteIndex] - '0');
                    if (nextByteVal > 9 || byteAfterNext != 0) // if the second byte isn't zero, this isn't an ASCII-equivalent code unit and we can quit here
															   // if nextByteVal > 9, we know it is not a digit because any value less than '0' will overflow
															   // to greater than 9 since byte is an unsigned type.
                    {
                        if (bytesConsumed == 2 && signed) // if the first character happened to be a '-' or a '+', we reset the byte counter so logic proceeds as normal
                        {
                            bytesConsumed = 0;
                        }
                        if (bytesConsumed == 0) // check to see if we've processed any digits at all
                        {
                            return false;
                        }
                        else
                        {
                            if (negative) // We check if the value is negative at the very end to save on comp time
                            {
                                value = (int)-value;
								if (value > 0)
								{
									value = 0;
									bytesConsumed = 0;
									return false;
								}
							}
                            return true; // otherwise return true
                        }
                    }
                    else if (value > Int32.MaxValue / 10) // overflow
                    {
                        value = default(int);
                        bytesConsumed = 0;
                        return false;
                    }
					// This next check uses a hardcoded 8 because the max values for unsigned types all end in 7s.
					// The min values all end in 8s, which is why that addition exists.
                    else if (value == Int32.MaxValue / 10 &&  nextByteVal >= 8 + (negative ? 1 : 0) ) // overflow
                    {
                        value = default(int);
                        bytesConsumed = 0;
                        return false;
                    }
                    value = (int)(value * 10 + nextByteVal); // parse the current digit to a int and add it to the left-shifted value
                    bytesConsumed += 2; // increment the number of bytes consumed, then loop
                }

                if (negative) // We check if the value is negative at the very end to save on comp time
                {
                    value = (int)-value;
					if (value > 0)
					{
						value = 0;
						bytesConsumed = 0;
						return false;
					}
				}
                return true;
            }
			else
			{
				int byteIndex = index;
				int codeUnitsConsumed = 0;
                while (byteIndex < utf8Text.Length)
                {
                    uint result;
					int oldIndex = byteIndex;
                    bool success = cultureAndEncodingInfo.TryParseNextCodingUnit(ref utf8Text, ref byteIndex, out result);

                    if (!success || result > 9)
                    {
						if (bytesConsumed == 0 && result == (int)FormattingData.Symbol.MinusSign)
						{
							negative = true;
							signed = true;
                            bytesConsumed += byteIndex - oldIndex;
							codeUnitsConsumed++;
						}
						else if (bytesConsumed == 0 && result == (int)FormattingData.Symbol.PlusSign)
						{
							negative = true;
							signed = true;
                            bytesConsumed += byteIndex - oldIndex;
						}
						else if (codeUnitsConsumed == 1 && signed) // if the first character happened to be a '-' or a '+', we reset the byte counter so logic proceeds as normal.
                        {
                            bytesConsumed = 0;
							return false;
                        }
                        else if (bytesConsumed == 0) // check to see if we've processed any digits at all
                        {
                            return false;
                        }
                        else
                        {
                            if (negative) // We check if the value is negative at the very end to save on comp time
                            {
                                value = (int)-value;
								if (value > 0)
								{
									value = 0;
									bytesConsumed = 0;
									return false;
								}
                            }
                            return true; // otherwise return true
                        }
                    }
                    else if (value > Int32.MaxValue / 10)
                    {
                        value = default(int);
                        bytesConsumed = 0;
                        return false;
                    }
                    // This next check uses a hardcoded 8 because the max values for unsigned types all end in 7s.
					// The min values all end in 8s, which is why that addition exists.
                    else if (value == Int32.MaxValue / 10 &&  result >= 8 + (negative ? 1 : 0) ) // overflow
                    {
                        value = default(int);
                        bytesConsumed = 0;
                        return false;
                    }
					else
					{
						value = (int)(value * 10 + result); // left shift the value and add the nextByte
						bytesConsumed += byteIndex - oldIndex;
						codeUnitsConsumed++;
					}
                }

				if (negative) // We check if the value is negative at the very end to save on comp time
                {
                    value = (int)-value;
					if (value > 0)
					{
						value = default(int);
						bytesConsumed = 0;
						return false;
					}
                }
                return true; // otherwise return true
			}
        }
Beispiel #50
0
 public static bool TryFormat(this float value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
 {
     if (format.IsDefault)
     {
         format.Symbol = 'G';
     }
     Precondition.Require(format.Symbol == 'G');
     return(FloatFormatter.TryFormatNumber(value, true, buffer, format, formattingData, out bytesWritten));
 }
        public static bool TryFormat(this Guid value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (format.IsDefault)
            {
                format.Symbol = 'G';
            }
            Precondition.Require(format.Symbol == 'G' || format.Symbol == 'D' || format.Symbol == 'N' || format.Symbol == 'B' || format.Symbol == 'P');
            bool dash = true;
            char tail = '\0';
            bytesWritten = 0;

            switch (format.Symbol)
            {
                case 'D':
                case 'G':
                    break;

                case 'N':
                    dash = false;
                    break;

                case 'B':
                    if (!TryWriteChar('{', buffer, formattingData, ref bytesWritten)) { return false; }
                    tail = '}';
                    break;

                case 'P':
                    if (!TryWriteChar('(', buffer, formattingData, ref bytesWritten)) { return false; }
                    tail = ')';
                    break;

                default:
                    Precondition.Require(false); // how did we get here? 
                    break;
            }


            var byteFormat = new Format.Parsed('x', 2);
            unsafe
            {
                byte* bytes = (byte*)&value;

                if (!TryWriteByte(bytes[3], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[2], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[1], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[0], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }

                if (dash)
                {
                    if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten)) { return false; }
                }

                if (!TryWriteByte(bytes[5], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[4], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }

                if (dash)
                {
                    if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten)) { return false; }
                }

                if (!TryWriteByte(bytes[7], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[6], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }

                if (dash)
                {
                    if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten)) { return false; }
                }

                if (!TryWriteByte(bytes[8], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[9], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }

                if (dash)
                {
                    if (!TryWriteChar('-', buffer, formattingData, ref bytesWritten)) { return false; }
                }

                if (!TryWriteByte(bytes[10], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[11], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[12], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[13], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[14], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
                if (!TryWriteByte(bytes[15], buffer, byteFormat, formattingData, ref bytesWritten)) { return false; }
            }

            if (tail != '\0')
            {
                if (!TryWriteChar(tail, buffer, formattingData, ref bytesWritten)) { return false; }
            }

            return true;
        }
        static bool TryFormatDateTimeFormagG(DateTime value, Span<byte> buffer, FormattingData formattingData, out int bytesWritten)
        {
            // for now it only works for invariant culture
            if(!formattingData.IsInvariantUtf16 && !formattingData.IsInvariantUtf8)
            {
                throw new NotImplementedException();
            }

            bytesWritten = 0;
            if (!TryWriteInt32(value.Month, buffer, G, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar('/', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(value.Day, buffer, G, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar('/', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(value.Year, buffer, G, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar(' ', buffer, formattingData, ref bytesWritten)) { return false; }

            var hour = value.Hour;
            if(hour == 0)
            {
                hour = 12; 
            }
            if(hour > 12)
            {
                hour = hour - 12;
            }

            if (!TryWriteInt32(hour, buffer, G, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(value.Minute, buffer, D2, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar(':', buffer, formattingData, ref bytesWritten)) { return false; }

            if (!TryWriteInt32(value.Second, buffer, D2, formattingData, ref bytesWritten)) { return false; }
            if (!TryWriteChar(' ', buffer, formattingData, ref bytesWritten)) { return false; }

            if(value.Hour > 11)
            {
                TryWriteString("PM", buffer, formattingData, ref bytesWritten);
            }
            else
            {
                TryWriteString("AM", buffer, formattingData, ref bytesWritten);
            }

            return true;
        }
        public static bool TryFormat(this char value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (formattingData.IsUtf16)
            {
                if (buffer.Length < 2)
                {
                    bytesWritten = 0;
                    return(false);
                }
                buffer[0]    = (byte)value;
                buffer[1]    = (byte)(value >> 8);
                bytesWritten = 2;
                return(true);
            }

            if (buffer.Length < 1)
            {
                bytesWritten = 0;
                return(false);
            }

            // fast path for ASCII
            if (value <= 127)
            {
                buffer[0]    = (byte)value;
                bytesWritten = 1;
                return(true);
            }

            // TODO: This can be directly encoded to SpanByte. There is no conversion between spans yet
            var encoded = new Utf8EncodedCodePoint(value);

            bytesWritten = encoded.Length;
            if (buffer.Length < bytesWritten)
            {
                bytesWritten = 0;
                return(false);
            }

            buffer[0] = encoded.Byte0;
            if (bytesWritten > 1)
            {
                buffer[1] = encoded.Byte1;
            }
            if (bytesWritten > 2)
            {
                buffer[2] = encoded.Byte2;
            }
            if (bytesWritten > 3)
            {
                buffer[3] = encoded.Byte3;
            }
            return(true);
        }