Browse Source

Optional parameter of substr and lastIndexOf

Pascal Peridont 19 years ago
parent
commit
a2c9d5eb1f
3 changed files with 9 additions and 6 deletions
  1. 1 1
      std/String.hx
  2. 2 1
      std/js/Boot.hx
  3. 6 4
      std/neko/NekoString__.hx

+ 1 - 1
std/String.hx

@@ -58,7 +58,7 @@ extern class String {
 		The optional [startIndex] parameter allows you to specify which character to start searching. The position returned is still relative to the beginning of the string.
 		The optional [startIndex] parameter allows you to specify which character to start searching. The position returned is still relative to the beginning of the string.
 	**/
 	**/
 	function indexOf( value : String, ?startIndex : Int ) : Int;
 	function indexOf( value : String, ?startIndex : Int ) : Int;
-	function lastIndexOf( value : String, startIndex : Int ) : Int;
+	function lastIndexOf( value : String, ?startIndex : Int ) : Int;
 	function split( delimiter : String ) : Array<String>;
 	function split( delimiter : String ) : Array<String>;
 	function substr( pos : Int, ?len : Int ) : String;
 	function substr( pos : Int, ?len : Int ) : String;
 
 

+ 2 - 1
std/js/Boot.hx

@@ -225,7 +225,8 @@ class Boot {
 			};
 			};
 			var oldsub = String.prototype.substr;
 			var oldsub = String.prototype.substr;
 			String.prototype.substr = function(pos,len){
 			String.prototype.substr = function(pos,len){
-				if( pos != null && pos != 0 && len < 0 ) return "";
+				if( pos != null && pos != 0 && len != null && len < 0 ) return "";
+				if( len == null ) len = this.length;
 				if( pos < 0 ){
 				if( pos < 0 ){
 					pos = this.length + pos;
 					pos = this.length + pos;
 					if( pos < 0 ) pos = 0;
 					if( pos < 0 ) pos = 0;

+ 6 - 4
std/neko/NekoString__.hx

@@ -67,11 +67,11 @@ class NekoString__ implements String {
 		}
 		}
 	}
 	}
 
 
-	public function lastIndexOf( str : String, pos ) {
+	public function lastIndexOf( str : String, ?pos ) {
 		untyped {
 		untyped {
 			var last = -1;
 			var last = -1;
 			if( pos == null )
 			if( pos == null )
-				pos = 0;
+				pos = __dollar__ssize(this.__s);
 			while( true ) {
 			while( true ) {
 				var p = try __dollar__sfind(this.__s,last+1,str.__s) catch( e : Dynamic ) null;
 				var p = try __dollar__sfind(this.__s,last+1,str.__s) catch( e : Dynamic ) null;
 				if( p == null || p > pos )
 				if( p == null || p > pos )
@@ -95,9 +95,11 @@ class NekoString__ implements String {
 	}
 	}
 
 
 	public function substr( pos, ?len ) {
 	public function substr( pos, ?len ) {
-		var sl = length;		
 		if( len == 0 ) return "";
 		if( len == 0 ) return "";
-		if( len == null ) len = sl - pos;
+		var sl = length;
+
+		if( len == null ) len = sl;
+
 		if( pos == null ) pos = 0;
 		if( pos == null ) pos = 0;
 		if( pos != 0 && len < 0 ){
 		if( pos != 0 && len < 0 ){
 			return "";
 			return "";