Futilities/v0.6/clade/Sys/IO/Store/Spider/@fx/FindDiffChars

From WoozleCodes
Jump to navigation Jump to search

About

  • Purpose: return a string that is basically the differential between the raw glob-path and a given found-path
    • This can then be appended to the target-path.
    • Possibly this function belongs in a string or filespec class, but I don't know how generally useful it's going to be; it's a little bit of a kluge to begin with.

History

  • 2026-05-05 For now, I'm only trying to support "?" and "*"; other syntax will have to take its chances.
    • If only PHP's glob functionality offered a way to retrieve more information about each match...

Code

as of 2026-05-08:

#
    protected function FindDiffChars(string $fpGlob, string $fpFound) : string {
        $nLen = strlen($fpGlob);
        $sOut = '';
        $n2 = 0;
        for ($n1=0; $n1<$nLen; $n1++) {
            $chGlob = $fpGlob[$n1];
            switch($chGlob) {
              case '?':
                $chFnd = $fpFound[$n2];
                $n2++;
                $sOut .= $chFnd;
                break;
              case '*':
                $n1++;
                $chGlob = $fpGlob[$n1];
                do {
                    $chFnd = $fpFound[$n2];
                    $isDone = $chFnd !== $chGlob;
                    if (!$isDone) {
                        $sOut .= $chFnd;
                    }
                } while (!$isDone);
                break;
              default:
                $chFnd = $fpFound[$n2];
                if ($chGlob != $chFnd) {
                    $sOut .= $chFnd;
                }
                $n2++;
            }
        }
        return $sOut;
    }