private static int auxgetinfo(lua_State L, CharPtr what, lua_Debug ar, Closure f, CallInfo ci) { int status = 1; if (f == null) { info_tailcall(ar); return(status); } for (; what[0] != 0; what = what.next()) { switch (what[0]) { case 'S': { funcinfo(ar, f); break; } case 'l': { ar.currentline = (ci != null) ? currentline(L, ci) : -1; break; } case 'u': { ar.nups = f.c.nupvalues; break; } case 'n': { ar.namewhat = (ci != null) ? getfuncname(L, ci, ref ar.name) : null; if (ar.namewhat == null) { ar.namewhat = ""; /* not found */ ar.name = null; } break; } case 'L': case 'f': /* handled by lua_getinfo */ break; default: status = 0; break; /* invalid option */ } } return(status); }
public static void luaO_chunkid(CharPtr out_, CharPtr source, uint bufflen) { //out_ = ""; if (source[0] == '=') { strncpy(out_, source + 1, (int)bufflen); /* remove first char */ out_[bufflen - 1] = '\0'; /* ensures null termination */ } else /* out = "source", or "...source" */ { if (source[0] == '@') { uint l; source = source.next(); /* skip the `@' */ bufflen -= (uint)(" '...' ".Length + 1); l = (uint)strlen(source); strcpy(out_, ""); if (l > bufflen) { source += (l - bufflen); /* get last part of file name */ strcat(out_, "..."); } strcat(out_, source); } else /* out = [string "string"] */ { uint len = strcspn(source, "\n\r"); /* stop at first newline */ bufflen -= (uint)(" [string \"...\"] ".Length + 1); if (len > bufflen) { len = bufflen; } strcpy(out_, "[string \""); if (source[len] != '\0') /* must truncate? */ { strncat(out_, source, (int)len); strcat(out_, "..."); } else { strcat(out_, source); } strcat(out_, "\"]"); } } }
//#define S(x) x,SS(x) private static void PrintHeader(Proto f) { CharPtr s=GetStr(f.source); if (s[0]=='@' || s[0]=='=') s = s.next(); else if (s[0]==LUA_SIGNATURE[0]) s="(bstring)"; else s="(string)"; printf("\n%s <%s:%d,%d> (%d Instruction%s, %d bytes at %p)\n", (f.linedefined==0)?"main":"function",s, f.linedefined,f.lastlinedefined, f.sizecode, SS(f.sizecode), f.sizecode * GetUnmanagedSize(typeof(Instruction)), VOID(f)); printf("%d%s param%s, %d slot%s, %d upvalue%s, ", f.numparams,(f.is_vararg != 0) ? "+" : "", SS(f.numparams), f.maxstacksize, SS(f.maxstacksize), f.nups, SS(f.nups)); printf("%d local%s, %d constant%s, %d function%s\n", f.sizelocvars, SS(f.sizelocvars), f.sizek, SS(f.sizek), f.sizep, SS(f.sizep)); }
private static CharPtr scanformat(lua_State L, CharPtr strfrmt, CharPtr form) { CharPtr p = strfrmt; while (p[0] != '\0' && strchr(FLAGS, p[0]) != null) { p = p.next(); /* skip flags */ } if ((uint)(p - strfrmt) >= (FLAGS.Length + 1)) { luaL_error(L, "invalid format (repeated flags)"); } if (isdigit((byte)(p[0]))) { p = p.next(); /* skip width */ } if (isdigit((byte)(p[0]))) { p = p.next(); /* (2 digits at most) */ } if (p[0] == '.') { p = p.next(); if (isdigit((byte)(p[0]))) { p = p.next(); /* skip precision */ } if (isdigit((byte)(p[0]))) { p = p.next(); /* (2 digits at most) */ } } if (isdigit((byte)(p[0]))) { luaL_error(L, "invalid format (width or precision too long)"); } form[0] = '%'; form = form.next(); strncpy(form, strfrmt, p - strfrmt + 1); form += p - strfrmt + 1; form[0] = '\0'; return(p); }
private static CharPtr min_expand(MatchState ms, CharPtr s, CharPtr p, CharPtr ep) { for (;;) { CharPtr res = match(ms, s, ep + 1); if (res != null) { return(res); } else if ((s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0)) { s = s.next(); /* try with one more repetition */ } else { return(null); } } }
public static int lua_getinfo(lua_State L, CharPtr what, lua_Debug ar) { int status; Closure f = null; CallInfo ci = null; lua_lock(L); if (what == '>') { StkId func = L.top - 1; luai_apicheck(L, ttisfunction(func)); what = what.next(); /* skip the '>' */ f = clvalue(func); StkId.dec(ref L.top); /* pop function */ } else if (ar.i_ci != 0) /* no tail call? */ { ci = L.base_ci[ar.i_ci]; lua_assert(ttisfunction(ci.func)); f = clvalue(ci.func); } status = auxgetinfo(L, what, ar, f, ci); if (strchr(what, 'f') != null) { if (f == null) { setnilvalue(L.top); } else { setclvalue(L, L.top, f); } incr_top(L); } if (strchr(what, 'L') != null) { collectvalidlines(L, f); } lua_unlock(L); return(status); }
public static int LuaGetInfo(LuaState L, CharPtr what, ref LuaDebug ar) { int status; Closure f = null; CallInfo ci = null; LuaLock(L); if (what == '>') { StkId func = L.top - 1; luai_apicheck(L, TTIsFunction(func)); what = what.next(); /* skip the '>' */ f = CLValue(func); StkId.Dec(ref L.top); /* pop function */ } else if (ar.i_ci != 0) /* no tail call? */ { ci = L.base_ci[ar.i_ci]; LuaAssert(TTIsFunction(ci.func)); f = CLValue(ci.func); } status = AuxGetInfo(L, what, ar, f, ci); if (strchr(what, 'f') != null) { if (f == null) { SetNilValue(L.top); } else { SetCLValue(L, L.top, f); } IncrTop(L); } if (strchr(what, 'L') != null) { CollectValidLines(L, f); } LuaUnlock(L); return(status); }
public static int lua_getinfo(lua_State L, CharPtr what, lua_Debug ar) { int status; Closure cl; CallInfo ci; StkId func; lua_lock(L); swapextra(L); if (what == '>') { ci = null; func = L.top - 1; api_check(L, ttisfunction(func), "function expected"); what = what.next(); /* skip the '>' */ StkId.dec(ref L.top); /* pop function */ } else { ci = ar.i_ci; func = ci.func; lua_assert(ttisfunction(ci.func)); } cl = ttisclosure(func) ? clvalue(func) : null; status = auxgetinfo(L, what, ar, cl, ci); if (strchr(what, 'f') != null) { setobjs2s(L, L.top, func); api_incr_top(L); } swapextra(L); /* correct before option 'L', which can raise a mem. error */ if (strchr(what, 'L') != null) { collectvalidlines(L, cl); } lua_unlock(L); return(status); }
private static void addquoted(LuaState L, LuaLBuffer b, int arg) { uint l; CharPtr s = LuaLCheckLString(L, arg, out l); LuaLAddChar(b, '"'); while ((l--) != 0) { switch (s[0]) { case '"': case '\\': case '\n': { LuaLAddChar(b, '\\'); LuaLAddChar(b, s[0]); break; } case '\r': { LuaLAddLString(b, "\\r", 2); break; } case '\0': { LuaLAddLString(b, "\\000", 4); break; } default: { LuaLAddChar(b, s[0]); break; } } s = s.next(); } LuaLAddChar(b, '"'); }
private static void addquoted(lua_State L, luaL_Buffer b, int arg) { uint l; CharPtr s = luaL_checklstring(L, arg, out l); luaL_addchar(b, '"'); while ((l--) != 0) { switch (s[0]) { case '"': case '\\': case '\n': { luaL_addchar(b, '\\'); luaL_addchar(b, s[0]); break; } case '\r': { luaL_addlstring(b, "\\r", 2); break; } case '\0': { luaL_addlstring(b, "\\000", 4); break; } default: { luaL_addchar(b, s[0]); break; } } s = s.next(); } luaL_addchar(b, '"'); }
private static CharPtr matchbalance(MatchState ms, CharPtr s, CharPtr p) { if ((p[0] == 0) || (p[1] == 0)) luaL_error(ms.L, "unbalanced pattern"); if (s[0] != p[0]) return null; else { int b = p[0]; int e = p[1]; int cont = 1; while ((s=s.next()) < ms.src_end) { if (s[0] == e) { if (--cont == 0) return s+1; } else if (s[0] == b) cont++; } } return null; /* string ends out of balance */ }
private static int auxgetinfo(lua_State L, CharPtr what, lua_Debug ar, Closure f, CallInfo ci) { int status = 1; for (; what[0] != 0; what = what.next()) { switch (what[0]) { case 'S': { funcinfo(ar, f); break; } case 'l': { ar.currentline = (ci != null && isLua(ci) != 0) ? currentline(ci) : -1; break; } case 'u': { ar.nups = (f == null) ? (byte)0 : f.c.nupvalues; //FIXME:(added) if (noLuaClosure(f)) { ar.isvararg = (char)1; ar.nparams = 0; } else { ar.isvararg = (char)(f.l.p.is_vararg); //FIXME: added (char) ar.nparams = f.l.p.numparams; } break; } case 't': { ar.istailcall = (ci != null) ? (char)(ci.callstatus & CIST_TAIL) : (char)0; //FIXME: added (char) break; } case 'n': { /* calling function is a known Lua function? */ if (ci != null && (ci.callstatus & CIST_TAIL) == 0 && isLua(ci.previous) != 0) { ar.namewhat = getfuncname(L, ci.previous, ref ar.name); } else { ar.namewhat = null; } if (ar.namewhat == null) { ar.namewhat = ""; /* not found */ ar.name = null; } break; } case 'L': case 'f': /* handled by lua_getinfo */ break; default: status = 0; break; /* invalid option */ } } return(status); }
private static CharPtr scanformat(lua_State L, CharPtr strfrmt, CharPtr form) { CharPtr p = strfrmt; while (p[0] != '\0' && strchr(FLAGS, p[0]) != null) p = p.next(); /* skip flags */ if ((uint)(p - strfrmt) >= (FLAGS.Length+1)) luaL_error(L, "invalid format (repeated flags)"); if (isdigit((byte)(p[0]))) p = p.next(); /* skip width */ if (isdigit((byte)(p[0]))) p = p.next(); /* (2 digits at most) */ if (p[0] == '.') { p = p.next(); if (isdigit((byte)(p[0]))) p = p.next(); /* skip precision */ if (isdigit((byte)(p[0]))) p = p.next(); /* (2 digits at most) */ } if (isdigit((byte)(p[0]))) luaL_error(L, "invalid format (width or precision too long)"); form[0] = '%'; form = form.next(); strncpy(form, strfrmt, p - strfrmt + 1); form += p - strfrmt + 1; form[0] = '\0'; return p; }
public static int LuaGetInfo (LuaState L, CharPtr what, ref LuaDebug ar) { int status; Closure f = null; CallInfo ci = null; LuaLock(L); if (what == '>') { StkId func = L.top - 1; luai_apicheck(L, TTIsFunction(func)); what = what.next(); /* skip the '>' */ f = CLValue(func); StkId.Dec(ref L.top); /* pop function */ } else if (ar.i_ci != 0) { /* no tail call? */ ci = L.base_ci[ar.i_ci]; LuaAssert(TTIsFunction(ci.func)); f = CLValue(ci.func); } status = AuxGetInfo(L, what, ar, f, ci); if (strchr(what, 'f') != null) { if (f == null) SetNilValue(L.top); else SetCLValue(L, L.top, f); IncrTop(L); } if (strchr(what, 'L') != null) CollectValidLines(L, f); LuaUnlock(L); return status; }
private static int AuxGetInfo (LuaState L, CharPtr what, LuaDebug ar, Closure f, CallInfo ci) { int status = 1; if (f == null) { InfoTailCall(ar); return status; } for (; what[0] != 0; what = what.next()) { switch (what[0]) { case 'S': { FuncInfo(ar, f); break; } case 'l': { ar.currentline = (ci != null) ? CurrentLine(L, ci) : -1; break; } case 'u': { ar.nups = f.c.nupvalues; break; } case 'n': { ar.namewhat = (ci!=null) ? GetFuncName(L, ci, ref ar.name) : null; if (ar.namewhat == null) { ar.namewhat = ""; /* not found */ ar.name = null; } break; } case 'L': case 'f': /* handled by lua_getinfo */ break; default: status = 0; break;/* invalid option */ } } return status; }
//https://github.com/weimingtom/KopiLuaCompare/blob/1450ff7c6b1885b6e9aa9af9e78dc7fd19678aec/lua-5.1.5/kopilua/luaconf_ex.h.cs //from strtoul public static double strtod(CharPtr s, ref CharPtr end) { int base_ = 10; try { end = new CharPtr(s.chars, s.index); // skip over any leading whitespace while (end[0] == ' ') { end = end.next(); } // ignore any leading 0x if ((end[0] == '0') && (end[1] == 'x')) { end = end.next().next(); } else if ((end[0] == '0') && (end[1] == 'X')) { end = end.next().next(); } // do we have a leading + or - sign? bool negate = false; if (end[0] == '+') { end = end.next(); } else if (end[0] == '-') { negate = true; end = end.next(); } // loop through all chars bool invalid = false; bool had_digits = false; ulong result = 0; while (true) { // get this char char ch = end[0]; // which digit is this? int this_digit = 0; if (Char.IsDigit(ch)) //(isdigit(ch)) { this_digit = ch - '0'; } else if (Char.IsLetter(ch)) //(isalpha(ch)) { this_digit = tolower(ch) - 'a' + 10; } else { break; } // is this digit valid? if (this_digit >= base_) { invalid = true; } else { had_digits = true; result = result * (ulong)base_ + (ulong)this_digit; } end = end.next(); } // were any of the digits invalid? if (invalid || (!had_digits)) { end = s; return(System.UInt64.MaxValue); } // if the value was a negative then negate it here if (negate) { result = (ulong)-(long)result; } // ok, we're done return((ulong)result); } catch { end = s; return(0); } }
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 str_gsub(LuaState L) { uint srcl; CharPtr src = LuaLCheckLString(L, 1, out srcl); CharPtr p = LuaLCheckString(L, 2); int tr = LuaType(L, 3); int max_s = LuaLOptInt(L, 4, (int)(srcl + 1)); int anchor = 0; if (p[0] == '^') { p = p.next(); anchor = 1; } int n = 0; MatchState ms = new MatchState(); LuaLBuffer b = new LuaLBuffer(); LuaLArgCheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || tr == LUA_TFUNCTION || tr == LUA_TTABLE || tr == LUA_TUSERDATA, 3, "string/function/table expected"); LuaLBuffInit(L, b); ms.L = L; ms.matchdepth = MAXCCALLS; ms.src_init = src; ms.src_end = src + srcl; while (n < max_s) { CharPtr e; ms.level = 0; LuaAssert(ms.matchdepth == MAXCCALLS); e = match(ms, src, p); if (e != null) { n++; add_value(ms, b, src, e); } if ((e != null) && e > src) /* non empty match? */ { src = e; /* skip it */ } else if (src < ms.src_end) { char c = src[0]; src = src.next(); LuaLAddChar(b, c); } else { break; } if (anchor != 0) { break; } } LuaLAddLString(b, src, (uint)(ms.src_end - src)); LuaLPushResult(b); LuaPushInteger(L, n); /* number of substitutions */ return(2); }
private static int str_format(LuaState L) { int top = LuaGetTop(L); int arg = 1; uint sfl; CharPtr strfrmt = LuaLCheckLString(L, arg, out sfl); CharPtr strfrmt_end = strfrmt + sfl; LuaLBuffer b = new LuaLBuffer(); LuaLBuffInit(L, b); while (strfrmt < strfrmt_end) { if (strfrmt[0] != L_ESC) { LuaLAddChar(b, strfrmt[0]); strfrmt = strfrmt.next(); } else if (strfrmt[1] == L_ESC) { LuaLAddChar(b, strfrmt[0]); /* %% */ strfrmt = strfrmt + 2; } else { /* format item */ strfrmt = strfrmt.next(); CharPtr form = new char[MAX_FORMAT]; /* to store the format (`%...') */ CharPtr buff = new char[MAX_ITEM]; /* to store the formatted item */ if (++arg > top) { LuaLArgError(L, arg, "no value"); } strfrmt = scanformat(L, strfrmt, form); char ch = strfrmt[0]; strfrmt = strfrmt.next(); switch (ch) { case 'c': { sprintf(buff, form, (int)LuaLCheckNumber(L, arg)); break; } case 'd': case 'i': { addintlen(form); sprintf(buff, form, (LUA_INTFRM_T)LuaLCheckNumber(L, arg)); break; } case 'o': case 'u': case 'x': case 'X': { addintlen(form); sprintf(buff, form, (UNSIGNED_LUA_INTFRM_T)LuaLCheckNumber(L, arg)); break; } case 'e': case 'E': case 'f': case 'g': case 'G': { sprintf(buff, form, (double)LuaLCheckNumber(L, arg)); break; } case 'q': { addquoted(L, b, arg); continue; /* skip the 'addsize' at the end */ } case 's': { uint l; CharPtr s = LuaLCheckLString(L, arg, out l); if ((strchr(form, '.') == null) && l >= 100) { /* no precision and string is too long to be formatted; * keep original string */ LuaPushValue(L, arg); LuaLAddValue(b); continue; /* skip the `addsize' at the end */ } else { sprintf(buff, form, s); break; } } default: { /* also treat cases `pnLlh' */ return(LuaLError(L, "invalid option " + LUA_QL("%%%c") + " to " + LUA_QL("format"), strfrmt[-1])); } } LuaLAddLString(b, buff, (uint)strlen(buff)); } } LuaLPushResult(b); return(1); }
private static int matchbracketclass(int c, CharPtr p, CharPtr ec) { int sig = 1; if (p[1] == '^') { sig = 0; p = p.next(); /* skip the `^' */ } while ((p=p.next()) < ec) { if (p == L_ESC) { p = p.next(); if (match_class(c, (byte)(p[0])) != 0) return sig; } else if ((p[1] == '-') && (p + 2 < ec)) { p+=2; if ((byte)((p[-2])) <= c && (c <= (byte)p[0])) return sig; } else if ((byte)(p[0]) == c) return sig; } return (sig == 0) ? 1 : 0; }
private static CharPtr min_expand(MatchState ms, CharPtr s, CharPtr p, CharPtr ep) { for (;;) { CharPtr res = match(ms, s, ep+1); if (res != null) return res; else if ( (s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0) ) s = s.next(); /* try with one more repetition */ else return null; } }
private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) { s = new CharPtr(s); p = new CharPtr(p); bool runDflt = false; bool runInit = true; if (ms.matchdepth-- == 0) LuaLError(ms.L, "pattern too complex"); //init: while (runInit) { // Replaces "init:" in order to be compatible with Mono. runInit = false; // No "goto init" until further notice. if (p != '\0') { /* end of pattern? */ switch (p[0]) { case '(': { /* start capture */ if (p[1] == ')') { /* position capture? */ s = start_capture(ms, s, p + 2, CAP_POSITION); } else { s = start_capture(ms, s, p + 1, CAP_UNFINISHED); } break; } case ')': { /* end capture */ s = end_capture(ms, s, p + 1); break; } case '$': { if (p[1] != '\0') { /* is the `$' the last char in pattern? */ runDflt = true; //goto dflt; /* no; go to default */ } s = (s == ms.src_end) ? s : null; /* check end of string */ break; } case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ switch (p[1]) { case 'b': { /* balanced string? */ s = matchbalance(ms, s, p + 2); if (s != null) { p += 4; runInit = true; //goto init; /* return match(ms, s, p+4); */ } /* else fail (s == NULL) */ break; } case 'f': { /* frontier? */ CharPtr ep; char previous; p += 2; if (p[0] != '[') { LuaLError(ms.L, "missing " + LUA_QL("[") + " after " + LUA_QL("%%f") + " in pattern"); } ep = classend(ms, p); /* points to what is next */ previous = (s == ms.src_init) ? '\0' : s[-1]; if ((matchbracketclass((byte)(previous), p, ep - 1) == 0) || (matchbracketclass((byte)(s[0]), p, ep - 1) != 0)) { p = ep; runInit = true; //goto init; /* else return match(ms, s, ep); */ } s = null; /* match failed */ break; } default: { if (isdigit((byte)(p[1]))) { /* capture results (%0-%9)? */ s = match_capture(ms, s, (byte)(p[1])); if (s != null) { p += 2; runInit = true; //goto init; /* else return match(ms, s, p+2) */ } break; } runDflt = true; //goto dflt; break; } } break; } default: { runDflt = true; // goto dflt break; } } } //dflt: if (runDflt) // Replaces "dflt:" in order to be compatible with Mono. { /* pattern class plus optional suffix */ runDflt = false; // no more "goto dflt" until further notice. CharPtr ep = classend(ms, p); /* points to optional suffix */ /* does not match at least once? */ if ((s >= ms.src_end) || (singlematch((byte)(s[0]), p, ep) == 0)) { if (ep == '*' || ep == '?' || ep == '-') { /* accept empty? */ p = ep + 1; runInit = true; //goto init; /* return match(ms, s, ep + 1); */ } else /* '+' or no suffix */ s = null; /* fail */ } else { /* matched once */ switch (ep[0]) { case '?': { /* optional */ CharPtr res; if ((res = match(ms, s + 1, ep + 1)) != null) { s = res; } else { p = ep + 1; runInit = true; //goto init; /* else return match(ms, s, ep+1); */ } break; } case '+': { /* 1 or more repetitions */ s = s.next(); /* 1 match already done */ s = max_expand(ms, s, p, ep); // cannot fall through, repeating '*' instruction instead. break; } case '*': { /* 0 or more repetitions */ s = max_expand(ms, s, p, ep); break; } case '-': { /* 0 or more repetitions (minimum) */ s = min_expand(ms, s, p, ep); break; } default: { /* no suffix */ s = s.next(); p = ep; runInit = true; //goto init; /* return match(ms, s+1, ep); */ break; } } } } } ms.matchdepth++; return s; }
private static CharPtr PushNextTemplate(LuaState L, CharPtr path) { CharPtr l; while (path[0] == LUA_PATHSEP[0]) path = path.next(); /* skip separators */ if (path[0] == '\0') return null; /* no more templates */ l = strchr(path, LUA_PATHSEP[0]); /* find next separator */ if (l == null) l = path + strlen(path); LuaPushLString(L, path, (uint)(l - path)); /* template */ return l; }
private static int str_format(lua_State L) { int top = lua_gettop(L); int arg = 1; uint sfl; CharPtr strfrmt = luaL_checklstring(L, arg, out sfl); CharPtr strfrmt_end = strfrmt + sfl; luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); while (strfrmt < strfrmt_end) { if (strfrmt[0] != L_ESC) { luaL_addchar(b, strfrmt[0]); strfrmt = strfrmt.next(); } else if (strfrmt[1] == L_ESC) { luaL_addchar(b, strfrmt[0]); /* %% */ strfrmt = strfrmt + 2; } else /* format item */ { strfrmt = strfrmt.next(); CharPtr form = new char[MAX_FORMAT]; /* to store the format (`%...') */ CharPtr buff = luaL_prepbuffsize(b, MAX_ITEM); /* to put formatted item */ int nb = 0; /* number of bytes in added item */ if (++arg > top) { luaL_argerror(L, arg, "no value"); } strfrmt = scanformat(L, strfrmt, form); char ch = strfrmt[0]; //FIXME:added, move here strfrmt = strfrmt.next(); //FIXME:added, move here switch (ch) { case 'c': { nb = sprintf(buff, form, luaL_checkint(L, arg)); break; } case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { lua_Number n = luaL_checknumber(L, arg); LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n : (LUA_INTFRM_T)(UInt64)n; //FIXME: changed here addlenmod(form, LUA_INTFRMLEN); nb = sprintf(buff, form, r); break; } case 'e': case 'E': case 'f': //#if defined(LUA_USE_AFORMAT) case 'a': case 'A': //#endif case 'g': case 'G': { addlenmod(form, LUA_FLTFRMLEN); nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg)); break; } case 'q': { addquoted(L, b, arg); break; } case 's': { uint l; CharPtr s = luaL_tolstring(L, arg, out l); if ((strchr(form, '.') == null) && l >= 100) { /* no precision and string is too long to be formatted; * keep original string */ luaL_addvalue(b); break; } else { nb = sprintf(buff, form, s); lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ break; } } default: { /* also treat cases `pnLlh' */ return(luaL_error(L, "invalid option " + LUA_QL("%%%c") + " to " + LUA_QL("format"), strfrmt[-1])); } } luaL_addsize(b, (uint)nb); //FIXME:changed, (uint) } } luaL_pushresult(b); return(1); }
private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) { s = new CharPtr(s); //FIXME:added p = new CharPtr(p); //FIXME:added init: /* using goto's to optimize tail recursion */ if (p == ms.p_end) /* end of pattern? */ { return(s); /* match succeeded */ } switch (p[0]) { case '(': { /* start capture */ if (p[1] == ')') /* position capture? */ { return(start_capture(ms, s, p + 2, CAP_POSITION)); } else { return(start_capture(ms, s, p + 1, CAP_UNFINISHED)); } } case ')': { /* end capture */ return(end_capture(ms, s, p + 1)); } case '$': { if ((p + 1) == ms.p_end) /* is the `$' the last char in pattern? */ { return((s == ms.src_end) ? s : null); /* check end of string */ } else { goto dflt; } } case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ switch (p[1]) { case 'b': { /* balanced string? */ s = matchbalance(ms, s, p + 2); if (s == null) { return(null); } p += 4; goto init; /* else return match(ms, s, p+4); */ } case 'f': { /* frontier? */ CharPtr ep; char previous; p += 2; if (p[0] != '[') { luaL_error(ms.L, "missing " + LUA_QL("[") + " after " + LUA_QL("%%f") + " in pattern"); } ep = classend(ms, p); /* points to what is next */ previous = (s == ms.src_init) ? '\0' : s[-1]; if ((matchbracketclass((byte)(previous), p, ep - 1) != 0) || (matchbracketclass((byte)(s[0]), p, ep - 1) == 0)) { return(null); } p = ep; goto init; /* else return match(ms, s, ep); */ } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { /* capture results (%0-%9)? */ s = match_capture(ms, s, (byte)(p[1])); if (s == null) { return(null); } p += 2; goto init; /* else return match(ms, s, p+2) */ } default: goto dflt; /* go through to 'dflt' */ } break; //FIXME:added??? } default: dflt : { /* pattern class plus optional suffix */ CharPtr ep = classend(ms, p); /* points to what is next */ int m = (s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0) ? 1 : 0; switch (ep[0]) { case '?': { /* optional */ CharPtr res; if ((m != 0) && ((res = match(ms, s + 1, ep + 1)) != null)) { return(res); } p = ep + 1; goto init; /* else return match(ms, s, ep+1); */ } case '*': { /* 0 or more repetitions */ return(max_expand(ms, s, p, ep)); } case '+': { /* 1 or more repetitions */ return((m != 0) ? max_expand(ms, s + 1, p, ep) : null); } case '-': { /* 0 or more repetitions (minimum) */ return(min_expand(ms, s, p, ep)); } default: { if (m == 0) { return(null); } s = s.next(); p = ep; goto init; /* else return match(ms, s+1, ep); */ } } } } return(null); //FIXME: added ??? }
private static int str_format(lua_State L) { int arg = 1; uint sfl; CharPtr strfrmt = luaL_checklstring(L, arg, out sfl); CharPtr strfrmt_end = strfrmt + sfl; luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); while (strfrmt < strfrmt_end) { if (strfrmt[0] != L_ESC) { luaL_addchar(b, strfrmt[0]); strfrmt = strfrmt.next(); } else if (strfrmt[1] == L_ESC) { luaL_addchar(b, strfrmt[0]); /* %% */ strfrmt = strfrmt + 2; } else { /* format item */ strfrmt = strfrmt.next(); CharPtr form = new char[MAX_FORMAT]; /* to store the format (`%...') */ CharPtr buff = new char[MAX_ITEM]; /* to store the formatted item */ arg++; strfrmt = scanformat(L, strfrmt, form); char ch = strfrmt[0]; strfrmt = strfrmt.next(); switch (ch) { case 'c': { sprintf(buff, form, (int)luaL_checknumber(L, arg)); break; } case 'd': case 'i': { addintlen(form); sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); break; } case 'o': case 'u': case 'x': case 'X': { addintlen(form); sprintf(buff, form, (UNSIGNED_LUA_INTFRM_T)luaL_checknumber(L, arg)); break; } case 'e': case 'E': case 'f': case 'g': case 'G': { sprintf(buff, form, (double)luaL_checknumber(L, arg)); break; } case 'q': { addquoted(L, b, arg); continue; /* skip the 'addsize' at the end */ } case 's': { uint l; CharPtr s = luaL_checklstring(L, arg, out l); if ((strchr(form, '.') == null) && l >= 100) { /* no precision and string is too long to be formatted; * keep original string */ lua_pushvalue(L, arg); luaL_addvalue(b); continue; /* skip the `addsize' at the end */ } else { sprintf(buff, form, s); break; } } default: { /* also treat cases `pnLlh' */ return(luaL_error(L, "invalid option " + LUA_QL("%%%c") + " to " + LUA_QL("format"), strfrmt[-1])); } } luaL_addlstring(b, buff, (uint)strlen(buff)); } } luaL_pushresult(b); return(1); }
public static int lua_getinfo(lua_State L, CharPtr what, lua_Debug ar) { int status; Closure f = null; CallInfo ci = null; lua_lock(L); if (what == '>') { StkId func = L.top - 1; luai_apicheck(L, ttisfunction(func)); what = what.next(); /* skip the '>' */ f = clvalue(func); StkId.dec(ref L.top); /* pop function */ } else if (ar.i_ci != 0) { /* no tail call? */ ci = L.base_ci[ar.i_ci]; lua_assert(ttisfunction(ci.func)); f = clvalue(ci.func); } status = auxgetinfo(L, what, ar, f, ci); if (strchr(what, 'f') != null) { if (f == null) setnilvalue(L.top); else setclvalue(L, L.top, f); incr_top(L); } if (strchr(what, 'L') != null) collectvalidlines(L, f); lua_unlock(L); return status; }
private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) { s = new CharPtr(s); p = new CharPtr(p); init: /* using goto's to optimize tail recursion */ switch (p[0]) { case '(': { /* start capture */ if (p[1] == ')') /* position capture? */ { return(start_capture(ms, s, p + 2, CAP_POSITION)); } else { return(start_capture(ms, s, p + 1, CAP_UNFINISHED)); } } case ')': { /* end capture */ return(end_capture(ms, s, p + 1)); } case L_ESC: { switch (p[1]) { case 'b': { /* balanced string? */ s = matchbalance(ms, s, p + 2); if (s == null) { return(null); } p += 4; goto init; /* else return match(ms, s, p+4); */ } case 'f': { /* frontier? */ CharPtr ep; char previous; p += 2; if (p[0] != '[') { luaL_error(ms.L, "missing " + LUA_QL("[") + " after " + LUA_QL("%%f") + " in pattern"); } ep = classend(ms, p); /* points to what is next */ previous = (s == ms.src_init) ? '\0' : s[-1]; if ((matchbracketclass((byte)(previous), p, ep - 1) != 0) || (matchbracketclass((byte)(s[0]), p, ep - 1) == 0)) { return(null); } p = ep; goto init; /* else return match(ms, s, ep); */ } default: { if (isdigit((byte)(p[1]))) /* capture results (%0-%9)? */ { s = match_capture(ms, s, (byte)(p[1])); if (s == null) { return(null); } p += 2; goto init; /* else return match(ms, s, p+2) */ } //ismeretlen hiba miatt lett ide átmásolva { /* it is a pattern item */ CharPtr ep = classend(ms, p); /* points to what is next */ int m = (s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0) ? 1 : 0; switch (ep[0]) { case '?': { /* optional */ CharPtr res; if ((m != 0) && ((res = match(ms, s + 1, ep + 1)) != null)) { return(res); } p = ep + 1; goto init; /* else return match(ms, s, ep+1); */ } case '*': { /* 0 or more repetitions */ return(max_expand(ms, s, p, ep)); } case '+': { /* 1 or more repetitions */ return((m != 0) ? max_expand(ms, s + 1, p, ep) : null); } case '-': { /* 0 or more repetitions (minimum) */ return(min_expand(ms, s, p, ep)); } default: { if (m == 0) { return(null); } s = s.next(); p = ep; goto init; /* else return match(ms, s+1, ep); */ } } } //goto dflt; /* case default */ } } } case '\0': { /* end of pattern */ return(s); /* match succeeded */ } case '$': { if (p[1] == '\0') /* is the `$' the last char in pattern? */ { return((s == ms.src_end) ? s : null); /* check end of string */ } else { goto dflt; } } default: dflt : { /* it is a pattern item */ CharPtr ep = classend(ms, p); /* points to what is next */ int m = (s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0) ? 1 : 0; switch (ep[0]) { case '?': { /* optional */ CharPtr res; if ((m != 0) && ((res = match(ms, s + 1, ep + 1)) != null)) { return(res); } p = ep + 1; goto init; /* else return match(ms, s, ep+1); */ } case '*': { /* 0 or more repetitions */ return(max_expand(ms, s, p, ep)); } case '+': { /* 1 or more repetitions */ return((m != 0) ? max_expand(ms, s + 1, p, ep) : null); } case '-': { /* 0 or more repetitions (minimum) */ return(min_expand(ms, s, p, ep)); } default: { if (m == 0) { return(null); } s = s.next(); p = ep; goto init; /* else return match(ms, s+1, ep); */ } } } } }
public static void LuaOChunkID(CharPtr out_, CharPtr source, uint bufflen) { //out_ = ""; if (source[0] == '=') { strncpy(out_, source+1, (int)bufflen); /* remove first char */ out_[bufflen-1] = '\0'; /* ensures null termination */ } else { /* out = "source", or "...source" */ if (source[0] == '@') { uint l; source = source.next(); /* skip the `@' */ bufflen -= (uint)(" '...' ".Length + 1); l = (uint)strlen(source); strcpy(out_, ""); if (l > bufflen) { source += (l-bufflen); /* get last part of file name */ strcat(out_, "..."); } strcat(out_, source); } else { /* out = [string "string"] */ uint len = strcspn(source, "\n\r"); /* stop at first newline */ bufflen -= (uint)(" [string \"...\"] ".Length + 1); if (len > bufflen) len = bufflen; strcpy(out_, "[string \""); if (source[len] != '\0') { /* must truncate? */ strncat(out_, source, (int)len); strcat(out_, "..."); } else strcat(out_, source); strcat(out_, "\"]"); } } }
public static void luaL_addlstring (luaL_Buffer B, CharPtr s, uint l) { while (l-- != 0) { char c = s[0]; s = s.next(); luaL_addchar(B, c); } }
private static int str_find_aux(lua_State L, int find) { uint ls, lp; CharPtr s = luaL_checklstring(L, 1, out ls); CharPtr p = luaL_checklstring(L, 2, out lp); uint init = posrelat(luaL_optinteger(L, 3, 1), ls); if (init < 1) { init = 1; } else if (init > ls + 1) /* start after string's end? */ { lua_pushnil(L); /* cannot find anything */ return(1); } /* explicit request or no special characters? */ if ((find != 0) && ((lua_toboolean(L, 4) != 0) || nospecials(p, lp) != 0)) { /* do a plain search */ CharPtr s2 = lmemfind(s + init - 1, ls - init + 1, p, lp); if (s2 != null) { lua_pushinteger(L, s2 - s + 1); lua_pushinteger(L, (int)(s2 - s + lp)); return(2); } } else { MatchState ms = new MatchState(); CharPtr s1 = s + init - 1; int anchor = (p[0] == '^')?1:0; if (anchor != 0) { /*p++*/ p = p + 1; lp--; /* skip anchor character */ //FIXME:changed, ++ } ms.L = L; ms.src_init = s; ms.src_end = s + ls; ms.p_end = p + lp; do { CharPtr res; ms.level = 0; if ((res = match(ms, s1, p)) != null) { if (find != 0) { lua_pushinteger(L, s1 - s + 1); /* start */ lua_pushinteger(L, res - s); /* end */ return(push_captures(ms, null, null) + 2); } else { return(push_captures(ms, s1, res)); } } } while (((s1 = s1.next()) <= ms.src_end) && (anchor == 0)); } lua_pushnil(L); /* not found */ return(1); }
public static void LuaLAddLString (LuaLBuffer B, CharPtr s, uint l) { while (l-- != 0) { char c = s[0]; s = s.next(); LuaLAddChar(B, c); } }
private static CharPtr classend(MatchState ms, CharPtr p) { p = new CharPtr(p); char c = p[0]; p = p.next(); switch (c) { case L_ESC: { if (p[0] == '\0') luaL_error(ms.L, "malformed pattern (ends with " + LUA_QL("%%") + ")"); return p+1; } case '[': { if (p[0] == '^') p = p.next(); do { /* look for a `]' */ if (p[0] == '\0') luaL_error(ms.L, "malformed pattern (missing " + LUA_QL("]") + ")"); c = p[0]; p = p.next(); if (c == L_ESC && p[0] != '\0') p = p.next(); /* skip escapes (e.g. `%]') */ } while (p[0] != ']'); return p+1; } default: { return p; } } }
private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) { s = new CharPtr(s); p = new CharPtr(p); init: /* using goto's to optimize tail recursion */ switch (p[0]) { case '(': { /* start capture */ if (p[1] == ')') /* position capture? */ return start_capture(ms, s, p+2, CAP_POSITION); else return start_capture(ms, s, p+1, CAP_UNFINISHED); } case ')': { /* end capture */ return end_capture(ms, s, p+1); } case L_ESC: { switch (p[1]) { case 'b': { /* balanced string? */ s = matchbalance(ms, s, p+2); if (s == null) return null; p+=4; goto init; /* else return match(ms, s, p+4); */ } case 'f': { /* frontier? */ CharPtr ep; char previous; p += 2; if (p[0] != '[') luaL_error(ms.L, "missing " + LUA_QL("[") + " after " + LUA_QL("%%f") + " in pattern"); ep = classend(ms, p); /* points to what is next */ previous = (s == ms.src_init) ? '\0' : s[-1]; if ((matchbracketclass((byte)(previous), p, ep-1)!=0) || (matchbracketclass((byte)(s[0]), p, ep-1)==0)) return null; p=ep; goto init; /* else return match(ms, s, ep); */ } default: { if (isdigit((byte)(p[1]))) { /* capture results (%0-%9)? */ s = match_capture(ms, s, (byte)(p[1])); if (s == null) return null; p+=2; goto init; /* else return match(ms, s, p+2) */ } goto dflt; /* case default */ } } } case '\0': { /* end of pattern */ return s; /* match succeeded */ } case '$': { if (p[1] == '\0') /* is the `$' the last char in pattern? */ return (s == ms.src_end) ? s : null; /* check end of string */ else goto dflt; } default: dflt: { /* it is a pattern item */ CharPtr ep = classend(ms, p); /* points to what is next */ int m = (s<ms.src_end) && (singlematch((byte)(s[0]), p, ep)!=0) ? 1 : 0; switch (ep[0]) { case '?': { /* optional */ CharPtr res; if ((m!=0) && ((res=match(ms, s+1, ep+1)) != null)) return res; p=ep+1; goto init; /* else return match(ms, s, ep+1); */ } case '*': { /* 0 or more repetitions */ return max_expand(ms, s, p, ep); } case '+': { /* 1 or more repetitions */ return ((m!=0) ? max_expand(ms, s+1, p, ep) : null); } case '-': { /* 0 or more repetitions (minimum) */ return min_expand(ms, s, p, ep); } default: { if (m==0) return null; s = s.next(); p=ep; goto init; /* else return match(ms, s+1, ep); */ } } } } }
private static int str_find_aux(lua_State L, int find) { uint l1, l2; CharPtr s = luaL_checklstring(L, 1, out l1); CharPtr p = luaL_checklstring(L, 2, out l2); ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; if (init < 0) { init = 0; } else if ((uint)(init) > l1) { init = (ptrdiff_t)l1; } if ((find != 0) && ((lua_toboolean(L, 4) != 0) || /* explicit request? */ strpbrk(p, SPECIALS) == null)) /* or no special characters? */ /* do a plain search */ { CharPtr s2 = lmemfind(s + init, (uint)(l1 - init), p, (uint)(l2)); if (s2 != null) { lua_pushinteger(L, s2 - s + 1); lua_pushinteger(L, (int)(s2 - s + l2)); return(2); } } else { MatchState ms = new MatchState(); int anchor = 0; if (p[0] == '^') { p = p.next(); anchor = 1; } CharPtr s1 = s + init; ms.L = L; ms.src_init = s; ms.src_end = s + l1; do { CharPtr res; ms.level = 0; if ((res = match(ms, s1, p)) != null) { if (find != 0) { lua_pushinteger(L, s1 - s + 1); /* start */ lua_pushinteger(L, res - s); /* end */ return(push_captures(ms, null, null) + 2); } else { return(push_captures(ms, s1, res)); } } } while (((s1 = s1.next()) <= ms.src_end) && (anchor == 0)); } lua_pushnil(L); /* not found */ return(1); }
private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) { s = new CharPtr(s); p = new CharPtr(p); bool runDflt = false; bool runInit = true; if (ms.matchdepth-- == 0) { LuaLError(ms.L, "pattern too complex"); } //init: while (runInit) // Replaces "init:" in order to be compatible with Mono. { runInit = false; // No "goto init" until further notice. if (p != '\0') /* end of pattern? */ { switch (p[0]) { case '(': { /* start capture */ if (p[1] == ')') /* position capture? */ { s = start_capture(ms, s, p + 2, CAP_POSITION); } else { s = start_capture(ms, s, p + 1, CAP_UNFINISHED); } break; } case ')': { /* end capture */ s = end_capture(ms, s, p + 1); break; } case '$': { if (p[1] != '\0') /* is the `$' the last char in pattern? */ { runDflt = true; //goto dflt; /* no; go to default */ } s = (s == ms.src_end) ? s : null; /* check end of string */ break; } case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ switch (p[1]) { case 'b': { /* balanced string? */ s = matchbalance(ms, s, p + 2); if (s != null) { p += 4; runInit = true; //goto init; /* return match(ms, s, p+4); */ } /* else fail (s == NULL) */ break; } case 'f': { /* frontier? */ CharPtr ep; char previous; p += 2; if (p[0] != '[') { LuaLError(ms.L, "missing " + LUA_QL("[") + " after " + LUA_QL("%%f") + " in pattern"); } ep = classend(ms, p); /* points to what is next */ previous = (s == ms.src_init) ? '\0' : s[-1]; if ((matchbracketclass((byte)(previous), p, ep - 1) == 0) || (matchbracketclass((byte)(s[0]), p, ep - 1) != 0)) { p = ep; runInit = true; //goto init; /* else return match(ms, s, ep); */ } s = null; /* match failed */ break; } default: { if (isdigit((byte)(p[1]))) /* capture results (%0-%9)? */ { s = match_capture(ms, s, (byte)(p[1])); if (s != null) { p += 2; runInit = true; //goto init; /* else return match(ms, s, p+2) */ } break; } runDflt = true; //goto dflt; break; } } break; } default: { runDflt = true; // goto dflt break; } } } //dflt: if (runDflt) // Replaces "dflt:" in order to be compatible with Mono. { /* pattern class plus optional suffix */ runDflt = false; // no more "goto dflt" until further notice. CharPtr ep = classend(ms, p); /* points to optional suffix */ /* does not match at least once? */ if ((s >= ms.src_end) || (singlematch((byte)(s[0]), p, ep) == 0)) { if (ep == '*' || ep == '?' || ep == '-') /* accept empty? */ { p = ep + 1; runInit = true; //goto init; /* return match(ms, s, ep + 1); */ } else /* '+' or no suffix */ { s = null; /* fail */ } } else /* matched once */ { switch (ep[0]) { case '?': { /* optional */ CharPtr res; if ((res = match(ms, s + 1, ep + 1)) != null) { s = res; } else { p = ep + 1; runInit = true; //goto init; /* else return match(ms, s, ep+1); */ } break; } case '+': { /* 1 or more repetitions */ s = s.next(); /* 1 match already done */ s = max_expand(ms, s, p, ep); // cannot fall through, repeating '*' instruction instead. break; } case '*': { /* 0 or more repetitions */ s = max_expand(ms, s, p, ep); break; } case '-': { /* 0 or more repetitions (minimum) */ s = min_expand(ms, s, p, ep); break; } default: { /* no suffix */ s = s.next(); p = ep; runInit = true; //goto init; /* return match(ms, s+1, ep); */ break; } } } } } ms.matchdepth++; return(s); }