Example #1
0
		protected static void LuaLAddString(LuaLBuffer b, string s)
		{
			b.StringBuilder.Append(s.ToString());
		}
Example #2
0
		protected static void LuaLAddChar(LuaLBuffer b, char p)
		{
			b.StringBuilder.Append(p);
		}
Example #3
0
		protected static void LuaLAddValue(LuaLBuffer b)
		{
			b.StringBuilder.Append(b.LuaState.Pop().ToPrintString());
		}
Example #4
0
		protected static void LuaLAddLString(LuaLBuffer b, CharPtr s, uint p)
		{
			b.StringBuilder.Append(s.ToString((int)p));
		}
Example #5
0
 protected static void LuaLBuffInit(LuaState L, LuaLBuffer b)
 {
 }
Example #6
0
 protected static void LuaLPushResult(LuaLBuffer b)
 {
     LuaPushLiteral(b.LuaState, b.StringBuilder.ToString());
 }
Example #7
0
 protected static void LuaLAddString(LuaLBuffer b, string s)
 {
     b.StringBuilder.Append(s.ToString());
 }
Example #8
0
        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 */
        }
Example #9
0
 protected static void LuaLAddValue(LuaLBuffer b)
 {
     b.StringBuilder.Append(b.LuaState.Pop().ToPrintString());
 }
Example #10
0
 protected static void LuaLAddLString(LuaLBuffer b, CharPtr s, uint p)
 {
     b.StringBuilder.Append(s.ToString((int)p));
 }
Example #11
0
        public static int str_format(LuaState L)
        {
            var top = LuaGetTop(L);
            var arg = 1;
            uint sfl;
            CharPtr strfrmt = LuaLCheckLString(L, arg, out sfl);
            var strfrmt_end = strfrmt + sfl;
            var b = new LuaLBuffer(L);
            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);
                    var 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, (long) LuaLCheckNumber(L, arg));
                            break;
                        }
                        case 'o':
                        case 'u':
                        case 'x':
                        case 'X':
                        {
                            addintlen(form);
                            sprintf(buff, form, (ulong) LuaLCheckNumber(L, arg));
                            break;
                        }
                        case 'e':
                        case 'E':
                        case 'f':
                        case 'g':
                        case 'G':
                        {
                            sprintf(buff, form, 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 */
                            }
                            sprintf(buff, form, s);
                            break;
                        }
                        default:
                        {
                            /* also treat cases `pnLlh' */
                            return LuaLError(L, "invalid option " + LUA_QL("%" + ch) + " to " +
                                                LUA_QL("format"), strfrmt[-1]);
                        }
                    }
                    LuaLAddLString(b, buff, (uint) strlen(buff));
                }
            }
            LuaLPushResult(b);
            return 1;
        }
Example #12
0
        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;
                    }
                    default:
                    {
                        if (s[0] < (char) 16)
                        {
                            var isfollowedbynum = false;

                            if (l >= 1)
                            {
                                if (char.IsNumber(s[1]))
                                    isfollowedbynum = true;
                            }

                            if (isfollowedbynum)
                                LuaLAddString(b, string.Format("\\{0:000}", (int) s[0]));
                            else
                                LuaLAddString(b, string.Format("\\{0}", (int) s[0]));
                        }
                        else
                        {
                            LuaLAddChar(b, s[0]);
                        }
                        break;
                    }
                }
                s = s.next();
            }
            LuaLAddChar(b, '"');
        }
Example #13
0
 public static int str_gsub(LuaState L)
 {
     uint srcl;
     CharPtr src = LuaLCheckLString(L, 1, out srcl);
     CharPtr p = PatchPattern(LuaLCheckStringStr(L, 2));
     var tr = LuaType(L, 3);
     var max_s = LuaLOptInt(L, 4, (int) (srcl + 1));
     var anchor = 0;
     if (p[0] == '^')
     {
         p = p.next();
         anchor = 1;
     }
     var n = 0;
     var ms = new MatchState();
     var b = new LuaLBuffer(L);
     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);
         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)
         {
             var 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;
 }
Example #14
0
		protected static void LuaLBuffInit(LuaState L, LuaLBuffer b)
		{
		}
Example #15
0
 protected static void LuaLAddChar(LuaLBuffer b, char p)
 {
     b.StringBuilder.Append(p);
 }
Example #16
0
		protected static void LuaLPushResult(LuaLBuffer b)
		{
			LuaPushLiteral(b.LuaState, b.StringBuilder.ToString());
		}
Example #17
0
 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 */
             }
         }
     }
 }