summaryrefslogtreecommitdiff
path: root/core/src/xmake/utf8/char.c
blob: 760dab264eedca1e11701afbe1b37257c4af71e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*!A cross-platform build utility based on Lua
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (C) 2015-present, Xmake Open Source Community.
 *
 * @author      ruki
 * @file        char.c
 *
 */

/* //////////////////////////////////////////////////////////////////////////////////////
 * includes
 */
#include "utf8.h"

/* //////////////////////////////////////////////////////////////////////////////////////
 * private implementation
 */

static void xm_utf8_char_push(lua_State *lua, tb_int_t arg) {
    lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(lua, arg);
    luaL_argcheck(lua, code <= XM_UTF8_MAXUTF, arg, "value out of range");
    
    tb_char_t buf[8];
    tb_size_t n = xm_utf8_encode(buf, (xm_utf8_int_t)code);
    if (n > 0) {
        lua_pushlstring(lua, buf, n);
    } else {
        luaL_error(lua, "value out of range");
    }
}

/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */

/* utfchar(n1, n2, ...)  -> char(n1)..char(n2)...
 */
tb_int_t xm_utf8_char(lua_State *lua) {
    tb_assert_and_check_return_val(lua, 0);

    tb_int_t n = lua_gettop(lua);  // number of arguments
    if (n == 1) { // optimize common case of single char
        xm_utf8_char_push(lua, 1);
    } else {
        tb_int_t i;
        luaL_Buffer b;
        luaL_buffinit(lua, &b);
        for (i = 1; i <= n; i++) {
            xm_utf8_char_push(lua, i);
            luaL_addvalue(&b);
        }
        luaL_pushresult(&b);
    }
    return 1;
}