//#endif //#if !defined(lua_checkmode) /* ** Check whether 'mode' matches '[rwa]%+?b?'. ** Change this macro to accept other modes for 'fopen' besides ** the standard ones. */ public static bool lua_checkmode(CharPtr mode) { if (!(mode[0] != '\0')) { return(false); } if (!(strchr("rwa", mode[0]) != null)) { mode.inc(); return(false); } mode.inc(); if (!(mode[0] != '+')) { return(false); } mode.inc(); /* skip if char is '+' */ if (!(mode[0] != 'b')) { return(false); } mode.inc(); /* skip if char is 'b' */ if (!(mode[0] == '\0')) { return(false); } return(true); }
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; } }
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 os_date(lua_State L) { CharPtr s = luaL_optstring(L, 1, "%c"); DateTime stm; if (s[0] == '!') /* UTC? */ { stm = DateTime.UtcNow; s.inc(); /* skip `!' */ } else { stm = DateTime.Now; } if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setfield(L, "sec", stm.Second); setfield(L, "min", stm.Minute); setfield(L, "hour", stm.Hour); setfield(L, "day", stm.Day); setfield(L, "month", stm.Month); setfield(L, "year", stm.Year); setfield(L, "wday", (int)stm.DayOfWeek); setfield(L, "yday", stm.DayOfYear); setboolfield(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0); } else { #if true luaL_error(L, "strftime not implemented yet"); // todo: implement this - mjf #else CharPtr cc = new char[3]; luaL_Buffer b = null; cc[0] = '%'; cc[2] = '\0'; luaL_buffinit(L, b); for (; s[0] != 0; s.inc()) { if (s[0] != '%' || s[1] == '\0') /* no conversion specifier? */ { luaL_addchar(b, s[0]); } else { uint reslen; CharPtr buff = new char[200]; /* should be big enough for any conversion result */ s.inc(); cc[1] = s[0]; //reslen = strftime(buff, buff.Length, cc, stm); luaL_addlstring(b, buff, reslen); } } luaL_pushresult(b); #endif // #if 0 } return(1); }
private static lua_Number readhexa(ref CharPtr s, lua_Number r, ref int count) { for (; lisxdigit(cast_uchar(s[0])) != 0; s.inc()) /* read integer part */ { r = (r * 16.0) + cast_num(luaO_hexavalue(cast_uchar(s[0]))); s.inc(); count++; } return(r); }
/* ** convert an hexadecimal numeric string to a number, following ** C99 specification for 'strtod' */ private static lua_Number lua_strx2number(CharPtr s, out CharPtr endptr) { lua_Number r = 0.0; int e = 0, i = 0; int neg = 0; /* 1 if number is negative */ endptr = (CharPtr)(s); /* nothing is valid yet */ while (lisspace(cast_uchar(s[0])) != 0) { s.inc(); /* skip initial spaces */ } neg = isneg(ref s); /* check signal */ if (!(s[0] == '0' && (s[0 + 1] == 'x' || s[0 + 1] == 'X'))) /* check '0x' */ { return(0.0); /* invalid format (no '0x') */ } s += 2; /* skip '0x' */ r = readhexa(ref s, r, ref i); /* read integer part */ if (s[0] == '.') { s.inc(); /* skip dot */ r = readhexa(ref s, r, ref e); /* read fractional part */ } if (i == 0 && e == 0) { return(0.0); /* invalid format (no digit) */ } e *= -4; /* each fractional digit divides value by 2^-4 */ endptr = (CharPtr)(s); /* valid up to here */ if (s[0] == 'p' || s[0] == 'P') /* exponent part? */ { int exp1 = 0; int neg1; s.inc(); /* skip 'p' */ neg1 = isneg(ref s); /* signal */ if (0 == lisdigit(cast_uchar(s[0]))) { goto ret; /* must have at least one digit */ } while (lisdigit(cast_uchar(s[0])) != 0) /* read exponent */ { exp1 = exp1 * 10 + s[0] - '0'; s.inc(); } if (neg1 != 0) { exp1 = -exp1; } e += exp1; } endptr = (CharPtr)(s); /* valid up to here */ ret: if (neg != 0) { r = -r; } return(ldexp(r, e)); }
private static int luaB_tonumber(lua_State L) { if (lua_isnoneornil(L, 2)) /* standard conversion */ { int isnum = 0; //FIXME:changed, =0 lua_Number n = lua_tonumberx(L, 1, ref isnum); if (isnum != 0) { lua_pushnumber(L, n); return(1); } /* else not a number; must be something */ luaL_checkany(L, 1); } else { uint l; CharPtr s = luaL_checklstring(L, 1, out l); CharPtr e = s + l; /* end point for 's' */ int base_ = luaL_checkint(L, 2); int neg = 0; luaL_argcheck(L, 2 <= base_ && base_ <= 36, 2, "base out of range"); s += strspn(s, SPACECHARS); /* skip initial spaces */ if (s[0] == '-') { s.inc(); neg = 1; } /* handle signal */ //FIXME:changed, s++; else if (s[0] == '+') { s.inc(); //FIXME:changed, s++; } if (isalnum((byte)s[0])) { lua_Number n = 0; do { int digit = (isdigit((byte)s[0])) ? s[0] - '0' : toupper((byte)s[0]) - 'A' + 10; if (digit >= base_) { break; /* invalid numeral; force a fail */ } n = n * (lua_Number)base_ + (lua_Number)digit; s.inc(); //FIXME:changed, s++; } while (isalnum((byte)s[0])); s += strspn(s, SPACECHARS); /* skip trailing spaces */ if (s == e) /* no invalid trailing characters? */ { lua_pushnumber(L, (neg != 0) ? -n : n); return(1); } /* else not a number */ } /* else not a number */ } lua_pushnil(L); /* not a number */ return(1); }
/* ** convert an hexadecimal numeric string to a number, following ** C99 specification for 'strtod' */ private static lua_Number lua_strx2number (CharPtr s, out CharPtr endptr) { s = new CharPtr(s); //FIXME: added lua_Number r = 0.0; /* result (accumulator) */ int sigdig = 0; /* number of significant digits */ int nosigdig = 0; /* number of non-significant digits */ int e = 0; /* exponent correction */ int neg = 0; /* 1 if number is negative */ int dot = 0; /* true after seen a dot */ endptr = (CharPtr)(s); /* nothing is valid yet */ while (lisspace((byte)(s[0]))!=0) s.inc(); /* skip initial spaces */ neg = isneg(ref s); /* check signal */ if (!(s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))) /* check '0x' */ return 0.0; /* invalid format (no '0x') */ for (s += 2; ; s.inc()) { /* skip '0x' and read numeral */ if (s[0] == '.') { if (dot!=0) break; /* second dot? stop loop */ else dot = 1; } else if (lisxdigit((byte)(s[0]))!=0) { if (sigdig == 0 && s[0] == '0') { /* non-significant zero? */ nosigdig++; if (dot!=0) e--; /* zero after dot? correct exponent */ } else { if (++sigdig <= MAXSIGDIG) { /* can read it without overflow? */ r = (r * cast_num(16.0)) + luaO_hexavalue((byte)(s[0])); if (dot!=0) e--; /* decimal digit */ } else /* too many digits; ignore */ if (dot==0) e++; /* still count it for exponent */ } } else break; /* neither a dot nor a digit */ } if (nosigdig + sigdig == 0) /* no digits? */ return 0.0; /* invalid format */ endptr = (CharPtr)(s); /* valid up to here */ e *= 4; /* each digit multiplies/divides value by 2^4 */ if (s[0] == 'p' || s[0] == 'P') { /* exponent part? */ int exp1 = 0; /* exponent value */ int neg1; /* exponent signal */ s.inc(); /* skip 'p' */ neg1 = isneg(ref s); /* signal */ if (0==lisdigit((byte)(s[0]))) return 0.0; /* invalid; must have at least one digit */ while (lisdigit((byte)(s[0]))!=0) { /* read exponent */ exp1 = exp1 * 10 + s[0] - '0'; s.inc(); } if (neg1!=0) exp1 = -exp1; e += exp1; endptr = (CharPtr)(s); /* valid up to here */ } if (neg!=0) r = -r; return ldexp(r, e); }
private static int OSDate(LuaState L) { CharPtr s = LuaLOptString(L, 1, "%c"); DateTime stm; if (s[0] == '!') /* UTC? */ { stm = DateTime.UtcNow; s.inc(); /* skip `!' */ } else { stm = DateTime.Now; } if (strcmp(s, "*t") == 0) { LuaCreateTable(L, 0, 9); /* 9 = number of fields */ SetField(L, "sec", stm.Second); SetField(L, "min", stm.Minute); SetField(L, "hour", stm.Hour); SetField(L, "day", stm.Day); SetField(L, "month", stm.Month); SetField(L, "year", stm.Year); SetField(L, "wday", (int)stm.DayOfWeek); SetField(L, "yday", stm.DayOfYear); SetBoolField(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0); } else { CharPtr cc = new char[3]; LuaLBuffer b = new LuaLBuffer(); cc[0] = '%'; cc[2] = '\0'; LuaLBuffInit(L, b); for (; s[0] != 0; s.inc()) { if (s[0] != '%' || s[1] == '\0') /* no conversion specifier? */ { LuaLAddChar(b, s[0]); } else { uint reslen; CharPtr buff = new char[200]; /* should be big enough for any conversion result */ s.inc(); cc[1] = s[0]; reslen = strftime(buff, (uint)buff.chars.Length, cc, stm); buff.index = 0; LuaLAddLString(b, buff, reslen); } } LuaLPushResult(b); } return(1); }
private static int isneg(ref CharPtr s) { if (s[0] == '-') { s.inc(); return(1); } else if (s[0] == '+') { s.inc(); } return(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); } }
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); } }
private static int os_date(lua_State L) //FIXME:changed, implemented by self { CharPtr s = luaL_optstring(L, 1, "%c"); DateTime stm; if (s[0] == '!') /* UTC? */ { stm = DateTime.UtcNow; s.inc(); /* skip `!' */ } else { stm = DateTime.Now; } if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setallfields(L, stm); } else { luaL_error(L, "strftime not implemented yet"); // todo: implement this - mjf #if false //FIXME:not implemented ------------------> char cc[4]; /* buffer for individual conversion specifiers */
private static int os_date(lua_State L) { CharPtr s = luaL_optstring(L, 1, "%c"); DateTime stm; if (s[0] == '!') /* UTC? */ { stm = DateTime.UtcNow; s.inc(); /* skip `!' */ } else { stm = DateTime.Now; } if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setfield(L, "sec", stm.Second); setfield(L, "min", stm.Minute); setfield(L, "hour", stm.Hour); setfield(L, "day", stm.Day); setfield(L, "month", stm.Month); setfield(L, "year", stm.Year); setfield(L, "wday", (int)stm.DayOfWeek); setfield(L, "yday", stm.DayOfYear); setboolfield(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0); } else { luaL_error(L, "strftime not implemented yet"); // todo: implement this - mjf #if false //FIXME:not implemented ------------------> char cc[4];
/* * make header */ public static void luaU_header(CharPtr h) { h = new CharPtr(h); int x = 1; memcpy(h, LUA_SIGNATURE, LUA_SIGNATURE.Length); h = h.add(LUA_SIGNATURE.Length); h[0] = (char)LUAC_VERSION; h.inc(); h[0] = (char)LUAC_FORMAT; h.inc(); //*h++=(char)*(char*)&x; /* endianness */ h[0] = (char)x; /* endianness */ h.inc(); h[0] = (char)sizeof(int); h.inc(); h[0] = (char)sizeof(uint); h.inc(); h[0] = (char)sizeof(Instruction); h.inc(); h[0] = (char)sizeof(lua_Number); h.inc(); //(h++)[0] = ((lua_Number)0.5 == 0) ? 0 : 1; /* is lua_Number integral? */ h[0] = (char)0; // always 0 on this build }
/* ** Given a constant string, eliminate its delimeters (" or '), search it at ** constant table and return its index. If not found, allocate at end of ** the table, checking oveflow and return its index. ** ** For each allocation, the function allocate a extra char to be used to ** mark used string (it's necessary to deal with constant and string ** uniformily). The function store at the table the second position allocated, ** that represents the beginning of the real string. On error, return -1. ** */ public static int lua_findenclosedconstant(CharPtr s) { int i, j, l = (int)strlen(s); CharPtr c = new CharPtr(new char[l]); /* make a copy */ c.inc(); /* create mark space */ /* introduce scape characters */ for (i = 1, j = 0; i < l - 1; i++) { if (s[i] == '\\') { switch (s[++i]) { case 'n': c[j++] = '\n'; break; case 't': c[j++] = '\t'; break; case 'r': c[j++] = '\r'; break; default: c[j++] = '\\'; c[j++] = c[i]; break; } } else { c[j++] = s[i]; } } c[j++] = '\0'; for (i = 0; i < lua_nconstant; i++) { if (streq(c, lua_constant[i])) { free(c - 1); return(i); } } if (lua_nconstant >= MAXCONSTANT - 1) { lua_error("lua: constant string table overflow"); return(-1); } lua_constant[lua_nconstant++] = c; return(lua_nconstant - 1); }
/* ** Duplicate a string, creating a mark space at the beginning. ** Return the new string pointer. */ public static CharPtr lua_strdup(CharPtr l) { CharPtr s = (CharPtr)calloc_char(strlen(l) + 2); if (s == null) { lua_error("not enough memory"); return(null); } s[0] = '\0'; s.inc(); // create mark space return(strcpy(s, l)); }
/* ** Concatenate two given string, creating a mark space at the beginning. ** Return the new string pointer. */ internal static CharPtr lua_strconc(CharPtr l, CharPtr r) { CharPtr s = (CharPtr)calloc_char(strlen(l) + strlen(r) + 2); if (s == null) { lua_error("not enough memory"); return(null); } s[0] = '\0'; s.inc(); // create mark space return(strcat(strcpy(s, l), r)); }
/* ** Function to get the next character from the input string */ private static int stringinput() { st.inc(); int ret = (int)st[-1]; // if (ret == 34) // { // int a = 0; // } // printf("stringinput >>> %d\n", ret); return(ret); }
//#endif /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ private static int l_checkmode(CharPtr mode) { if (!(mode[0] != '\0')) { return(0); } if (!(strchr("rwa", mode[0]) != null)) { mode.inc(); return(0); } mode.inc(); if (!(mode[0] != '+')) { return(0); } mode.inc(); /* skip if char is '+' */ if (!(strspn(mode, L_MODEEXT) == strlen(mode))) { return(0); } return(1); /* check extensions */ }
//#endif /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ private static bool l_checkmode(CharPtr mode) { if (!(mode[0] != '\0')) { return(false); } if (!(strchr("rwa", mode[0]) != null)) { mode.inc(); return(false); } mode.inc(); if (!(mode[0] != '+')) { return(false); } mode.inc(); /* skip if char is '+' */ if (!(strspn(mode, L_MODEEXT) == strlen(mode))) { return(false); } return(true); }
private const int FORMAT = 0; /* this is the official format */ /* * make header for precompiled chunks * if you change the code below be sure to update LoadHeader and FORMAT above * and LUAC_HEADERSIZE in lundump.h */ public static void luaU_header(CharPtr h) //FIXME:changed, lu_byte* { int x = 1; memcpy(h, LUA_SIGNATURE, (uint)LUA_SIGNATURE.Length); //FIXME:changed, sizeof(LUA_SIGNATURE)-sizeof(char) h = h.add(LUA_SIGNATURE.Length); //FIXME:changed, sizeof(LUA_SIGNATURE)-sizeof(char); h[0] = (char)(byte)VERSION; h.inc(); //FIXME:changed, (char) h[0] = (char)(byte)FORMAT; h.inc(); //FIXME:changed, (char) h[0] = (char)(byte)x; h.inc(); /* endianness */ //FIXME:changed, *h++=cast_byte(*(char*)&x); //FIXME:changed, (char) h[0] = (char)(byte)GetUnmanagedSize(typeof(int)); h.inc(); //FIXME:changed, (char) h[0] = (char)(byte)GetUnmanagedSize(typeof(uint)); h.inc(); //FIXME:changed, (char) h[0] = (char)(byte)GetUnmanagedSize(typeof(Instruction)); h.inc(); //FIXME:changed, (char) h[0] = (char)(byte)GetUnmanagedSize(typeof(lua_Number)); h.inc(); //FIXME:changed, (char) h[0] = (char)(byte)(((lua_Number)0.5) == 0 ? 1 : 0); h.inc(); /* is lua_Number integral? */ //FIXME:???always 0 on this build //FIXME:changed, (char) memcpy(h, LUAC_TAIL, (uint)LUAC_TAIL.Length); //FIXME:changed, sizeof(LUAC_TAIL)-sizeof(char) }
private static CharPtr StrFTimeAdd(CharPtr str, CharPtr pt, CharPtr ptlim) { pt[0] = str[0]; str = str.next(); while (pt < ptlim && pt[0] != 0) { pt.inc(); pt[0] = str[0]; str = str.next(); } return(pt); }
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); }
private static int os_date(lua_State L) { CharPtr s = luaL_optstring(L, 1, "%c"); DateTime stm; if (s[0] == '!') /* UTC? */ { stm = DateTime.UtcNow; s.inc(); /* skip `!' */ } else { stm = DateTime.Now; } if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setfield(L, "sec", stm.Second); setfield(L, "min", stm.Minute); setfield(L, "hour", stm.Hour); setfield(L, "day", stm.Day); setfield(L, "month", stm.Month); setfield(L, "year", stm.Year); setfield(L, "wday", (int)stm.DayOfWeek); setfield(L, "yday", stm.DayOfYear); setboolfield(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0); } else { luaL_error(L, "strftime not implemented yet"); // todo: implement this - mjf #if false //FIXME:not implemented ------------------> CharPtr cc = new char[3]; luaL_Buffer b; cc[0] = '%'; luaL_buffinit(L, &b); for (; *s; s++) { if (*s != '%') /* no conversion specifier? */ { luaL_addchar(&b, *s); } else { size_t reslen; int i = 1; char buff[200]; /* should be big enough for any conversion result */
/* ** Convert a string to lower case. ** LUA interface: ** lowercase = strlower (string) */ private static void str_lower() { CharPtr s, c; lua_Object o = lua_getparam(1); if (lua_isstring(o) == 0) { lua_error("incorrect arguments to function `strlower'"); return; } s = lua_getstring(o); c = new CharPtr(s); while (c[0] != '\0') { c[0] = tolower(c[0]); c.inc(); } lua_pushstring(s); free(s); }
private static Word hashindex(Hash t, Object_ @ref) /* hash function */ { switch (tag(@ref)) { case lua_Type.LUA_T_NIL: lua_reportbug("unexpected type to index table"); return((Word)((-1) & 0xffff)); /* UNREACHEABLE */ case lua_Type.LUA_T_NUMBER: return((Word)(((Word)nvalue(@ref)) % nhash(t))); case lua_Type.LUA_T_STRING: { ulong h = tsvalue(@ref).hash; if (h == 0) { CharPtr name = svalue(@ref); while (name[0] != (char)0) { h = ((h << 5) - h) ^ (byte)(name[0]); name.inc(); } tsvalue(@ref).hash = h; } return((Word)((Word)(h & 0xffff) % nhash(t))); /* make it a valid index */ } case lua_Type.LUA_T_FUNCTION: return((Word)(((IntPoint)bvalue(@ref).GetHashCode()) % nhash(t))); case lua_Type.LUA_T_CFUNCTION: return((Word)(((IntPoint)fvalue(@ref).GetHashCode()) % nhash(t))); case lua_Type.LUA_T_ARRAY: return((Word)(((IntPoint)avalue(@ref).GetHashCode()) % nhash(t))); default: /* user data */ return((Word)(((IntPoint)uvalue(@ref).GetHashCode()) % nhash(t))); } }
/* ** Given a constant string, search it at constant table and return its index. ** If not found, allocate at end of the table, checking oveflow and return ** its index. ** ** For each allocation, the function allocate a extra char to be used to ** mark used string (it's necessary to deal with constant and string ** uniformily). The function store at the table the second position allocated, ** that represents the beginning of the real string. On error, return -1. ** */ public static int lua_findconstant(CharPtr s) { int i; for (i = 0; i < lua_nconstant; i++) { if (streq(s, lua_constant[i])) { return(i); } } if (lua_nconstant >= MAXCONSTANT - 1) { lua_error("lua: constant string table overflow"); return(-1); } { CharPtr c = new CharPtr(new char[strlen(s) + 2]); c.inc(); /* create mark space */ lua_constant[lua_nconstant++] = strcpy(c, s); } return(lua_nconstant - 1); }
private static int head(Hash t, Object_ @ref) /* hash function */ { if (tag(@ref) == Type.T_NUMBER) { return(((int)nvalue(@ref)) % nhash(t)); } else if (tag(@ref) == Type.T_STRING) { int h; CharPtr name = svalue(@ref); for (h = 0; name[0] != 0; name.inc()) /* interpret name as binary number */ { h <<= 8; h += (byte)name[0]; /* avoid sign extension */ h %= nhash(t); /* make it a valid index */ } return(h); } else { lua_reportbug("unexpected type to index table"); return(-1); } }
/* * make header */ public static void luaU_header(CharPtr h) { h = new CharPtr(h); int x=1; memcpy(h, LUA_SIGNATURE, LUA_SIGNATURE.Length); h = h.add(LUA_SIGNATURE.Length); h[0] = (char)LUAC_VERSION; h.inc(); h[0] = (char)LUAC_FORMAT; h.inc(); //*h++=(char)*(char*)&x; /* endianness */ h[0] = (char)x; /* endianness */ h.inc(); h[0] = (char)sizeof(int); h.inc(); h[0] = (char)sizeof(uint); h.inc(); h[0] = (char)sizeof(Instruction); h.inc(); h[0] = (char)sizeof(lua_Number); h.inc(); //(h++)[0] = ((lua_Number)0.5 == 0) ? 0 : 1; /* is lua_Number integral? */ h[0] = (char)0; // always 0 on this build }
private static int os_date (lua_State L) { CharPtr s = new CharPtr(luaL_optstring(L, 1, "%c")); DateTime stm; if (s[0] == '!') { /* UTC? */ stm = DateTime.UtcNow; s.inc(); /* skip `!' */ } else stm = DateTime.Now; if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setfield(L, "sec", stm.Second); setfield(L, "min", stm.Minute); setfield(L, "hour", stm.Hour); setfield(L, "day", stm.Day); setfield(L, "month", stm.Month); setfield(L, "year", stm.Year); setfield(L, "wday", (int)stm.DayOfWeek); setfield(L, "yday", stm.DayOfYear); setboolfield(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0); } else { luaL_error(L, "strftime not implemented yet"); // todo: implement this - mjf #if false CharPtr cc = new char[3]; luaL_Buffer b; cc[0] = '%'; cc[2] = '\0'; luaL_buffinit(L, b); for (; s[0] != 0; s.inc()) { if (s[0] != '%' || s[1] == '\0') /* no conversion specifier? */ luaL_addchar(b, s[0]); else { uint reslen; CharPtr buff = new char[200]; /* should be big enough for any conversion result */ s.inc(); cc[1] = s[0]; reslen = strftime(buff, buff.Length, cc, stm); luaL_addlstring(b, buff, reslen); } } luaL_pushresult(b); #endif // #if 0 } return 1; }
private static int OSDate(LuaState L) { CharPtr s = new CharPtr(LuaLOptString(L, 1, "%c")); DateTime stm; // Parses the second argument if there's one. If not, uses Now as time. if (LuaIsNoneOrNil(L, 2)) { stm = DateTime.Now; } else { LuaLCheckType(L, 2, LUA_TNUMBER); double seconds = LuaToNumber(L, 2); stm = new DateTime((long)seconds * TimeSpan.TicksPerSecond); } if (s[0] == '!') { /* UTC? */ stm = stm.ToUniversalTime(); s.inc(); /* skip `!' */ } if (strcmp(s, "*t") == 0) { LuaCreateTable(L, 0, 9); /* 9 = number of fields */ SetField(L, "sec", stm.Second); SetField(L, "min", stm.Minute); SetField(L, "hour", stm.Hour); SetField(L, "day", stm.Day); SetField(L, "month", stm.Month); SetField(L, "year", stm.Year); SetField(L, "wday", (int)stm.DayOfWeek + 1); SetField(L, "yday", stm.DayOfYear); SetBoolField(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0); } else { CharPtr cc = new char[3]; LuaLBuffer b = new LuaLBuffer(); cc[0] = '%'; cc[2] = '\0'; LuaLBuffInit(L, b); for (; s[0] != 0; s.inc()) { if (s[0] != '%' || s[1] == '\0') /* no conversion specifier? */ LuaLAddChar(b, s[0]); else { uint reslen; CharPtr buff = new char[200]; /* should be big enough for any conversion result */ s.inc(); cc[1] = s[0]; reslen = strftime(buff, (uint)buff.chars.Length, cc, stm); buff.index = 0; LuaLAddLString(b, buff, reslen); } } LuaLPushResult(b); } return 1; }
private static CharPtr StrFTimeAdd(CharPtr str, CharPtr pt, CharPtr ptlim) { pt[0] = str[0]; str = str.next(); while (pt < ptlim && pt[0] != 0) { pt.inc(); pt[0] = str[0]; str = str.next(); } return pt; }
private static CharPtr StrFTimeFmt(CharPtr baseFormat, DateTime t, CharPtr pt, CharPtr ptlim) { CharPtr format = new CharPtr(baseFormat); for (; format[0] != 0; format.inc()) { if (format == '%') { format.inc(); if (format == 'E') { format.inc(); // Alternate Era is ignored } else if (format == 'O') { format.inc(); // Alternate numeric symbols is ignored } switch (format[0]) { case '\0': format.dec(); break; case 'A': // Full day of week //pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ? "?" : _days[t->tm_wday], pt, ptlim); pt = StrFTimeAdd(t.ToString("dddd"), pt, ptlim); continue; case 'a': // Abbreviated day of week //pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ? "?" : _days_abbrev[t->tm_wday], pt, ptlim); pt = StrFTimeAdd(t.ToString("ddd"), pt, ptlim); continue; case 'B': // Full month name //pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ? "?" : _months[t->tm_mon], pt, ptlim); pt = StrFTimeAdd(t.ToString("MMMM"), pt, ptlim); continue; case 'b': // Abbreviated month name case 'h': // Abbreviated month name //pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ? "?" : _months_abbrev[t->tm_mon], pt, ptlim); pt = StrFTimeAdd(t.ToString("MMM"), pt, ptlim); continue; case 'C': // First two digits of year (a.k.a. Year divided by 100 and truncated to integer (00-99)) //pt = _conv((t->tm_year + TM_YEAR_BASE) / 100, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("yyyy").Substring(0, 2), pt, ptlim); continue; case 'c': // Abbreviated date/time representation (e.g. Thu Aug 23 14:55:02 2001) pt = StrFTimeFmt("%a %b %e %H:%M:%S %Y", t, pt, ptlim); continue; case 'D': // Short MM/DD/YY date pt = StrFTimeFmt("%m/%d/%y", t, pt, ptlim); continue; case 'd': // Day of the month, zero-padded (01-31) //pt = _conv(t->tm_mday, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("dd"), pt, ptlim); continue; case 'e': // Day of the month, space-padded ( 1-31) //pt = _conv(t->tm_mday, "%2d", pt, ptlim); pt = StrFTimeAdd(t.Day.ToString().PadLeft(2, ' '), pt, ptlim); continue; case 'F': // Short YYYY-MM-DD date pt = StrFTimeFmt("%Y-%m-%d", t, pt, ptlim); continue; case 'H': // Hour in 24h format (00-23) //pt = _conv(t->tm_hour, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("HH"), pt, ptlim); continue; case 'I': // Hour in 12h format (01-12) //pt = _conv((t->tm_hour % 12) ? (t->tm_hour % 12) : 12, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("hh"), pt, ptlim); continue; case 'j': // Day of the year (001-366) pt = StrFTimeAdd(t.DayOfYear.ToString().PadLeft(3, ' '), pt, ptlim); continue; case 'k': // (Non-standard) // Hours in 24h format, space-padded ( 1-23) //pt = _conv(t->tm_hour, "%2d", pt, ptlim); pt = StrFTimeAdd(t.ToString("%H").PadLeft(2, ' '), pt, ptlim); continue; case 'l': // (Non-standard) // Hours in 12h format, space-padded ( 1-12) //pt = _conv((t->tm_hour % 12) ? (t->tm_hour % 12) : 12, "%2d", pt, ptlim); pt = StrFTimeAdd(t.ToString("%h").PadLeft(2, ' '), pt, ptlim); continue; case 'M': // Minute (00-59) //pt = _conv(t->tm_min, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("mm"), pt, ptlim); continue; case 'm': // Month as a decimal number (01-12) //pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("MM"), pt, ptlim); continue; case 'n': // New-line character. pt = StrFTimeAdd(Environment.NewLine, pt, ptlim); continue; case 'p': // AM or PM designation (locale dependent). //pt = _add((t->tm_hour >= 12) ? "pm" : "am", pt, ptlim); pt = StrFTimeAdd(t.ToString("tt"), pt, ptlim); continue; case 'R': // 24-hour HH:MM time, equivalent to %H:%M pt = StrFTimeFmt("%H:%M", t, pt, ptlim); continue; case 'r': // 12-hour clock time (locale dependent). pt = StrFTimeFmt("%I:%M:%S %p", t, pt, ptlim); continue; case 'S': // Second ((00-59) //pt = _conv(t->tm_sec, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("ss"), pt, ptlim); continue; case 'T': // ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S pt = StrFTimeFmt("%H:%M:%S", t, pt, ptlim); continue; case 't': // Horizontal-tab character pt = StrFTimeAdd("\t", pt, ptlim); continue; case 'U': // Week number with the first Sunday as the first day of week one (00-53) //pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7, "%02d", pt, ptlim); pt = StrFTimeAdd(System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(t, System.Globalization.CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday).ToString(), pt, ptlim); continue; case 'u': // ISO 8601 weekday as number with Monday as 1 (1-7) (locale independant). //pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday, "%d", pt, ptlim); pt = StrFTimeAdd(t.DayOfWeek == DayOfWeek.Sunday ? "7" : ((int)t.DayOfWeek).ToString(), pt, ptlim); continue; case 'G': // ISO 8601 year (four digits) case 'g': // ISO 8601 year (two digits) case 'V': // ISO 8601 week number // See http://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date DateTime isoTime = t; DayOfWeek day = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(isoTime); if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday) { isoTime = isoTime.AddDays(3); } if (format[0] == 'V') // ISO 8601 week number { int isoWeek = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(isoTime, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); pt = StrFTimeAdd(isoWeek.ToString(), pt, ptlim); } else { string isoYear = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetYear(isoTime).ToString(); // ISO 8601 year (four digits) if (format[0] == 'g') // ISO 8601 year (two digits) { isoYear = isoYear.Substring(isoYear.Length - 2, 2); } pt = StrFTimeAdd(isoYear, pt, ptlim); } continue; case 'W': // Week number with the first Monday as the first day of week one (00-53) //pt = _conv((t->tm_yday + 7 - (t->tm_wday ? (t->tm_wday - 1) : 6)) / 7, "%02d", pt, ptlim); pt = StrFTimeAdd(System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(t, System.Globalization.CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday).ToString(), pt, ptlim); continue; case 'w': // Weekday as a decimal number with Sunday as 0 (0-6) //pt = _conv(t->tm_wday, "%d", pt, ptlim); pt = StrFTimeAdd(((int)t.DayOfWeek).ToString(), pt, ptlim); continue; case 'X': // Long time representation (locale dependent) //pt = _fmt("%H:%M:%S", t, pt, ptlim); // fails to comply with spec! pt = StrFTimeAdd(t.ToString("%T"), pt, ptlim); continue; case 'x': // Short date representation (locale dependent) //pt = _fmt("%m/%d/%y", t, pt, ptlim); // fails to comply with spec! pt = StrFTimeAdd(t.ToString("%d"), pt, ptlim); continue; case 'y': // Last two digits of year (00-99) //pt = _conv((t->tm_year + TM_YEAR_BASE) % 100, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("yy"), pt, ptlim); continue; case 'Y': // Full year (all digits) //pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d", pt, ptlim); pt = StrFTimeAdd(t.Year.ToString(), pt, ptlim); continue; case 'Z': // Timezone name or abbreviation (locale dependent) or nothing if unavailable (e.g. CDT) pt = StrFTimeAdd(TimeZoneInfo.Local.StandardName, pt, ptlim); continue; case 'z': // ISO 8601 offset from UTC in timezone (+/-hhmm), or nothing if unavailable TimeSpan ts = TimeZoneInfo.Local.GetUtcOffset(t); string offset = (ts.Ticks < 0 ? "-" : "+") + ts.TotalHours.ToString("#00") + ts.Minutes.ToString("00"); pt = StrFTimeAdd(offset, pt, ptlim); continue; case '%': // Add '%' pt = StrFTimeAdd("%", pt, ptlim); continue; default: break; } } if (pt == ptlim) break; pt[0] = format[0]; pt.inc(); } return pt; }
/* ** Function to get the next character from the input string */ private static int stringinput() { st.inc(); return((int)st[-1]); }