Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.
Merged
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
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $send($s->getString()); // same
```

A fast string manipulation library with multi-byte support.
Based on "Stringy" library, with focus on speed.
Inspired by the ["Stringy"](https://github.com/danielstjules/Stringy) library, with focus on speed.

Lib uses php7 features and does not throw any
exceptions (because all input parameters are
Expand Down Expand Up @@ -125,8 +125,35 @@ composer require str/str
- [x] toTitleCase
- [x] underscored

### Todo New
### New Features
- [x] move
- [x] overwrite
- [x] snakeize
- [x] afterFirst
- [x] beforeFirst
- [x] afterLast
- [x] beforeLast

### Todo New
- [ ] appendUniqueIdentifier
- [ ] ord
- [ ] quote
- [ ] unquote
- [ ] words
- [ ] formatNumber
- [ ] toNumber
- [ ] toInt
- [ ] toFloat
- [ ] join
- [ ] chop
- [ ] random
- [ ] pop
- [ ] shift
- [ ] shiftReversed
- [ ] popReversed
- [ ] isEmail
- [ ] isUrl
- [ ] isIp

## Optimization

Expand Down
163 changes: 163 additions & 0 deletions src/Lib/StrMB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1836,3 +1836,166 @@ function libstr_underscored(string $str): string

return libstr_delimit($innerStr, '_');
}

/** @noinspection MoreThanThreeArgumentsInspection */
/**
* Move substring of desired $length to $destination index of the original $str.
* In case $destination is less than $length returns $str untouched.
*
* @param string $str
* @param int $start
* @param int $length
* @param int $destination
* @return string
*/
function libstr_move(string $str, int $start, int $length, int $destination): string
{
$innerStr = $str;

if ($destination <= $length) { return $innerStr; }

$substr = libstr_substr($innerStr, $start, $length);
$result = libstr_insert($innerStr, $substr, $destination);

return libstr_replace($result, $substr, '', 1);
}

/** @noinspection MoreThanThreeArgumentsInspection */
/**
* Replaces substring in the original $str of $length with given $substr.
*
* @param string $str
* @param int $start
* @param int $length
* @param string $substr
* @return string
*/
function libstr_overwrite(string $str, int $start, int $length, string $substr): string
{
$innerStr = $str;

if ($length <= 0) { return $innerStr; }

$sub = libstr_substr($innerStr, $start, $length);

return libstr_replace($innerStr, $sub, $substr, 1);
}

/**
* Returns a snake_case version of the string.
*
* @todo refactoring + abbreviations support
* @param string $str
* @return string
*/
function libstr_snakeize(string $str): string
{
$innerStr = $str;

$innerStr = \mb_ereg_replace('-', '_', $innerStr);
$innerStr = \mb_ereg_replace_callback(
'([\d|A-Z])',
function ($matches) {
$match = $matches[1];
$matchInt = (int)$match;
if ("$matchInt" === $match) {
return '_' . $match . '_';
}
return '_' . libstr_toLowerCase($match);
},
$innerStr
);

$innerStr = \mb_ereg_replace('\s+', '_', $innerStr);
$innerStr = \mb_ereg_replace('^\s+|\s+$', '', $innerStr);
$innerStr = \mb_ereg_replace('_+', '_', $innerStr);

$innerStr = libstr_trim($innerStr, '_');
$innerStr = libstr_toLowerCase($innerStr);

return $innerStr;
}

/** @noinspection MoreThanThreeArgumentsInspection */
/**
* Inserts given $substr $times times into the original $str after
* the first occurrence of $needle.
*
* @param string $str
* @param string $needle
* @param string $substr
* @param int $times
* @return string
*/
function libstr_afterFirst(string $str, string $needle, string $substr, int $times = 1): string
{
$innerStr = $str;
$idx = libstr_indexOf($innerStr, $needle);
$needleLen = \mb_strlen($needle);
$idxEnd = $idx + $needleLen;
$innerSubstr = libstr_repeat($substr, $times);

return libstr_insert($innerStr, $innerSubstr, $idxEnd);
}

/** @noinspection MoreThanThreeArgumentsInspection */
/**
* Inserts given $substr $times times into the original $str before
* the first occurrence of $needle.
*
* @param string $str
* @param string $needle
* @param string $substr
* @param int $times
* @return string
*/
function libstr_beforeFirst(string $str, string $needle, string $substr, int $times = 1): string
{
$innerStr = $str;
$idx = libstr_indexOf($innerStr, $needle);
$innerSubstr = libstr_repeat($substr, $times);

return libstr_insert($innerStr, $innerSubstr, $idx);
}

/** @noinspection MoreThanThreeArgumentsInspection */
/**
* Inserts given $substr $times times into the original $str after
* the last occurrence of $needle.
*
* @param string $str
* @param string $needle
* @param string $substr
* @param int $times
* @return string
*/
function libstr_afterLast(string $str, string $needle, string $substr, int $times = 1): string
{
$innerStr = $str;
$idx = libstr_indexOfLast($innerStr, $needle);
$needleLen = \mb_strlen($needle);
$idxEnd = $idx + $needleLen;
$innerSubstr = libstr_repeat($substr, $times);

return libstr_insert($innerStr, $innerSubstr, $idxEnd);
}

/** @noinspection MoreThanThreeArgumentsInspection */
/**
* Inserts given $substr $times times into the original $str before
* the last occurrence of $needle.
*
* @param string $str
* @param string $needle
* @param string $substr
* @param int $times
* @return string
*/
function libstr_beforeLast(string $str, string $needle, string $substr, int $times = 1): string
{
$innerStr = $str;
$idx = libstr_indexOfLast($innerStr, $needle);
$innerSubstr = libstr_repeat($substr, $times);

return libstr_insert($innerStr, $innerSubstr, $idx);
}
107 changes: 107 additions & 0 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace Str;

use function Str\Lib\libstr_afterFirst;
use function Str\Lib\libstr_afterLast;
use function Str\Lib\libstr_at;
use function Str\Lib\libstr_append;
use function Str\Lib\libstr_applyPadding;
use function Str\Lib\libstr_beforeFirst;
use function Str\Lib\libstr_beforeLast;
use function Str\Lib\libstr_between;
use function Str\Lib\libstr_camelize;
use function Str\Lib\libstr_chars;
Expand Down Expand Up @@ -42,6 +46,9 @@
use function Str\Lib\libstr_isUUIDv4;
use function Str\Lib\libstr_langSpecificCharsArray;
use function Str\Lib\libstr_last;
use function Str\Lib\libstr_move;
use function Str\Lib\libstr_overwrite;
use function Str\Lib\libstr_snakeize;
use function Str\Lib\libstr_split;
use function Str\Lib\libstr_length;
use function Str\Lib\libstr_lines;
Expand Down Expand Up @@ -1219,4 +1226,104 @@ public function underscored(): Str
$this->str = libstr_underscored($this->str);
return $this;
}

/**
* Move substring of desired $length to $destination index of the original $str.
* In case $destination is less than $length returns $str untouched.
*
* @param int $start
* @param int $length
* @param int $destination
* @return Str
*/
public function move(int $start, int $length, int $destination): Str
{
$this->str = libstr_move($this->str, $start, $length, $destination);
return $this;
}

/**
* Replaces substring in the original $str of $length with given $substr.
*
* @param int $start
* @param int $length
* @param string $substr
* @return Str
*/
public function overwrite(int $start, int $length, string $substr): Str
{
$this->str = libstr_overwrite($this->str, $start, $length, $substr);
return $this;
}

/**
* Returns a snake_case version of the string.
*
* @return Str
*/
public function snakeize(): Str
{
$this->str = libstr_snakeize($this->str);
return $this;
}

/**
* Inserts given $substr $times times into the original $str after
* the first occurrence of $needle.
*
* @param string $needle
* @param string $substr
* @param int $times
* @return Str
*/
public function afterFirst(string $needle, string $substr, int $times = 1): Str
{
$this->str = libstr_afterFirst($this->str, $needle, $substr, $times);
return $this;
}

/**
* Inserts given $substr $times times into the original $str before
* the first occurrence of $needle.
*
* @param string $needle
* @param string $substr
* @param int $times
* @return Str
*/
public function beforeFirst(string $needle, string $substr, int $times = 1): Str
{
$this->str = libstr_beforeFirst($this->str, $needle, $substr, $times);
return $this;
}

/**
* Inserts given $substr $times times into the original $str after
* the last occurrence of $needle.
*
* @param string $needle
* @param string $substr
* @param int $times
* @return Str
*/
public function afterLast(string $needle, string $substr, int $times = 1): Str
{
$this->str = libstr_afterLast($this->str, $needle, $substr, $times);
return $this;
}

/**
* Inserts given $substr $times times into the original $str before
* the last occurrence of $needle.
*
* @param string $needle
* @param string $substr
* @param int $times
* @return Str
*/
public function beforeLast(string $needle, string $substr, int $times = 1): Str
{
$this->str = libstr_beforeLast($this->str, $needle, $substr, $times);
return $this;
}
}
Loading