Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

added mb_ str functions #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cogpowered/FineDiff/Parser/Operations/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getFromLen()
*/
public function getToLen()
{
return strlen($this->text);
return mb_strlen($this->text);
}

/**
Expand All @@ -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}";
Expand Down
4 changes: 2 additions & 2 deletions src/cogpowered/FineDiff/Parser/Operations/Replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getFromLen()
*/
public function getToLen()
{
return strlen($this->text);
return mb_strlen($this->text);
}

/**
Expand All @@ -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}";
Expand Down
30 changes: 15 additions & 15 deletions src/cogpowered/FineDiff/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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])) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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?
Expand All @@ -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));
}
}

Expand All @@ -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)) {

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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));
}
}

Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/cogpowered/FineDiff/Render/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -37,7 +37,7 @@ public function callback($opcode, $from, $from_offset, $from_len)
$html = '<del>'.htmlentities($deletion).'</del>';

} else /* if ( $opcode === 'i' ) */ {
$html = '<ins>'.htmlentities(substr($from, $from_offset, $from_len)).'</ins>';
$html = '<ins>'.htmlentities(mb_substr($from, $from_offset, $from_len)).'</ins>';
}

return $html;
Expand Down
8 changes: 4 additions & 4 deletions src/cogpowered/FineDiff/Render/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cogpowered/FineDiff/Render/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down