Skip to content

Commit bdccea7

Browse files
Permit trailing whitespace in numeric strings
1 parent 81bca7f commit bdccea7

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
Acceptance of whitespace in numeric strings
3+
--FILE--
4+
<?php
5+
6+
$strings = [
7+
"123",
8+
"123 ",
9+
"123 \t\n\r\v\f",
10+
" 123",
11+
" \t\n\r\v\f123",
12+
" 123 ",
13+
" \t\n\r\v\f123 \t\n\r\v\f",
14+
"123.0",
15+
"123.0 ",
16+
"123.0 \t\n\r\v\f",
17+
" 123.0",
18+
" \t\n\r\v\f123.0",
19+
" 123.0 ",
20+
" \t\n\r\v\f123 \t\n\r\v\f",
21+
"123e0",
22+
"123e0 ",
23+
"123e0 \t\n\r\v\f",
24+
" 123e0",
25+
" \t\n\r\v\f123e0",
26+
" 123e0 ",
27+
" \t\n\r\v\f123e0 \t\n\r\v\f"
28+
];
29+
30+
function takes_integer(int $i) {
31+
\assert($i === 123);
32+
}
33+
function takes_float(float $f) {
34+
\assert($f === 123.0);
35+
}
36+
37+
foreach ($strings as $string) {
38+
\assert($string == 123);
39+
$num = +$string;
40+
\assert($num == 123);
41+
takes_integer($string);
42+
takes_float($string);
43+
\assert(\intdiv($string, 1) === 123);
44+
\assert(\is_numeric($string));
45+
}
46+
47+
echo "OK!", PHP_EOL;
48+
49+
?>
50+
--EXPECT--
51+
OK!

Zend/zend_operators.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,11 +3079,19 @@ ZEND_API zend_uchar ZEND_FASTCALL _is_numeric_string_ex(const char *str, size_t
30793079
}
30803080

30813081
if (ptr != str + length) {
3082-
if (!allow_errors) {
3083-
return 0;
3082+
const char *endptr = ptr;
3083+
while (*endptr == ' ' || *endptr == '\t' || *endptr == '\n' || *endptr == '\r' || *endptr == '\v' || *endptr == '\f') {
3084+
endptr++;
3085+
length--;
30843086
}
3085-
if (allow_errors == -1) {
3086-
zend_error(E_NOTICE, "A non well formed numeric value encountered");
3087+
3088+
if (ptr != str + length) {
3089+
if (!allow_errors) {
3090+
return 0;
3091+
}
3092+
if (allow_errors == -1) {
3093+
zend_error(E_NOTICE, "A non well formed numeric value encountered");
3094+
}
30873095
}
30883096
}
30893097

ext/standard/tests/general_functions/is_numeric.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ $numerics = array(
7878
'-1',
7979
'1e2',
8080
' 1',
81+
"1 ",
8182
'2974394749328742328432',
8283
'-1e-2',
8384
"0123",
@@ -118,7 +119,6 @@ $not_numerics = array(
118119
array(),
119120
array("string"),
120121
"",
121-
"1 ",
122122
"- 1",
123123
"1.2.4",
124124
"1e7.6",
@@ -313,6 +313,8 @@ bool(true)
313313
bool(true)
314314
-- Iteration 76 --
315315
bool(true)
316+
-- Iteration 77 --
317+
bool(true)
316318

317319
*** Testing is_numeric() on non numeric types ***
318320
-- Iteration 1 --
@@ -371,8 +373,6 @@ bool(false)
371373
bool(false)
372374
-- Iteration 28 --
373375
bool(false)
374-
-- Iteration 29 --
375-
bool(false)
376376

377377
*** Testing error conditions ***
378378

0 commit comments

Comments
 (0)