Beispiel #1
0
static void luaI_waddquoted (lua_State *L, luaL_Buffer *b, int arg) {
    size_t l;
    const lua_WChar *s = luaL_checklwstring(L, arg, &l);
    luaL_addwchar(b, '"');
    while (l--) {
        switch (*s) {
        case '"':
        case '\\':
        case '\n': {
            luaL_addwchar(b, '\\');
            luaL_addwchar(b, *s);
            break;
        }
        case '\r': {
            luaL_addlstring(b, "\\r", 2);
            break;
        }
        case '\0': {
            luaL_addlwstring(b, (const lua_WChar*)L"\\000", 4);
            break;
        }
        default: {
            luaL_addwchar(b, *s);
            break;
        }
        }
        s++;
    }
    luaL_addwchar(b, '"');
}
static int luaB_towstring (lua_State *L) {
	char buff[64];
	luaL_checkany(L, 1);
	if (luaL_callmeta(L, 1, "__towstring"))  /* is there a metafield? */
		return 1;  /* use its value */
	switch (lua_type(L, 1)) {
	case LUA_TNUMBER:
		lua_pushwstring(L, lua_towstring(L, 1));
		break;
	case LUA_TSTRING: {
		luaL_Buffer b;
		size_t l;
		size_t i;
		const char *s = lua_tostring(L, 1);
		l = lua_strlen(L, 1);
		if (l == 0)
		{
			lua_WChar str[] = { '\0' };
			lua_pushwstring(L, (const lua_WChar*)str);
		}
		else
		{
			luaL_wbuffinit(L, &b);
			for (i=0; i<l; i++)
				luaL_addwchar(&b, (lua_WChar)(unsigned char)s[i]);
			luaL_pushresult(&b);
		}
		return 1;
    }
	case LUA_TWSTRING:
		lua_pushvalue(L, 1);
		return 1;
	case LUA_TBOOLEAN:
		strcpy(buff, (lua_toboolean(L, 1) ? "true" : "false"));
		break;
	case LUA_TNIL:
		strcpy(buff, "nil");
		break;
	default:
		sprintf(buff, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
		break;
	}

	{
		luaL_Buffer b;
		size_t l;
		size_t i;
		const lua_WChar *s = lua_towstring(L, 1);
		l = lua_strlen(L, 1);
		luaL_wbuffinit(L, &b);
		for (i=0; i<l; i++)
			luaL_addwchar(&b, (lua_WChar)(s[i]));
		luaL_pushresult(&b);
	}

	return 1;
}
Beispiel #3
0
static int wstr_gsub (lua_State *L) {
    size_t srcl;
    const lua_WChar *src = luaL_checklwstring(L, 1, &srcl);
    const lua_WChar *p = luaL_checkwstring(L, 2);
    int max_s = luaL_optint(L, 4, srcl+1);
    int anchor = (*p == '^') ? (p++, 1) : 0;
    int n = 0;
    WMatchState ms;
    luaL_Buffer b;
    luaL_wbuffinit(L, &b);
    ms.L = L;
    ms.src_init = src;
    ms.src_end = src+srcl;
    while (n < max_s) {
        const lua_WChar *e;
        ms.level = 0;
        e = wmatch(&ms, src, p);
        if (e) {
            n++;
            wadd_value(&ms, &b, src, e);
        }
        if (e && e>src) /* non empty wmatch? */
            src = e;  /* skip it */
        else if (src < ms.src_end)
            luaL_addwchar(&b, *src++);
        else break;
        if (anchor) break;
    }
    luaL_addlwstring(&b, src, ms.src_end-src);
    luaL_pushresult(&b);
    lua_pushinteger(L, n);  /* number of substitutions */
    return 2;
}
Beispiel #4
0
static int wstr_reverse (lua_State *L) {
    size_t l;
    luaL_Buffer b;
    const lua_WChar *s = luaL_checklwstring(L, 1, &l);
    luaL_wbuffinit(L, &b);
    while (l--) luaL_addwchar(&b, s[l]);
    luaL_pushresult(&b);
    return 1;
}
Beispiel #5
0
static void wadd_s (WMatchState *ms, luaL_Buffer *b, const lua_WChar *s,
                    const lua_WChar *e) {
    size_t l, i;
    const lua_WChar *news = lua_tolwstring(ms->L, 3, &l);
    for (i = 0; i < l; i++) {
        if (news[i] != WL_ESC)
            luaL_addwchar(b, news[i]);
        else {
            i++;  /* skip ESC */
            if (!iswdigit(uchar(news[i])))
                luaL_addwchar(b, news[i]);
            else if (news[i] == '0')
                luaL_addlwstring(b, s, e - s);
            else {
                wpush_onecapture(ms, news[i] - '1', s, e);
                luaL_addvalue(b);  /* add capture to accumulated result */
            }
        }
    }
}
Beispiel #6
0
static int wstr_upper (lua_State *L) {
    size_t l;
    size_t i;
    luaL_Buffer b;
    const lua_WChar *s = luaL_checklwstring(L, 1, &l);
    luaL_wbuffinit(L, &b);
    for (i=0; i<l; i++)
        luaL_addwchar(&b, towupper(s[i]));
    luaL_pushresult(&b);
    return 1;
}
Beispiel #7
0
static int wstr_char (lua_State *L) {
    int n = lua_gettop(L);  /* number of arguments */
    int i;
    luaL_Buffer b;
    luaL_wbuffinit(L, &b);
    for (i=1; i<=n; i++) {
        int c = luaL_checkint(L, i);
        luaL_argcheck(L, (lua_WChar)(c) == c, i, "invalid value");
        luaL_addwchar(&b, (lua_WChar)(c));
    }
    luaL_pushresult(&b);
    return 1;
}
Beispiel #8
0
int wwstr_format_helper (luaL_Buffer* b, lua_State *L, int arg) {
    size_t sfl;
    const lua_WChar *strfrmt = luaL_checklwstring(L, arg, &sfl);
    const lua_WChar *strfrmt_end = strfrmt+sfl;
    luaL_wbuffinit(L, b);
    while (strfrmt < strfrmt_end) {
        if (*strfrmt != WL_ESC)
            luaL_addwchar(b, *strfrmt++);
        else if (*++strfrmt == WL_ESC)
            luaL_addwchar(b, *strfrmt++);  /* %% */
        else { /* format item */
            lua_WChar form[MAX_FORMAT];  /* to store the format (`%...') */
            lua_WChar buff[MAX_ITEM];  /* to store the formatted item */
            char _form[MAX_FORMAT];
            char _buff[MAX_ITEM];
            if (iswdigit(*strfrmt) && *(strfrmt+1) == '$')
                return luaL_error(L, "obsolete `format' option (d$)");
            arg++;
            strfrmt = wscanformat(L, strfrmt, form);
            switch (*strfrmt++) {
            case 'c': {
                translate_wide_to_single(form, _form);
                sprintf(_buff, _form, luaL_checkint(L, arg));
                translate_single_to_wide(_buff, buff);
                break;
            }
            case 'd':
            case 'i': {
                waddintlen(form);
                translate_wide_to_single(form, _form);
                sprintf(_buff, _form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
                translate_single_to_wide(_buff, buff);
                break;
            }
            case 'o':
            case 'u':
            case 'x':
            case 'X': {
                //?? How should this be for integers?
                translate_wide_to_single(form, _form);
                sprintf(_buff, _form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
                translate_single_to_wide(_buff, buff);
                break;
            }
            case 'e':
            case 'E':
            case 'f':
            case 'g':
            case 'G': {
                translate_wide_to_single(form, _form);
                sprintf(_buff, _form, luaL_checknumber(L, arg));
                translate_single_to_wide(_buff, buff);
                break;
            }
            case 'q': {
                luaI_waddquoted(L, b, arg);
                continue;  /* skip the 'addsize' at the end */
            }
            case 's': {
                size_t l;
                const lua_WChar *s = luaL_checklwstring(L, arg, &l);
                (void)s;
                if (!lua_WChar_chr(form, '.') && 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 {
                    assert(0);
//			swprintf((wchar_t*)buff, (wchar_t*)form, s);
                    break;
                }
            }
            case 'b':
            {
                buff[1] = buff[2] = buff[3] = buff[4] = buff[5] = buff[6] = buff[7] = buff[8] = 0;
                switch (*strfrmt++)
                {
                case 'b': {
                    unsigned int num = (unsigned int)luaL_checkint(L, arg);
                    buff[0] = (unsigned char)num;
                    luaL_addlwstring(b, buff, 1);
                    break;
                }
                case 'd': {
                    unsigned int num = (unsigned int)luaL_checkint(L, arg);
                    *(unsigned int*)(&buff) = num;
                    luaL_addlwstring(b, buff, 4);
                    break;
                }
                case 'w': {
                    unsigned int num = (unsigned int)luaL_checkint(L, arg);
                    *(unsigned short*)(&buff) = (unsigned short)num;
                    luaL_addlwstring(b, buff, 2);
                    break;
                }
                case 'f': {
                    float numF = (float)luaL_checknumber(L, arg);
                    *(float*)(&buff) = numF;
                    luaL_addlwstring(b, buff, 4);
                    break;
                }
                case 'F': {
                    double numD = (double)luaL_checknumber(L, arg);
                    *(double*)(&buff) = numD;
                    luaL_addlwstring(b, buff, 8);
                    break;
                }

                default:
                    break;
                }
                buff[0] = 0;

                break;
            }
            }
            luaL_addlwstring(b, buff, lua_WChar_len(buff));
        }
    }
    return 1;
}
Beispiel #9
0
static int wstr_lualex(lua_State *L) {
    size_t l = 0;
    const lua_WChar *str = luaL_checklwstring(L, 1, &l);
    int isWide = luaL_checkint(L, 2) != 0;
    size_t i;
    luaL_Buffer b;
    luaL_wbuffinit(L, &b);

    for (i = 0; i < l; ++i)
    {
//	int needUnicodeZero = 1;

        switch (str[i])
        {
        case '\\':
            ++i;
            switch (str[i]) {
            case 'a':
                luaL_addwchar(&b, '\a');
                break;
            case 'b':
                luaL_addwchar(&b, '\b');
                break;
            case 'f':
                luaL_addwchar(&b, '\f');
                break;
            case 'n':
                luaL_addwchar(&b, '\n');
                break;
            case 'r':
                luaL_addwchar(&b, '\r');
                break;
            case 't':
                luaL_addwchar(&b, '\t');
                break;
            case 'v':
                luaL_addwchar(&b, '\v');
                break;
            case 'x': {
                int ch;
                ++i;
                ch = tolower(str[i]);
                if (!isdigit(ch) && !(ch >= 'a' && ch <= 'f') )
                {
                    --i;
                    luaL_addwchar(&b, 'x');
                }
                else {  /* \xxx */
                    size_t start = i;
                    int c = 0;
                    size_t numDigits = isWide ? 4 : 2;
                    do {
                        ch = towlower(str[i]);
                        if (iswdigit((wint_t)ch))
                            c = 16*c + (ch-'0');
                        else if (ch >= 'a' && ch <= 'f')
                            c = 16*c + (ch-'a') + 10;
                        ++i;
                        ch = towlower(str[i]);
                    } while (i - start < numDigits && (iswdigit((wint_t)ch) || (ch >= 'a' && ch <= 'f')));
                    luaL_addwchar(&b, c);
                    --i;
//				  needUnicodeZero = 0;
                }
                break;
            }
            default: {
                if (!iswdigit(str[i]))
                    luaL_addwchar(&b, str[i]);
                else {  /* \xxx */
                    int c = 0;
                    int count = 0;
                    do {
                        c = 10*c + (str[i]-'0');
                        ++i;
                    } while (++count<3 && iswdigit(str[i]));
                    luaL_addwchar(&b, c);
                }
            }
            }
            break;
        default:
            luaL_addwchar(&b, str[i]);
        }
    }

    luaL_pushresult(&b);
    return 1;
}
Beispiel #10
0
LUALIB_API void luaL_addlwstring (luaL_Buffer *B, const lua_WChar *s, size_t l) {
  while (l--)
    luaL_addwchar(B, *s++);
}
Beispiel #11
0
static void luaI_addquotedbinary (lua_State *L, luaL_Buffer *b, int arg) {
  size_t l;
  if (lua_type(L, arg) == LUA_TWSTRING)
  {
	  const lua_WChar *s = luaL_checklwstring(L, arg, &l);
	  luaL_addwchar(b, 'L');
	  luaL_addwchar(b, '"');
	  while (l--) {
		switch (*s) {
		  case '"':  case '\\':
			luaL_addwchar(b, '\\');
			luaL_addwchar(b, *s);
			break;
			case '\a':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'a');		break;
			case '\b':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'b');		break;
			case '\f':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'f');		break;
			case '\n':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'n');		break;
			case '\r':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'r');		break;
			case '\t':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 't');		break;
			case '\v':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'v');		break;
			default:
				if (*s < 256  &&  isprint((unsigned char)*s))
				{
					luaL_addwchar(b, *s);
				}
				else
				{
					char str[10];
					lua_WChar wstr[10];
					sprintf(str, "\\x%04x", (unsigned long)*s);
					translate_single_to_wide(str, wstr);
					luaL_addwstring(b, wstr);
				}
		}
		s++;
	  }
	  luaL_addwchar(b, '"');
  }
  else
  {
	  const char *s = luaL_checklstring(L, arg, &l);
	  luaL_addwchar(b, '"');
	  while (l--) {
		switch (*s) {
		  case '"':  case '\\':
			luaL_addwchar(b, '\\');
			luaL_addwchar(b, *s);
			break;
			case '\a':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'a');		break;
			case '\b':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'b');		break;
			case '\f':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'f');		break;
			case '\n':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'n');		break;
			case '\r':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'r');		break;
			case '\t':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 't');		break;
			case '\v':		luaL_addwchar(b, '\\');  luaL_addwchar(b, 'v');		break;
			default:
				if (isprint((unsigned char)*s))
				{
					luaL_addwchar(b, *s);
				}
				else
				{
					char str[10];
					lua_WChar wstr[10];
					sprintf(str, "\\x%02x", (unsigned int)*s);
					translate_single_to_wide(str, wstr);
					luaL_addwstring(b, wstr);
				}
		}
		s++;
	  }
	  luaL_addwchar(b, '"');
  }
}