Skip to content

Commit b3b0595

Browse files
committed
Code tidy
Signed-off-by: Jack Cherng <[email protected]>
1 parent 7caae81 commit b3b0595

File tree

3 files changed

+48
-38
lines changed

3 files changed

+48
-38
lines changed

src/Renderer/Html/LineRenderer/Word.php

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,10 @@ final class Word extends AbstractLineRenderer
1616
*/
1717
public function render(MbString $mbOld, MbString $mbNew): LineRendererInterface
1818
{
19-
static $punctuationsRange = (
20-
// Latin-1 Supplement
21-
// @see https://unicode-table.com/en/blocks/latin-1-supplement/
22-
"\u{0080}-\u{00BB}" .
23-
// Spacing Modifier Letters
24-
// @see https://unicode-table.com/en/blocks/spacing-modifier-letters/
25-
"\u{02B0}-\u{02FF}" .
26-
// Combining Diacritical Marks
27-
// @see https://unicode-table.com/en/blocks/combining-diacritical-marks/
28-
"\u{0300}-\u{036F}" .
29-
// Small Form Variants
30-
// @see https://unicode-table.com/en/blocks/small-form-variants/
31-
"\u{FE50}-\u{FE6F}" .
32-
// General Punctuation
33-
// @see https://unicode-table.com/en/blocks/general-punctuation/
34-
"\u{2000}-\u{206F}" .
35-
// Supplemental Punctuation
36-
// @see https://unicode-table.com/en/blocks/supplemental-punctuation/
37-
"\u{2E00}-\u{2E7F}" .
38-
// CJK Symbols and Punctuation
39-
// @see https://unicode-table.com/en/blocks/cjk-symbols-and-punctuation/
40-
"\u{3000}-\u{303F}" .
41-
// Ideographic Symbols and Punctuation
42-
// @see https://unicode-table.com/en/blocks/ideographic-symbols-and-punctuation/
43-
"\u{16FE0}-\u{16FFF}" .
44-
// hmm... seems to be no rule
45-
" \t$,.:;!?'\"()\[\]{}%@<=>_+\-*\/~\\\\|" .
46-
' $,.:;!?’"()[]{}%@<=>_+-*/~\|' .
47-
'「」『』〈〉《》【】()()‘’“”' .
48-
'.‧・・•·¿'
49-
);
19+
static $splitRegex = '/([' . RendererConstant::PUNCTUATIONS_RANGE . ']++)/uS';
5020

51-
$oldWords = $mbOld->toArraySplit("/([{$punctuationsRange}]++)/uS", -1, \PREG_SPLIT_DELIM_CAPTURE);
52-
$newWords = $mbNew->toArraySplit("/([{$punctuationsRange}]++)/uS", -1, \PREG_SPLIT_DELIM_CAPTURE);
21+
$oldWords = $mbOld->toArraySplit($splitRegex, -1, \PREG_SPLIT_DELIM_CAPTURE);
22+
$newWords = $mbNew->toArraySplit($splitRegex, -1, \PREG_SPLIT_DELIM_CAPTURE);
5323

5424
$opcodes = $this->getChangedExtentSegments($oldWords, $newWords);
5525

src/Renderer/RendererConstant.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,43 @@ final class RendererConstant
3131
* @var string
3232
*/
3333
const IMPLODE_DELIMITER = "\u{ff2fa}\u{fcffc}\u{fff42}";
34+
35+
/**
36+
* Regex range for punctuations.
37+
*
38+
* Assuming regex delimiter is "/".
39+
*
40+
* @var string
41+
*/
42+
const PUNCTUATIONS_RANGE = (
43+
// Latin-1 Supplement
44+
// @see https://unicode-table.com/en/blocks/latin-1-supplement/
45+
"\u{0080}-\u{00BB}" .
46+
// Spacing Modifier Letters
47+
// @see https://unicode-table.com/en/blocks/spacing-modifier-letters/
48+
"\u{02B0}-\u{02FF}" .
49+
// Combining Diacritical Marks
50+
// @see https://unicode-table.com/en/blocks/combining-diacritical-marks/
51+
"\u{0300}-\u{036F}" .
52+
// Small Form Variants
53+
// @see https://unicode-table.com/en/blocks/small-form-variants/
54+
"\u{FE50}-\u{FE6F}" .
55+
// General Punctuation
56+
// @see https://unicode-table.com/en/blocks/general-punctuation/
57+
"\u{2000}-\u{206F}" .
58+
// Supplemental Punctuation
59+
// @see https://unicode-table.com/en/blocks/supplemental-punctuation/
60+
"\u{2E00}-\u{2E7F}" .
61+
// CJK Symbols and Punctuation
62+
// @see https://unicode-table.com/en/blocks/cjk-symbols-and-punctuation/
63+
"\u{3000}-\u{303F}" .
64+
// Ideographic Symbols and Punctuation
65+
// @see https://unicode-table.com/en/blocks/ideographic-symbols-and-punctuation/
66+
"\u{16FE0}-\u{16FFF}" .
67+
// hmm... these seem to be no rule
68+
" \t$,.:;!?'\"()\[\]{}%@<=>_+\-*\/~\\\\|" .
69+
' $,.:;!?’"()[]{}%@<=>_+-*/~\|' .
70+
'「」『』〈〉《》【】()()‘’“”' .
71+
'.‧・・•·¿'
72+
);
3473
}

src/Utility/ReverseIterator.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
final class ReverseIterator
88
{
9-
const ITERATOR_GET_KEY = 1 << 1;
10-
const ITERATOR_GET_BOTH = 1 << 2;
9+
const ITERATOR_GET_KEY = 1 << 0;
10+
const ITERATOR_GET_BOTH = 1 << 1;
1111

1212
/**
1313
* The constructor.
@@ -20,14 +20,13 @@ private function __construct()
2020
* Iterate the array reversely.
2121
*
2222
* @param array $array the array
23+
* @param int $flags the flags
2324
*
2425
* @return \Generator
2526
*/
2627
public static function fromArray(array $array, int $flags = 0): \Generator
2728
{
28-
// it may worth unrolling if-conditions to be out of for-loop
29-
// so it does have to check if-conditions in each iteration
30-
29+
// iterate [key => value] pair
3130
if ($flags & self::ITERATOR_GET_BOTH) {
3231
for (\end($array); ($key = \key($array)) !== null; \prev($array)) {
3332
yield $key => \current($array);
@@ -36,6 +35,7 @@ public static function fromArray(array $array, int $flags = 0): \Generator
3635
return;
3736
}
3837

38+
// iterate only key
3939
if ($flags & self::ITERATOR_GET_KEY) {
4040
for (\end($array); ($key = \key($array)) !== null; \prev($array)) {
4141
yield $key;
@@ -44,6 +44,7 @@ public static function fromArray(array $array, int $flags = 0): \Generator
4444
return;
4545
}
4646

47+
// iterate only value
4748
for (\end($array); \key($array) !== null; \prev($array)) {
4849
yield \current($array);
4950
}

0 commit comments

Comments
 (0)