// find c in str protected static CharPtr strchr(CharPtr str, char c) { for (int index = str.index; str.chars[index] != 0; index++) if (str.chars[index] == c) return new CharPtr(str.chars, index); return null; }
protected static CharPtr memchr(CharPtr ptr, char c, uint count) { for (uint i = 0; i < count; i++) if (ptr[i] == c) return new CharPtr(ptr.chars, (int) (ptr.index + i)); return null; }
protected static CharPtr strcpy(CharPtr dst, CharPtr src) { int i; for (i = 0; src[i] != '\0'; i++) dst[i] = src[i]; dst[i] = '\0'; return dst; }
protected static CharPtr strpbrk(CharPtr str, CharPtr charset) { for (var i = 0; str[i] != '\0'; i++) for (var j = 0; charset[j] != '\0'; j++) if (str[i] == charset[j]) return new CharPtr(str.chars, str.index + i); return null; }
protected static int memcmp(CharPtr ptr1, CharPtr ptr2, int size) { for (var i = 0; i < size; i++) if (ptr1[i] != ptr2[i]) { if (ptr1[i] < ptr2[i]) return -1; return 1; } return 0; }
private static int matchbracketclass(int c, CharPtr p, CharPtr ec) { var 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((char) c, 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; }
protected static void LuaPushLString(LuaState L, CharPtr s, uint len) { string ss = s.ToString((int)len); L.Push(DynValue.NewString(ss)); }
public CharPtr(CharPtr ptr) { chars = ptr.chars; index = ptr.index; }
protected static void LuaLAddLString(LuaLBuffer b, CharPtr s, uint p) { b.StringBuilder.Append(s.ToString((int)p)); }
private static CharPtr matchbalance(MatchState ms, CharPtr s, CharPtr p) { if ((p[0] == 0) || (p[1] == 0)) LuaLError(ms.L, "unbalanced pattern"); if (s[0] != p[0]) return null; int b = p[0]; int e = p[1]; var 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 */ }
public static void sprintf(CharPtr buffer, CharPtr str, params object[] argv) { string temp = Tools.sprintf(str.ToString(), argv); strcpy(buffer, temp); }
protected static CharPtr strncpy(CharPtr dst, CharPtr src, int length) { var index = 0; while ((src[index] != '\0') && (index < length)) { dst[index] = src[index]; index++; } while (index < length) dst[index++] = '\0'; return dst; }
private static CharPtr min_expand(MatchState ms, CharPtr s, CharPtr p, CharPtr ep) { for (;;) { var res = match(ms, s, ep + 1); if (res != null) return res; 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); if (ms.matchdepth-- == 0) LuaLError(ms.L, "pattern too complex"); 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); 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] != '[') 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)) return null; p = ep; goto init; /* else return match(ms, s, ep); */ } default: { if (isdigit(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 */ var ep = classend(ms, p); /* points to what is next */ var 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 */ goto dflt; } default: dflt: { /* it is a pattern item */ var ep = classend(ms, p); /* points to what is next */ var 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 CharPtr lmemfind(CharPtr s1, uint l1, CharPtr s2, uint l2) { if (l2 == 0) return s1; /* empty strings are everywhere */ if (l2 > l1) return null; /* avoids a negative `l1' */ CharPtr init; /* to search for a `*s2' inside `s1' */ l2--; /* 1st char will be checked by `memchr' */ l1 = l1 - l2; /* `s2' cannot be found after that */ while (l1 > 0 && (init = memchr(s1, s2[0], l1)) != null) { init = init.next(); /* 1st char is already checked */ if (memcmp(init, s2 + 1, l2) == 0) return init - 1; /* correct `l1' and `s1' to try again */ l1 -= (uint) (init - s1); s1 = init; } return null; /* not found */ }
private static CharPtr match_capture(MatchState ms, CharPtr s, int l) { uint len; l = check_capture(ms, l); len = (uint) ms.capture[l].len; if ((uint) (ms.src_end - s) >= len && memcmp(ms.capture[l].init, s, len) == 0) return s + len; return null; }
private static CharPtr end_capture(MatchState ms, CharPtr s, CharPtr p) { var l = capture_to_close(ms); CharPtr res; ms.capture[l].len = s - ms.capture[l].init; /* close capture */ if ((res = match(ms, s, p)) == null) /* match failed? */ ms.capture[l].len = CAP_UNFINISHED; /* undo capture */ return res; }
private static CharPtr start_capture(MatchState ms, CharPtr s, CharPtr p, int what) { CharPtr res; var level = ms.level; if (level >= LUA_MAXCAPTURES) LuaLError(ms.L, "too many captures"); ms.capture[level].init = s; ms.capture[level].len = what; ms.level = level + 1; if ((res = match(ms, s, p)) == null) /* match failed? */ ms.level--; /* undo capture */ return res; }
protected static int memcmp(CharPtr ptr1, CharPtr ptr2, uint size) { return memcmp(ptr1, ptr2, (int) size); }
public static void sprintf(CharPtr buffer, CharPtr str, params object[] argv) { var temp = Tools.sprintf(str.ToString(), argv); strcpy(buffer, temp); }
private static void push_onecapture(MatchState ms, int i, CharPtr s, CharPtr e) { if (i >= ms.level) { if (i == 0) /* ms.level == 0, too */ LuaPushLString(ms.L, s, (uint) (e - s)); /* add whole match */ else LuaLError(ms.L, "invalid capture index"); } else { var l = ms.capture[i].len; if (l == CAP_UNFINISHED) LuaLError(ms.L, "unfinished capture"); if (l == CAP_POSITION) LuaPushInteger(ms.L, ms.capture[i].init - ms.src_init + 1); else LuaPushLString(ms.L, ms.capture[i].init, (uint) l); } }
private static CharPtr max_expand(MatchState ms, CharPtr s, CharPtr p, CharPtr ep) { var i = 0; /* counts maximum expand for item */ while ((s + i < ms.src_end) && (singlematch((byte) (s[i]), p, ep) != 0)) i++; /* keeps trying to match with the maximum repetitions */ while (i >= 0) { var res = match(ms, (s + i), ep + 1); if (res != null) return res; i--; /* else didn't match; reduce 1 repetition to try again */ } return null; }
protected static int strlen(CharPtr str) { var index = 0; while (str[index] != '\0') index++; return index; }
public CharPtr(CharPtr ptr, int index) { this.chars = ptr.chars; this.index = index; }
private static int push_captures(MatchState ms, CharPtr s, CharPtr e) { int i; var nlevels = ((ms.level == 0) && (s != null)) ? 1 : ms.level; LuaLCheckStack(ms.L, nlevels, "too many captures"); for (i = 0; i < nlevels; i++) push_onecapture(ms, i, s, e); return nlevels; /* number of strings pushed */ }
private static int singlematch(int c, CharPtr p, CharPtr ep) { switch (p[0]) { case '.': return 1; /* matches any char */ case L_ESC: return match_class((char) c, p[1]); case '[': return matchbracketclass(c, p, ep - 1); default: return ((byte) (p[0]) == c) ? 1 : 0; } }
public CharPtr(CharPtr ptr) { this.chars = ptr.chars; this.index = ptr.index; }
private static void add_s(MatchState ms, LuaLBuffer b, CharPtr s, CharPtr e) { uint l, i; CharPtr news = LuaToLString(ms.L, 3, out l); for (i = 0; i < l; i++) { if (news[i] != L_ESC) LuaLAddChar(b, news[i]); else { i++; /* skip ESC */ if (!isdigit(news[i])) { if (news[i] != L_ESC) { LuaLError(ms.L, "invalid use of '%' in replacement string"); } LuaLAddChar(b, news[i]); } else if (news[i] == '0') LuaLAddLString(b, s, (uint) (e - s)); else { push_onecapture(ms, news[i] - '1', s, e); LuaLAddValue(b); /* add capture to accumulated result */ } } } }
protected static lua_Integer memcmp(CharPtr ptr1, CharPtr ptr2, uint size) { return(memcmp(ptr1, ptr2, (int)size)); }
private static void add_value(MatchState ms, LuaLBuffer b, CharPtr s, CharPtr e) { var L = ms.L; switch (LuaType(L, 3)) { case LUA_TNUMBER: case LUA_TSTRING: { add_s(ms, b, s, e); return; } // case LUA_TUSERDATA: /// +++ does this make sense ?? case LUA_TFUNCTION: { int n; LuaPushValue(L, 3); n = push_captures(ms, s, e); LuaCall(L, n, 1); break; } case LUA_TTABLE: { push_onecapture(ms, 0, s, e); LuaGetTable(L, 3); break; } } if (LuaToBoolean(L, -1) == 0) { /* nil or false? */ LuaPop(L, 1); LuaPushLString(L, s, (uint) (e - s)); /* keep original text */ } else if (LuaIsString(L, -1) == 0) LuaLError(L, "invalid replacement value (a {0})", LuaLTypeName(L, -1)); LuaLAddValue(b); /* add result to accumulator */ }
private static CharPtr scanformat(LuaState L, CharPtr strfrmt, CharPtr form) { var p = strfrmt; while (p[0] != '\0' && strchr(FLAGS, p[0]) != null) p = p.next(); /* skip flags */ if ((uint) (p - strfrmt) >= (FLAGS.Length + 1)) LuaLError(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]))) LuaLError(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 CharPtr(CharPtr ptr, int index) { chars = ptr.chars; this.index = index; }
private static void addintlen(CharPtr form) { var l = (uint) strlen(form); var spec = form[l - 1]; strcpy(form + l - 1, LUA_INTFRMLEN); form[l + (LUA_INTFRMLEN.Length + 1) - 2] = spec; form[l + (LUA_INTFRMLEN.Length + 1) - 1] = '\0'; }