Skip to content

Commit 39b46a5

Browse files
committed
Implement Unicode conditional casing rules for Greek letter sigma
The capital Greek letter sigma (Σ) should be lowercased as σ except when it appears at the end of a word; in that case, it should be lowercased as the special form ς. This rule is included in the Unicode data file SpecialCasing.txt. The condition for applying the rule is called "Final_Sigma" and is defined in Unicode technical report 21. The rule is: • For the special casing form to apply, the capital letter sigma must be preceded by 0 or more "case-ignorable" characters, preceded by at least 1 "cased" character. • Further, capital sigma must NOT be followed by 0 or more case-ignorable characters and then at least 1 cased character. "Case-ignorable" characters include certain punctuation marks, like the apostrophe, as well as various accent marks. There are actually close to 500 different case-ignorable characters, including accent marks from Cyrillic, Hebrew, Armenian, Arabic, Syriac, Bengali, Gujarati, Telugu, Tibetan, and many other alphabets. This category also includes zero-width spaces, codepoints which indicate RTL/LTR text direction, certain musical symbols, etc. Since the rule involves scanning over "0 or more" of such case-ignorable characters, it may be necessary to scan arbitrarily far to the left and right of capital sigma to determine whether the special lowercase form should be used or not. However, since we are trying to be both memory-efficient and CPU-efficient, this implementation limits how far to the left we will scan. Generally, we scan up to 63 characters to the left looking for a "cased" character, but not more. When scanning to the right, we go up to the end of the string if necessary, even if it means scanning over thousands of characters. Anyways, it is almost impossible to imagine that natural text will include "words" with more than 63 successive apostrophes (for example) followed by a capital sigma. Closes GH-8096.
1 parent 24b311b commit 39b46a5

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ PHP NEWS
4747
. mb_detect_encoding's "non-strict" mode now behaves as described in the
4848
documentation. Previously, it would return false if the very first byte
4949
of the input string was invalid in all candidate encodings. (Alex Dowad)
50+
. mb_strtolower, mb_strtotitle, and mb_convert_case implement conditional
51+
casing rules for the Greek letter sigma. For mb_convert_case, conditional
52+
casing only applies to MB_CASE_LOWER and MB_CASE_TITLE modes, not to
53+
MB_CASE_LOWER_SIMPLE and MB_CASE_TITLE_SIMPLE. (Alex Dowad)
5054

5155
- Opcache:
5256
. Added start, restart and force restart time to opcache's

UPGRADING

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ PHP 8.3 UPGRADE NOTES
5656
"buffer_size" => int
5757
See GH-9336
5858

59+
- MBString:
60+
. mb_strtolower, mb_strtotitle, and mb_convert_case implement conditional
61+
casing rules for the Greek letter sigma. For mb_convert_case, conditional
62+
casing only applies to MB_CASE_LOWER and MB_CASE_TITLE modes, not to
63+
MB_CASE_LOWER_SIMPLE and MB_CASE_TITLE_SIMPLE. (Alex Dowad)
64+
5965
- Standard:
6066
. E_NOTICEs emitted by unserialized() have been promoted to E_WARNING.
6167
RFC: https://wiki.php.net/rfc/improve_unserialize_error_handling

ext/mbstring/php_unicode.c

+80
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,45 @@ static uint32_t *emit_special_casing_sequence(uint32_t w, uint32_t *out)
238238
return out;
239239
}
240240

241+
/* Used when determining whether special casing rules should be applied to Greek letter sigma */
242+
static bool scan_ahead_for_cased_letter(unsigned char *in, size_t in_len, unsigned int state, const mbfl_encoding *encoding)
243+
{
244+
uint32_t wchar_buf[64];
245+
246+
while (in_len) {
247+
size_t out_len = encoding->to_wchar(&in, &in_len, wchar_buf, 64, &state);
248+
ZEND_ASSERT(out_len <= 64);
249+
for (unsigned int i = 0; i < out_len; i++) {
250+
uint32_t w = wchar_buf[i];
251+
if (php_unicode_is_cased(w)) {
252+
return true;
253+
}
254+
if (!php_unicode_is_case_ignorable(w)) {
255+
return false;
256+
}
257+
}
258+
}
259+
260+
return false;
261+
}
262+
263+
/* Used when determining whether special casing rules should be applied to Greek letter sigma */
264+
static bool scan_back_for_cased_letter(uint32_t *begin, uint32_t *end)
265+
{
266+
if (end != NULL) {
267+
while (--end >= begin) {
268+
uint32_t w = *end;
269+
if (php_unicode_is_cased(w)) {
270+
return true;
271+
}
272+
if (!php_unicode_is_case_ignorable(w)) {
273+
return false;
274+
}
275+
}
276+
}
277+
return false;
278+
}
279+
241280
MBSTRING_API zend_string *php_unicode_convert_case(php_case_mode case_mode, const char *srcstr, size_t in_len, const mbfl_encoding *src_encoding, const mbfl_encoding *dst_encoding, int illegal_mode, uint32_t illegal_substchar)
242281
{
243282
/* A Unicode codepoint can expand out to up to 3 codepoints when uppercased, lowercased, or title cased
@@ -246,6 +285,9 @@ MBSTRING_API zend_string *php_unicode_convert_case(php_case_mode case_mode, cons
246285
unsigned int state = 0, title_mode = 0;
247286
unsigned char *in = (unsigned char*)srcstr;
248287
enum mbfl_no_encoding enc = src_encoding->no_encoding;
288+
/* In rare cases, we need to scan backwards through the previously converted codepoints to see
289+
* if special conversion rules should be used for the Greek letter sigma */
290+
uint32_t *converted_end = NULL;
249291

250292
mb_convert_buf buf;
251293
mb_convert_buf_init(&buf, in_len + 1, illegal_substchar, illegal_mode);
@@ -315,6 +357,43 @@ MBSTRING_API zend_string *php_unicode_convert_case(php_case_mode case_mode, cons
315357
*p++ = w;
316358
continue;
317359
}
360+
if (w == 0x3A3) {
361+
/* For Greek capital letter sigma, there is a special casing rule;
362+
* if it is the last letter in a word, it should be downcased to U+03C2
363+
* (GREEK SMALL LETTER FINAL SIGMA)
364+
* Specifically, we need to check if this codepoint is preceded by any
365+
* number of case-ignorable codepoints, preceded by a cased letter, AND
366+
* is NOT followed by any number of case-ignorable codepoints followed
367+
* by a cased letter.
368+
* Ref: http://www.unicode.org/reports/tr21/tr21-5.html
369+
* Ref: https://unicode.org/Public/UNIDATA/SpecialCasing.txt
370+
*
371+
* While the special casing rules say we should scan backwards through "any number"
372+
* of case-ignorable codepoints, that is a great implementation burden
373+
* It would basically mean we need to keep all the codepoints in a big buffer
374+
* during this conversion operation, but we don't want to do that (to reduce the
375+
* amount of temporary scratch memory used)
376+
* Hence, we only scan back through the codepoints in wchar_buf, and if we hit the
377+
* beginning of the buffer, whatever codepoints have not yet been overwritten in
378+
* the latter part of converted_buf */
379+
int j = i - 1;
380+
while (j >= 0 && php_unicode_is_case_ignorable(wchar_buf[j])) {
381+
j--;
382+
}
383+
if (j >= 0 ? php_unicode_is_cased(wchar_buf[j]) : scan_back_for_cased_letter(p, converted_end)) {
384+
/* Now scan ahead to look for a cased letter */
385+
j = i + 1;
386+
while (j < out_len && php_unicode_is_case_ignorable(wchar_buf[j])) {
387+
j++;
388+
}
389+
/* If we hit the end of wchar_buf, convert more of the input string into
390+
* codepoints and continue scanning */
391+
if (j >= out_len ? !scan_ahead_for_cased_letter(in, in_len, state, src_encoding) : !php_unicode_is_cased(wchar_buf[j])) {
392+
*p++ = 0x3C2;
393+
continue;
394+
}
395+
}
396+
}
318397
w = php_unicode_tolower_raw(w, enc);
319398
if (UNEXPECTED(w > 0xFFFFFF)) {
320399
p = emit_special_casing_sequence(w, p);
@@ -362,6 +441,7 @@ MBSTRING_API zend_string *php_unicode_convert_case(php_case_mode case_mode, cons
362441
EMPTY_SWITCH_DEFAULT_CASE()
363442
}
364443

444+
converted_end = p;
365445
ZEND_ASSERT(p - converted_buf <= 192);
366446
dst_encoding->from_wchar(converted_buf, p - converted_buf, &buf, !in_len);
367447
}

ext/mbstring/tests/casemapping.phpt

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ echo bin2hex(mb_convert_case($str, MB_CASE_UPPER_SIMPLE)), "\n";
5858
echo bin2hex(mb_convert_case($str, MB_CASE_FOLD)), "\n";
5959
echo bin2hex(mb_convert_case($str, MB_CASE_FOLD_SIMPLE)), "\n";
6060

61+
// Check handling of Greek letter capital sigma
62+
echo mb_convert_case("ΚΑΛΗΣΠΕΡΑ ΣΑΣ", MB_CASE_TITLE, "UTF-8"), "\n";
63+
echo mb_convert_case("ΚΑΛΗΣΠΕΡΑ ΣΑΣ", MB_CASE_TITLE_SIMPLE, "UTF-8"), "\n";
64+
echo mb_convert_case("ΚΑΛΗΣΠΕΡΑ ΣΑΣ", MB_CASE_LOWER, "UTF-8"), "\n";
65+
echo mb_convert_case("ΚΑΛΗΣΠΕΡΑ ΣΑΣ", MB_CASE_LOWER_SIMPLE, "UTF-8"), "\n";
66+
6167
?>
6268
--EXPECT--
6369
String: ß
@@ -109,3 +115,7 @@ dd
109115
dd
110116
69
111117
69
118+
Καλησπερα Σασ
119+
Καλησπερα Σασ
120+
καλησπερα σας
121+
καλησπερα σασ

ext/mbstring/tests/mb_strtolower_basic.phpt

+44
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ if ($mb == $greek_lower) {
3535
echo "Incorrectly converted\n";
3636
}
3737

38+
echo "\n-- Greek letter sigma --\n";
39+
var_dump(mb_strtolower("Σ", 'UTF-8'));
40+
var_dump(mb_strtolower("", 'UTF-8'));
41+
var_dump(mb_strtolower("aΣb", 'UTF-8'));
42+
var_dump(mb_strtolower("aΣ b", 'UTF-8'));
43+
var_dump(mb_strtolower(" ΣΣΣΣ ", 'UTF-8'));
44+
45+
// Apostrophe, full stop, colon, etc. are "case-ignorable"
46+
// When checking whether capital sigma is at the end of a word or not, we skip over
47+
// any number of case-ignorable characters, both when scanning back and when scanning forward
48+
var_dump(mb_strtolower("", 'UTF-8'));
49+
var_dump(mb_strtolower("ab'Σ", 'UTF-8'));
50+
var_dump(mb_strtolower("Σ'", 'UTF-8'));
51+
var_dump(mb_strtolower("Σ'a", 'UTF-8'));
52+
var_dump(mb_strtolower("a'Σ'a", 'UTF-8'));
53+
54+
// We scan back by at least 63 characters when necessary,
55+
// but there is no guarantee that we will scan back further than that
56+
var_dump(mb_strtolower('a' . str_repeat('.', 63) . "Σ", 'UTF-8'));
57+
var_dump(mb_strtolower('a' . str_repeat('.', 64) . "Σ", 'UTF-8')); // Context-sensitive casing doesn't work here!
58+
59+
// When scanning forward to confirm if capital sigma is at the end of a word or not,
60+
// there is no limit as to how far we will scan
61+
var_dump(mb_strtolower("abcΣ" . str_repeat('.', 64) . ' abc', 'UTF-8'));
62+
var_dump(mb_strtolower("abcΣ" . str_repeat('.', 64) . 'a abc', 'UTF-8'));
63+
var_dump(mb_strtolower("abcΣ" . str_repeat('.', 256) . ' abc', 'UTF-8'));
64+
3865
echo "Done";
3966
?>
4067
--EXPECT--
@@ -47,4 +74,21 @@ Correctly converted
4774
-- Multibyte String --
4875
string(64) "zrHOss6zzrTOtc62zrfOuM65zrrOu868zr3Ovs6/z4DPgc+Dz4TPhc+Gz4fPiM+J"
4976
Correctly converted
77+
78+
-- Greek letter sigma --
79+
string(2) "σ"
80+
string(3) "aς"
81+
string(4) "aσb"
82+
string(5) "aς b"
83+
string(10) " σσσς "
84+
string(3) "'σ"
85+
string(5) "ab'ς"
86+
string(3) "σ'"
87+
string(4) "σ'a"
88+
string(6) "a'σ'a"
89+
string(66) "a...............................................................ς"
90+
string(67) "a................................................................σ"
91+
string(73) "abcς................................................................ abc"
92+
string(74) "abcσ................................................................a abc"
93+
string(265) "abcς................................................................................................................................................................................................................................................................ abc"
5094
Done

0 commit comments

Comments
 (0)