From: Pierre Joye Date: Thu, 20 Jul 2006 12:58:13 +0000 (+0000) Subject: - make boolean logical filter works like int/float and php itself X-Git-Tag: php-5.2.0RC1~42 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d6c2c283ed45c5527c3a49f4e4c9087267a4aa87;p=php - make boolean logical filter works like int/float and php itself - add more tests for boolean input --- diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c index fa352abf2c..2631a8ced7 100644 --- a/ext/filter/logical_filters.c +++ b/ext/filter/logical_filters.c @@ -217,20 +217,29 @@ stateE: void php_filter_boolean(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */ { + char *str = Z_STRVAL_P(value); + + if (str) { + /* fast(er) trim */ + while (*str == ' ') { + str++; + } + } + /* returns true for "1", "true", "on" and "yes" * returns false for "0", "false", "off", "no", and "" * null otherwise. */ - if ((strncasecmp(Z_STRVAL_P(value), "true", sizeof("true")) == 0) || - (strncasecmp(Z_STRVAL_P(value), "yes", sizeof("yes")) == 0) || - (strncasecmp(Z_STRVAL_P(value), "on", sizeof("on")) == 0) || - (strncmp(Z_STRVAL_P(value), "1", sizeof("1")) == 0)) + if ((strncasecmp(str, "true", sizeof("true")) == 0) || + (strncasecmp(str, "yes", sizeof("yes")) == 0) || + (strncasecmp(str, "on", sizeof("on")) == 0) || + (strncmp(str, "1", sizeof("1")) == 0)) { zval_dtor(value); ZVAL_BOOL(value, 1); - } else if ((strncasecmp(Z_STRVAL_P(value), "false", sizeof("false")) == 0) || - (strncasecmp(Z_STRVAL_P(value), "off", sizeof("off")) == 0) || - (strncasecmp(Z_STRVAL_P(value), "no", sizeof("no")) == 0) || - (strncmp(Z_STRVAL_P(value), "0", sizeof("0")) == 0) || + } else if ((strncasecmp(str, "false", sizeof("false")) == 0) || + (strncasecmp(str, "off", sizeof("off")) == 0) || + (strncasecmp(str, "no", sizeof("no")) == 0) || + (strncmp(str, "0", sizeof("0")) == 0) || Z_STRLEN_P(value) == 0) { zval_dtor(value); diff --git a/ext/filter/tests/034.phpt b/ext/filter/tests/034.phpt new file mode 100644 index 0000000000..7854f904ba --- /dev/null +++ b/ext/filter/tests/034.phpt @@ -0,0 +1,30 @@ +--TEST-- +Logical filter: boolean +--FILE-- + true, +'On' => true, +'Off' => true, +'True' => true, +'TrUe' => true, +'oN' => true, + +'0' => false, +'Off' => false, +'False' => false, +'faLsE' => false, +'oFf' => false, +'' => false +); + +foreach($booleans as $val=>$exp) { + $res =filter_data($val, FILTER_VALIDATE_BOOLEAN); + if ($res !== $exp) { + echo "$val failed,'$exp' expect, '$res' received.\n"; + } +} +echo "Ok."; +?> +--EXPECTF-- +Ok.