Exemple #1
0
 private string GetUnicodeCharsText()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.Unicode, "");
     return(_bufferReader.GetUnicodeString(_offset, _length));
 }
 private void GetBase64(byte[] buffer, int offset, int count)
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.Base64, "");
     _bufferReader.GetBase64(_offset, buffer, offset, count);
 }
 public void SetValue(PrefixHandleType type)
 {
     DiagnosticUtility.DebugAssert(type != PrefixHandleType.Buffer, "");
     _type = type;
 }
Exemple #4
0
 private double GetDouble()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.Double, "");
     return(_bufferReader.GetDouble(_offset));
 }
Exemple #5
0
 private UniqueId GetUniqueId()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.UniqueId, "");
     return(_bufferReader.GetUniqueId(_offset));
 }
Exemple #6
0
 private int GetInt32()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.Int32, "");
     return(_bufferReader.GetInt32(_offset));
 }
Exemple #7
0
 private ulong GetUInt64()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.UInt64, "");
     return(_bufferReader.GetUInt64(_offset));
 }
Exemple #8
0
 protected void WriteByte(char ch)
 {
     DiagnosticUtility.DebugAssert(ch < 0x80, "");
     WriteByte((byte)ch);
 }
Exemple #9
0
 protected Task WriteByteAsync(char ch)
 {
     DiagnosticUtility.DebugAssert(ch < 0x80, "");
     return(WriteByteAsync((byte)ch));
 }
Exemple #10
0
        public bool TryReadChars(char[] chars, int offset, int count, out int actual)
        {
            DiagnosticUtility.DebugAssert(offset + count <= chars.Length, string.Format("offset '{0}' + count '{1}' MUST BE <= chars.Length '{2}'", offset, count, chars.Length));

            if (_type == ValueHandleType.Unicode)
            {
                return(TryReadUnicodeChars(chars, offset, count, out actual));
            }

            if (_type != ValueHandleType.UTF8)
            {
                actual = 0;
                return(false);
            }

            int charOffset = offset;
            int charCount  = count;

            byte[] bytes      = _bufferReader.Buffer;
            int    byteOffset = _offset;
            int    byteCount  = _length;
            bool   insufficientSpaceInCharsArray = false;

            var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

            while (true)
            {
                while (charCount > 0 && byteCount > 0)
                {
                    // fast path for codepoints U+0000 - U+007F
                    byte b = bytes[byteOffset];
                    if (b >= 0x80)
                    {
                        break;
                    }
                    chars[charOffset] = (char)b;
                    byteOffset++;
                    byteCount--;
                    charOffset++;
                    charCount--;
                }

                if (charCount == 0 || byteCount == 0 || insufficientSpaceInCharsArray)
                {
                    break;
                }

                int actualByteCount;
                int actualCharCount;

                try
                {
                    // If we're asking for more than are possibly available, or more than are truly available then we can return the entire thing
                    if (charCount >= encoding.GetMaxCharCount(byteCount) || charCount >= encoding.GetCharCount(bytes, byteOffset, byteCount))
                    {
                        actualCharCount = encoding.GetChars(bytes, byteOffset, byteCount, chars, charOffset);
                        actualByteCount = byteCount;
                    }
                    else
                    {
                        Decoder decoder = encoding.GetDecoder();

                        // Since x bytes can never generate more than x characters this is a safe estimate as to what will fit
                        actualByteCount = Math.Min(charCount, byteCount);

                        // We use a decoder so we don't error if we fall across a character boundary
                        actualCharCount = decoder.GetChars(bytes, byteOffset, actualByteCount, chars, charOffset);

                        // We might have gotten zero characters though if < 4 bytes were requested because
                        // codepoints from U+0000 - U+FFFF can be up to 3 bytes in UTF-8, and represented as ONE char
                        // codepoints from U+10000 - U+10FFFF (last Unicode codepoint representable in UTF-8) are represented by up to 4 bytes in UTF-8
                        //                                    and represented as TWO chars (high+low surrogate)
                        // (e.g. 1 char requested, 1 char in the buffer represented in 3 bytes)
                        while (actualCharCount == 0)
                        {
                            // Note the by the time we arrive here, if actualByteCount == 3, the next decoder.GetChars() call will read the 4th byte
                            // if we don't bail out since the while loop will advance actualByteCount only after reading the byte.
                            if (actualByteCount >= 3 && charCount < 2)
                            {
                                // If we reach here, it means that we're:
                                // - trying to decode more than 3 bytes and,
                                // - there is only one char left of charCount where we're stuffing decoded characters.
                                // In this case, we need to back off since decoding > 3 bytes in UTF-8 means that we will get 2 16-bit chars
                                // (a high surrogate and a low surrogate) - the Decoder will attempt to provide both at once
                                // and an ArgumentException will be thrown complaining that there's not enough space in the output char array.

                                // actualByteCount = 0 when the while loop is broken out of; decoder goes out of scope so its state no longer matters

                                insufficientSpaceInCharsArray = true;
                                break;
                            }
                            else
                            {
                                DiagnosticUtility.DebugAssert(byteOffset + actualByteCount < bytes.Length,
                                                              string.Format("byteOffset {0} + actualByteCount {1} MUST BE < bytes.Length {2}", byteOffset, actualByteCount, bytes.Length));

                                // Request a few more bytes to get at least one character
                                actualCharCount = decoder.GetChars(bytes, byteOffset + actualByteCount, 1, chars, charOffset);
                                actualByteCount++;
                            }
                        }

                        // Now that we actually retrieved some characters, figure out how many bytes it actually was
                        actualByteCount = encoding.GetByteCount(chars, charOffset, actualCharCount);
                    }
                }
                catch (FormatException exception)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateEncodingException(bytes, byteOffset, byteCount, exception));
                }

                // Advance
                byteOffset += actualByteCount;
                byteCount  -= actualByteCount;

                charOffset += actualCharCount;
                charCount  -= actualCharCount;
            }

            _offset = byteOffset;
            _length = byteCount;

            actual = (count - charCount);
            return(true);
        }
Exemple #11
0
 protected void Advance(int count)
 {
     DiagnosticUtility.DebugAssert(_offset + count <= bufferLength, "");
     _offset += count;
 }
Exemple #12
0
        public string GetString()
        {
            ValueHandleType type = _type;

            if (type == ValueHandleType.UTF8)
            {
                return(GetCharsText());
            }

            switch (type)
            {
            case ValueHandleType.False:
                return("false");

            case ValueHandleType.True:
                return("true");

            case ValueHandleType.Zero:
                return("0");

            case ValueHandleType.One:
                return("1");

            case ValueHandleType.Int8:
            case ValueHandleType.Int16:
            case ValueHandleType.Int32:
                return(XmlConverter.ToString(ToInt()));

            case ValueHandleType.Int64:
                return(XmlConverter.ToString(GetInt64()));

            case ValueHandleType.UInt64:
                return(XmlConverter.ToString(GetUInt64()));

            case ValueHandleType.Single:
                return(XmlConverter.ToString(GetSingle()));

            case ValueHandleType.Double:
                return(XmlConverter.ToString(GetDouble()));

            case ValueHandleType.Decimal:
                return(XmlConverter.ToString(GetDecimal()));

            case ValueHandleType.DateTime:
                return(XmlConverter.ToString(ToDateTime()));

            case ValueHandleType.Empty:
                return(string.Empty);

            case ValueHandleType.Unicode:
                return(GetUnicodeCharsText());

            case ValueHandleType.EscapedUTF8:
                return(GetEscapedCharsText());

            case ValueHandleType.Char:
                return(GetCharText());

            case ValueHandleType.Dictionary:
                return(GetDictionaryString().Value);

            case ValueHandleType.Base64:
                byte[] bytes = ToByteArray();
                DiagnosticUtility.DebugAssert(bytes != null, "");
                return(Base64Encoding.GetString(bytes, 0, bytes.Length));

            case ValueHandleType.List:
                return(XmlConverter.ToString(ToList()));

            case ValueHandleType.UniqueId:
                return(XmlConverter.ToString(ToUniqueId()));

            case ValueHandleType.Guid:
                return(XmlConverter.ToString(ToGuid()));

            case ValueHandleType.TimeSpan:
                return(XmlConverter.ToString(ToTimeSpan()));

            case ValueHandleType.QName:
                return(GetQNameDictionaryText());

            case ValueHandleType.ConstString:
                return(s_constStrings[_offset]);

            default:
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException());
            }
        }
Exemple #13
0
 private string GetQNameDictionaryText()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.QName, "");
     return(string.Concat(PrefixHandle.GetString(PrefixHandle.GetAlphaPrefix(_length)), ":", _bufferReader.GetDictionaryString(_offset)));
 }
Exemple #14
0
 private XmlDictionaryString GetDictionaryString()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.Dictionary, "");
     return(_bufferReader.GetDictionaryString(_offset));
 }
Exemple #15
0
 private string GetEscapedCharsText()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.EscapedUTF8, "");
     return(_bufferReader.GetEscapedString(_offset, _length));
 }
Exemple #16
0
 protected void WriteBytes(char ch1, char ch2)
 {
     DiagnosticUtility.DebugAssert(ch1 < 0x80 && ch2 < 0x80, "");
     WriteBytes((byte)ch1, (byte)ch2);
 }
Exemple #17
0
 private int GetChar()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.Char, "");
     return(_offset);
 }
Exemple #18
0
 protected Task WriteBytesAsync(char ch1, char ch2)
 {
     DiagnosticUtility.DebugAssert(ch1 < 0x80 && ch2 < 0x80, "");
     return(WriteBytesAsync((byte)ch1, (byte)ch2));
 }
Exemple #19
0
 private long GetInt64()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.Int64 || _type == ValueHandleType.TimeSpan || _type == ValueHandleType.DateTime, "");
     return(_bufferReader.GetInt64(_offset));
 }
Exemple #20
0
 public void Advance(int count)
 {
     DiagnosticUtility.DebugAssert(_offset + count <= _offsetMax, "");
     _offset += count;
 }
Exemple #21
0
 private float GetSingle()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.Single, "");
     return(_bufferReader.GetSingle(_offset));
 }
 public static string GetString(PrefixHandleType type)
 {
     DiagnosticUtility.DebugAssert(type != PrefixHandleType.Buffer, "");
     return(s_prefixStrings[(int)type]);
 }
Exemple #23
0
 private decimal GetDecimal()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.Decimal, "");
     return(_bufferReader.GetDecimal(_offset));
 }
 public static PrefixHandleType GetAlphaPrefix(int index)
 {
     DiagnosticUtility.DebugAssert(index >= 0 && index < 26, "");
     return((PrefixHandleType)(PrefixHandleType.A + index));
 }
Exemple #25
0
 private Guid GetGuid()
 {
     DiagnosticUtility.DebugAssert(_type == ValueHandleType.Guid, "");
     return(_bufferReader.GetGuid(_offset));
 }
Exemple #26
0
 public void ToPrefixHandle(PrefixHandle prefix)
 {
     DiagnosticUtility.DebugAssert(_type == StringHandleType.UTF8, "");
     prefix.SetValue(_offset, _length);
 }