Exemple #1
0
        public static IOInfo Parse(RubyContext /*!*/ context, MutableString /*!*/ modeAndEncoding)
        {
            if (!modeAndEncoding.IsAscii())
            {
                throw IOModeEnum.IllegalMode(modeAndEncoding.ToAsciiString());
            }

            string[] parts = modeAndEncoding.ToString().Split(':');
            return(new IOInfo(
                       IOModeEnum.Parse(parts[0]),
                       (parts.Length > 1) ? TryParseEncoding(context, parts[1]) : null,
                       (parts.Length > 2) ? TryParseEncoding(context, parts[2]) : null
                       ));
        }
Exemple #2
0
        public IOInfo AddEncoding(RubyContext /*!*/ context, MutableString /*!*/ encoding)
        {
            if (!encoding.IsAscii())
            {
                context.ReportWarning(String.Format("Unsupported encoding {0} ignored", encoding.ToAsciiString()));
                return(this);
            }

            if (HasEncoding)
            {
                throw RubyExceptions.CreateArgumentError("encoding specified twice");
            }

            string[] parts = encoding.ToString().Split(':');
            return(new IOInfo(
                       _mode,
                       TryParseEncoding(context, parts[0]),
                       (parts.Length > 1) ? TryParseEncoding(context, parts[1]) : null
                       ));
        }
Exemple #3
0
        public static CharacterMap /*!*/ Create(MutableString /*!*/ from, MutableString /*!*/ to)
        {
            Debug.Assert(!from.IsEmpty);

            int  fromLength   = from.GetCharCount();
            bool complemental = from.StartsWith('^') && fromLength > 1;

            // TODO: kcodings
            // TODO: surrogates
            // TODO: max - min > threshold

            int min, max;

            if (from.IsBinaryEncoded || from.Encoding.IsKCoding || from.IsAscii())
            {
                min = 0;
                max = 255;
            }
            else
            {
                min = Int32.MaxValue;
                max = -1;
                for (int i = (complemental ? 1 : 0); i < fromLength; i++)
                {
                    int c = from.GetChar(i);
                    if (c < min)
                    {
                        min = c;
                    }
                    if (c > max)
                    {
                        max = c;
                    }
                }
            }

            BitArray map;

            char[] image;

            if (complemental || to.IsEmpty)
            {
                image = null;
                map   = MakeBitmap(from, fromLength, complemental, min, max);
            }
            else
            {
                map   = null;
                image = new char[max - min + 1];

                // no need to initialize the array:
                Debug.Assert(Unmapped == 0);

                bool needMap = false;
                var  toEnum  = ExpandRanges(to, 0, to.GetCharCount(), true).GetEnumerator();
                foreach (var f in ExpandRanges(from, 0, fromLength, false))
                {
                    toEnum.MoveNext();
                    needMap |= (image[f - min] = toEnum.Current) == Unmapped;
                }

                if (needMap)
                {
                    map = MakeBitmap(from, fromLength, false, min, max);
                }
            }

            return(new CharacterMap(map, image, complemental ? to.GetLastChar() : -1, complemental, min, max));
        }