private static CharPtr b_str2int(CharPtr s, int base_, ref lua_Integer pn)
        {
            s = new CharPtr(s);       //FIXME:???
            lua_Unsigned n   = 0;
            int          neg = 0;

            s += strspn(s, SPACECHARS);        /* skip initial spaces */
            if (s[0] == '-')
            {
                s.inc(); neg = 1;
            }                                             /* handle signal */
            else if (s[0] == '+')
            {
                s.inc();
            }
            if (!isalnum((byte)s[0]))        /* no digit? */
            {
                return(null);
            }
            do
            {
                int digit = (isdigit((byte)s[0])) ? s[0] - '0'
                        : (toupper((byte)s[0]) - 'A') + 10;
                if (digit >= base_)
                {
                    return(null);                     /* invalid numeral */
                }
                n = (lua_Unsigned)(n * base_ + digit);
                s.inc();
            } while (isalnum((byte)s[0]));
            s += strspn(s, SPACECHARS);        /* skip trailing spaces */
            pn = (lua_Integer)((neg != 0) ? (0u - n) : n);
            return(s);
        }
        private static int unpack(lua_State L)
        {
            TabA         ta = new TabA();
            lua_Integer  i, e;
            lua_Unsigned n = 0;

            checktab(L, 1, ta);
            i = luaL_optinteger(L, 2, 1);
            e = luaL_opt_integer(L, luaL_checkinteger, 3, luaL_len(L, 1));
            if (i > e)
            {
                return(0);                   /* empty range */
            }
            n = (uint)((lua_Unsigned)e - i); /* number of elements minus 1 (avoid overflows) */
            if (n >= (uint)INT_MAX || 0 == lua_checkstack(L, (int)(++n)))
            {
                return(luaL_error(L, "too many results to unpack"));
            }
            do                    /* must have at least one element */
            {
                ta.geti(L, 1, i); /* push arg[i..e] */
            } while (i++ < e);

            return((int)n);
        }
Exemple #3
0
		private static CharPtr l_str2int (CharPtr s, ref lua_Integer result) {
		  s = new CharPtr(s); //FIXME:added			
		  lua_Unsigned a = 0;
		  int empty = 1;
		  int neg;
		  while (lisspace((byte)(s[0]))!=0) s.inc();  /* skip initial spaces */
		  neg = isneg(ref s);
		  if (s[0] == '0' &&
		      (s[1] == 'x' || s[1] == 'X')) {  /* hexa? */
		    s += 2;  /* skip '0x' */
		    for (; lisxdigit((byte)(s[0]))!=0; s.inc()) {
		      a = (uint)(a * 16 + luaO_hexavalue(cast_uchar(s[0])));
		      empty = 0;
		    }
		  }
		  else {  /* decimal */
		  	for (; lisdigit(cast_uchar(s[0]))!=0; s.inc()) {
		  	  a = (uint)(a * 10 + luaO_hexavalue(cast_uchar(s[0])));
		      empty = 0;
		    }
		  }
		  while (lisspace((byte)(s[0]))!=0) s.inc();  /* skip trailing spaces */
		  if (empty!=0 || s[0] != '\0') return null;  /* something wrong in the numeral */
		  else {
		    result = l_castU2S((neg!=0) ? 0u - a : a);
		    return s;
		  }
		}
Exemple #4
0
 public static void lua_pushunsigned(lua_State L, lua_Unsigned u)
 {
     lua_lock(L);
     setivalue(L.top, cast_integer(u));
     api_incr_top(L);
     lua_unlock(L);
 }
Exemple #5
0
        public static void lua_pushunsigned(LuaState L, lua_Unsigned u)
        {
            lua_Number n;

            lua_lock(L);
            n = (((u) <= (lua_Unsigned)int.MaxValue) ? (lua_Number)(int)(u) : (lua_Number)(u));
            setnvalue(L.top, n);
            api_incr_top(L);
            lua_unlock(L);
        }
Exemple #6
0
        public static void lua_pushunsigned(lua_State L, lua_Unsigned u)
        {
            lua_Number n;

            lua_lock(L);
            n = lua_unsigned2number(u);
            setnvalue(L.top, n);
            api_incr_top(L);
            lua_unlock(L);
        }
        public static lua_Unsigned luaL_checkunsigned(lua_State L, int narg)
        {
            int          isnum = 0; //FIXME: changed, =0
            lua_Unsigned d     = lua_tounsignedx(L, narg, ref isnum);

            if (isnum == 0)
            {
                tag_error(L, narg, LUA_TNUMBER);
            }
            return(d);
        }
Exemple #8
0
        public static lua_Unsigned luaL_checkunsigned(LuaState L, int narg)
        {
            bool         isnum = false;
            lua_Unsigned d     = lua_tounsignedx(L, narg, ref isnum);

            if (!isnum)
            {
                tag_error(L, narg, LUA_TNUMBER);
            }
            return(d);
        }
Exemple #9
0
        public static int luaO_str2int(CharPtr s, uint len, ref lua_Integer result)
        {
            s = new CharPtr(s);       //FIXME:added
            CharPtr      ends  = s + len;
            lua_Unsigned a     = 0;
            int          empty = 1;
            int          neg;

            while (lisspace((byte)(s[0])) != 0)
            {
                s.inc();                                      /* skip initial spaces */
            }
            neg = isneg(ref s);
            if (s[0] == '0' &&
                (s[1] == 'x' || s[1] == 'X')) /* hexa? */
            {
                s += 2;                       /* skip '0x' */
                for (; lisxdigit((byte)(s[0])) != 0; s.inc())
                {
                    a     = (uint)(a * 16 + luaO_hexavalue(cast_uchar(s[0])));
                    empty = 0;
                }
            }
            else          /* decimal */
            {
                for (; lisdigit(cast_uchar(s[0])) != 0; s.inc())
                {
                    a     = (uint)(a * 10 + luaO_hexavalue(cast_uchar(s[0])));
                    empty = 0;
                }
            }
            while (lisspace((byte)(s[0])) != 0)
            {
                s.inc();                                      /* skip trailing spaces */
            }
            if (empty != 0 || s != ends)
            {
                return(0);                              /* something wrong in the numeral */
            }
            else
            {
                if (neg != 0)
                {
                    result = -((lua_Integer)a);
                }
                else
                {
                    result = (lua_Integer)(a);
                }
                return(1);
            }
        }
Exemple #10
0
        static int b_replace(LuaState L)
        {
            int          w = 0;
            b_uint       r = luaL_checkunsigned(L, 1);
            b_uint       v = luaL_checkunsigned(L, 2);
            int          f = fieldargs(L, 3, ref w);
            lua_Unsigned m = mask(w);

            v &= m;  /* erase bits outside given width */
            r  = (r & ~(m << f)) | (v << f);
            lua_pushunsigned(L, r);
            return(1);
        }
Exemple #11
0
        private static CharPtr l_str2int(CharPtr s, ref lua_Integer result)
        {
            s = new CharPtr(s);       //FIXME:added
            lua_Unsigned a     = 0;
            int          empty = 1;
            int          neg;

            while (lisspace((byte)(s[0])) != 0)
            {
                s.inc();                                      /* skip initial spaces */
            }
            neg = isneg(ref s);
            if (s[0] == '0' &&
                (s[1] == 'x' || s[1] == 'X')) /* hex? */
            {
                s += 2;                       /* skip '0x' */
                for (; lisxdigit((byte)(s[0])) != 0; s.inc())
                {
                    a     = (uint)(a * 16 + luaO_hexavalue(s[0]));
                    empty = 0;
                }
            }
            else          /* decimal */
            {
                for (; lisdigit(cast_uchar(s[0])) != 0; s.inc())
                {
                    int d = s[0] - '0';
                    if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
                    {
                        return(null);                                        /* do not accept it (as integer) */
                    }
                    a     = (uint)(a * 10 + d);
                    empty = 0;
                }
            }
            while (lisspace((byte)(s[0])) != 0)
            {
                s.inc();                                      /* skip trailing spaces */
            }
            if (empty != 0 || s[0] != '\0')
            {
                return(null);                                 /* something wrong in the numeral */
            }
            else
            {
                result = l_castU2S((neg != 0) ? 0u - a : a);
                return(s);
            }
        }
Exemple #12
0
        public static lua_Unsigned lua_tounsignedx(lua_State L, int idx, ref int pisnum)
        {
            lua_Unsigned res   = 0;
            TValue       o     = index2addr(L, idx);
            int          isnum = 0;

            switch (ttype(o))
            {
            case LUA_TNUMINT: {
                res   = cast_unsigned(ivalue(o));
                isnum = 1;
                break;
            }

            case LUA_TNUMFLT: {                                         /* compute floor(n) % 2^(numbits in an integer) */
                lua_Number twop = cast_num(MAX_UINTEGER) + cast_num(1); /* 2^n */
                lua_Number n    = fltvalue(o);                          /* get value */
                int        neg  = 0;
                n = floor(n);                                           /* get its floor */
                if (n < 0)
                {
                    neg = 1;
                    n   = -n;           /* make 'n' positive, so that 'fmod' is the same as '%' */
                }
                n = fmod(n, twop);      /* n = n % 2^(numbits in an integer) */
                if (luai_numisnan(n))   /* not a number? */
                {
                    break;              /* not an integer, too */
                }
                res = cast_unsigned(n); /* 'n' now must fit in an unsigned */
                if (neg != 0)
                {
                    res = 0u - res;                /* back to negative, if needed */
                }
                isnum = 1;
                break;
            }

            default: break;
            }
            /*if (pisnum)*/ pisnum = isnum;
            return(res);
        }
Exemple #13
0
        public static lua_Unsigned lua_tounsignedx(lua_State L, int idx, ref int pisnum)
        {
            lua_Unsigned res   = 0;
            TValue       o     = index2addr(L, idx);
            int          isnum = 0;

            switch (ttype(o))
            {
            case LUA_TNUMINT: {
                res   = cast_unsigned(ivalue(o));
                isnum = 1;
                break;
            }

            case LUA_TNUMFLT: {
                lua_Number twop = (~(lua_Unsigned)0) + cast_num(1);
                lua_Number n    = fltvalue(o);
                int        neg  = 0;
                n = floor(n);
                if (n < 0)
                {
                    neg = 1;
                    n   = -n;
                }
                n = fmod(n, twop);
                if (luai_numisnan(L, n)) /* not a number? */
                {
                    break;               /* not an integer, too */
                }
                res = cast_unsigned(n);
                if (neg != 0)
                {
                    res = 0u - res;
                }
                isnum = 1;
                break;
            }

            default: break;
            }
            /*if (pisnum)*/ pisnum = isnum;
            return(res);
        }
Exemple #14
0
        private static int unpack(lua_State L)
        {
            lua_Unsigned n = 0;
            lua_Integer  i = luaL_optinteger(L, 2, 1);
            lua_Integer  e = luaL_opt_integer(L, luaL_checkinteger, 3, luaL_len(L, 1));

            if (i > e)
            {
                return(0);                   /* empty range */
            }
            n = (uint)((lua_Unsigned)e - i); /* number of elements minus 1 (avoid overflows) */
            if (n >= (uint)INT_MAX || 0 == lua_checkstack(L, (int)(++n)))
            {
                return(luaL_error(L, "too many results to unpack"));
            }
            for (; i < e; i++)          /* push arg[i..e - 1] (to avoid overflows) */
            {
                lua_geti(L, 1, i);
            }
            lua_geti(L, 1, e);        /* push last element */
            return((int)n);
        }
        private static int b_str2int(CharPtr s, CharPtr e, int base_, ref lua_Integer pn)
        {
            lua_Unsigned n   = 0;
            int          neg = 0;

            s += strspn(s, SPACECHARS);        /* skip initial spaces */
            if (s[0] == '-')
            {
                s.inc(); neg = 1;
            }                                             /* handle signal */
            else if (s[0] == '+')
            {
                s.inc();
            }
            if (!isalnum((byte)s[0]))        /* no digit? */
            {
                return(0);
            }
            do
            {
                int digit = (isdigit((byte)s[0])) ? s[0] - '0'
                        : toupper((byte)s[0]) - 'A' + 10;
                if (digit >= base_)
                {
                    return(0);                     /* invalid numeral */
                }
                n = (lua_Unsigned)(n * base_ + digit);
                s.inc();
            } while (isalnum((byte)s[0]));
            s += strspn(s, SPACECHARS); /* skip trailing spaces */
            if (s != e)                 /* invalid trailing characters? */
            {
                return(0);
            }
            pn = (neg != 0) ? -(lua_Integer)n : (lua_Integer)n;
            return(1);
        }
Exemple #16
0
        public static lua_Unsigned unbound_search(Table t, lua_Unsigned j)
        {
            lua_Unsigned i = j;        /* i is zero or a present index */

            j++;
            /* find 'i' and 'j' such that i is present and j is not */
            while (!ttisnil(luaH_getint(t, (int)j)))         //FIXME:(int)
            {
                i = j;
                if (j > l_castS2U(LUA_MAXINTEGER) / 2)            /* overflow? */
                /* table was built with bad purposes: resort to linear search */
                {
                    i = 1;
                    while (!ttisnil(luaH_getint(t, (int)i)))
                    {
                        i++;                                            //FIXME:(int)
                    }
                    return(i - 1);
                }
                j *= 2;
            }
            /* now do a binary search between them */
            while (j - i > 1)
            {
                lua_Unsigned m = (i + j) / 2;
                if (ttisnil(luaH_getint(t, (int)m)))
                {
                    j = m;                                          //FIXME:(int)
                }
                else
                {
                    i = m;
                }
            }
            return(i);
        }
 public static lua_Unsigned luaL_optunsigned(lua_State L, int narg,
                                             lua_Unsigned def)
 {
     return(luaL_opt_unsigned(L, luaL_checkunsigned, narg, def));
 }
Exemple #18
0
        /* }============================================================== */


        /*
        ** {==============================================================
        ** compatibility macros for unsigned conversions
        ** ===============================================================
        */
        //#if defined(LUA_COMPAT_APIINTCASTS)

        public static void lua_pushunsigned(lua_State L, lua_Unsigned n)
        {
            lua_pushinteger(L, (lua_Integer)(n));
        }
Exemple #19
0
 /* macro to trim extra bits */
 public static lua_Unsigned trim(lua_Unsigned x)
 {
     return((x) & ALLONES);
 }
Exemple #20
0
 /* macro to trim extra bits */
 public static lua_Unsigned trim(lua_Unsigned x)
 {
     return ((x) & ALLONES);
 }
Exemple #21
0
 public static lua_Unsigned luaL_opt_unsigned(lua_State L, luaL_opt_delegate_unsigned f, int n, lua_Unsigned u)
 {
     return((lua_Unsigned)(lua_isnoneornil(L, n) ? u : f(L, (n))));
 }