Skip to content

Commit

Permalink
Added String.substring()
Browse files Browse the repository at this point in the history
  • Loading branch information
fadrian06 committed Jan 6, 2024
1 parent 8712884 commit fcd521e
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Tasks.todo
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ JSPHP:
☐ String.slice()
☐ String.split()
☐ String.startsWith()
String.substring()
String.substring() @done (06/01/2024 16:00:51)
☐ String.toLocaleLowerCase()
☐ String.toLocaleUpperCase()
✔ String.toLowerCase() @done (6/12/2023, 0:45:36)
Expand Down
5 changes: 4 additions & 1 deletion jsphp.sublime-project
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@
{
"name": "Coverage",
"shell_cmd": "composer coverage -- --colors=never",
"quiet": true
"quiet": true,
}
],
"debugger_configurations":
[
],
}
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache">
cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerWarnings="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
Expand Down
36 changes: 36 additions & 0 deletions src/JSString.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,42 @@ function indexOf(?string $searchString = null, int $position = 0): int {
return $position === false ? -1 : $position;
}

/**
* Returns the substring at the specified location within a String object.
* @param int $start The zero-based index number indicating the beginning of the substring.
* @param ?int $end Zero-based index number indicating the end of the substring. The substring includes the characters up to, but not including, the character indicated by end.
* If end is omitted, the characters from start through the end of the original string are returned.
*/
function substring(int $start, ?int $end = null): self {
$result = '';

if ($end === null) {
$end = $this->length;
}

if ($start < 0) {
$start = 0;
}

if ($end < 0) {
$end = 0;
}

if ($start > $end) {
return $this->substring($end, $start);
}

for ($i = $start; $i < $end; ++$i) {
if (!isset($this->value[$i])) {
break;
}

$result .= $this->value[$i];
}

return new self($result);
}

/**
* Returns the character at the specified index.
* @param int|float $pos The zero-based index of the desired character.
Expand Down
83 changes: 83 additions & 0 deletions tests/PHP/JSString/substringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Tests\PHP\JSString;

use JSString;
use PHPUnit\Framework\TestCase;

require_once __DIR__ . '/../../../vendor/autoload.php';

final class substringTest extends TestCase {
function test_Demo_String_substring(): void {
$str = String('Mozilla');

self::assertEquals('oz', $str->substring(1, 3));
self::assertEquals('zilla', $str->substring(2));
}

function test_Using_substring(): void {
$anyString = String('Mozilla');

self::assertEquals('M', $anyString->substring(0, 1));
self::assertEquals('M', $anyString->substring(1, 0));
self::assertEquals('Mozill', $anyString->substring(0, 6));
self::assertEquals('lla', $anyString->substring(4));
self::assertEquals('lla', $anyString->substring(4, 7));
self::assertEquals('lla', $anyString->substring(7, 4));
self::assertEquals('Mozilla', $anyString->substring(0, 7));
self::assertEquals('Mozilla', $anyString->substring(0, 10));
}

function test_Using_substring_with_length_property(): void {
$text = String('Mozilla');

self::assertEquals('illa', $text->substring($text->length - 4));
self::assertEquals('zilla', $text->substring($text->length - 5));
}

function test_The_difference_between_substring_and_substr(): void {
$text = String('Mozilla');

self::assertEquals('zil', $text->substring(2, 5));
// self::assertEquals('zil', $text->substr(2, 3)); // TODO
}

function test_Differences_between_substring_and_slice(): void {
$text = String('Mozilla');

self::assertEquals('zil', $text->substring(5, 2));
// self::assertEquals("", $text->slice(5, 2)); // TODO

/* If either or both of the arguments are negative or NaN, the substring()
method treats them as if they were 0. */
self::assertEquals('Mo', $text->substring(-5, 2));
self::assertEquals('', $text->substring(-5, -2));

/* slice() also treats NaN arguments as 0, but when it is given negative
values it counts backwards from the end of the string to find the indexes. */
// self::assertEquals('', $text->slice(-5, 2)); // TODO
// self::assertEquals('zil', $text->slice(-5, -2)); // TODO
}

function test_Replacing_a_substring_within_a_string(): void {
// Replaces oldS with newS in the string fullS
$replaceString = function (JSString $oldS, string $newS, JSString $fullS): JSString {
for ($i = 0; $i < $fullS->length; ++$i) {
if ($fullS->substring($i, $i + $oldS->length) == $oldS) {
$fullS = String($fullS->substring(0, $i) . $newS . $fullS->substring($i + $oldS->length, $fullS->length));
}
}

return $fullS;
};

// $replaceStringBetterMethod = function (string $oldS, string $newS, JSString $fullS): JSString {
// return $fullS->split($oldS)->join($newS); // TODO
// };

self::assertEquals('Brave New Web', $replaceString(String('World'), 'Web', String('Brave New World')));
// self::assertEquals('Brave New Web', $replaceStringBetterMethod('World', 'Web', String('Brave New World')));
}
}

0 comments on commit fcd521e

Please sign in to comment.