From 85109e1b7a0236b8e65333202fbbd776509a5811 Mon Sep 17 00:00:00 2001 From: Pavel Bariev Date: Tue, 28 Jun 2016 15:13:15 +0400 Subject: [PATCH] added mb_ str functions --- .../FineDiff/Parser/Operations/Insert.php | 4 +-- .../FineDiff/Parser/Operations/Replace.php | 4 +-- src/cogpowered/FineDiff/Parser/Parser.php | 30 +++++++++---------- src/cogpowered/FineDiff/Render/Html.php | 6 ++-- src/cogpowered/FineDiff/Render/Renderer.php | 8 ++--- src/cogpowered/FineDiff/Render/Text.php | 2 +- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/cogpowered/FineDiff/Parser/Operations/Insert.php b/src/cogpowered/FineDiff/Parser/Operations/Insert.php index 64a73c2..efa2631 100644 --- a/src/cogpowered/FineDiff/Parser/Operations/Insert.php +++ b/src/cogpowered/FineDiff/Parser/Operations/Insert.php @@ -46,7 +46,7 @@ public function getFromLen() */ public function getToLen() { - return strlen($this->text); + return mb_strlen($this->text); } /** @@ -62,7 +62,7 @@ public function getText() */ public function getOpcode() { - $to_len = strlen($this->text); + $to_len = mb_strlen($this->text); if ( $to_len === 1 ) { return "i:{$this->text}"; diff --git a/src/cogpowered/FineDiff/Parser/Operations/Replace.php b/src/cogpowered/FineDiff/Parser/Operations/Replace.php index 515af13..fe5f3b3 100644 --- a/src/cogpowered/FineDiff/Parser/Operations/Replace.php +++ b/src/cogpowered/FineDiff/Parser/Operations/Replace.php @@ -43,7 +43,7 @@ public function getFromLen() */ public function getToLen() { - return strlen($this->text); + return mb_strlen($this->text); } /** @@ -67,7 +67,7 @@ public function getOpcode() $del_opcode = "d{$this->fromLen}"; } - $to_len = strlen($this->text); + $to_len = mb_strlen($this->text); if ($to_len === 1) { return "{$del_opcode}i:{$this->text}"; diff --git a/src/cogpowered/FineDiff/Parser/Parser.php b/src/cogpowered/FineDiff/Parser/Parser.php index b85415b..10dbb87 100644 --- a/src/cogpowered/FineDiff/Parser/Parser.php +++ b/src/cogpowered/FineDiff/Parser/Parser.php @@ -155,7 +155,7 @@ protected function process($from_text, $to_text) // increase granularity if ($fragment instanceof Replace && $has_next_stage) { $this->process( - substr($this->from_text, $this->from_offset, $fragment->getFromLen()), + mb_substr($this->from_text, $this->from_offset, $fragment->getFromLen()), $fragment->getText() ); } @@ -195,8 +195,8 @@ protected function diff($from_text, $to_text, $delimiters) $result = array(); // fragment-level diffing - $from_text_len = strlen($from_text); - $to_text_len = strlen($to_text); + $from_text_len = mb_strlen($from_text); + $to_text_len = mb_strlen($to_text); $from_fragments = $this->extractFragments($from_text, $delimiters); $to_fragments = $this->extractFragments($to_text, $delimiters); @@ -218,7 +218,7 @@ protected function diff($from_text, $to_text, $delimiters) if ( $from_segment_length ) { $result[$from_segment_start * 4] = new Delete($from_segment_length); } else if ( $to_segment_length ) { - $result[$from_segment_start * 4 + 1] = new Insert(substr($to_text, $to_segment_start, $to_segment_length)); + $result[$from_segment_start * 4 + 1] = new Insert(mb_substr($to_text, $to_segment_start, $to_segment_length)); } continue; @@ -233,7 +233,7 @@ protected function diff($from_text, $to_text, $delimiters) while ( $from_base_fragment_index < $from_segment_end ) { $from_base_fragment = $from_fragments[$from_base_fragment_index]; - $from_base_fragment_length = strlen($from_base_fragment); + $from_base_fragment_length = mb_strlen($from_base_fragment); // performance boost: cache array keys if (!isset($cached_array_keys_for_current_segment[$from_base_fragment])) { @@ -297,7 +297,7 @@ protected function diff($from_text, $to_text, $delimiters) break; } - $fragment_length = strlen($from_fragments[$fragment_from_index]); + $fragment_length = mb_strlen($from_fragments[$fragment_from_index]); $fragment_index_offset += $fragment_length; } @@ -308,7 +308,7 @@ protected function diff($from_text, $to_text, $delimiters) } } - $from_base_fragment_index += strlen($from_base_fragment); + $from_base_fragment_index += mb_strlen($from_base_fragment); // If match is larger than half segment size, no point trying to find better // TODO: Really? @@ -328,7 +328,7 @@ protected function diff($from_text, $to_text, $delimiters) $result[$best_from_start * 4 + 2] = new Copy($best_copy_length); $jobs[] = array($best_from_start + $best_copy_length, $from_segment_end, $best_to_start + $best_copy_length, $to_segment_end); } else { - $result[$from_segment_start * 4 ] = new Replace($from_segment_length, substr($to_text, $to_segment_start, $to_segment_length)); + $result[$from_segment_start * 4 ] = new Replace($from_segment_length, mb_substr($to_text, $to_segment_start, $to_segment_length)); } } @@ -346,7 +346,7 @@ protected function diff($from_text, $to_text, $delimiters) protected function charDiff($from_text, $to_text) { $result = array(); - $jobs = array(array(0, strlen($from_text), 0, strlen($to_text))); + $jobs = array(array(0, mb_strlen($from_text), 0, mb_strlen($to_text))); while ($job = array_pop($jobs)) { @@ -362,7 +362,7 @@ protected function charDiff($from_text, $to_text) if ($from_segment_len) { $result[$from_segment_start * 4 + 0] = new Delete($from_segment_len); } else if ( $to_segment_len ) { - $result[$from_segment_start * 4 + 1] = new Insert(substr($to_text, $to_segment_start, $to_segment_len)); + $result[$from_segment_start * 4 + 1] = new Insert(mb_substr($to_text, $to_segment_start, $to_segment_len)); } continue; @@ -379,7 +379,7 @@ protected function charDiff($from_text, $to_text) while ($to_copy_start <= $to_copy_start_max) { - $from_copy_start = strpos(substr($from_text, $from_segment_start, $from_segment_len), substr($to_text, $to_copy_start, $copy_len)); + $from_copy_start = mb_strpos(mb_substr($from_text, $from_segment_start, $from_segment_len), mb_substr($to_text, $to_copy_start, $copy_len)); if ($from_copy_start !== false) { $from_copy_start += $from_segment_start; @@ -402,7 +402,7 @@ protected function charDiff($from_text, $to_text) while ($from_copy_start <= $from_copy_start_max) { - $to_copy_start = strpos(substr($to_text, $to_segment_start, $to_segment_len), substr($from_text, $from_copy_start, $copy_len)); + $to_copy_start = mb_strpos(mb_substr($to_text, $to_segment_start, $to_segment_len), mb_substr($from_text, $from_copy_start, $copy_len)); if ($to_copy_start !== false) { $to_copy_start += $to_segment_start; @@ -424,7 +424,7 @@ protected function charDiff($from_text, $to_text) } // no match, so delete all, insert all else { - $result[$from_segment_start * 4] = new Replace($from_segment_len, substr($to_text, $to_segment_start, $to_segment_len)); + $result[$from_segment_start * 4] = new Replace($from_segment_len, mb_substr($to_text, $to_segment_start, $to_segment_len)); } } @@ -448,7 +448,7 @@ protected function extractFragments($text, $delimiters) // special case: split into characters if (empty($delimiters)) { $chars = str_split($text, 1); - $chars[strlen($text)] = ''; + $chars[mb_strlen($text)] = ''; return $chars; } @@ -466,7 +466,7 @@ protected function extractFragments($text, $delimiters) break; } - $fragments[$start] = substr($text, $start, $end - $start); + $fragments[$start] = mb_substr($text, $start, $end - $start); $start = $end; } diff --git a/src/cogpowered/FineDiff/Render/Html.php b/src/cogpowered/FineDiff/Render/Html.php index 3788598..7060ee7 100644 --- a/src/cogpowered/FineDiff/Render/Html.php +++ b/src/cogpowered/FineDiff/Render/Html.php @@ -25,10 +25,10 @@ class Html extends Renderer public function callback($opcode, $from, $from_offset, $from_len) { if ($opcode === 'c') { - $html = htmlentities(substr($from, $from_offset, $from_len)); + $html = htmlentities(mb_substr($from, $from_offset, $from_len)); } else if ($opcode === 'd') { - $deletion = substr($from, $from_offset, $from_len); + $deletion = mb_substr($from, $from_offset, $from_len); if (strcspn($deletion, " \n\r") === 0) { $deletion = str_replace(array("\n","\r"), array('\n','\r'), $deletion); @@ -37,7 +37,7 @@ public function callback($opcode, $from, $from_offset, $from_len) $html = ''.htmlentities($deletion).''; } else /* if ( $opcode === 'i' ) */ { - $html = ''.htmlentities(substr($from, $from_offset, $from_len)).''; + $html = ''.htmlentities(mb_substr($from, $from_offset, $from_len)).''; } return $html; diff --git a/src/cogpowered/FineDiff/Render/Renderer.php b/src/cogpowered/FineDiff/Render/Renderer.php index 7d848ea..fd4efa5 100644 --- a/src/cogpowered/FineDiff/Render/Renderer.php +++ b/src/cogpowered/FineDiff/Render/Renderer.php @@ -43,18 +43,18 @@ public function process($from_text, $opcodes) // Holds the generated string that is returned $output = ''; - $opcodes_len = strlen($opcodes); + $opcodes_len = mb_strlen($opcodes); $from_offset = 0; $opcodes_offset = 0; while ($opcodes_offset < $opcodes_len) { - $opcode = substr($opcodes, $opcodes_offset, 1); + $opcode = mb_substr($opcodes, $opcodes_offset, 1); $opcodes_offset++; - $n = intval(substr($opcodes, $opcodes_offset)); + $n = intval(mb_substr($opcodes, $opcodes_offset)); if ($n) { - $opcodes_offset += strlen(strval($n)); + $opcodes_offset += mb_strlen(strval($n)); } else { $n = 1; } diff --git a/src/cogpowered/FineDiff/Render/Text.php b/src/cogpowered/FineDiff/Render/Text.php index 699e864..42be0d0 100644 --- a/src/cogpowered/FineDiff/Render/Text.php +++ b/src/cogpowered/FineDiff/Render/Text.php @@ -23,7 +23,7 @@ class Text extends Renderer public function callback($opcode, $from, $from_offset, $from_len) { if ($opcode === 'c' || $opcode === 'i') { - return substr($from, $from_offset, $from_len); + return mb_substr($from, $from_offset, $from_len); } return '';