コード例 #1
0
        public override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount)
        {
            if ((bytes == null) ||
                (chars == null) ||
                (charCount < 0) ||
                (byteCount < 0))
            {
                EncodingForwarder.ThrowValidationFailedException(chars, charCount, bytes);
            }
            Contract.EndContractBlock();

            int bytesWritten;

            if (charCount > 0)
            {
                if (byteCount == 0)
                {
                    // Definitely not enough space, early bail
                    EncodingForwarder.ThrowBytesOverflow(this);
                }
                int charactersConsumed;
                if (!EncodingForwarder.TryEncode(chars, charCount, bytes, byteCount, out charactersConsumed, out bytesWritten))
                {
                    // Not all ASCII, GetBytesFallback for remaining conversion
                    bytesWritten += GetBytesFallback(chars + charactersConsumed, charCount - charactersConsumed, bytes + bytesWritten, byteCount - bytesWritten, null);
                }
            }
            else
            {
                // Nothing to encode
                bytesWritten = 0;
            }

            return(bytesWritten);
        }
コード例 #2
0
        private unsafe byte[] GetBytesValidated(char *input, int charCount)
        {
            int remaining = 0;

            // Assume string is all ASCII and size array for that
            byte[] bytes = new byte[charCount];

            int bytesWritten;

            fixed(byte *output = &bytes[0])
            {
                int charactersConsumed;

                if (!EncodingForwarder.TryEncode(input, charCount, output, charCount, out charactersConsumed, out bytesWritten))
                {
                    // Not all ASCII, get the byte count for the remaining encoded conversion
                    remaining = GetByteCount(input + charactersConsumed, charCount - charactersConsumed, null);
                }
            }

            if (remaining > 0)
            {
                // Not all ASCII, fallback to slower path for remaining encoding
                var encoded = ResizeGetRemainingBytes(input, charCount, ref bytes, bytesWritten, remaining);
                Debug.Assert(encoded == remaining);
            }

            return(bytes);
        }
コード例 #3
0
        // Encodes a range of characters in a character array into a range of bytes
        // in a byte array. An exception occurs if the byte array is not large
        // enough to hold the complete encoding of the characters. The
        // GetByteCount method can be used to determine the exact number of
        // bytes that will be produced for a given range of characters.
        // Alternatively, the GetMaxByteCount method can be used to
        // determine the maximum number of bytes that will be produced for a given
        // number of characters, regardless of the actual character values.
        public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
        {
            if ((chars == null) ||
                (bytes == null) ||
                (charIndex < 0) ||
                (charCount < 0) ||
                (chars.Length - charIndex < charCount) ||
                (byteIndex < 0 || byteIndex > bytes.Length))
            {
                EncodingForwarder.ThrowValidationFailedException(chars, charIndex, charCount, bytes);
            }
            Contract.EndContractBlock();

            // Note that byteCount is the # of bytes to decode, not the size of the array
            int byteCount = bytes.Length - byteIndex;
            int bytesWritten;

            if (charCount > 0)
            {
                if (byteCount == 0)
                {
                    // Definitely not enough space, early bail
                    EncodingForwarder.ThrowBytesOverflow(this);
                }

                fixed(char *pInput = &chars[0])
                fixed(byte *pOutput = &bytes[0])
                {
                    char *input  = pInput + charIndex;
                    byte *output = pOutput + byteIndex;
                    int   charactersConsumed;

                    if (!EncodingForwarder.TryEncode(input, charCount, output, byteCount, out charactersConsumed, out bytesWritten))
                    {
                        // Not all ASCII, GetBytesFallback for remaining conversion
                        bytesWritten += GetBytesFallback(input + charactersConsumed, charCount - charactersConsumed, output + bytesWritten, byteCount - bytesWritten, null);
                    }
                }
            }
            else
            {
                // Nothing to encode
                bytesWritten = 0;
            }

            return(bytesWritten);
        }
コード例 #4
0
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            // Just need to Assert, this is called by internal EncoderNLS and parameters should already be checked
            Debug.Assert(this != null);
            Debug.Assert(bytes != null);
            Debug.Assert(chars != null);
            Debug.Assert(charCount >= 0);
            Debug.Assert(byteCount >= 0);

            int bytesWritten;
            int charactersConsumed = 0;

            if (((encoder?.InternalHasFallbackBuffer ?? false) &&
                 (encoder.FallbackBuffer.Remaining > 0)) ||
                (charCount > byteCount))
            {
                // Data already in Fallback buffer, so straight to GetBytesFallback
                bytesWritten = GetBytesFallback(chars, charCount, bytes, byteCount, encoder);
            }
            else if (charCount > 0)
            {
                if (byteCount == 0)
                {
                    // Definitely not enough space, early bail
                    EncodingForwarder.ThrowBytesOverflow(this);
                }
                if (!EncodingForwarder.TryEncode(chars, charCount, bytes, byteCount, out charactersConsumed, out bytesWritten))
                {
                    // Not all ASCII, use GetBytesFallback for remaining conversion
                    bytesWritten += GetBytesFallback(chars + charactersConsumed, charCount - charactersConsumed, bytes + bytesWritten, byteCount - bytesWritten, encoder);
                }
            }
            else
            {
                // Nothing to encode
                bytesWritten = 0;
            }

            if (encoder != null)
            {
                encoder.m_charsUsed += charactersConsumed;
            }

            return(bytesWritten);
        }