| 1 | extern "C" { |
| 2 | # include <lua.h> |
| 3 | # include <lauxlib.h> |
| 4 | } |
| 5 | |
| 6 | #include <cstring> |
| 7 | |
| 8 | #include <unicode/brkiter.h> |
| 9 | #include <unicode/unistr.h> |
| 10 | |
| 11 | using namespace icu; |
| 12 | |
| 13 | static int |
| 14 | _handle_error(lua_State *L, UErrorCode error) |
| 15 | { |
| 16 | lua_pushnil(L); |
| 17 | lua_pushstring(L, u_errorName(error)); |
| 18 | return 2; |
| 19 | } |
| 20 | |
| 21 | static int |
| 22 | luaunicode_length(lua_State *L) throw () |
| 23 | { |
| 24 | UErrorCode error = U_ZERO_ERROR; |
| 25 | UText text = UTEXT_INITIALIZER; |
| 26 | const char *utf8bytes = luaL_checkstring(L, 1); |
| 27 | |
| 28 | utext_openUTF8(&text, utf8bytes, -1, &error); |
| 29 | |
| 30 | if(U_FAILURE(error)) { |
| 31 | return _handle_error(L, error); |
| 32 | } |
| 33 | |
| 34 | BreakIterator *i = BreakIterator::createCharacterInstance(Locale::getUS(), error); |
| 35 | |
| 36 | if(U_FAILURE(error)) { |
| 37 | utext_close(&text); |
| 38 | return _handle_error(L, error); |
| 39 | } |
| 40 | |
| 41 | i->setText(&text, error); |
| 42 | |
| 43 | if(U_FAILURE(error)) { |
| 44 | delete i; |
| 45 | utext_close(&text); |
| 46 | return _handle_error(L, error); |
| 47 | } |
| 48 | |
| 49 | int length = 0; |
| 50 | int32_t p = i->first(); |
| 51 | for(int32_t p = i->first(); p != BreakIterator::DONE; p = i->next()) { |
| 52 | length++; |
| 53 | } |
| 54 | length--; // NUL byte appears to be counted |
| 55 | |
| 56 | lua_pushinteger(L, length); |
| 57 | |
| 58 | delete i; |
| 59 | utext_close(&text); |
| 60 | |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | static int |
| 65 | luaunicode_sub(lua_State *L) throw () |
| 66 | { |
| 67 | UErrorCode error = U_ZERO_ERROR; |
| 68 | UText text = UTEXT_INITIALIZER; |
| 69 | const char *utf8bytes = luaL_checkstring(L, 1); |
| 70 | int start = luaL_checkinteger(L, 2); |
| 71 | int end = lua_tointeger(L, 3); |
| 72 | size_t start_byte; |
| 73 | size_t end_byte; |
| 74 | |
| 75 | end_byte = strlen(utf8bytes); |
| 76 | |
| 77 | if(start <= 0 || end < 0) { |
| 78 | return luaL_error(L, "unicode.sub: start and end must be greater than zero"); |
| 79 | } |
| 80 | if(end != 0 && start > end) { |
| 81 | return luaL_error(L, "unicode.sub: start must be less than or equal to end"); |
| 82 | } |
| 83 | |
| 84 | utext_openUTF8(&text, utf8bytes, -1, &error); |
| 85 | |
| 86 | if(U_FAILURE(error)) { |
| 87 | return _handle_error(L, error); |
| 88 | } |
| 89 | |
| 90 | BreakIterator *i = BreakIterator::createCharacterInstance(Locale::getUS(), error); |
| 91 | |
| 92 | if(U_FAILURE(error)) { |
| 93 | utext_close(&text); |
| 94 | return _handle_error(L, error); |
| 95 | } |
| 96 | |
| 97 | i->setText(&text, error); |
| 98 | |
| 99 | if(U_FAILURE(error)) { |
| 100 | delete i; |
| 101 | utext_close(&text); |
| 102 | return _handle_error(L, error); |
| 103 | } |
| 104 | |
| 105 | for(int32_t pos = 1, p = i->first(); p != BreakIterator::DONE; p = i->next(), pos++) { |
| 106 | if(pos == start) { |
| 107 | start_byte = p; |
| 108 | if(! end) { |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | if(end && pos == end + 1) { |
| 113 | end_byte = p; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | lua_pushlstring(L, utf8bytes + start_byte, end_byte - start_byte); |
| 118 | delete i; |
| 119 | utext_close(&text); |
| 120 | |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | static luaL_Reg luaunicode_functions[] = { |
| 125 | { "length", luaunicode_length }, |
| 126 | { "sub", luaunicode_sub }, |
| 127 | |
| 128 | { NULL, NULL } |
| 129 | }; |
| 130 | |
| 131 | extern "C" int |
| 132 | luaopen_obvious_lib_unicode_native(lua_State *L) |
| 133 | { |
| 134 | #if LUA_VERSION_NUM >= 502 |
| 135 | luaL_newlib(L, luaunicode_functions); |
| 136 | #else |
| 137 | lua_newtable(L); |
| 138 | luaL_register(L, NULL, luaunicode_functions); |
| 139 | #endif |
| 140 | |
| 141 | return 1; |
| 142 | } |