Commit 0fb80228f17 for php
commit 0fb80228f17f01778b195ac7d56100faad669a28
Author: ChrisAbner <chrisabner@hotmail.com>
Date: Wed Dec 2 18:43:59 2026 +0000
[RFC] ext/intl: Add grapheme_str_pad()
Adds grapheme_str_pad() to ext/intl, the grapheme cluster counterpart of
str_pad() and mb_str_pad().
The target length and the input length are counted in grapheme clusters,
like grapheme_strlen(). The padding string is repeated and its last copy is
only cut on a grapheme cluster boundary. Errors follow mb_str_pad()
(ValueError for an empty padding string or an invalid $pad_type, no error
when nothing needs padding). Invalid UTF-8 returns false and sets the intl
error, like grapheme_substr(). ASCII input without "\r\n" takes a fast path
and gives the same result as str_pad().
RFC: https://wiki.php.net/rfc/grapheme_str_pad
Close GH-25179
diff --git a/NEWS b/NEWS
index 362e8c1e535..f1c3310b687 100644
--- a/NEWS
+++ b/NEWS
@@ -69,6 +69,7 @@ PHP NEWS
. Added IntlNumberFormatter::createFromSkeleton() (GH-24165). (David Carlier)
. Fixed UConverter::setSubstChars() truncating the substitution string
length. (Weilin Du)
+ . Added grapheme_str_pad(). (Chris Abner Herrera Navarro)
- JSON:
. Improved performance of json_encode() for strings with non-ASCII
diff --git a/UPGRADING b/UPGRADING
index 66ab8b394a1..b614390b0af 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -173,6 +173,9 @@ PHP 8.7 UPGRADE NOTES
as a normalized, locale-independent ICU number skeleton.
. Added Spoofchecker::getRestrictionLevel() to retrieve the configured
restriction level as one of the Spoofchecker restriction level constants.
+ . Added grapheme_str_pad() to pad a string to a length counted in grapheme
+ clusters, the grapheme counterpart of str_pad() and mb_str_pad().
+ RFC: https://wiki.php.net/rfc/grapheme_str_pad
========================================
7. New Classes and Interfaces
diff --git a/ext/intl/grapheme/grapheme_string.cpp b/ext/intl/grapheme/grapheme_string.cpp
index 9c7fa09cb73..b4ec5e032ae 100644
--- a/ext/intl/grapheme/grapheme_string.cpp
+++ b/ext/intl/grapheme/grapheme_string.cpp
@@ -23,6 +23,7 @@
#endif
#include <php.h>
+#include "ext/standard/php_string.h"
#include "grapheme.h"
#include "grapheme_util.h"
@@ -1151,6 +1152,222 @@ out_ustring1:
efree(ustring1);
}
+/* {{{ Returns the byte length, in the UTF-8 string str, of the first upos UTF-16 code units of ustr,
+ * its UTF-16 conversion. */
+static size_t grapheme_str_pad_utf8_offset(const zend_string *str, const UChar *ustr, int32_t upos)
+{
+ int32_t code_points = u_countChar32(ustr, upos);
+ int32_t offset = 0;
+ U8_FWD_N(ZSTR_VAL(str), offset, (int32_t) ZSTR_LEN(str), code_points);
+ return offset;
+}
+/* }}} */
+
+/* {{{ Counts the grapheme clusters of a UTF-8 string, but stops once limit clusters have been counted.
+ * Also stores the number of bytes taken by its first prefix1 and prefix2 clusters in bytes1 and bytes2,
+ * if the string has that many (0 otherwise). Returns false on failure. */
+static bool grapheme_str_pad_measure(const zend_string *str, size_t limit, size_t *count,
+ size_t prefix1, size_t *bytes1, size_t prefix2, size_t *bytes2)
+{
+ if (grapheme_ascii_check((const unsigned char *) ZSTR_VAL(str), ZSTR_LEN(str)) >= 0) {
+ *count = ZSTR_LEN(str);
+ *bytes1 = prefix1 <= ZSTR_LEN(str) ? prefix1 : 0;
+ *bytes2 = prefix2 <= ZSTR_LEN(str) ? prefix2 : 0;
+ return true;
+ }
+
+ if (UNEXPECTED(ZSTR_LEN(str) > INT32_MAX)) {
+ intl_error_set_code(nullptr, U_INDEX_OUTOFBOUNDS_ERROR);
+ intl_error_set_custom_msg(nullptr, "String is too long");
+ return false;
+ }
+
+ /* Like grapheme_strlen(), work on UTF-16. The conversion also rejects invalid UTF-8. */
+ UErrorCode ustatus = U_ZERO_ERROR;
+ UChar *ustr = nullptr;
+ int32_t ulen = 0;
+ intl_convert_utf8_to_utf16(&ustr, &ulen, ZSTR_VAL(str), ZSTR_LEN(str), &ustatus);
+
+ if (U_FAILURE(ustatus)) {
+ intl_error_set_code(nullptr, ustatus);
+ intl_error_set_custom_msg(nullptr, "Invalid UTF-8 string");
+ if (ustr) {
+ efree(ustr);
+ }
+ return false;
+ }
+
+ UBreakIterator *bi = grapheme_get_break_iterator(&ustatus);
+
+ if (U_FAILURE(ustatus)) {
+ intl_error_set_code(nullptr, ustatus);
+ intl_error_set_custom_msg(nullptr, "Error creating grapheme break iterator");
+ efree(ustr);
+ return false;
+ }
+
+ ubrk_setText(bi, ustr, ulen, &ustatus);
+
+ if (U_FAILURE(ustatus)) {
+ intl_error_set_code(nullptr, ustatus);
+ intl_error_set_custom_msg(nullptr, "Error setting up grapheme break iterator");
+ ubrk_close(bi);
+ efree(ustr);
+ return false;
+ }
+
+ size_t graphemes = 0;
+ int32_t upos1 = 0, upos2 = 0;
+
+ for (int32_t pos = ubrk_next(bi); pos != UBRK_DONE && graphemes < limit; pos = ubrk_next(bi)) {
+ graphemes++;
+ if (graphemes == prefix1) {
+ upos1 = pos;
+ }
+ if (graphemes == prefix2) {
+ upos2 = pos;
+ }
+ }
+
+ ubrk_close(bi);
+
+ *bytes1 = upos1 ? grapheme_str_pad_utf8_offset(str, ustr, upos1) : 0;
+ *bytes2 = upos2 ? grapheme_str_pad_utf8_offset(str, ustr, upos2) : 0;
+ *count = graphemes;
+ efree(ustr);
+ return true;
+}
+/* }}} */
+
+/* {{{ Writes full_copies copies of the padding string followed by its first remaining_bytes bytes. */
+static char *grapheme_str_pad_fill(char *buffer, const zend_string *pad, size_t full_copies, size_t remaining_bytes)
+{
+ if (full_copies > 0) {
+ size_t full_bytes = full_copies * ZSTR_LEN(pad);
+ if (ZSTR_LEN(pad) == 1) {
+ memset(buffer, ZSTR_VAL(pad)[0], full_bytes);
+ buffer += full_bytes;
+ } else {
+ const char *start = buffer;
+ const char *end = buffer + full_bytes;
+ buffer = (char *) zend_mempcpy(buffer, ZSTR_VAL(pad), ZSTR_LEN(pad));
+
+ /* Double the filled area on each iteration, like mb_str_pad(). */
+ while (buffer < end) {
+ size_t len = MIN((size_t) (buffer - start), (size_t) (end - buffer));
+ buffer = (char *) zend_mempcpy(buffer, start, len);
+ }
+ }
+ }
+ return (char *) zend_mempcpy(buffer, ZSTR_VAL(pad), remaining_bytes);
+}
+/* }}} */
+
+/* {{{ Pad a string to a certain length, counted in grapheme clusters, with another string */
+U_CFUNC PHP_FUNCTION(grapheme_str_pad)
+{
+ zend_string *input, *pad = ZSTR_CHAR(' ');
+ zend_long pad_to_length;
+ zend_long pad_type_val = PHP_STR_PAD_RIGHT;
+
+ ZEND_PARSE_PARAMETERS_START(2, 4)
+ Z_PARAM_STR(input)
+ Z_PARAM_LONG(pad_to_length)
+ Z_PARAM_OPTIONAL
+ Z_PARAM_STR(pad)
+ Z_PARAM_LONG(pad_type_val)
+ ZEND_PARSE_PARAMETERS_END();
+
+ intl_error_reset(nullptr);
+
+ /* Counting stops past pad_to_length: a longer input is returned unchanged anyway. */
+ size_t input_length, unused;
+ if (!grapheme_str_pad_measure(input, pad_to_length < 0 ? 0 : (size_t) pad_to_length + 1, &input_length,
+ 0, &unused, 0, &unused)) {
+ RETURN_FALSE;
+ }
+
+ if (pad_to_length < 0 || (size_t) pad_to_length <= input_length) {
+ RETURN_STR_COPY(input);
+ }
+
+ if (ZSTR_LEN(pad) == 0) {
+ zend_argument_must_not_be_empty_error(3);
+ RETURN_THROWS();
+ }
+
+ if (pad_type_val < PHP_STR_PAD_LEFT || pad_type_val > PHP_STR_PAD_BOTH) {
+ zend_argument_value_error(4, "must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH");
+ RETURN_THROWS();
+ }
+
+ size_t num_pad_graphemes = (size_t) pad_to_length - input_length;
+ size_t left_pad = 0, right_pad = 0;
+ switch (pad_type_val) {
+ case PHP_STR_PAD_RIGHT:
+ right_pad = num_pad_graphemes;
+ break;
+ case PHP_STR_PAD_LEFT:
+ left_pad = num_pad_graphemes;
+ break;
+ case PHP_STR_PAD_BOTH:
+ left_pad = num_pad_graphemes / 2;
+ right_pad = num_pad_graphemes - left_pad;
+ break;
+ }
+
+ /* Only the first MAX(left_pad, right_pad) clusters of the padding string can be needed. If it has
+ * that many, a prefix of it is enough on each side; otherwise it is repeated. */
+ size_t pad_length, left_remaining_bytes, right_remaining_bytes;
+ size_t full_left_copies = 0, full_right_copies = 0;
+ if (!grapheme_str_pad_measure(pad, MAX(left_pad, right_pad), &pad_length,
+ left_pad, &left_remaining_bytes, right_pad, &right_remaining_bytes)) {
+ RETURN_FALSE;
+ }
+ if (pad_length < MAX(left_pad, right_pad)) {
+ full_left_copies = left_pad / pad_length;
+ full_right_copies = right_pad / pad_length;
+ if (!grapheme_str_pad_measure(pad, pad_length, &unused,
+ left_pad % pad_length, &left_remaining_bytes, right_pad % pad_length, &right_remaining_bytes)) {
+ RETURN_FALSE;
+ }
+ }
+
+ if (UNEXPECTED(full_left_copies > (ZSTR_MAX_LEN - left_remaining_bytes) / ZSTR_LEN(pad)
+ || full_right_copies > (ZSTR_MAX_LEN - right_remaining_bytes) / ZSTR_LEN(pad))) {
+ goto overflow;
+ }
+
+ {
+ size_t left_bytes = full_left_copies * ZSTR_LEN(pad) + left_remaining_bytes;
+ size_t right_bytes = full_right_copies * ZSTR_LEN(pad) + right_remaining_bytes;
+
+ if (UNEXPECTED(left_bytes > ZSTR_MAX_LEN - right_bytes
+ || ZSTR_LEN(input) > ZSTR_MAX_LEN - left_bytes - right_bytes)) {
+ goto overflow;
+ }
+
+ zend_string *result = zend_string_safe_falloc(1, ZSTR_LEN(input), left_bytes + right_bytes, false, /* fallible */ true);
+ if (UNEXPECTED(!result)) {
+ RETURN_OOM();
+ }
+ char *buffer = ZSTR_VAL(result);
+
+ buffer = grapheme_str_pad_fill(buffer, pad, full_left_copies, left_remaining_bytes);
+ memcpy(buffer, ZSTR_VAL(input), ZSTR_LEN(input));
+ buffer += ZSTR_LEN(input);
+ buffer = grapheme_str_pad_fill(buffer, pad, full_right_copies, right_remaining_bytes);
+ *buffer = '\0';
+
+ RETURN_NEW_STR(result);
+ }
+
+overflow:
+ zend_throw_error(nullptr, "String size overflow");
+ RETURN_THROWS();
+}
+/* }}} */
+
U_CFUNC PHP_FUNCTION(grapheme_strrev)
{
zend_string *string;
diff --git a/ext/intl/php_intl.stub.php b/ext/intl/php_intl.stub.php
index e132a4f670c..5141f4c7262 100644
--- a/ext/intl/php_intl.stub.php
+++ b/ext/intl/php_intl.stub.php
@@ -449,6 +449,8 @@ function grapheme_levenshtein(string $string1, string $string2, int $insertion_c
function grapheme_strrev(string $string): string|false {}
+function grapheme_str_pad(string $string, int $length, string $pad_string = " ", int $pad_type = STR_PAD_RIGHT): string|false {}
+
/** @param int $next */
function grapheme_extract(string $haystack, int $size, int $type = GRAPHEME_EXTR_COUNT, int $offset = 0, &$next = null): string|false {}
diff --git a/ext/intl/php_intl_arginfo.h b/ext/intl/php_intl_arginfo.h
index a360a0be99e..e98d5f8c2e6 100644
Binary files a/ext/intl/php_intl_arginfo.h and b/ext/intl/php_intl_arginfo.h differ
diff --git a/ext/intl/tests/grapheme_str_pad_basic.phpt b/ext/intl/tests/grapheme_str_pad_basic.phpt
new file mode 100644
index 00000000000..1e9c3ce47df
--- /dev/null
+++ b/ext/intl/tests/grapheme_str_pad_basic.phpt
@@ -0,0 +1,65 @@
+--TEST--
+grapheme_str_pad() pads to a length counted in grapheme clusters
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+// ASCII behaves like str_pad()
+var_dump(grapheme_str_pad("abc", 6, "-") === str_pad("abc", 6, "-"));
+var_dump(grapheme_str_pad("abc", 7, "xy", STR_PAD_LEFT) === str_pad("abc", 7, "xy", STR_PAD_LEFT));
+var_dump(grapheme_str_pad("abc", 8, "xy", STR_PAD_BOTH) === str_pad("abc", 8, "xy", STR_PAD_BOTH));
+
+// NFC and NFD forms of the same text get the same padding
+$nfc = "M\u{00FC}ller";
+$nfd = "Mu\u{0308}ller";
+var_dump(grapheme_str_pad($nfc, 8, "."));
+var_dump(grapheme_str_pad($nfd, 8, ".") === $nfd . "..");
+
+// Japanese with combining dakuten, Devanagari, emoji with modifier and ZWJ sequence
+var_dump(grapheme_str_pad("\u{304B}\u{3099}\u{304D}\u{3099}", 4, "*") === "\u{304B}\u{3099}\u{304D}\u{3099}**");
+var_dump(grapheme_strlen(grapheme_str_pad("नमस्ते", 5, "-")));
+var_dump(grapheme_str_pad("👍🏽", 3, "-", STR_PAD_LEFT) === "--👍🏽");
+var_dump(grapheme_str_pad("x", 4, "👨👩👧", STR_PAD_RIGHT) === "x👨👩👧👨👩👧👨👩👧");
+
+// The padding string is cut on a grapheme boundary
+var_dump(grapheme_str_pad("a", 4, "u\u{0308}o\u{0308}") === "au\u{0308}o\u{0308}u\u{0308}");
+var_dump(grapheme_str_pad("ab", 5, "\u{304B}\u{3099}x", STR_PAD_BOTH) === "\u{304B}\u{3099}ab\u{304B}\u{3099}x");
+
+// "\r\n" is a single grapheme
+var_dump(grapheme_str_pad("\r\n", 2, "-") === "\r\n-");
+
+// Only a prefix of a long padding string is used
+var_dump(grapheme_str_pad("ab", 5, "\u{304B}\u{3099}xyz", STR_PAD_BOTH) === "\u{304B}\u{3099}ab\u{304B}\u{3099}x");
+
+// Many copies of the padding string, with a partial copy at the end
+var_dump(grapheme_str_pad("x", 1001, "e\u{0301}ab", STR_PAD_LEFT) === str_repeat("e\u{0301}ab", 333) . "e\u{0301}x");
+var_dump(grapheme_str_pad("x", 1002, "\u{00B7}", STR_PAD_BOTH) === str_repeat("\u{00B7}", 500) . "x" . str_repeat("\u{00B7}", 501));
+
+// Nothing to pad
+var_dump(grapheme_str_pad("abc", 3, "-"));
+var_dump(grapheme_str_pad("abc", -1, "-"));
+var_dump(grapheme_str_pad($nfd, 6, ".") === $nfd);
+var_dump(grapheme_str_pad($nfd, 2, ".") === $nfd);
+var_dump(grapheme_str_pad("", 3));
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
+string(9) "Müller.."
+bool(true)
+bool(true)
+int(5)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+string(3) "abc"
+string(3) "abc"
+bool(true)
+bool(true)
+string(3) " "
diff --git a/ext/intl/tests/grapheme_str_pad_errors.phpt b/ext/intl/tests/grapheme_str_pad_errors.phpt
new file mode 100644
index 00000000000..6e9d22e5428
--- /dev/null
+++ b/ext/intl/tests/grapheme_str_pad_errors.phpt
@@ -0,0 +1,47 @@
+--TEST--
+grapheme_str_pad() error conditions
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+try {
+ grapheme_str_pad("abc", 5, "");
+} catch (Throwable $e) {
+ echo $e::class, ': ', $e->getMessage(), PHP_EOL;
+}
+
+try {
+ grapheme_str_pad("abc", 5, "-", 3);
+} catch (Throwable $e) {
+ echo $e::class, ': ', $e->getMessage(), PHP_EOL;
+}
+
+// No error when there is nothing to pad, like mb_str_pad(): the padding string and the pad type are not checked
+var_dump(grapheme_str_pad("abc", 2, ""));
+var_dump(grapheme_str_pad("abc", 2, "\xff"));
+var_dump(grapheme_str_pad("abc", 3, "-", 3));
+
+// Invalid UTF-8 in the input or in the padding string
+var_dump(grapheme_str_pad("a\xffb", 5, "."));
+var_dump(intl_get_error_message());
+var_dump(grapheme_str_pad("ab\u{00FC}", 5, "\xff"));
+
+// The input is always checked, even when no padding is needed
+var_dump(grapheme_str_pad("a\xffb", 1, "."));
+
+// A successful call clears the error of a previous one
+var_dump(grapheme_str_pad("ab\u{00FC}", 5, "."));
+var_dump(intl_get_error_code());
+?>
+--EXPECT--
+ValueError: grapheme_str_pad(): Argument #3 ($pad_string) must not be empty
+ValueError: grapheme_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH
+string(3) "abc"
+string(3) "abc"
+string(3) "abc"
+bool(false)
+string(62) "grapheme_str_pad(): Invalid UTF-8 string: U_INVALID_CHAR_FOUND"
+bool(false)
+bool(false)
+string(6) "abü.."
+int(0)
diff --git a/ext/intl/tests/grapheme_str_pad_merge.phpt b/ext/intl/tests/grapheme_str_pad_merge.phpt
new file mode 100644
index 00000000000..6968cfd2d36
--- /dev/null
+++ b/ext/intl/tests/grapheme_str_pad_merge.phpt
@@ -0,0 +1,32 @@
+--TEST--
+grapheme_str_pad() with padding or input fragments that merge at a join
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+// $string and each copy of $pad_string are measured on their own. A fragment that
+// attaches to its neighbour merges at the join, so the result has fewer grapheme
+// clusters than $length, as in any concatenation.
+$cases = [
+ 'padding starting with a combining mark' => ["e", 3, "\u{0301}", STR_PAD_RIGHT, "e\u{0301}\u{0301}"],
+ 'padding starting with U+200D ZWJ' => ["\u{1F468}", 3, "\u{200D}", STR_PAD_RIGHT, "\u{1F468}\u{200D}\u{200D}"],
+ 'lone regional indicator' => ["", 2, "\u{1F1EA}", STR_PAD_RIGHT, "\u{1F1EA}\u{1F1EA}"],
+ 'Hangul conjoining jamo' => ["\u{1100}", 3, "\u{1161}", STR_PAD_RIGHT, "\u{1100}\u{1161}\u{1161}"],
+ 'string starting with a combining mark' => ["\u{0301}x", 4, "a", STR_PAD_LEFT, "aa\u{0301}x"],
+ '"\r" padding before a "\n"' => ["\nx", 4, "\r", STR_PAD_LEFT, "\r\r\nx"],
+ 'string ending with "\r" before "\n" padding' => ["x\r", 4, "\n", STR_PAD_RIGHT, "x\r\n\n"],
+];
+
+foreach ($cases as $name => [$string, $length, $pad, $type, $expected]) {
+ $result = grapheme_str_pad($string, $length, $pad, $type);
+ echo $name, ': ', var_export($result === $expected, true), ', ', grapheme_strlen($result), ' of ', $length, PHP_EOL;
+}
+?>
+--EXPECT--
+padding starting with a combining mark: true, 1 of 3
+padding starting with U+200D ZWJ: true, 1 of 3
+lone regional indicator: true, 1 of 2
+Hangul conjoining jamo: true, 1 of 3
+string starting with a combining mark: true, 3 of 4
+"\r" padding before a "\n": true, 3 of 4
+string ending with "\r" before "\n" padding: true, 3 of 4
diff --git a/ext/intl/tests/grapheme_str_pad_oom.phpt b/ext/intl/tests/grapheme_str_pad_oom.phpt
new file mode 100644
index 00000000000..ac3adf43635
--- /dev/null
+++ b/ext/intl/tests/grapheme_str_pad_oom.phpt
@@ -0,0 +1,14 @@
+--TEST--
+grapheme_str_pad() respects memory_limit when the result is too large
+--EXTENSIONS--
+intl
+--INI--
+memory_limit=128M
+--FILE--
+<?php
+
+var_dump(grapheme_str_pad("a", PHP_INT_MAX, "b"));
+
+?>
+--EXPECTF--
+Fatal error: Allowed memory size of 134217728 bytes exhausted %S(tried to allocate %d bytes) in %s on line %d
diff --git a/ext/intl/tests/grapheme_str_pad_overflow.phpt b/ext/intl/tests/grapheme_str_pad_overflow.phpt
new file mode 100644
index 00000000000..3337634e616
--- /dev/null
+++ b/ext/intl/tests/grapheme_str_pad_overflow.phpt
@@ -0,0 +1,25 @@
+--TEST--
+grapheme_str_pad() throws when the result size overflows, like mb_str_pad()
+--EXTENSIONS--
+intl
+--SKIPIF--
+<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
+--FILE--
+<?php
+// One side alone does not fit
+try {
+ grapheme_str_pad("a", PHP_INT_MAX, "\u{1F600}");
+} catch (Throwable $e) {
+ echo $e::class, ': ', $e->getMessage(), PHP_EOL;
+}
+
+// Each side fits, both together do not
+try {
+ grapheme_str_pad("a", PHP_INT_MAX, "\u{00E9}", STR_PAD_BOTH);
+} catch (Throwable $e) {
+ echo $e::class, ': ', $e->getMessage(), PHP_EOL;
+}
+?>
+--EXPECT--
+Error: String size overflow
+Error: String size overflow