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
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
lib-code-tests:
./vendor/bin/phpbench run --config=bench_lib.json --report=str
./vendor/bin/phpbench run --config=bench_lib.json --report=str --iterations=10 --revs=5000

rank:
./vendor/bin/phpbench run --config=bench_lib.json --report=str | php benchmarks/score.php
./vendor/bin/phpbench run --config=bench_lib.json --report=str --iterations=10 --revs=1000 | php benchmarks/score.php

md:
./vendor/bin/phpbench run --config=bench_lib.json --report=str -o markdown > benchmark.md

internal-tests:
./vendor/bin/phpbench run --config=bench_internal.json --report=str --iterations=100 --revs=1000
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ _in dev, please do not use in production_
# str/str

```php
$s = new Str('Hello, 世界');
$s->last(2); // 世界
$s->chars(); // ['H','e','l','l','o',',',' ','世','界']
$str = new Str('Hello, 世界');
$str->last(2); // 世界
$str->chars(); // ['世', '界']

$s
->ensureLeft('H') // Hello, 世界
$str
->ensureLeft('Hello, ') // Hello, 世界
->ensureRight('!!!') // Hello, 世界!!!
->trimRight('!') // Hello, 世界
->append('Str say - '); // Str say - Hello, 世界
->prepend('Str say - '); // Str say - Hello, 世界

$send = function (string $s) {};
$send((string)$s); // same
$send($s->getString()); // same
$send((string)$str); // same
$send($str->getString()); // same
```

A fast string manipulation library with multi-byte support.
Expand Down Expand Up @@ -708,7 +708,7 @@ echo (string)$str->htmlEncode();
```
-----
### humanize
Capitalizes the first word of the string, replaces underscores with spaces, and strips '_id'.
Capitalizes the first word of the string, replaces underscores with spaces.

- __return__ *Str*

Expand Down Expand Up @@ -1192,7 +1192,7 @@ In case $destination is less than $length returns the string untouched.
__Example:__
```php
$str = new Str('/Acme/');
echo (string)$str->move(0, 2, 2);
echo (string)$str->move(0, 2, 4);
// cm/Ae/
```
-----
Expand Down Expand Up @@ -1350,7 +1350,7 @@ will consist of ASCII alphanumeric chars.
- __param__ *int* $sizeMax If given and is > $size, the generated string will have random length
between $size and $sizeMax. Defaults to -1.
- __param__ *string* $possibleChars If given, specifies allowed characters to make the string of.
Defaults to empty string.
Defaults to ASCII alphanumeric chars.
- __return__ *Str*

__Example:__
Expand All @@ -1372,7 +1372,7 @@ random length between $size and $sizeMax to the original string.
- __param__ *int* $sizeMax If given and is > $size, the generated string will have random length
between $size and $sizeMax. Defaults to -1.
- __param__ *string* $possibleChars If given, specifies allowed characters to make the string of.
Defaults to empty string.
Defaults to ASCII alphanumeric chars.
- __return__ *Str*

__Example:__
Expand Down Expand Up @@ -1456,7 +1456,7 @@ Returns the substring of the string from the last occurrence of $delimiter to th
__Example:__
```php
$str = new Str('Acme/foo');
echo $str->pop('/']);
echo $str->pop('/');
// foo
```
-----
Expand All @@ -1470,7 +1470,7 @@ of $delimiter.
__Example:__
```php
$str = new Str('Acme/foo');
echo $str->shift('/']);
echo $str->shift('/');
// Acme
```
-----
Expand All @@ -1483,7 +1483,7 @@ Returns the substring of the original string from the first occurrence of $delim
__Example:__
```php
$str = new Str('Acme/foo/bar');
echo $str->shiftReversed('/']);
echo $str->shiftReversed('/');
// foo/bar
```
-----
Expand All @@ -1497,7 +1497,7 @@ to the last occurrence of $delimiter.
__Example:__
```php
$str = new Str('Acme/foo/bar');
echo $str->popReversed('/']);
echo $str->popReversed('/');
// Acme/foo
```
-----
Expand Down
28 changes: 28 additions & 0 deletions benchmarks/internal/ComparingArrayBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

class ComparingArrayBench
{
public function bench_empty()
{
$s = [];
return empty($s);
}

public function bench_literal_comparison()
{
$s = [];
return [] !== $s;
}

public function bench_null_comparison()
{
$s = [];
return null !== $s;
}

public function bench_bool_comparison()
{
$s = [];
return (bool)$s;
}
}
25 changes: 25 additions & 0 deletions benchmarks/internal/ComparingArraySetBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

class ComparingArraySetBench
{
public function bench_access()
{
$s = [];
$s[] = 1;
$s[] = 1;
}

public function bench_unshift()
{
$s = [];
array_unshift($s, 1);
array_unshift($s, 1);
}

public function bench_push()
{
$s = [];
array_push($s, 1);
array_push($s, 1);
}
}
40 changes: 40 additions & 0 deletions benchmarks/internal/ComparingLoopsBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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()
{
for ($i=0, $iMax=count($this->arr);$i<$iMax;$i++) {
$this->arr[$i] = substr($this->arr[$i], 0, 8);
}
}

public function bench_foreach()
{
foreach ($this->arr1 as &$i) {
$i = substr($i, 0, 8);
}
unset($i);
}
}
34 changes: 34 additions & 0 deletions benchmarks/internal/ComparingStringBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

class ComparingStringBench
{
public function bench_empty()
{
$s = '';
return empty($s);
}

public function bench_literal_comparison()
{
$s = '';
return '' !== $s;
}

public function bench_null_comparison()
{
$s = '';
return null !== $s;
}

public function bench_bool_comparison()
{
$s = '';
return (bool)$s;
}

public function bench_native_bool_comparison(): bool
{
$s = '';
return $s;
}
}
28 changes: 28 additions & 0 deletions benchmarks/internal/IntCastingBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

class IntCastingBench
{
public function bench_int()
{
$s = "4dfgsdgf4";
return (int)$s;
}

public function bench_add_plus()
{
$s = "4dfgsdgf4";
return $s+0;
}

public function bench_intval()
{
$s = "4dfgsdgf4";
return intval($s);
}

public function bench_settype()
{
$s = "4dfgsdgf4";
return settype($s, 'integer');
}
}
2 changes: 1 addition & 1 deletion benchmarks/lib/ReplaceBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class ReplaceBench
{
public function bench_replace_Str() {
(new Str('oink oink oink'))->replace('k', 'ky', 3);
(new Str('oink oink oink'))->replace('k', 'ky');
}

public function bench_replace_Stringy() {
Expand Down
12 changes: 6 additions & 6 deletions benchmarks/lib/TrimBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@
class TrimBench
{
public function bench_both_Str() {
(new Str(' hello world '))->trim();
(new Str(' hello world '))->trim(' h');
}

public function bench_both_Stringy() {
(new Stringy(' hello world '))->trim();
(new Stringy(' hello world '))->trim(' h');
}

public function bench_left_Str() {
(new Str(' hello world '))->trimLeft();
(new Str(' hello world '))->trimLeft(' h');
}

public function bench_left_Stringy() {
(new Stringy(' hello world '))->trimLeft();
(new Stringy(' hello world '))->trimLeft(' h');
}

public function bench_right_Str() {
(new Str(' hello world '))->trimRight();
(new Str(' hello world '))->trimRight(' h');
}

public function bench_right_Stringy() {
(new Stringy(' hello world '))->trimRight();
(new Stringy(' hello world '))->trimRight(' h');
}
}
Loading