Пример #1
0
        private static bool MatchClass(char c, char cl)
        {
            bool res;

            switch (cl)
            {
            case 'a': res = Char.IsLetter(c); break;

            case 'c': res = Char.IsControl(c); break;

            case 'd': res = Char.IsDigit(c); break;

            case 'g': throw new System.NotImplementedException();

            case 'l': res = Char.IsLower(c); break;

            case 'p': res = Char.IsPunctuation(c); break;

            case 's': res = Char.IsWhiteSpace(c); break;

            case 'u': res = Char.IsUpper(c); break;

            case 'w': res = Char.IsLetterOrDigit(c); break;

            case 'x': res = IsXDigit(c); break;

            case 'z': res = (c == '\0'); break;                      /* deprecated option */

            default: return(cl == c);
            }
            return(res);
        }
Пример #2
0
        public static int B_ToNumber(ILuaState lua)
        {
            LuaType t = lua.Type(2);

            if (t == LuaType.LUA_TNONE || t == LuaType.LUA_TNIL)              // standard conversion
            {
                bool   isnum;
                double n = lua.ToNumberX(1, out isnum);
                if (isnum)
                {
                    lua.PushNumber(n);
                    return(1);
                }                 // else not a number; must be something
                lua.L_CheckAny(1);
            }
            else
            {
                string s        = lua.L_CheckString(1);
                int    numBase  = lua.L_CheckInteger(2);
                bool   negative = false;
                lua.L_ArgCheck((2 <= numBase && numBase <= 36), 2,
                               "base out of range");
                s = s.Trim(' ', '\f', '\n', '\r', '\t', '\v');
                s = s + '\0';                 // guard
                int pos = 0;
                if (s[pos] == '-')
                {
                    pos++; negative = true;
                }
                else if (s[pos] == '+')
                {
                    pos++;
                }
                if (Char.IsLetterOrDigit(s, pos))
                {
                    double n = 0.0;
                    do
                    {
                        int digit;
                        if (Char.IsDigit(s, pos))
                        {
                            digit = Int32.Parse(s[pos].ToString());
                        }
                        else
                        {
                            digit = Char.ToUpper(s[pos]) - 'A' + 10;
                        }
                        if (digit >= numBase)
                        {
                            break;                             // invalid numeral; force a fail
                        }
                        n = n * (double)numBase + (double)digit;
                        pos++;
                    } while(Char.IsLetterOrDigit(s, pos));
                    if (pos == s.Length - 1)                      // except guard, no invalid trailing characters?
                    {
                        lua.PushNumber(negative ? -n : n);
                        return(1);
                    }             // else not a number
                }                 // else not a number
            }
            lua.PushNil();        // not a number
            return(1);
        }