Browse Source

[php7] inlining for most of the String methods

Alexander Kuzmenko 8 years ago
parent
commit
ad363f9d3a
3 changed files with 217 additions and 1 deletions
  1. 1 1
      std/php7/Boot.hx
  2. 189 0
      std/php7/_std/String.hx
  3. 27 0
      tests/unit/src/unit/TestPhp.hx

+ 1 - 1
std/php7/Boot.hx

@@ -627,7 +627,7 @@ private class HxEnum {
 private class HxString {
 private class HxString {
 
 
 	public static function toUpperCase( str:String ) : String {
 	public static function toUpperCase( str:String ) : String {
-		return Global.strtoupper(str);
+		return str.toUpperCase();
 	}
 	}
 
 
 	public static function toLowerCase( str:String ) : String {
 	public static function toLowerCase( str:String ) : String {

+ 189 - 0
std/php7/_std/String.hx

@@ -0,0 +1,189 @@
+/*
+ * Copyright (C)2005-2017 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 php.*;
+
+/**
+	The basic String class.
+
+	A Haxe String is immutable, it is not possible to modify individual
+	characters. No method of this class changes the state of `this` String.
+
+	Strings can be constructed using the String literal syntax `"string value"`.
+
+	String can be concatenated by using the `+` operator. If an operand is not a
+	String, it is passed through `Std.string()` first.
+
+	@see https://haxe.org/manual/std-String.html
+**/
+@:coreApi extern class String {
+
+	/**
+		The number of characters in `this` String.
+	**/
+	var length(default,null) : Int;
+
+	/**
+		Creates a copy from a given String.
+	**/
+	@:pure function new(string:String) : Void;
+
+	/**
+		Returns a String where all characters of `this` String are upper case.
+
+		Affects the characters `a-z`. Other characters remain unchanged.
+	**/
+	@:pure @:runtime inline function toUpperCase() : String {
+		return Global.strtoupper(this);
+	}
+
+	/**
+		Returns a String where all characters of `this` String are lower case.
+
+		Affects the characters `A-Z`. Other characters remain unchanged.
+	**/
+	@:pure @:runtime inline function toLowerCase() : String {
+		return Global.strtolower(this);
+	}
+
+	/**
+		Returns the character at position `index` of `this` String.
+
+		If `index` is negative or exceeds `this.length`, the empty String `""`
+		is returned.
+	**/
+	@:pure @:runtime inline function charAt(index : Int) : String {
+		return (index < 0 || index >= this.length ? '' : (this:NativeString)[index]);
+	}
+
+	/**
+		Returns the character code at position `index` of `this` String.
+
+		If `index` is negative or exceeds `this.length`, `null` is returned.
+
+		To obtain the character code of a single character, `"x".code` can be
+		used instead to @:runtime inline the character code at compile time. Note that
+		this only works on String literals of length 1.
+	**/
+	@:pure @:runtime inline function charCodeAt( index : Int) : Null<Int> {
+		return (index < 0 || index >= this.length ? null : Global.ord((this:NativeString)[index]));
+	}
+
+	/**
+		Returns the position of the leftmost occurrence of `str` within `this`
+		String.
+
+		If `startIndex` is given, the search is performed within the substring
+		of `this` String starting from `startIndex`. Otherwise the search is
+		performed within `this` String. In either case, the returned position
+		is relative to the beginning of `this` String.
+
+		If `str` cannot be found, -1 is returned.
+	**/
+	@:pure function indexOf( str : String, ?startIndex : Int ) : Int;
+
+	/**
+		Returns the position of the rightmost occurrence of `str` within `this`
+		String.
+
+		If `startIndex` is given, the search is performed within the substring
+		of `this` String from 0 to `startIndex`. Otherwise the search is
+		performed within `this` String. In either case, the returned position
+		is relative to the beginning of `this` String.
+
+		If `str` cannot be found, -1 is returned.
+	**/
+	@:pure function lastIndexOf( str : String, ?startIndex : Int ) : Int;
+
+	/**
+		Splits `this` String at each occurrence of `delimiter`.
+
+		If `this` String is the empty String `""`, the result is not consistent
+		across targets and may either be `[]` (on Js, Cpp) or `[""]`.
+
+		If `delimiter` is the empty String `""`, `this` String is split into an
+		Array of `this.length` elements, where the elements correspond to the
+		characters of `this` String.
+
+		If `delimiter` is not found within `this` String, the result is an Array
+		with one element, which equals `this` String.
+
+		If `delimiter` is null, the result is unspecified.
+
+		Otherwise, `this` String is split into parts at each occurrence of
+		`delimiter`. If `this` String starts (or ends) with `delimiter`, the
+		result `Array` contains a leading (or trailing) empty String `""` element.
+		Two subsequent delimiters also result in an empty String `""` element.
+	**/
+	@:pure @:runtime inline function split( delimiter : String ) : Array<String> {
+		return @:privateAccess Array.wrap(delimiter == '' ? Global.str_split(this) : Global.explode(delimiter, this));
+	}
+
+	/**
+		Returns `len` characters of `this` String, starting at position `pos`.
+
+		If `len` is omitted, all characters from position `pos` to the end of
+		`this` String are included.
+
+		If `pos` is negative, its value is calculated from the end of `this`
+		String by `this.length + pos`. If this yields a negative value, 0 is
+		used instead.
+
+		If the calculated position + `len` exceeds `this.length`, the characters
+		from that position to the end of `this` String are returned.
+
+		If `len` is negative, the result is unspecified.
+	**/
+	@:pure function substr( pos : Int, ?len : Int ) : String;
+
+	/**
+		Returns the part of `this` String from `startIndex` to but not including `endIndex`.
+
+		If `startIndex` or `endIndex` are negative, 0 is used instead.
+
+		If `startIndex` exceeds `endIndex`, they are swapped.
+
+		If the (possibly swapped) `endIndex` is omitted or exceeds
+		`this.length`, `this.length` is used instead.
+
+		If the (possibly swapped) `startIndex` exceeds `this.length`, the empty
+		String `""` is returned.
+	**/
+	@:pure function substring( startIndex : Int, ?endIndex : Int ) : String;
+
+	/**
+		Returns the String itself.
+	**/
+	@:pure @:runtime inline function toString() : String {
+		return this;
+	}
+
+	/**
+		Returns the String corresponding to the character code `code`.
+
+		If `code` is negative or has another invalid value, the result is
+		unspecified.
+	**/
+	@:pure @:runtime static inline function fromCharCode( code : Int ) : String {
+		return Global.chr(code);
+	}
+}

+ 27 - 0
tests/unit/src/unit/TestPhp.hx

@@ -121,6 +121,33 @@ class TestPhp extends Test
 		eq('HxDynamicStr', className);
 		eq('HxDynamicStr', className);
 		eq('FOO', fn());
 		eq('FOO', fn());
 	}
 	}
+
+	function testStringAsAnon() {
+		var str = 'bar';
+		var anon:{
+			function toUpperCase() : String;
+			function toLowerCase() : String;
+			function charAt( index : Int) : String;
+			function indexOf( str : String, ?startIndex : Int ) : Int;
+			function lastIndexOf( str : String, ?startIndex : Int ) : Int;
+			function split( delimiter : String ) : Array<String>;
+			function toString() : String;
+			function substring( startIndex : Int, ?endIndex : Int ) : String;
+			function charCodeAt( index : Int) : Null<Int> ;
+			function substr( pos : Int, ?len : Int ) : String ;
+		} = str;
+
+		eq(str.charAt(0), anon.charAt(0));
+		eq(str.charCodeAt(0), anon.charCodeAt(0));
+		eq(str.indexOf("a"), anon.indexOf("a"));
+		eq(str.lastIndexOf("r"), anon.lastIndexOf("r"));
+		eq(str.split("a")[1], anon.split("a")[1]);
+		eq(str.substr(1),anon.substr(1, 2));
+		eq(str.substring(0, 2), anon.substring(0, 2));
+		eq(str, anon.toLowerCase());
+		eq(str.toUpperCase(), anon.toUpperCase());
+		eq(str.toString(), anon.toString());
+	}
 #end
 #end
 }
 }