コード例 #1
0
        public static int Each(string str, Func <char, bool> callback)
        {
            int n = 0;

            DrawStringMode mode = DrawStringMode.Normal;

            string fg = string.Empty;
            string bg = string.Empty;

            foreach (char c in str)
            {
                switch (mode)
                {
                case DrawStringMode.Normal:
                    if (c == '\x0000')
                    {
                        mode = DrawStringMode.ForegroundColor;
                    }
                    else
                    {
                        if (!callback(c))
                        {
                            return(n);
                        }
                        n++;
                    }
                    break;

                case DrawStringMode.ForegroundColor:
                    if (char.IsNumber(c))
                    {
                        fg += c;
                    }
                    else if (c == ',')
                    {
                        mode = DrawStringMode.Background;
                    }
                    else if (c == ' ')
                    {
                        ushort foreground = ushort.MaxValue;
                        if (fg != string.Empty)
                        {
                            foreground = ushort.Parse(fg);
                        }
                        Curses.attron(ColorPair.From(foreground, ushort.MaxValue).Attribute);
                        fg   = string.Empty;
                        mode = DrawStringMode.Normal;
                    }
                    else
                    {
                        throw new Exception("Malformed color string");
                    }
                    break;

                case DrawStringMode.Background:
                    if (char.IsNumber(c))
                    {
                        bg += c;
                    }
                    else if (c == ' ')
                    {
                        Curses.attron(ColorPair.From(ushort.Parse(fg), ushort.Parse(bg)).Attribute);
                        fg   = string.Empty;
                        bg   = string.Empty;
                        mode = DrawStringMode.Normal;
                    }
                    else
                    {
                        throw new Exception("Malformed color string");
                    }
                    break;
                }
            }
            return(n);
        }