Skip to content

Commit a5553d6

Browse files
committed
Deprecate passing a negative width to mb_strimwidth()
1 parent 18067c8 commit a5553d6

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

ext/mbstring/mbstring.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2649,6 +2649,8 @@ PHP_FUNCTION(mb_strimwidth)
26492649
}
26502650

26512651
if (width < 0) {
2652+
php_error_docref(NULL, E_DEPRECATED,
2653+
"passing a negative integer to argument #3 ($width) is deprecated");
26522654
width += mb_get_strwidth(str, enc);
26532655

26542656
if (from > 0) {

ext/mbstring/tests/mb_strimwidth.phpt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ testStrimwidth($utf16le, -3, 5, 'UTF-16LE');
7171
// We'll count back 4 characters, then allow a width of ((4 * 2) - 2) = 6
7272
// Since the output will not reach the END of the string, the trim marker
7373
// will have to be added, and will consume a width of 3
74-
testStrimwidth($utf16le, -4, -2, 'UTF-16LE');
74+
// We also suppress the deprecation for negative width as of PHP 8.3
75+
@testStrimwidth($utf16le, -4, -2, 'UTF-16LE');
7576

7677
echo "\n== EUC-JP ==\n";
7778

@@ -103,22 +104,26 @@ testStrimwidth($euc_jp, 9, 5, 'EUC-JP');
103104

104105
// Skip 15 characters, which leaves a total width of 42. Then trim string down
105106
// to 5 less than that, which is a width of 37.
106-
testStrimwidth($euc_jp, 15, -5, 'EUC-JP');
107+
// We also suppress the deprecation for negative width as of PHP 8.3
108+
@testStrimwidth($euc_jp, 15, -5, 'EUC-JP');
107109

108110
// Take the last 30 characters, which have a width of 54. Trim string down to
109111
// 25 less than that, which is 29.
110-
testStrimwidth($euc_jp, -30, -25, 'EUC-JP');
112+
// We also suppress the deprecation for negative width as of PHP 8.3
113+
@testStrimwidth($euc_jp, -30, -25, 'EUC-JP');
111114

112115
// Skip over 39 characters... but since string is only 39 characters long,
113116
// it takes us to the end of the string, and output is empty
114117
testStrimwidth($euc_jp, 39, 10, 'EUC-JP');
115118

116119
// Take the last 10 characters, which have a width of 20. Trim string down to
117120
// 12 less than that, which is a width of 8.
118-
testStrimwidth($euc_jp, -10, -12, 'EUC-JP');
121+
// We also suppress the deprecation for negative width as of PHP 8.3
122+
@testStrimwidth($euc_jp, -10, -12, 'EUC-JP');
119123

120124
try {
121-
var_dump(mb_strimwidth($euc_jp, 0, -100,'...','EUC-JP'));
125+
// We also suppress the deprecation for negative width as of PHP 8.3
126+
var_dump(@mb_strimwidth($euc_jp, 0, -100,'...','EUC-JP'));
122127
} catch (\ValueError $e) {
123128
echo $e->getMessage() . \PHP_EOL;
124129
}
@@ -133,7 +138,8 @@ try {
133138
echo $e->getMessage() . \PHP_EOL;
134139
}
135140
try {
136-
var_dump(mb_strimwidth($euc_jp, -10, -21,'...','EUC-JP'));
141+
// We also suppress the deprecation for negative width as of PHP 8.3
142+
var_dump(@mb_strimwidth($euc_jp, -10, -21,'...','EUC-JP'));
137143
} catch (\ValueError $e) {
138144
echo $e->getMessage() . \PHP_EOL;
139145
}
@@ -147,7 +153,8 @@ for ($from = -5; $from <= 5; $from++) {
147153
// This case is illegal and will throw an exception
148154
$pass = false;
149155
try {
150-
mb_strimwidth($str, $from, $width, '...', 'ASCII');
156+
/* Shut up deprecation notice for now */
157+
@mb_strimwidth($str, $from, $width, '...', 'ASCII');
151158
} catch (\ValueError $e) {
152159
$pass = true;
153160
}
@@ -156,7 +163,8 @@ for ($from = -5; $from <= 5; $from++) {
156163
continue;
157164
}
158165

159-
$result = mb_strimwidth($str, $from, $width, '...', 'ASCII');
166+
/* Shut up deprecation notice for now */
167+
$result = @mb_strimwidth($str, $from, $width, '...', 'ASCII');
160168

161169
if ($from < 0 && $width < 0 && ($width - $from) <= 3) {
162170
if ($result !== '...')
@@ -211,7 +219,8 @@ testStrimwidth("日本語abc", -3, 10, 'UTF-8');
211219
// Regression test; old implementation did not handle positive 'from' argument
212220
// combined with negative 'width' argument correctly when portion being skipped
213221
// over included fullwidth characters
214-
testStrimwidth("日本語abcdef", 3, -1, 'UTF-8');
222+
// We also suppress the deprecation for negative width as of PHP 8.3
223+
@testStrimwidth("日本語abcdef", 3, -1, 'UTF-8');
215224

216225
?>
217226
--EXPECT--
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
mb_strimwidth() is deprecated with negative width
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
var_dump(mb_strimwidth("some string", 1, -2, '...', 'ASCII'));
8+
?>
9+
--EXPECTF--
10+
Deprecated: mb_strimwidth(): passing a negative integer to argument #3 ($width) is deprecated in %s on line %d
11+
string(8) "ome s..."

0 commit comments

Comments
 (0)