private static void addfield(lua_State L, luaL_Buffer b, int i) { lua_rawgeti(L, 1, i); if (lua_isstring(L, -1)==0) luaL_error(L, "invalid value (%s) at index %d in table for " + LUA_QL("concat"), luaL_typename(L, -1), i); luaL_addvalue(b); }
public static void luaL_addchar(luaL_Buffer B, char c) { if (B.p >= LUAL_BUFFERSIZE) { luaL_prepbuffer(B); } B.buffer[B.p++] = c; }
public static CharPtr luaL_prepbuffer(luaL_Buffer B) { if (emptybuffer(B) != 0) { adjuststack(B); } return(new CharPtr(B.buffer, B.p)); }
public static void luaL_addchar(luaL_Buffer B, char c) { if (!(B.n < B.size)) { luaL_prepbuffsize(B, 1); //FIXME: changed, ||->if } B.b[B.n++] = c; }
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 int str_gsub(lua_State L) { uint srcl, lp; CharPtr src = luaL_checklstring(L, 1, out srcl); CharPtr p = luaL_checklstring(L, 2, out lp); int tr = lua_type(L, 3); uint max_s = (uint)luaL_optinteger(L, 4, (int)(srcl + 1)); int anchor = (p[0] == '^') ? 1 : 0; uint n = 0; MatchState ms = new MatchState(); luaL_Buffer b = new luaL_Buffer(); luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, "string/function/table expected"); luaL_buffinit(L, b); if (anchor != 0) { /*p++*/ p = p + 1; lp--; /* skip anchor character */ //FIXME:changed } ms.L = L; ms.src_init = src; ms.src_end = src + srcl; ms.p_end = p + lp; while (n < max_s) { CharPtr e; ms.level = 0; e = match(ms, src, p); if (e != null) { n++; add_value(ms, b, src, e, tr); } 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(); luaL_addchar(b, c); } else { break; } if (anchor != 0) { break; } } luaL_addlstring(b, src, (uint)(ms.src_end - src)); luaL_pushresult(b); lua_pushinteger(L, (int)n); /* number of substitutions */ return(2); }
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_reverse (lua_State L) { uint l; luaL_Buffer b = new luaL_Buffer(); CharPtr s = luaL_checklstring(L, 1, out l); luaL_buffinit(L, b); while ((l--) != 0) luaL_addchar(b, s[l]); luaL_pushresult(b); return 1; }
private static void addfield(lua_State L, luaL_Buffer b, TabA ta, lua_Integer i) { ta.geti(L, 1, i); if (lua_isstring(L, -1) == 0) { luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", luaL_typename(L, -1), i); } luaL_addvalue(b); }
public static void luaL_pushresult(luaL_Buffer B) { lua_State L = B.L; lua_pushlstring(L, B.b, B.n); if (buffonstack(B) != 0) { lua_remove(L, -2); /* remove old buffer */ } }
private static void addfield(lua_State L, luaL_Buffer b, int i) { lua_rawgeti(L, 1, i); if (lua_isstring(L, -1) == 0) { luaL_error(L, "invalid value (%s) at index %d in table for " + LUA_QL("concat"), luaL_typename(L, -1), i); } luaL_addvalue(b); }
private static int str_gsub(lua_State L) { uint srcl; CharPtr src = luaL_checklstring(L, 1, out srcl); CharPtr p = luaL_checkstring(L, 2); int max_s = luaL_optint(L, 4, (int)(srcl + 1)); int anchor = 0; if (p[0] == '^') { p = p.next(); anchor = 1; } int n = 0; MatchState ms = new MatchState(); luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); ms.L = L; ms.src_init = src; ms.src_end = src + srcl; while (n < max_s) { CharPtr e; ms.level = 0; 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(); luaL_addchar(b, c); } else { break; } if (anchor != 0) { break; } } luaL_addlstring(b, src, (uint)(ms.src_end - src)); luaL_pushresult(b); lua_pushinteger(L, n); /* number of substitutions */ return(2); }
private static int read_chars (lua_State L, StreamProxy f, uint n) { uint nr; /* number of chars actually read */ CharPtr p; luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); p = luaL_prepbuffsize(b, n); /* prepare buffer to read whole block */ nr = (uint)fread(p, 1/*sizeof(char)*/, (int)n, f); /* try to read 'n' chars */ //FIXME:changed luaL_addsize(b, nr); luaL_pushresult(b); /* close buffer */ return (nr > 0)?1:0; /* true iff read something */ }
private static int str_upper (lua_State L) { uint l; uint i; luaL_Buffer b = new luaL_Buffer(); CharPtr s = luaL_checklstring(L, 1, out l); luaL_buffinit(L, b); for (i=0; i<l; i++) luaL_addchar(b, toupper(s[i])); luaL_pushresult(b); return 1; }
public static void luaL_addvalue(luaL_Buffer B) { lua_State L = B.L; uint l; CharPtr s = lua_tolstring(L, -1, out l); if (buffonstack(B) != 0) { lua_insert(L, -2); /* put value below buffer */ } luaL_addlstring(B, s, l); lua_remove(L, (buffonstack(B) != 0) ? -2 : -1); /* remove value */ }
private static int str_reverse(lua_State L) { uint l; luaL_Buffer b = new luaL_Buffer(); CharPtr s = luaL_checklstring(L, 1, out l); luaL_buffinit(L, b); while ((l--) != 0) { luaL_addchar(b, s[l]); } luaL_pushresult(b); return(1); }
private static int str_reverse(lua_State L) { uint l, i; luaL_Buffer b = new luaL_Buffer(); CharPtr s = luaL_checklstring(L, 1, out l); CharPtr p = luaL_buffinitsize(L, b, l); for (i = 0; i < l; i++) { p[i] = s[l - i - 1]; } luaL_pushresultsize(b, l); return(1); }
private static int str_dump(lua_State L) { luaL_Buffer b = new luaL_Buffer(); luaL_checktype(L, 1, LUA_TFUNCTION); lua_settop(L, 1); luaL_buffinit(L, b); if (lua_dump(L, writer, b) != 0) { luaL_error(L, "unable to dump given function"); } luaL_pushresult(b); return(1); }
private const int MAX_SIZE_T = MAX_INT;//(~(size_t)0); //FIXME:changed; private static void read_all (lua_State L, StreamProxy f) { uint rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */ luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); for (;;) { CharPtr p = luaL_prepbuffsize(b, rlen); uint nr = (uint)fread(p, 1/*sizeof(char)*/, (int)rlen, f); //FIXME:changed luaL_addsize(b, nr); if (nr < rlen) break; /* eof? */ else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */ rlen *= 2; /* double buffer size at each iteration */ } luaL_pushresult(b); /* close buffer */ }
private static int str_rep(lua_State L) { uint l; luaL_Buffer b = new luaL_Buffer(); CharPtr s = luaL_checklstring(L, 1, out l); int n = luaL_checkint(L, 2); luaL_buffinit(L, b); while (n-- > 0) { luaL_addlstring(b, s, l); } luaL_pushresult(b); return(1); }
private static int str_upper(lua_State L) { uint l; uint i; luaL_Buffer b = new luaL_Buffer(); CharPtr s = luaL_checklstring(L, 1, out l); luaL_buffinit(L, b); for (i = 0; i < l; i++) { luaL_addchar(b, toupper(s[i])); } luaL_pushresult(b); return(1); }
private static int str_upper(lua_State L) { uint l; uint i; luaL_Buffer b = new luaL_Buffer(); CharPtr s = luaL_checklstring(L, 1, out l); CharPtr p = luaL_buffinitsize(L, b, l); for (i = 0; i < l; i++) { p[i] = toupper(uchar(s[i])); } luaL_pushresultsize(b, l); return(1); }
private static int emptybuffer(luaL_Buffer B) { uint l = (uint)bufflen(B); if (l == 0) { return(0); /* put nothing on stack */ } else { lua_pushlstring(B.L, B.buffer, l); B.p = 0; B.lvl++; return(1); } }
private static int str_char(lua_State L) { int n = lua_gettop(L); /* number of arguments */ int i; luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); for (i = 1; i <= n; i++) { int c = luaL_checkint(L, i); luaL_argcheck(L, (byte)(c) == c, i, "invalid value"); luaL_addchar(b, (char)(byte)c); } luaL_pushresult(b); return(1); }
private static int emptybuffer(luaL_Buffer B) { int l = bufflen(B); if (l == 0) { return(0); /* put nothing on stack */ } else { lua_pushstring(B.L, new string( B.buffer.chars, 0, l )); B.p = 0; B.lvl++; return(1); } }
private static int str_char(lua_State L) { int n = lua_gettop(L); /* number of arguments */ int i; luaL_Buffer b = new luaL_Buffer(); CharPtr p = luaL_buffinitsize(L, b, (uint)n); //FIXME:added, (uint) for (i = 1; i <= n; i++) { int c = luaL_checkint(L, i); luaL_argcheck(L, (byte)(c) == c, i, "invalid value"); //FIXME: uchar() p[i - 1] = (char)(c); //FIXME: uchar()->(char) } luaL_pushresultsize(b, (uint)n); //FIXME:added, (uint) return(1); }
/* }====================================================== */ public static CharPtr luaL_gsub(lua_State L, CharPtr s, CharPtr p, CharPtr r) { CharPtr wild; uint l = (uint)strlen(p); luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); while ((wild = strstr(s, p)) != null) { luaL_addlstring(b, s, (uint)(wild - s)); /* push prefix */ luaL_addstring(b, r); /* push replacement in place of pattern */ s = wild + l; /* continue after `p' */ } luaL_addstring(b, s); /* push last suffix */ luaL_pushresult(b); return(lua_tostring(L, -1)); }
private static void add_value(MatchState ms, luaL_Buffer b, CharPtr s, CharPtr e) { lua_State L = ms.L; switch (lua_type(L, 3)) { case LUA_TNUMBER: case LUA_TSTRING: { add_s(ms, b, s, e); return; } case LUA_TFUNCTION: { int n; lua_pushvalue(L, 3); n = push_captures(ms, s, e); lua_call(L, n, 1); break; } case LUA_TTABLE: { push_onecapture(ms, 0, s, e); lua_gettable(L, 3); break; } default: { luaL_argerror(L, 3, "string/function/table expected"); return; } } if (lua_toboolean(L, -1) == 0) /* nil or false? */ { lua_pop(L, 1); lua_pushlstring(L, s, (uint)(e - s)); /* keep original text */ } else if (lua_isstring(L, -1) == 0) { luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); } luaL_addvalue(b); /* add result to accumulator */ }
private static int read_line (lua_State L, StreamProxy f, int chop) { luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); for (;;) { uint l; CharPtr p = luaL_prepbuffer(b); if (fgets(p, f) == null) { /* eof? */ luaL_pushresult(b); /* close buffer */ return (lua_rawlen(L, -1) > 0) ? 1 : 0; /* check whether read something */ } l = (uint)strlen(p); if (l == 0 || p[l-1] != '\n') luaL_addsize(b, (uint)(int)l); //FIXME:added, (uint) else { luaL_addsize(b, (uint)(l - chop)); /* chop 'eol' if needed */ //FIXME:added, (uint) luaL_pushresult(b); /* close buffer */ return 1; /* read at least an `eol' */ } } }
private static int read_chars(lua_State L, StreamProxy f, uint n) { uint rlen; /* how much to read */ uint nr; /* number of chars actually read */ luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ do { CharPtr p = luaL_prepbuffer(b); if (rlen > n) { rlen = n; /* cannot read more than asked */ } nr = (uint)fread(p, GetUnmanagedSize(typeof(char)), (int)rlen, f); luaL_addsize(b, (int)nr); n -= nr; /* still have to read `n' chars */ } while (n > 0 && nr == rlen); /* until end of count or eof */ luaL_pushresult(b); /* close buffer */ return((n == 0 || lua_objlen(L, -1) > 0) ? 1 : 0); }
private static int tconcat(lua_State L) { luaL_Buffer b = new luaL_Buffer(); lua_Integer last = aux_getn(L, 1, TAB_R); uint lsep; CharPtr sep = luaL_optlstring(L, 2, "", out lsep); lua_Integer i = luaL_optinteger(L, 3, 1); last = luaL_optinteger(L, 4, last); luaL_buffinit(L, b); for (; i < last; i++) { addfield(L, b, i); luaL_addlstring(b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ { addfield(L, b, i); } luaL_pushresult(b); return(1); }
/* ** utfchar(n1, n2, ...) -> char(n1)..char(n2)... */ private static int utfchar(lua_State L) { int n = lua_gettop(L); /* number of arguments */ if (n == 1) /* optimize common case of single char */ { pushutfchar(L, 1); } else { int i; luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); for (i = 1; i <= n; i++) { pushutfchar(L, i); luaL_addvalue(b); } luaL_pushresult(b); } return(1); }
private static int tconcat(lua_State L) { luaL_Buffer b = new luaL_Buffer(); int i, last; string sep = luaL_optstring(L, 2, ""); luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optinteger(L, 3, 1); last = luaL_opt_integer(L, luaL_checkinteger, 4, luaL_getn(L, 1)); luaL_buffinit(L, b); for ( ; i < last; i++) { addfield(L, b, i); luaL_addstring(b, sep); } if (i == last) /* add last value (if interval was not empty) */ { addfield(L, b, i); } luaL_pushresult(b); return(1); }
public static void luaL_pushresult(luaL_Buffer B) { emptybuffer(B); lua_concat(B.L, B.lvl); B.lvl = 1; }
private static void add_value(MatchState ms, luaL_Buffer b, CharPtr s, CharPtr e) { lua_State L = ms.L; switch (lua_type(L, 3)) { case LUA_TNUMBER: case LUA_TSTRING: { add_s(ms, b, s, e); return; } case LUA_TUSERDATA: case LUA_TFUNCTION: { int n; lua_pushvalue(L, 3); n = push_captures(ms, s, e); lua_call(L, n, 1); break; } case LUA_TTABLE: { push_onecapture(ms, 0, s, e); lua_gettable(L, 3); break; } } if (lua_toboolean(L, -1)==0) { /* nil or false? */ lua_pop(L, 1); lua_pushlstring(L, s, (uint)(e - s)); /* keep original text */ } else if (lua_isstring(L, -1)==0) luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); luaL_addvalue(b); /* add result to accumulator */ }
public static extern void luaL_buffinit(lua_State L, ref luaL_Buffer B);
private static int str_gsub(lua_State L) { uint srcl; CharPtr src = luaL_checklstring(L, 1, out srcl); CharPtr p = luaL_checkstring(L, 2); int tr = lua_type(L, 3); int max_s = luaL_optint(L, 4, (int)(srcl+1)); int anchor = 0; if (p[0] == '^') { p = p.next(); anchor = 1; } int n = 0; MatchState ms = new MatchState(); luaL_Buffer b = new luaL_Buffer(); luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || tr == LUA_TFUNCTION || tr == LUA_TTABLE || tr == LUA_TUSERDATA, 3, "string/function/table expected"); luaL_buffinit(L, b); ms.L = L; ms.src_init = src; ms.src_end = src+srcl; while (n < max_s) { CharPtr e; ms.level = 0; 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(); luaL_addchar(b, c); } else break; if (anchor != 0) break; } luaL_addlstring(b, src, (uint)(ms.src_end-src)); luaL_pushresult(b); lua_pushinteger(L, n); /* number of substitutions */ return 2; }
/* ** {====================================================== ** Generic Buffer manipulation ** ======================================================= */ private static int bufflen(luaL_Buffer B) { return B.p; }
private static void add_s(MatchState ms, luaL_Buffer b, CharPtr s, CharPtr e) { uint l, i; CharPtr news = lua_tolstring(ms.L, 3, out l); for (i = 0; i < l; i++) { if (news[i] != L_ESC) luaL_addchar(b, news[i]); else { i++; /* skip ESC */ if (!isdigit((byte)(news[i]))) luaL_addchar(b, news[i]); else if (news[i] == '0') luaL_addlstring(b, s, (uint)(e - s)); else { push_onecapture(ms, news[i] - '1', s, e); luaL_addvalue(b); /* add capture to accumulated result */ } } } }
public static void luaL_addsize(luaL_Buffer B, int n) { B.p += n; }
/* }====================================================== */ public static CharPtr luaL_gsub(LuaState L, CharPtr s, CharPtr p, CharPtr r) { CharPtr wild; uint l = (uint)strlen(p); luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); while ((wild = strstr(s, p)) != null) { luaL_addlstring(b, s, (uint)(wild - s)); /* push prefix */ luaL_addstring(b, r); /* push replacement in place of pattern */ s = wild + l; /* continue after `p' */ } luaL_addstring(b, s); /* push last suffix */ luaL_pushresult(b); return lua_tostring(L, -1); }
///* compatibility only */ public static void luaL_putchar(luaL_Buffer B, char c) { luaL_addchar(B, c); }
public static void luaL_addchar(luaL_Buffer B, char c) { if (B.p >= LUAL_BUFFERSIZE) luaL_prepbuffer(B); B.buffer[B.p++] = c; }
private static int str_rep(lua_State L) { uint l; luaL_Buffer b = new luaL_Buffer(); CharPtr s = luaL_checklstring(L, 1, out l); int n = luaL_checkint(L, 2); luaL_buffinit(L, b); while (n-- > 0) luaL_addlstring(b, s, l); luaL_pushresult(b); return 1; }
public static void luaL_addvalue(luaL_Buffer B) { LuaState L = B.L; uint vl; CharPtr s = lua_tolstring(L, -1, out vl); if (vl <= bufffree(B)) { /* fit into buffer? */ CharPtr dst = new CharPtr(B.buffer.chars, B.buffer.index + B.p); CharPtr src = new CharPtr(s.chars, s.index); for (uint i = 0; i < vl; i++) dst[i] = src[i]; B.p += (int)vl; lua_pop(L, 1); /* remove from stack */ } else { if (emptybuffer(B) != 0) lua_insert(L, -2); /* put buffer before new value */ B.lvl++; /* add new value into B stack */ adjuststack(B); } }
private static int bufffree(luaL_Buffer B) { return LUAL_BUFFERSIZE - bufflen(B); }
public static void luaL_buffinit(LuaState L, luaL_Buffer B) { B.L = L; B.p = /*B.buffer*/ 0; B.lvl = 0; }
private static int read_line (lua_State L, Stream f) { luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); for (;;) { uint l; CharPtr p = luaL_prepbuffer(b); if (fgets(p, f) == null) { /* eof? */ luaL_pushresult(b); /* close buffer */ return (lua_objlen(L, -1) > 0) ? 1 : 0; /* check whether read something */ } l = (uint)strlen(p); if (l == 0 || p[l-1] != '\n') luaL_addsize(b, (int)l); else { luaL_addsize(b, (int)(l - 1)); /* do not include `eol' */ luaL_pushresult(b); /* close buffer */ return 1; /* read at least an `eol' */ } } }
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 int tconcat(LuaState L) { luaL_Buffer b = new luaL_Buffer(); uint lsep; int i, last; CharPtr sep = luaL_optlstring(L, 2, "", out lsep); luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optint(L, 3, 1); last = luaL_opt_integer(L, luaL_checkint, 4, luaL_getn(L, 1)); luaL_buffinit(L, b); for (; i < last; i++) { addfield(L, b, i); luaL_addlstring(b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ addfield(L, b, i); luaL_pushresult(b); return 1; }
private static int emptybuffer(luaL_Buffer B) { uint l = (uint)bufflen(B); if (l == 0) return 0; /* put nothing on stack */ else { lua_pushlstring(B.L, B.buffer, l); B.p = 0; B.lvl++; return 1; } }
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; }
private static int read_chars (lua_State L, Stream f, uint n) { uint rlen; /* how much to read */ uint nr; /* number of chars actually read */ luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ do { CharPtr p = luaL_prepbuffer(b); if (rlen > n) rlen = n; /* cannot read more than asked */ nr = (uint)fread(p, GetUnmanagedSize(typeof(char)), (int)rlen, f); luaL_addsize(b, (int)nr); n -= nr; /* still have to read `n' chars */ } while (n > 0 && nr == rlen); /* until end of count or eof */ luaL_pushresult(b); /* close buffer */ return (n == 0 || lua_objlen(L, -1) > 0) ? 1 : 0; }
private static void adjuststack(luaL_Buffer B) { if (B.lvl > 1) { LuaState L = B.L; int toget = 1; /* number of levels to concat */ uint toplen = lua_strlen(L, -1); do { uint l = lua_strlen(L, -(toget + 1)); if (B.lvl - toget + 1 >= LIMIT || toplen > l) { toplen += l; toget++; } else break; } while (toget < B.lvl); lua_concat(L, toget); B.lvl = B.lvl - toget + 1; } }
public static CharPtr luaL_prepbuffer(luaL_Buffer B) { if (emptybuffer(B) != 0) adjuststack(B); return new CharPtr(B.buffer, B.p); }
public static extern void luaL_addvalue(ref luaL_Buffer B);
public static void luaL_addstring(luaL_Buffer B, CharPtr s) { luaL_addlstring(B, s, (uint)strlen(s)); }
private static int str_dump(lua_State L) { luaL_Buffer b = new luaL_Buffer(); luaL_checktype(L, 1, LUA_TFUNCTION); lua_settop(L, 1); luaL_buffinit(L,b); if (lua_dump(L, writer, b) != 0) luaL_error(L, "unable to dump given function"); luaL_pushresult(b); return 1; }
private static int str_char(lua_State L) { int n = lua_gettop(L); /* number of arguments */ int i; luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); for (i=1; i<=n; i++) { int c = luaL_checkint(L, i); luaL_argcheck(L, (byte)(c) == c, i, "invalid value"); luaL_addchar(b, (char)(byte)c); } luaL_pushresult(b); return 1; }