File: StringExt.hx

package info (click to toggle)
haxe 1%3A3.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 23,464 kB
  • ctags: 9,612
  • sloc: ml: 83,200; ansic: 1,724; makefile: 473; java: 349; cs: 314; python: 250; sh: 43; cpp: 39; xml: 25
file content (218 lines) | stat: -rw-r--r-- 5,609 bytes parent folder | download
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (C)2005-2012 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 cs.internal;
import cs.internal.Function;
private typedef NativeString = cs.system.String;

@:keep @:nativeGen @:native("haxe.lang.StringExt") class StringExt
{
	@:readOnly static var empty(default,never) = new NativeString(cast 0,0);

	public static function charAt(me:NativeString, index:Int):NativeString
	{
		if (cast(index,UInt) >= me.Length)
			return empty;
		else
			return new NativeString(me[index], 1);
	}

	public static function charCodeAt(me:NativeString, index:Int):Null<Int>
	{
		if (cast(index,UInt) >= me.Length)
			return null;
		else
			return cast(me[index], Int);
	}

	public static function indexOf(me:NativeString, str:String, ?startIndex:Int):Int
	{
		var sIndex:Int = startIndex != null ? startIndex : 0;
		if (sIndex >= me.Length)
			return -1;
		return @:privateAccess me.IndexOf(str, sIndex, cs.system.StringComparison.Ordinal);
	}

	public static function lastIndexOf(me:NativeString, str:NativeString, ?startIndex:Int):Int
	{
		var sIndex:Int = startIndex == null ? me.Length - 1 : startIndex;
		if (sIndex >= me.Length)
			sIndex = me.Length - 1;
		else if (sIndex < 0)
			return -1;

		//TestBaseTypes.hx@133 fix
		if (startIndex != null)
		{
			var i = sIndex + 1;
			while (i --> 0)
			{
				var found = true;
				for (j in 0...str.Length)
				{
					if(me[i + j] != str[j])
					{
						found = false;
						break;
					}
				}

				if (found)
					return i;
			}

			return -1;
		} else {
			return me.LastIndexOf(untyped str, sIndex, cs.system.StringComparison.Ordinal);
		}
		return -1;
	}

	public static function split(me:NativeString, delimiter:NativeString):Array<String>
	{
		var native:NativeArray<String>;
		if (delimiter.Length == 0)
		{
			var len = me.Length;
			native = new NativeArray(len);
			for (i in 0...len)
				native[i] = untyped new NativeString(me[i],1);
		} else {
			var str = new NativeArray<String>(1);
			str[0] = cast delimiter;

			native = me.Split(str, cs.system.StringSplitOptions.None);
		}

		return cs.Lib.array(native);
	}

	public static function substr(me:NativeString, pos:Int, ?len:Int):String
	{
		var meLen = me.Length;
		var targetLen = meLen;
		if (len != null)
		{
			targetLen = len;
			if (targetLen == 0 || (pos != 0 && targetLen < 0))
				return "";
		}

		if (pos < 0)
		{
			pos = meLen + pos;
			if (pos < 0) pos = 0;
		} else if (targetLen < 0) {
			targetLen = meLen + targetLen - pos;
		}

		if (pos + targetLen > meLen)
		{
			targetLen = meLen - pos;
		}

		if (pos < 0 || targetLen <= 0) return "";

		return me.Substring(pos, targetLen);
	}

	public static function substring(me:NativeString, startIndex:Int, ?endIndex:Int):String
	{
		var len = me.Length;
		var endIdx:Int;
		if (endIndex == null)
			endIdx = len;
		else if ( (endIdx = endIndex) < 0 )
			endIdx = 0;
		else if (endIdx > len)
			endIdx = len;

		if (startIndex < 0)
			startIndex = 0;
		else if (startIndex > len)
			startIndex = len;

		if (startIndex > endIdx)
		{
			var tmp = startIndex;
			startIndex = endIdx;
			endIdx = tmp;
		}

		return me.Substring(startIndex, endIdx - startIndex);
	}

	public static function toString(me:NativeString):NativeString
	{
		return me;
	}

	public static function toLowerCase(me:NativeString):String
	{
		return me.ToLowerInvariant();
	}

	public static function toUpperCase(me:NativeString):String
	{
		return me.ToUpperInvariant();
	}

	public static function toNativeString(me:NativeString):NativeString
	{
		return me;
	}

	public static function fromCharCode(code:Int):NativeString
	{
		return new NativeString( cast(code,cs.StdTypes.Char16), 1 );
	}
}

@:keep @:nativeGen @:native('haxe.lang.StringRefl') class StringRefl
{
	public static var fields = ["length", "toUpperCase", "toLowerCase", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "substr", "substring"];

	public static function handleGetField(str:String, f:String, throwErrors:Bool):Dynamic
	{
		switch(f)
		{
			case "length": return str.length;
			case "toUpperCase", "toLowerCase", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "substr", "substring":
				return new Closure(str, f, 0);
			default:
				if (throwErrors)
					throw "Field not found: '" + f + "' in String";
				else
					return null;
		}
	}

	public static function handleCallField(str:NativeString, f:String, args:Array<Dynamic>):Dynamic
	{
		var _args:Array<Dynamic> = [str];
		if (args == null)
			args = _args;
		else
			args = _args.concat(args);

		return Runtime.slowCallField(StringExt, f, args);
	}
}