Ver Fonte

Lua: Wip on utf8 extern supporting default 5.3 impl, and luautf8 lib < 5.3

Justin Donaldson há 9 anos atrás
pai
commit
e9828517c0
2 ficheiros alterados com 141 adições e 0 exclusões
  1. 30 0
      std/lua/Utf8.hx
  2. 111 0
      std/lua/_std/haxe/Utf8.hx

+ 30 - 0
std/lua/Utf8.hx

@@ -0,0 +1,30 @@
+package lua;
+#if (lua_ver > 53)
+@native("_G.utf8")
+#else
+@:luaRequire("lua-utf8")
+#end
+extern class  Utf8 {
+	public static function escape(str:String) : String;
+	public static function charpos(str:String, charpos:Int, offset:Int) : Int; //and Int
+	public static function next(str:String, charpos:Int, offset:Int) : Int; //and Int
+	public static function insert(str:String, idx:Int, substring:String) : String;
+	public static function remove(str:String, start:Int, stop:Int) : String;
+	public static function width(str:String, ambi_is_double:Bool, default_width:Int) : Int;
+	public static function widthindex(str:String, location:Int, ambi_is_double:Bool, default_width:Int) : Int;
+	public static function title(str:String) : String;
+	public static function fold(str:String) : String;
+	public static function nbasecmp(a:String, b:String) : Table<Int,Int>;
+	public static function byte(str:String, pos:Int) : Int;
+	public static function char(codes :haxe.extern.Rest<Int>) : String;
+	public static function len(str:String) : Int;
+	// public static function find() : String;
+	// public static function gmatch() : String;
+	// public static function gsub() : String;
+	// public static function len() : String;
+	// public static function lower() : String;
+	// public static function match() : String;
+	// public static function reverse() : String;
+	public static function sub(str:String, start:Int, end:Int) : String;
+	// public static function upper() : String;
+}

+ 111 - 0
std/lua/_std/haxe/Utf8.hx

@@ -0,0 +1,111 @@
+/*
+ * Copyright (C)2005-2016 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package haxe;
+
+/**
+  A Lua-specific implementation of Utf8, using a helper library.
+**/
+
+class Utf8 {
+
+	var __b : String;
+
+	/**
+		Allocate a new Utf8 buffer using an optional bytes size.
+	**/
+	public function new( ?size : Int ) {
+		__b = "";
+	}
+
+	/**
+		Add the given UTF8 character code to the buffer.
+	**/
+	public inline function addChar( c : Int ) : Void {
+		__b =  lua.Utf8.insert(__b, lua.Utf8.len(__b)+1, lua.Utf8.char(c));
+	}
+
+	/**
+		Returns the buffer converted to a String;
+	**/
+	public inline function toString() : String {
+		return __b;
+	}
+
+	/**
+		Call the `chars` function for each UTF8 char of the string.
+	**/
+	public static function iter( s : String, chars : Int -> Void ) {
+		for( i in 0...s.length )
+			chars(s.charCodeAt(i));
+	}
+
+	/**
+		Encode the input ISO string into the corresponding UTF8 one.
+	**/
+	public static function encode( s : String ) : String {
+		throw "Not implemented";
+	}
+
+	/**
+		Decode an UTF8 string back to an ISO string.
+		Throw an exception if a given UTF8 character is not supported by the decoder.
+	**/
+	public static function decode( s : String ) : String {
+		throw "Not implemented";
+	}
+
+	/**
+		Similar to `String.charCodeAt` but uses the UTF8 character position.
+	**/
+	public static inline function charCodeAt( s : String, index : Int ) : Int {
+		return lua.Utf8.byte(s,index+1);
+	}
+
+	/**
+		Tells if the String is correctly encoded as UTF8.
+	**/
+	public static inline function validate( s : String ) : Bool {
+		return true;
+	}
+
+	/**
+		Returns the number of UTF8 chars of the String.
+	**/
+	public static inline function length( s : String ) : Int {
+		return lua.Utf8.len(s);
+	}
+
+	/**
+		Compare two UTF8 strings, character by character.
+	**/
+	public static function compare( a : String, b : String ) : Int {
+		return a > b ? 1 : (a == b ? 0 : -1);
+	}
+
+	/**
+		This is similar to `String.substr` but the `pos` and `len` parts are considering UTF8 characters.
+	**/
+	public static inline function sub( s : String, pos : Int, len : Int ) : String {
+		return lua.Utf8.sub(s,pos+1,pos+len);
+	}
+
+}