Browse Source

Lua: Fix String.substring for situations with negative offsets

Justin Donaldson 9 years ago
parent
commit
15a6f72223
1 changed files with 8 additions and 1 deletions
  1. 8 1
      std/lua/_std/String.hx

+ 8 - 1
std/lua/_std/String.hx

@@ -94,7 +94,14 @@ class String {
 	}
 	public function substring( startIndex : Int, ?endIndex : Int ) : String {
 		if (endIndex == null) endIndex = this.length;
-		return untyped lua.StringTools.sub(this, startIndex + 1, endIndex);
+		if (endIndex < 0) endIndex = 0;
+		if (startIndex < 0) startIndex = 0;
+		if (endIndex == 0) {
+			// swap the index positions
+			return untyped lua.StringTools.sub(this, endIndex+1, startIndex);
+		} else {
+			return untyped lua.StringTools.sub(this, startIndex+1, endIndex);
+		}
 	}
 
 	function get_length() : Int {