Ejemplo n.º 1
0
        private static Encoding MakeSourceEncoding()
        {
            Encoding enc = new PythonAsciiEncoding();

#if FEATURE_ENCODING
            // we need to Clone the new instance here so that the base class marks us as non-readonly
            enc = (Encoding)enc.Clone();
            StringOps.SetDecoderFallback(enc, new SourceNonStrictDecoderFallback());
#endif
            return(enc);
        }
Ejemplo n.º 2
0
        private static PythonTuple DoDecode(Encoding encoding, object input, string errors, bool fAlwaysThrow)
        {
            // input should be character buffer of some form...
            string res;

            if (!Converter.TryConvertToString(input, out res))
            {
                Bytes tempBytes = input as Bytes;
                if (tempBytes == null)
                {
                    throw PythonOps.TypeErrorForBadInstance("argument 1 must be string, got {0}", input);
                }
                else
                {
                    res = tempBytes.ToString();
                }
            }

            int preOffset = CheckPreamble(encoding, res);

            byte[] bytes = new byte[res.Length - preOffset];
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = (byte)res[i + preOffset];
            }

#if FEATURE_ENCODING    // DecoderFallback
            encoding = (Encoding)encoding.Clone();
            ExceptionFallBack fallback = null;
            if (fAlwaysThrow)
            {
                StringOps.SetDecoderFallback(encoding, DecoderFallback.ExceptionFallback);
            }
            else
            {
                fallback = (encoding is UTF8Encoding && DotNet) ?
                           // This is a workaround for a bug, see ExceptionFallbackBufferUtf8DotNet
                           // for more details.
                           new ExceptionFallBackUtf8DotNet(bytes):
                           new ExceptionFallBack(bytes);
                StringOps.SetDecoderFallback(encoding, fallback);
            }
#endif
            string decoded      = encoding.GetString(bytes, 0, bytes.Length);
            int    badByteCount = 0;


#if FEATURE_ENCODING    // DecoderFallback
            if (!fAlwaysThrow)
            {
                byte[] badBytes = fallback.buffer.badBytes;
                if (badBytes != null)
                {
                    badByteCount = badBytes.Length;
                }
            }
#endif

            PythonTuple tuple = PythonTuple.MakeTuple(decoded, bytes.Length - badByteCount);
            return(tuple);
        }