File: Utf8.hx

package info (click to toggle)
haxe 1%3A4.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 40,556 kB
  • sloc: ml: 125,171; ansic: 2,408; makefile: 436; java: 362; cs: 323; cpp: 318; python: 316; sh: 75; objc: 64; php: 39; xml: 30; javascript: 11
file content (106 lines) | stat: -rw-r--r-- 3,513 bytes parent folder | download | duplicates (4)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (C)2005-2019 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 neko;

@:coreApi
class Utf8 {
	var __b:Dynamic;

	public function new(?size:Int):Void {
		__b = utf8_buf_alloc(if (size == null) 1 else size);
	}

	public function addChar(c:Int):Void {
		utf8_buf_add(__b, c);
	}

	public function toString():String {
		return new String(utf8_buf_content(__b));
	}

	public static function encode(s:String):String {
		s = untyped s.__s;
		var sl = untyped __dollar__ssize(s);
		var buf:Dynamic = utf8_buf_alloc(sl);
		var i = 0;
		while (i < sl) {
			utf8_buf_add(buf, untyped __dollar__sget(s, i));
			i += 1;
		}
		return new String(utf8_buf_content(buf));
	}

	public static function decode(s:String):String {
		s = untyped s.__s;
		var sl = untyped __dollar__ssize(s);
		var ret = untyped __dollar__smake(sl);
		var i = 0;
		utf8_iter(s, function(c) {
			if (c == 8364) // euro symbol
				c = 164;
			else if (c == 0xFEFF) // BOM
				return;
			else if (c > 255)
				throw "Utf8::decode invalid character (" + c + ")";
			untyped __dollar__sset(ret, i, c);
			i += 1;
		});
		return new String(untyped __dollar__ssub(ret, 0, i));
	}

	public static function iter(s:String, chars:Int->Void):Void {
		utf8_iter(untyped s.__s, chars);
	}

	public static function charCodeAt(s:String, index:Int):Int {
		return utf8_get(untyped s.__s, index);
	}

	public static function validate(s:String):Bool {
		return utf8_validate(untyped s.__s);
	}

	public static function length(s:String):Int {
		return utf8_length(untyped s.__s);
	}

	public static function compare(a:String, b:String):Int {
		return utf8_compare(untyped a.__s, untyped b.__s);
	}

	public static function sub(s:String, pos:Int, len:Int):String {
		return new String(utf8_sub(untyped s.__s, pos, len));
	}

	static var utf8_buf_alloc = neko.Lib.load("std", "utf8_buf_alloc", 1);
	static var utf8_buf_add = neko.Lib.load("std", "utf8_buf_add", 2);
	static var utf8_buf_content = neko.Lib.load("std", "utf8_buf_content", 1);
	static var utf8_buf_length = neko.Lib.load("std", "utf8_buf_length", 1);
	static var utf8_iter = neko.Lib.load("std", "utf8_iter", 2);

	static var utf8_get = neko.Lib.load("std", "utf8_get", 2);
	static var utf8_validate = neko.Lib.load("std", "utf8_validate", 1);
	static var utf8_length = neko.Lib.load("std", "utf8_length", 1);
	static var utf8_compare = neko.Lib.load("std", "utf8_compare", 2);
	static var utf8_sub = neko.Lib.load("std", "utf8_sub", 3);
}