コード例 #1
0
ファイル: LineWrapper.cs プロジェクト: Benedani/MCGalaxy
        static string CleanupColorCodes(string value)
        {
            if (value.IndexOf('&') == -1)
            {
                return(value);
            }
            char[] output = new char[value.Length];
            int    used = 0, last = -200;
            char   lastCol = 'f';

            for (int i = 0; i < value.Length; i++)
            {
                // Not a colour code so do nothing
                if (value[i] != '&')
                {
                    output[used++] = value[i]; continue;
                }
                if (i == value.Length - 1 || !Colors.ValidColor(value[i + 1]))
                {
                    output[used++] = value[i]; continue;
                }
                char col = value[i + 1];
                if (col >= 'A' && col <= 'F')
                {
                    col += ' ';
                }

                // Check for double colour codes in a row
                if (last == i - 2)
                {
                    used--;
                    output[used++] = col;
                }
                // Check for same colour codes but not in a row
                if (lastCol != col)
                {
                    output[used++] = '&';
                    output[used++] = col;
                }
                last    = i; i++; // skip over colour code
                lastCol = col;
            }

            // Trim trailing colour codes
            int j = value.Length;

            while ((j - 2) >= 0)
            {
                if (value[j - 2] != '&')
                {
                    break;
                }
                if (!Colors.ValidColor(value[j - 1]))
                {
                    break;
                }
                j -= 2; used -= 2;
            }
            return(new string(output, 0, used));
        }