Skip to content

Commit

Permalink
Fixing str_replace optimizer with array search parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Oct 24, 2014
1 parent 73b3319 commit b66b227
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 41 deletions.
4 changes: 4 additions & 0 deletions Library/Optimizers/EvalExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class EvalExpression
protected $_usedVariables = array();

/**
* Skips the not operator by recursively optimizing the expression at its right
*
* @param array $expr
* @param CompilationContext $compilationContext
*/
Expand All @@ -54,6 +56,8 @@ public function optimizeNot($expr, $compilationContext)
if ($conditions !== false) {
if ($this->_unreachable !== null) {
$this->_unreachable = !$this->_unreachable;
}
if ($this->_unreachableElse !== null) {
$this->_unreachableElse = !$this->_unreachableElse;
}
return '!(' . $conditions . ')';
Expand Down
2 changes: 1 addition & 1 deletion Library/Optimizers/FunctionCall/StrReplaceOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function optimize(array $expression, Call $call, CompilationContext $cont
}

if (count($expression['parameters']) != 3) {
throw new CompilerException("'str_replace' only accepts three parameter");
throw new CompilerException("'str_replace' only accepts three parameter", $expression);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Library/Optimizers/FunctionCall/UniqueKeyOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function optimize(array $expression, Call $call, CompilationContext $cont
}

if (count($expression['parameters']) != 2) {
throw new CompilerException("'unique_key' only accepts three parameter");
throw new CompilerException("'unique_key' only accepts three parameter", $expression);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Library/Optimizers/FunctionCall/UniquePathKeyOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function optimize(array $expression, Call $call, CompilationContext $cont
}

if (count($expression['parameters']) != 1) {
throw new CompilerException("'unique_path_key' only accepts three parameter");
throw new CompilerException("'unique_path_key' only accepts three parameter", $expression);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ext/kernel/fcall.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ typedef zend_function zephir_fcall_cache_entry;

int zephir_call_func_aparams(zval **return_value_ptr, const char *func_name, uint func_length,
zephir_fcall_cache_entry **cache_entry,
uint param_count, zval **params TSRMLS_DC) ZEPHIR_ATTR_WARN_UNUSED_RESULT;
uint param_count, zval **params TSRMLS_DC);

int zephir_call_zval_func_aparams(zval **return_value_ptr, zval *func_name,
zephir_fcall_cache_entry **cache_entry,
Expand Down
84 changes: 47 additions & 37 deletions ext/kernel/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,17 @@ void zephir_fast_str_replace(zval *return_value, zval *search, zval *replace, zv
return;
}

/**
* Fallback to userland function if the first parameter is an array
*/
if (Z_TYPE_P(search) == IS_ARRAY) {
do {
zval *params[] = { search, replace, subject };
zephir_call_func_aparams(&return_value, "str_replace", sizeof("str_replace")-1, NULL, 3, params TSRMLS_CC);
return;
} while(0);
}

if (Z_TYPE_P(replace) != IS_STRING) {
zend_make_printable_zval(replace, &replace_copy, &copy_replace);
if (copy_replace) {
Expand Down Expand Up @@ -922,54 +933,53 @@ void zephir_substr(zval *return_value, zval *str, long f, long l) {
RETURN_FALSE;
}

long str_len = Z_STRLEN_P(str);
long str_len = Z_STRLEN_P(str);

if ((l < 0 && -l > str_len)) {
RETURN_FALSE;
} else if (l > str_len) {
l = str_len;
} else if (l == 0) {
l = str_len;
}

if (f > str_len) {
RETURN_FALSE;
} else if (f < 0 && -f > str_len) {
f = 0;
}
if ((l < 0 && -l > str_len)) {
RETURN_FALSE;
} else if (l > str_len) {
l = str_len;
} else if (l == 0) {
l = str_len;
}

if (l < 0 && (l + str_len - f) < 0) {
RETURN_FALSE;
}
if (f > str_len) {
RETURN_FALSE;
} else if (f < 0 && -f > str_len) {
f = 0;
}

if (l < 0 && (l + str_len - f) < 0) {
RETURN_FALSE;
}

/* if "from" position is negative, count start position from the end
* of the string
*/
/* if "from" position is negative, count start position from the end
* of the string
*/
if (f < 0) {
f = str_len + f;
if (f < 0) {
f = str_len + f;
if (f < 0) {
f = 0;
}
f = 0;
}
}

/* if "length" position is negative, set it to the length
* needed to stop that many chars from the end of the string
*/
/* if "length" position is negative, set it to the length
* needed to stop that many chars from the end of the string
*/
if (l < 0) {
l = (str_len - f) + l;
if (l < 0) {
l = (str_len - f) + l;
if (l < 0) {
l = 0;
}
l = 0;
}
}

if (f >= str_len) {
RETURN_FALSE;
}
if (f >= str_len) {
RETURN_FALSE;
}

if ((f + l) > str_len) {
l = str_len - f;
}
if ((f + l) > str_len) {
l = str_len - f;
}

if (l <= 0){
RETURN_EMPTY_STRING();
Expand Down

0 comments on commit b66b227

Please sign in to comment.