Esempio n. 1
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.DetectByteCharacters())
            {
                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));
        }