Skip to content

Commit 53fe3e6

Browse files
committed
Copy UTF-8 flag for str_repeat
1 parent fc3cb6b commit 53fe3e6

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Zend/zend_string.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,13 @@ END_EXTERN_C()
8282
#define ZSTR_IS_INTERNED(s) (GC_FLAGS(s) & IS_STR_INTERNED)
8383
#define ZSTR_IS_VALID_UTF8(s) (GC_FLAGS(s) & IS_STR_VALID_UTF8)
8484

85+
/* These are properties, encoded as flags, that will hold on the resulting string
86+
* after concatenating two strings that have these property.
87+
* Example: concatenating two UTF-8 strings yields another UTF-8 string. */
8588
#define ZSTR_COPYABLE_CONCAT_PROPERTIES (IS_STR_VALID_UTF8)
8689

8790
#define ZSTR_GET_COPYABLE_CONCAT_PROPERTIES(s) (GC_FLAGS(s) & ZSTR_COPYABLE_CONCAT_PROPERTIES)
91+
/* This macro returns the copyable concat properties which hold on both strings. */
8892
#define ZSTR_GET_COPYABLE_CONCAT_PROPERTIES_BOTH(s1, s2) (GC_FLAGS(s1) & GC_FLAGS(s2) & ZSTR_COPYABLE_CONCAT_PROPERTIES)
8993

9094
#define ZSTR_COPY_CONCAT_PROPERTIES(out, in) do { \

ext/standard/string.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5059,6 +5059,7 @@ PHP_FUNCTION(str_repeat)
50595059
/* Initialize the result string */
50605060
result = zend_string_safe_alloc(ZSTR_LEN(input_str), mult, 0, 0);
50615061
result_len = ZSTR_LEN(input_str) * mult;
5062+
ZSTR_COPY_CONCAT_PROPERTIES(result, input_str);
50625063

50635064
/* Heavy optimization for situations where input string is 1 byte long */
50645065
if (ZSTR_LEN(input_str) == 1) {

ext/zend_test/tests/strings_marked_as_utf8.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ $s = $o . $o;
107107
var_dump($s);
108108
var_dump(zend_test_is_string_marked_as_valid_utf8($s));
109109

110+
echo "str_repeat:\n";
111+
$string = "a";
112+
$string_concat = str_repeat($string, 100);
113+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
114+
$string = "\xff";
115+
$string_concat = str_repeat($string, 100);
116+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
117+
110118
?>
111119
--EXPECT--
112120
Empty strings:
@@ -148,3 +156,6 @@ bool(true)
148156
Concatenation of objects:
149157
string(2) "zz"
150158
bool(true)
159+
str_repeat:
160+
bool(true)
161+
bool(false)

0 commit comments

Comments
 (0)