Added methods to count unique characters in a string.

This commit is contained in:
flash 2023-07-05 01:28:33 +00:00
parent bce5ba77a2
commit 8f908d7f68
8 changed files with 101053 additions and 7 deletions

View file

@ -1 +1 @@
0.2302.12024
0.2307.50127

View file

@ -1,7 +1,7 @@
<?php
// AString.php
// Created: 2021-04-26
// Updated: 2023-01-01
// Updated: 2023-07-05
namespace Index;
@ -206,6 +206,10 @@ final class AString implements IString {
return new AString(strrev($this->value));
}
public function countUnique(): int {
return XString::countUnique($this->value);
}
public function startsWith(IString|string $text): bool {
return str_starts_with($this->value, (string)$text);
}

View file

@ -1,7 +1,7 @@
<?php
// IString.php
// Created: 2021-06-20
// Updated: 2022-02-27
// Updated: 2023-07-05
namespace Index;
@ -179,6 +179,13 @@ interface IString extends ArrayAccess, Countable, IteratorAggregate, JsonSeriali
*/
function reverse(): IString;
/**
* Counts unique characters in a string.
*
* @return int Unique character count.
*/
function countUnique(): int;
/**
* Returns a copy of the standard PHP string.
*

View file

@ -1,7 +1,7 @@
<?php
// WString.php
// Created: 2021-06-22
// Updated: 2022-02-27
// Updated: 2023-07-05
namespace Index;
@ -166,6 +166,16 @@ final class WString implements IString {
return new WString(implode(array_reverse($chars)), $this->encoding);
}
public function countUnique(): int {
$chars = [];
foreach($this as $char)
if(!in_array($char, $chars, true))
$chars[] = $char;
return count($chars);
}
public function __toString(): string {
return $this->value;
}

View file

@ -1,7 +1,7 @@
<?php
// XString.php
// Created: 2022-02-03
// Updated: 2023-01-05
// Updated: 2023-07-05
namespace Index;
@ -39,6 +39,23 @@ final class XString {
return htmlspecialchars($value, $flags, $encoding, $doubleEncoding);
}
/**
* Counts unique characters in a string.
*
* @param string String to count unique characters of.
* @return int Unique character count.
*/
public static function countUnique(string $string): int {
$string = str_split($string);
$chars = [];
foreach($string as $char)
if(!in_array($char, $chars, true))
$chars[] = $char;
return count($chars);
}
/**
* Check if a string is null or empty.
*

View file

@ -1,7 +1,7 @@
<?php
// StringTest.php
// Created: 2021-04-26
// Updated: 2022-02-27
// Updated: 2023-07-05
declare(strict_types=1);
@ -278,4 +278,10 @@ final class StringTest extends TestCase {
)->equals(new WString("\x82\xA0\x82\xA6\x82\xA8\x82\xA6\x82\xA8", 'sjis'))
);
}
public function testCountUnique(): void {
$this->assertEquals(10, XString::countUnique('iaabbccddjjefghefghi'));
$this->assertEquals(11, (new AString('jeff has three apples'))->countUnique());
$this->assertEquals(5, (new WString('みさかみこと'))->countUnique());
}
}

101002
tools/phpunit Executable file

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,5 @@
<?php
// this file sucks, screw up the header if incomplete
// this file sucks, screws up the header if incomplete
// successfully corrects filenames though
// fix this if you ever feel like it, or don't