File: String.hx

package info (click to toggle)
haxe 1%3A4.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 42,984 kB
  • sloc: ml: 132,401; ansic: 2,403; makefile: 436; java: 365; cs: 336; cpp: 318; python: 315; sh: 75; objc: 64; php: 45; xml: 30; javascript: 11
file content (169 lines) | stat: -rw-r--r-- 4,637 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * 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.
 */

import lua.Lua;
import lua.Table;
import lua.Boot;

#if lua_vanilla
private typedef BaseString = lua.NativeStringTools;
#else
private typedef BaseString = lua.lib.luautf8.Utf8;
#end

@:coreApi
class String {
	static var __oldindex:String->String->Dynamic;

	public var length(default, null):Int;

	public inline function new(string:String)
		untyped {}

	@:keep
	static function __index(s:Dynamic, k:Dynamic):Dynamic {
		if (k == "length")
			return BaseString.len(s);
		else if (Reflect.hasField(untyped String.prototype, k))
			return untyped String.prototype[k];
		else if (__oldindex != null) {
			if (Lua.type(__oldindex) == "function") {
				return __oldindex(s, k);
			} else if (Lua.type(__oldindex) == "table") {
				return untyped __oldindex[k];
			}
			return null;
		} else
			return null;
	}

	public inline function toUpperCase():String
		return BaseString.upper(this);

	public inline function toLowerCase():String
		return BaseString.lower(this);

	public inline function indexOf(str:String, ?startIndex:Int):Int {
		if (startIndex == null)
			startIndex = 1;
		else
			startIndex += 1;
		if (str == "") {
			return indexOfEmpty(this, startIndex - 1);
		}
		var r = BaseString.find(this, str, startIndex, true).begin;
		if (r != null && r > 0)
			return r - 1;
		else
			return -1;
	}

	static function indexOfEmpty(s:String, startIndex:Int):Int {
		var length = BaseString.len(s);
		if(startIndex < 0) {
			startIndex = length + startIndex;
			if(startIndex < 0) startIndex = 0;
		}
		return startIndex > length ? length : startIndex;
	}

	public inline function lastIndexOf(str:String, ?startIndex:Int):Int {
		var ret = -1;
		if (startIndex == null)
			startIndex = length;
		while (true) {
			var p = indexOf(str, ret + 1);
			if (p == -1 || p > startIndex || p == ret)
				break;
			ret = p;
		}
		return ret;
	}

	public inline function split(delimiter:String):Array<String> {
		var idx = 1;
		var ret = [];
		while (idx != null) {
			var newidx = 0;
			if (delimiter.length > 0) {
				newidx = BaseString.find(this, delimiter, idx, true).begin;
			} else if (idx >= this.length) {
				newidx = null;
			} else {
				newidx = idx + 1;
			}

			if (newidx != null) {
				var match = BaseString.sub(this, idx, newidx - 1).match;
				ret.push(match);
				idx = newidx + delimiter.length;
			} else {
				ret.push(BaseString.sub(this, idx, this.length).match);
				idx = null;
			}
		}
		return ret;
	}

	public inline function toString():String {
		return this;
	}

	public inline function substring(startIndex:Int, ?endIndex:Int):String {
		if (endIndex == null)
			endIndex = this.length;
		if (endIndex < 0)
			endIndex = 0;
		if (startIndex < 0)
			startIndex = 0;
		if (endIndex < startIndex) {
			// swap the index positions
			return BaseString.sub(this, endIndex + 1, startIndex).match;
		} else {
			return BaseString.sub(this, startIndex + 1, endIndex).match;
		}
	}

	public inline function charAt(index:Int):String {
		return BaseString.sub(this, index + 1, index + 1).match;
	}

	public inline function charCodeAt(index:Int):Null<Int> {
		return BaseString.byte(this, index + 1);
	}

	public inline function substr(pos:Int, ?len:Int):String {
		if (len == null || len > pos + this.length)
			len = this.length;
		else if (len < 0)
			len = length + len;
		if (pos < 0)
			pos = length + pos;
		if (pos < 0)
			pos = 0;
		return BaseString.sub(this, pos + 1, pos + len).match;
	}

	public inline static function fromCharCode(code:Int):String {
		return BaseString.char(code);
	}
}