File: StringImpl.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 (120 lines) | stat: -rw-r--r-- 4,546 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
/*
 * 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 python.internal;

import python.internal.Internal;

@:native("HxString")
class StringImpl {

	@:ifFeature("dynamic_read.split", "anon_optional_read.split")
	public static inline function split (s:String, d:String) {
		return if (d == "") UBuiltins.list(s) else Syntax.callField(s, "split", d);
	}

	@:ifFeature("dynamic_read.charCodeAt", "anon_optional_read.charCodeAt")
	public static function charCodeAt(s:String, index:Int) {
		return
			if (s == null || s.length == 0 || index < 0 || index >= s.length) null
			else UBuiltins.ord(Syntax.arrayAccess(s, index));
	}

	@:ifFeature("dynamic_read.charAt", "anon_optional_read.charAt")
	public static inline function charAt(s:String, index:Int) {
		return if (index < 0 || index >= s.length) "" else Syntax.arrayAccess(s,index);
	}

	@:ifFeature("dynamic_read.lastIndexOf", "anon_optional_read.lastIndexOf")
	public static inline function lastIndexOf(s:String, str:String, ?startIndex:Int):Int {
		if (startIndex == null) {
			return Syntax.callField(s, "rfind", str, 0, s.length);
		} else {

			var i = Syntax.callField(s, "rfind", str, 0, startIndex+1);
			var startLeft = i == -1 ? UBuiltins.max(0, startIndex + 1 - str.length) : i + 1;
			var check = Syntax.callField(s,"find", str, startLeft, s.length);
			if (check > i && check <= startIndex) {
				return check;
			} else {
				return i;
			}
		}
	}

	@:ifFeature("dynamic_read.toUpperCase", "anon_optional_read.toUpperCase")
	public static inline function toUpperCase (s:String) {
		return Syntax.callField(s, "upper");
	}
	@:ifFeature("dynamic_read.toLowerCase", "anon_optional_read.toLowerCase")
	public static inline function toLowerCase (s:String) {
		return Syntax.callField(s, "lower");
	}
	@:ifFeature("dynamic_read.indexOf", "anon_optional_read.indexOf")
	public static inline function indexOf (s:String, str:String, ?startIndex:Int) {
		if (startIndex == null)
			return Syntax.callField(s, "find", str);
		else
			return Syntax.callField(s, "find", str, startIndex);
	}
	@:ifFeature("dynamic_read.toString", "anon_optional_read.toString")
	public static inline function toString (s:String) {
		return s;
	}
	@:ifFeature("dynamic_read.length", "anon_optional_read.length")
	public static inline function get_length (s:String) {
		return UBuiltins.len(s);
	}
	@:ifFeature("dynamic_read.fromCharCode", "anon_optional_read.fromCharCode")
	public static inline function fromCharCode( code : Int ) : String {
		#if doc_gen
		return "";
		#else
		return Syntax.callField('', "join", UBuiltins.map(UBuiltins.chr, cast [code])); // TODO: check cast
		#end
	}
	@:ifFeature("dynamic_read.substring", "anon_optional_read.substring")
	public static function substring( s:String, startIndex : Int, ?endIndex : Int ) : String {
		if (startIndex < 0) startIndex = 0;
		if (endIndex == null) {
			return Syntax.arrayAccessWithTrailingColon(s, startIndex);
		} else {
			if (endIndex < 0) endIndex = 0;
			if (endIndex < startIndex) {

				return Syntax.arrayAccess(s, endIndex, startIndex);
			} else {

				return Syntax.arrayAccess(s, startIndex, endIndex);
			}
		}
	}
	@:ifFeature("dynamic_read.substr", "anon_optional_read.substr")
	public static function substr( s:String, startIndex : Int, ?len : Int ) : String {
		if (len == null) {
			return Syntax.arrayAccessWithTrailingColon(s, startIndex);
		} else {
			if (len == 0) return "";
			return Syntax.arrayAccess(s, startIndex, startIndex+len);
		}

	}
}