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 !SILVERLIGHT // DecoderFallback encoding = (Encoding)encoding.Clone(); ExceptionFallBack fallback = null; if (fAlwaysThrow) { encoding.DecoderFallback = DecoderFallback.ExceptionFallback; } else { fallback = new ExceptionFallBack(bytes); encoding.DecoderFallback = fallback; } #endif string decoded = encoding.GetString(bytes, 0, bytes.Length); int badByteCount = 0; #if !SILVERLIGHT // 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); }
private static Tuple DoDecode(Encoding encoding, object input, string errors, bool fAlwaysThrow) { // input should be character buffer of some form... Conversion conv; string res = Converter.TryConvertToString(input, out conv); if (conv != Conversion.None) { 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]; } encoding = (Encoding)encoding.Clone(); ExceptionFallBack fallback = null; if (fAlwaysThrow) { encoding.DecoderFallback = DecoderFallback.ExceptionFallback; } else { fallback = new ExceptionFallBack(bytes); encoding.DecoderFallback = fallback; } string decoded = encoding.GetString(bytes); int badByteCount = 0; if (!fAlwaysThrow) { byte[] badBytes = fallback.buffer.badBytes; if (badBytes != null) { badByteCount = badBytes.Length; } } Tuple tuple = Tuple.MakeTuple(decoded, bytes.Length - badByteCount); return(tuple); } else { throw Ops.TypeError("argument 1 must be string, got {0}", Ops.GetDynamicType(input).__name__); } }
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 bs = input as Bytes; if (bs == null) { throw PythonOps.TypeErrorForBadInstance("argument 1 must be string, got {0}", input); } else { res = bs.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 !SILVERLIGHT // DecoderFallback encoding = (Encoding)encoding.Clone(); ExceptionFallBack fallback = null; if (fAlwaysThrow) { encoding.DecoderFallback = DecoderFallback.ExceptionFallback; } else { fallback = new ExceptionFallBack(bytes); encoding.DecoderFallback = fallback; } #endif string decoded = encoding.GetString(bytes, 0, bytes.Length); int badByteCount = 0; #if !SILVERLIGHT // 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; }
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); }