Jelajahi Sumber

slice optional length
fixed neko slice & splice

Nicolas Cannasse 18 tahun lalu
induk
melakukan
fdfab26d2e
2 mengubah file dengan 13 tambahan dan 5 penghapusan
  1. 1 1
      std/Array.hx
  2. 12 4
      std/neko/NekoArray__.hx

+ 1 - 1
std/Array.hx

@@ -76,7 +76,7 @@ extern class Array<T> {
 		negative to count from the end: -1 is the last item in
 		the array.
 	**/
-	function slice( pos : Int, end : Int ) : Array<T>;
+	function slice( pos : Int, ?end : Int ) : Array<T>;
 
 	/**
 		Sort the Array according to the comparison function [f].

+ 12 - 4
std/neko/NekoArray__.hx

@@ -197,17 +197,20 @@ class NekoArray__<T> implements Array<T> {
 		}
 	}
 
-	public function slice( pos, end ) {
+	public function slice( pos, ?end ) {
 		if( pos < 0 ){
 			pos = this.length + pos;
 			if( pos < 0 )
 				pos = 0;
 		}
-		if( end < 0 ){
+		if( end == null )
+			end = this.length;
+		else if( end < 0 )
 			end = this.length + end;
-		}
+		if( end > this.length )
+			end = this.length;
 		var len = end - pos;
-		if( len < 0 || pos + len > this.length ) return new Array();
+		if( len < 0 ) return new Array();
 		return untyped Array.new1(__dollar__asub(this.__a,pos,len),len);
 	}
 
@@ -242,6 +245,11 @@ class NekoArray__<T> implements Array<T> {
 			pos = this.length + pos;
 			if( pos < 0 ) pos = 0;
 		}
+		if( pos > this.length ) {
+			pos = 0;
+			len = 0;
+		} else if( pos + len > this.length )
+			len = this.length - pos;
 		untyped {
 			var a = this.__a;
 			var ret = Array.new1(__dollar__asub(a,pos,len),len);