Browse Source

[Php] added _hx_substring

Simon Krajewski 12 years ago
parent
commit
4f397d4898
2 changed files with 29 additions and 1 deletions
  1. 7 1
      genphp.ml
  2. 22 0
      std/php/Boot.hx

+ 7 - 1
genphp.ml

@@ -641,7 +641,7 @@ and gen_string_static_call ctx s e el =
 	| _ -> unsupported "gen_string_static_call " e.epos;
 
 and could_be_string_call s =
-	s = "substr" || s = "charAt" || s = "charCodeAt" || s = "indexOf" ||
+	s = "substr" || s = "substring" || s = "charAt" || s = "charCodeAt" || s = "indexOf" ||
 	s = "lastIndexOf" || s = "split" || s = "toLowerCase" || s = "toString" || s = "toUpperCase"
 
 and gen_string_call ctx s e el =
@@ -652,6 +652,12 @@ and gen_string_call ctx s e el =
 		spr ctx ", ";
 		concat ctx ", " (gen_value ctx) el;
 		spr ctx ")"
+	| "substring" ->
+		spr ctx "_hx_substring(";
+		gen_value ctx e;
+		spr ctx ", ";
+		concat ctx ", " (gen_value ctx) el;
+		spr ctx ")"
 	| "charAt" ->
 		spr ctx "_hx_char_at(";
 		gen_value ctx e;

+ 22 - 0
std/php/Boot.hx

@@ -633,6 +633,28 @@ function _hx_substr($s, $pos, $len) {
 		return $s;
 }
 
+function _hx_substring($s, $startIndex, $endIndex) {
+	$len = strlen($s);
+	if ($endIndex === null)
+		$endIndex = $len;
+	else if ($endIndex < 0)
+		$endIndex = 0;
+	else if ($endIndex > $len)
+		$endIndex  = $len;
+		
+	if ($startIndex < 0)
+		$startIndex = 0;
+	else if ($startIndex > $len)
+		$startIndex = $len;
+		
+	if ($startIndex > $endIndex) {
+		$tmp = $startIndex;
+		$startIndex = $endIndex;
+		$endIndex = $tmp;
+	}
+	return _hx_substr($s, $startIndex, $endIndex - $startIndex);
+}
+
 function _hx_trace($v, $i) {
 	$msg = $i !== null ? $i->fileName.':'.$i->lineNumber.': ' : '';
 	echo $msg._hx_string_rec($v, '').\"\n\";