Example #1
0
        /// <summary>
        /// Decodes the input string using the provided string mapping.
        /// </summary>
        public static PythonTuple charmap_decode(CodeContext context, [BytesConversion] IList <byte> input, string errors, [NotNull] string map)
        {
            EncodingMap m = new EncodingMap();

            for (int i = 0; i < map.Length; i++)
            {
                m.Mapping[i] = map[i];
            }
            return(CharmapDecodeWorker(context, input, errors, new EncodingMapEncoding(m, errors)));
        }
Example #2
0
        /// <summary>
        /// Creates an optimized encoding mapping that can be consumed by an optimized version of charmap_encode.
        /// </summary>
        public static EncodingMap charmap_build(string decoding_table)
        {
            if (decoding_table.Length != 256)
            {
                throw PythonOps.TypeError("charmap_build expected 256 character string");
            }

            EncodingMap map = new EncodingMap();

            for (int i = 0; i < decoding_table.Length; i++)
            {
                map.Mapping[(int)decoding_table[i]] = (char)i;
            }
            return(map);
        }
Example #3
0
        /// <summary>
        /// Encodes the input string with the specified optimized encoding map.
        /// </summary>
        public static PythonTuple charmap_encode(string input, string errors, [NotNull] EncodingMap map)
        {
            StringBuilder res  = new StringBuilder();
            var           dict = map.Mapping;

            for (int i = 0; i < input.Length; i++)
            {
                char val;

                if (!dict.TryGetValue(input[i], out val))
                {
                    if (errors == "strict")
                    {
                        throw PythonOps.UnicodeEncodeError("failed to find key in mapping");
                    }
                    res.Append("\ufffd");
                }
                else
                {
                    res.Append(val);
                }
            }
            return(PythonTuple.MakeTuple(res.ToString(), res.Length));
        }
Example #4
0
 /// <summary>
 /// Encodes the input string with the specified optimized encoding map.
 /// </summary>
 public static PythonTuple charmap_encode(CodeContext context, string input, string errors, [NotNull] EncodingMap map)
 {
     return(CharmapEncodeWorker(context, input, errors, new EncodingMapEncoding(map, errors)));
 }
Example #5
0
 public EncodingMapEncoding(EncodingMap map, string errors)
 {
     _map    = map;
     _errors = errors;
 }
Example #6
0
 public static PythonTuple charmap_encode(CodeContext context, [NotNull] string input, string?errors, [NotNull] EncodingMap map)
 {
     return(DoEncode(context, "charmap", new EncodingMapEncoding(map), input, errors).ToPythonTuple());
 }
Example #7
0
        public static PythonTuple charmap_decode(CodeContext context, [BytesConversion, NotNull] IList <byte> input, string?errors, [NotNull] string map)
        {
            EncodingMap em = new EncodingMap(map, compileForDecoding: true, compileForEncoding: false);

            return(DoDecode(context, "charmap", new EncodingMapEncoding(em), input, errors, input.Count).ToPythonTuple());
        }