Ejemplo n.º 1
0
        /// <summary>
        /// Create a copy of this string converted to uppercase.
        /// </summary>
        /// <remarks>
        /// This obviously allocates a new array to hold the uppercase data.
        ///
        /// The length of the output is not necessarily the same as the length of the input.
        /// </remarks>
        public utf8 ToUpper()
        {
            if (Length == 0)
            {
                return(Empty);
            }

            var it  = new Utf8Enumerator(this);
            int len = 0;

            while (it.MoveNext())
            {
                len += CharInfo.Utf8Length(CharInfo.ToUpper(it.Current.Value));
            }

            it.Reset();

            var buf    = new byte[len];
            var stream = new MemoryStream(buf);

            len = 0;
            while (it.MoveNext())
            {
                Utf8Writer.AppendCodepoint(stream, CharInfo.ToUpper(it.Current.Value));
            }

            return(new utf8(buf));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a copy of this string converted to lowercase.
        /// </summary>
        /// <remarks>
        /// This obviously allocates a new array to hold the lowercase data.
        ///
        /// The length of the output is not necessarily the same as the length of the input.
        /// </remarks>
        public Utf8String ToLower(CultureInfo info)
        {
            if (Length == 0)
            {
                return(Empty);
            }

            var ch  = new char[1];
            var it  = new Utf8Enumerator(this);
            int len = 0;

            while (it.MoveNext())
            {
                if (it.Current.Value <= 0x1FFFF)
                {
                    var lc = char.ToLower((char)it.Current.Value);
                    ch[0] = lc;
                    len  += Encoding.UTF8.GetByteCount(ch);
                }
                else
                {
                    // We're in emoji land, so no upper/lowercase.
                    // (This will fail in the future should unicode get codepoints representing
                    // upper/lowercase characters in this range.)
                    len += it.Current.EncodedLength;
                }
            }

            it.Reset();

            var buf = new byte[len];

            while (it.MoveNext())
            {
                if (it.Current.Value <= 0x1FFFF)
                {
                    var lc = char.ToLower((char)it.Current.Value);
                    ch[0] = lc;
                    len  += Encoding.UTF8.GetBytes(ch, 0, 1, buf, len);
                }
                else
                {
                    // We're in emoji land, so no upper/lowercase.
                    // (This will fail in the future should unicode get codepoints representing
                    // upper/lowercase characters in this range.)
                    for (int i = 0; i < it.Current.EncodedLength; i++)
                    {
                        buf[len] = this[i + it.Current.Index];
                        len++;
                    }
                }
            }

            return(new Utf8String(buf));
        }