https://www.mediawiki.org/wiki/Special:Code/MediaWiki/115002
Revision: 115002
Author: vvv
Date: 2012-04-22 01:23:14 +0000 (Sun, 22 Apr 2012)
Log Message:
-----------
ustring API niceties I found useful while writing the Lua version of it:
* Make pos() return false if substring is not found. That makes sense since even
zero is true in Lua.
* Implement tostring() interface to convert ustrings to stirngs.
Modified Paths:
--------------
trunk/php/luasandbox/ustring.c
Modified: trunk/php/luasandbox/ustring.c
===================================================================
--- trunk/php/luasandbox/ustring.c 2012-04-21 19:06:28 UTC (rev 115001)
+++ trunk/php/luasandbox/ustring.c 2012-04-22 01:23:14 UTC (rev 115002)
@@ -31,6 +31,7 @@
int luasandbox_ustr_concat(lua_State * L);
int luasandbox_ustr_eq(lua_State * L);
int luasandbox_ustr_index(lua_State * L);
+int luasandbox_ustr_tostring(lua_State * L);
int luasandbox_ustr_ucfirst(lua_State * L);
int luasandbox_ustr_uc(lua_State * L);
@@ -82,6 +83,10 @@
lua_pushcfunction( L, luasandbox_ustr_index );
lua_rawset( L, -3 );
+ lua_pushstring( L, "__tostring" );
+ lua_pushcfunction( L, luasandbox_ustr_tostring );
+ lua_rawset( L, -3 );
+
lua_pushcfunction( L, luasandbox_ustr_create );
lua_setglobal( L, "u" );
@@ -355,7 +360,7 @@
luasandbox_ustr_header *str;
uint8_t *raw;
- str = luaL_checkudata( L, 1, "luasandbox_ustr" );
+ str = luasandbox_checkustring( L, 1 );
raw = LUASANDBOX_USTR_RAW(str);
if( lua_type( L, 2 ) == LUA_TNUMBER ) {
@@ -390,6 +395,24 @@
}
/* }}} */
+/** {{{ luasandbox_ustr_tostring
+ *
+ * Lua function providing the tostring() interface.
+ * Returns the UTF-8 version of the ustring.
+ */
+int luasandbox_ustr_tostring(lua_State * L)
+{
+ luasandbox_ustr_header *str;
+ uint8_t *raw;
+
+ str = luasandbox_checkustring( L, 1 );
+ raw = LUASANDBOX_USTR_RAW(str);
+
+ lua_pushlstring( L, raw, str->raw_len );
+ return 1;
+}
+/* }}} */
+
/****************** Library ******************/
/** {{{ luasandbox_ustr_ucfirst
@@ -828,7 +851,7 @@
lua_pushinteger( L, result.cp_index + 1 );
return 1;
case UTF8_SEARCH_STATUS_NOTFOUND:
- lua_pushinteger( L, -1 );
+ lua_pushboolean( L, 0 );
return 1;
}
}
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs