Ferreteria/v0.6/clade/Sys/Data/Things/Array/types/AStr

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | Sys‎ | Data‎ | Things‎ | Array
Revision as of 18:26, 9 October 2025 by Woozle (talk | contribs) (Created page with "{{page/clade/v2 |fam= {{!}} align=right {{!}} ''(root)'' {{!}} align=center {{!}} → {{l/ver/clade|Sys\Data\Things\Array\types|AStr}} → {{!}} align=left {{!}} {{l/ver/clade/full|p=ferreteria|Sys\Data\Things\Array\DStor|AStr}} |alia= {{!}}- {{!}} '''QStr'''* [c,i] {{!!}} {{l/ver/clade/full|p=ferreteria|Data\Mem\QVar|Str}} }} ==Code== ''as of 2025-10-09'' {{fmt/php/block|1= interface iAStr { // SEARCH function StartsWith(string $sAsk) : ?string; } trait...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
clade: Sys\Data\Things\Array\types\AStr
Clade Family
(root) AStr Sys\Data\Things\Array\DStor\AStr
Clade Aliases
Alias Clade
QStr* [c,i] Data\Mem\QVar\Str
Subpages

Code

as of 2025-10-09

interface iAStr {
    // SEARCH
    function StartsWith(string $sAsk) : ?string;
}
trait tAStr { // IMPLEMENTS iAStr

    // ++ CONFIG ++ //

    protected function QVarClass(int|string $snKey) : string { return QStrClass::class; }

    // -- CONFIG -- //
    // ++ SEARCH ++ //

    /**
     * ACTION: find the best match for the given Ask string; return NULL if no match
     * NOTE: for now, I'm allowing *either* A-begins-B *or* B-begins-A. That should probably be settable by flags.
     */
    public function StartsWith(string $sAsk) : ?string {
        $nMatch = 0;
        $sFnd = NULL;
        $nlAsk = strlen($sAsk);
        foreach ($this as $sKey => $sVal) {
            if ($sKey === $sAsk) {
                // identical key found
                $sFnd = $sKey;
            } elseif (str_starts_with($sKey,$sAsk)) {
                // does this key-string  start with the Ask string?
                if ($nlAsk > $nMatch) {
                    $sFnd = $sKey;
                    $nMatch = $nlAsk;
                    #echo CRLF.' -- BEST';
                }
            } elseif (str_starts_with($sAsk,$sKey)) {
                // does the Ask string start with this key-string?
                $nlKey = strlen($sKey);
                if ($nlKey > $nMatch) {
                    $sFnd = $sKey;
                    $nMatch = $nlKey;
                }
            }
        }
        return $sFnd;
    }

    // -- SEARCH -- //
}