Updated slnunico package; made its utf8 flavor default

This commit is contained in:
Yevgen Muntyan 2011-10-22 00:21:56 -07:00
parent 4282d05180
commit a8419796b4
6 changed files with 36 additions and 21 deletions

1
THANKS
View File

@ -21,6 +21,7 @@ Kenneth Prugh
Kris Van Bruwaene
LoneFox
Lontronics
Malete Partner
Marco Barisione
Matthias Clasen
Nick Treleaven

View File

@ -88,7 +88,7 @@ local function testlen (str,bytes,codes,chars)
chars = chars or codes
return check(sprintf("len '%s'", str),
sprintf("%d/%d/%d", bytes, codes, chars),
sprintf("%d/%d/%d", string.len(str), utf8.len(str), unicode.grapheme.len(str)))
sprintf("%d/%d/%d", unicode.ascii.len(str), utf8.len(str), unicode.grapheme.len(str)))
end
-- 176 = 00B0;DEGREE SIGN -- UTF-8: C2,B0 = \194\176
@ -120,10 +120,11 @@ local function testbyte (ctype, ok, str, ...)
return checka(sprintf("%s.byte('%s',%s)",ctype,str,table.concat({...}, ",")),
ok, unicode[ctype].byte(str, ...))
end
testbyte("string","194,176","Ä°Ö",3,4) -- the UTF-8 seq for °
testbyte("ascii","194,176","Ä°Ö",3,4)
testbyte("ascii","194,176","Ä°Ö",3,4) -- the UTF-8 seq for °
testbyte("utf8","176,214","Ä°Ö",2,3) -- code points for °,Ö
testbyte("string","176,214","Ä°Ö",2,3) -- code points for °,Ö
testbyte("utf8","65,776","\204\136A\204\136O\204\136",2,3) -- decomposed
testbyte("string","65,776","\204\136A\204\136O\204\136",2,3) -- decomposed
testbyte("grapheme","65,776","\204\136A\204\136O\204\136",2) -- decomposed

View File

@ -1,5 +1,7 @@
require("munit")
local ascii = require("unicode.ascii")
local strings = {
{ "", "", "", 0, 0 },
{ "a", "A", "a", 1, 1 },
@ -12,21 +14,21 @@ local strings = {
{ "Мама", "МАМА", "мама", 8, 4 },
}
local test_ustring_one = function(case, ascii)
local test_ustring_one = function(case)
s = case[1]
upper = case[2]
lower = case[3]
byte_len = case[4]
char_len = case[5]
tassert(utf8.upper(s) == upper, '%q:upper() is %q, expected %q', s, utf8.upper(s), upper)
tassert(utf8.lower(s) == lower, '%q:lower() is %q, expected %q', s, utf8.lower(s), lower)
tassert(string.upper(s) == upper, '%q:upper() is %q, expected %q', s, string.upper(s), upper)
tassert(string.lower(s) == lower, '%q:lower() is %q, expected %q', s, string.lower(s), lower)
tassert(utf8.len(s) == char_len, 'utf8.len(%q) is %d, expected %d', s, utf8.len(s), char_len)
tassert(s:len() == byte_len, '%q:len() is %d, expected %d', s, s:len(), byte_len)
tassert(#s == s:len(), '#%q != %q:len()', s, s)
tassert(string.len(s) == char_len, 'utf8.len(%q) is %d, expected %d', s, string.len(s), char_len)
tassert(ascii.len(s) == byte_len, '%q:len() is %d, expected %d', s, ascii.len(s), byte_len)
--tassert(#s == s:len(), '#%q != %q:len()', s, s)
end
for index, case in pairs(strings) do
test_ustring_one(case, true)
test_ustring_one(case)
end

View File

@ -22,7 +22,8 @@ static const luaL_Reg lualibs[] = {
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
/*{LUA_STRLIBNAME, luaopen_string},*/
{SLN_UNICODENAME, luaopen_unicode},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_DBLIBNAME, luaopen_debug},
{"lfs", luaopen_lfs},

View File

@ -791,4 +791,8 @@ union luai_Cast { double l_d; long l_l; };
#define L_GNUC_NO_INSTRUMENT
#endif /* !__GNUC__ */
#define SLNUNICODE_AS_STRING 1
#define STRING_WITH_METAT
#define SLN_UNICODENAME "unicode"
#endif

View File

@ -1,7 +1,7 @@
/*
* Selene Unicode/UTF-8
* This additions
* Copyright (c) 2005 Malete Partner, Berlin, partner@malete.org
* Copyright (c) 2005-2011 Malete Partner, Berlin, partner@malete.org
* Available under "Lua 5.0 license", see http://www.lua.org/license.html#5
* $Id: slnunico.c,v 1.5 2006/07/26 17:20:04 paul Exp $
*
@ -1301,6 +1301,7 @@ static const luaL_reg uniclib[] = {
{NULL, NULL}
};
#if defined( SLNUNICODE_AS_STRING ) && defined( STRING_WITH_METAT )
static void createmetatable (lua_State *L) {
lua_newtable(L); /* create metatable for strings */
lua_pushliteral(L, ""); /* dummy string */
@ -1311,30 +1312,34 @@ static void createmetatable (lua_State *L) {
lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */
lua_pop(L, 1); /* pop metatable */
}
#endif
/*
** Open string library
*/
LUALIB_API int luaopen_string (lua_State *L) {
LUALIB_API int luaopen_unicode (lua_State *L) {
/* register unicode itself so require("unicode") works */
luaL_register(L, SLN_UNICODENAME,
uniclib + (sizeof uniclib/sizeof uniclib[0] - 1)); /* empty func list */
lua_pop(L, 1);
/* lua_pop(L, 1); http://lua-users.org/lists/lua-l/2007-11/msg00070.html */
lua_pushinteger(L, MODE_ASCII);
luaI_openlib(L, SLN_UNICODENAME ".ascii", uniclib, 1);
#if defined(LUA_COMPAT_GFIND)
lua_getfield(L, -1, "gmatch");
lua_setfield(L, -2, "gfind");
#endif
createmetatable(L);
lua_setfield(L, LUA_GLOBALSINDEX, "string");
lua_pushinteger(L, MODE_LATIN);
luaI_openlib(L, SLN_UNICODENAME ".latin1", uniclib, 1);
lua_pushinteger(L, MODE_GRAPH);
luaI_openlib(L, SLN_UNICODENAME ".grapheme", uniclib, 1);
lua_pushinteger(L, MODE_UTF8);
luaI_openlib(L, SLN_UNICODENAME ".utf8", uniclib, 1);
lua_setfield(L, LUA_GLOBALSINDEX, "utf8");
#ifdef SLNUNICODE_AS_STRING
#if defined(LUA_COMPAT_GFIND)
lua_getfield(L, -1, "gmatch");
lua_setfield(L, -2, "gfind");
#endif
#ifdef STRING_WITH_METAT
createmetatable(L);
#endif
lua_setfield(L, LUA_GLOBALSINDEX, "string");
#endif
#ifdef WANT_EXT_MATCH
{
unsigned i;
@ -1355,6 +1360,7 @@ LUALIB_API int luaopen_string (lua_State *L) {
}
}
#endif
lua_settop(L, 2); /* http://lua-users.org/lists/lua-l/2007-11/msg00070.html */
return 1;
}