|
| 1 | +--TEST-- |
| 2 | +IntlRuleBasedBreakIterator compiled rules outlive the source string |
| 3 | +--EXTENSIONS-- |
| 4 | +intl |
| 5 | +--SKIPIF-- |
| 6 | +<?php if (version_compare(INTL_ICU_VERSION, '68.1') < 0) die('skip for ICU >= 68.1'); ?> |
| 7 | +--FILE-- |
| 8 | +<?php |
| 9 | + |
| 10 | +$rules = <<<RULES |
| 11 | +\$LN = [[:letter:] [:number:]]; |
| 12 | +\$S = [.;,:]; |
| 13 | +
|
| 14 | +!!forward; |
| 15 | +\$LN+ {1}; |
| 16 | +\$S+ {42}; |
| 17 | +!!reverse; |
| 18 | +\$LN+ {1}; |
| 19 | +\$S+ {42}; |
| 20 | +!!safe_forward; |
| 21 | +!!safe_reverse; |
| 22 | +RULES; |
| 23 | + |
| 24 | +$src = new IntlRuleBasedBreakIterator($rules); |
| 25 | +$len = strlen($src->getBinaryRules()); |
| 26 | + |
| 27 | +$it = new IntlRuleBasedBreakIterator($src->getBinaryRules(), true); |
| 28 | +unset($src); |
| 29 | + |
| 30 | +/* ICU aliases the buffer it was built from, so the freed rules have to be |
| 31 | + reclaimed and overwritten for the iterator below to read stale bytes. */ |
| 32 | +$ballast = []; |
| 33 | +for ($i = 0; $i < 16; $i++) { |
| 34 | + $ballast[] = str_repeat("\xCC", $len); |
| 35 | +} |
| 36 | + |
| 37 | +$it->setText('ab,cd'); |
| 38 | +echo $it->first(), "\n"; |
| 39 | +while (true) { |
| 40 | + $n = $it->next(); |
| 41 | + if ($n === IntlBreakIterator::DONE) { |
| 42 | + break; |
| 43 | + } |
| 44 | + echo $n, "\n"; |
| 45 | +} |
| 46 | + |
| 47 | +$clone = clone $it; |
| 48 | +unset($it); |
| 49 | +$ballast[] = str_repeat("\xDD", $len); |
| 50 | +$clone->setText('xy'); |
| 51 | +echo $clone->first(), "\n"; |
| 52 | +echo $clone->next(), "\n"; |
| 53 | + |
| 54 | +$src = new IntlRuleBasedBreakIterator($rules); |
| 55 | +$it = new IntlRuleBasedBreakIterator($src->getBinaryRules(), true); |
| 56 | +unset($src); |
| 57 | +for ($i = 0; $i < 16; $i++) { |
| 58 | + $ballast[] = str_repeat("\xEE", $len); |
| 59 | +} |
| 60 | +$it->setText('ab,cd'); |
| 61 | +$parts = $it->getPartsIterator(); |
| 62 | +unset($it); |
| 63 | +foreach ($parts as $p) { |
| 64 | + echo $p, "\n"; |
| 65 | +} |
| 66 | + |
| 67 | +?> |
| 68 | +--EXPECT-- |
| 69 | +0 |
| 70 | +2 |
| 71 | +3 |
| 72 | +5 |
| 73 | +0 |
| 74 | +2 |
| 75 | +ab |
| 76 | +, |
| 77 | +cd |
0 commit comments