Skip to content

Commit

Permalink
Code tidy
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
  • Loading branch information
jfcherng committed Feb 21, 2019
1 parent 7caae81 commit b3b0595
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
36 changes: 3 additions & 33 deletions src/Renderer/Html/LineRenderer/Word.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,10 @@ final class Word extends AbstractLineRenderer
*/
public function render(MbString $mbOld, MbString $mbNew): LineRendererInterface
{
static $punctuationsRange = (
// Latin-1 Supplement
// @see https://unicode-table.com/en/blocks/latin-1-supplement/
"\u{0080}-\u{00BB}" .
// Spacing Modifier Letters
// @see https://unicode-table.com/en/blocks/spacing-modifier-letters/
"\u{02B0}-\u{02FF}" .
// Combining Diacritical Marks
// @see https://unicode-table.com/en/blocks/combining-diacritical-marks/
"\u{0300}-\u{036F}" .
// Small Form Variants
// @see https://unicode-table.com/en/blocks/small-form-variants/
"\u{FE50}-\u{FE6F}" .
// General Punctuation
// @see https://unicode-table.com/en/blocks/general-punctuation/
"\u{2000}-\u{206F}" .
// Supplemental Punctuation
// @see https://unicode-table.com/en/blocks/supplemental-punctuation/
"\u{2E00}-\u{2E7F}" .
// CJK Symbols and Punctuation
// @see https://unicode-table.com/en/blocks/cjk-symbols-and-punctuation/
"\u{3000}-\u{303F}" .
// Ideographic Symbols and Punctuation
// @see https://unicode-table.com/en/blocks/ideographic-symbols-and-punctuation/
"\u{16FE0}-\u{16FFF}" .
// hmm... seems to be no rule
" \t$,.:;!?'\"()\[\]{}%@<=>_+\-*\/~\\\\|" .
' $,.:;!?’"()[]{}%@<=>_+-*/~\|' .
'「」『』〈〉《》【】()()‘’“”' .
'.‧・・•·¿'
);
static $splitRegex = '/([' . RendererConstant::PUNCTUATIONS_RANGE . ']++)/uS';

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

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

Expand Down
39 changes: 39 additions & 0 deletions src/Renderer/RendererConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,43 @@ final class RendererConstant
* @var string
*/
const IMPLODE_DELIMITER = "\u{ff2fa}\u{fcffc}\u{fff42}";

/**
* Regex range for punctuations.
*
* Assuming regex delimiter is "/".
*
* @var string
*/
const PUNCTUATIONS_RANGE = (
// Latin-1 Supplement
// @see https://unicode-table.com/en/blocks/latin-1-supplement/
"\u{0080}-\u{00BB}" .
// Spacing Modifier Letters
// @see https://unicode-table.com/en/blocks/spacing-modifier-letters/
"\u{02B0}-\u{02FF}" .
// Combining Diacritical Marks
// @see https://unicode-table.com/en/blocks/combining-diacritical-marks/
"\u{0300}-\u{036F}" .
// Small Form Variants
// @see https://unicode-table.com/en/blocks/small-form-variants/
"\u{FE50}-\u{FE6F}" .
// General Punctuation
// @see https://unicode-table.com/en/blocks/general-punctuation/
"\u{2000}-\u{206F}" .
// Supplemental Punctuation
// @see https://unicode-table.com/en/blocks/supplemental-punctuation/
"\u{2E00}-\u{2E7F}" .
// CJK Symbols and Punctuation
// @see https://unicode-table.com/en/blocks/cjk-symbols-and-punctuation/
"\u{3000}-\u{303F}" .
// Ideographic Symbols and Punctuation
// @see https://unicode-table.com/en/blocks/ideographic-symbols-and-punctuation/
"\u{16FE0}-\u{16FFF}" .
// hmm... these seem to be no rule
" \t$,.:;!?'\"()\[\]{}%@<=>_+\-*\/~\\\\|" .
' $,.:;!?’"()[]{}%@<=>_+-*/~\|' .
'「」『』〈〉《》【】()()‘’“”' .
'.‧・・•·¿'
);
}
11 changes: 6 additions & 5 deletions src/Utility/ReverseIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

final class ReverseIterator
{
const ITERATOR_GET_KEY = 1 << 1;
const ITERATOR_GET_BOTH = 1 << 2;
const ITERATOR_GET_KEY = 1 << 0;
const ITERATOR_GET_BOTH = 1 << 1;

/**
* The constructor.
Expand All @@ -20,14 +20,13 @@ private function __construct()
* Iterate the array reversely.
*
* @param array $array the array
* @param int $flags the flags
*
* @return \Generator
*/
public static function fromArray(array $array, int $flags = 0): \Generator
{
// it may worth unrolling if-conditions to be out of for-loop
// so it does have to check if-conditions in each iteration

// iterate [key => value] pair
if ($flags & self::ITERATOR_GET_BOTH) {
for (\end($array); ($key = \key($array)) !== null; \prev($array)) {
yield $key => \current($array);
Expand All @@ -36,6 +35,7 @@ public static function fromArray(array $array, int $flags = 0): \Generator
return;
}

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

// iterate only value
for (\end($array); \key($array) !== null; \prev($array)) {
yield \current($array);
}
Expand Down

0 comments on commit b3b0595

Please sign in to comment.