Skip to content

Commit

Permalink
WIP: optimizing
Browse files Browse the repository at this point in the history
  • Loading branch information
irhen committed Mar 27, 2018
1 parent 77c87cb commit c504883
Show file tree
Hide file tree
Showing 7 changed files with 3,606 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ composer require str/str
- [x] hasPrefix
- [x] hasSuffix
- [x] contains
- [x] replace - add limit param
- [x] replace
- [x] toLowerCase
- [x] toUpperCase
- [x] trim
Expand Down Expand Up @@ -87,7 +87,6 @@ composer require str/str
- [x] lowerCaseFirst
- [x] regexReplace
- [x] upperCaseFirst
- [x] isUUIDv4
- [x] hasLowerCase
- [x] hasUpperCase
- [x] htmlDecode
Expand Down Expand Up @@ -125,6 +124,7 @@ composer require str/str
- [x] underscored

### New Features
- [x] replaceWithLimit
- [x] move
- [x] overwrite
- [x] snakeize
Expand All @@ -135,6 +135,7 @@ composer require str/str
- [x] isEmail
- [x] isIpV4
- [x] isIpV6
- [x] isUUIDv4
- [x] random
- [x] appendUniqueIdentifier
- [x] quote
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class SubstrBench
// (new Stringy(' hello world '))->slice(2);
// }
//
// public function bench_longest_common_substring_Str() {
// (new Str(' hello world '))->longestCommonSubstring(' helloworld');
// }
//
// public function bench_longest_common_substring_Stringy() {
// (new Stringy(' hello world '))->longestCommonSubstring(' helloworld');
// }
public function bench_longest_common_substring_Str() {
(new Str(' hello world '))->longestCommonSubstring(' helloworld');
}

public function bench_longest_common_substring_Stringy() {
(new Stringy(' hello world '))->longestCommonSubstring(' helloworld');
}
//
// public function bench_longest_common_suffix_Str() {
// (new Str(' hello world '))->longestCommonSuffix(' hello');
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions benchmarks/off/internal/ComparingLoopsBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ class ComparingLoopsBench
{
private $arr = [];
private $arr1 = [];
private $arr2 = [];
private $count = 0;

public function __construct()
{
for ($i=0;$i<1000;$i++) { $this->arr1[] = \Ramsey\Uuid\Uuid::uuid4(); }
$this->arr = $this->arr1;
$this->arr2 = $this->arr1;
$this->count = \count($this->arr2);
}

public function bench_while()
{
$t = $this->count;
while ($t--) {
$this->arr2[$t] = substr($this->arr2[$t], 0, 8);
}
}

public function bench_for()
Expand Down
53 changes: 50 additions & 3 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,12 @@ public function shuffle(): Str

public function between(string $start, string $end, int $offset = 0): Str
{
$o = [];
preg_match( '/.{' . $offset . '}' . $start . '([^' . $end . ']+)' . $end . '/u', $this->__str_buffer, $o);
$this->__str_buffer = [] !== $o ? $o[1] : '';
$posStart = \mb_strpos($this->__str_buffer, $start, $offset);
if ($posStart === false) { $this->__str_buffer = ''; return $this; }
$substrIndex = $posStart + \mb_strlen($start);
$posEnd = \mb_strpos($this->__str_buffer, $end, $substrIndex);
if ($posEnd === false || $posEnd === $substrIndex) { $this->__str_buffer = ''; return $this; }
$this->__str_buffer = \mb_substr($this->__str_buffer, $substrIndex, $posEnd - $substrIndex);
return $this;
}

Expand Down Expand Up @@ -444,4 +447,48 @@ public function split(string $pattern, int $limit = -1): array
foreach ($array as $string) { $result[] = $string; }
return $result;
}

public function longestCommonPrefix(string $otherStr): Str
{
$maxLength = min(\mb_strlen($this->__str_buffer), \mb_strlen($otherStr));
$longestCommonPrefix = '';
for ($i = 0; $i < $maxLength; $i++) {
$char = \mb_substr($this->__str_buffer, $i, 1);
if ($char === \mb_substr($otherStr, $i, 1)) { $longestCommonPrefix .= $char; } else { break; }
}
$this->__str_buffer = $longestCommonPrefix;
return $this;
}

public function longestCommonSuffix(string $otherStr): Str
{
$maxLength = min(\mb_strlen($this->__str_buffer), \mb_strlen($otherStr));
$longestCommonSuffix = '';
for ($i = 1; $i <= $maxLength; $i++) {
$char = \mb_substr($this->__str_buffer, -$i, 1);
if ($char === \mb_substr($otherStr, -$i, 1)) { $longestCommonSuffix = $char . $longestCommonSuffix; } else { break; }
}
$this->__str_buffer = $longestCommonSuffix;
return $this;
}

public function longestCommonSubstring(string $otherStr): Str
{
$strLength = \mb_strlen($this->__str_buffer);
$otherLength = \mb_strlen($otherStr);
$len = 0;
$end = 0;
$table = \array_fill(0, $strLength, \array_fill(0, $otherLength, 0));
for ($i = 1; $i <= $strLength; $i++) {
for ($j = 1; $j <= $otherLength; $j++) {
$strChar = \mb_substr($this->__str_buffer, $i - 1, 1);
$otherChar = \mb_substr($otherStr, $j - 1, 1);
if ($strChar === $otherChar) {
$table[$i][$j] = $table[$i - 1][$j - 1] + 1;
if ($table[$i][$j] > $len) { $len = $table[$i][$j]; $end = $i; } } else { $table[$i][$j] = 0; }
}
}
$this->__str_buffer = \mb_substr($this->__str_buffer, $end - $len, $len);
return $this;
}
}
Loading

0 comments on commit c504883

Please sign in to comment.