Skip to content

Core\Path

Reads and builds file paths as text. It never touches the disk. Every method accepts / and \ as separators on every platform, and writes Path::SEPARATOR.

Core\Path is string algebra over path text: no member reads the disk, so nothing here needs a capability, follows a symlink or checks that a file exists — that is Core\IO. Both / and \ are accepted on every platform, and a member that answers a path renders it with Core\Path::SEPARATOR — \ on Windows, / elsewhere. join only appends: a segment's own leading separator or drive is dropped rather than allowed to replace the base. normalize resolves . and .. lexically; neither member makes an untrusted path safe, which is Core\IO::within. Absence is null: extension on a name without one, relativeTo where no relative path exists.

matches checks whether a path matches a pattern, such as *.jpg or src/**/*.nvs. The pattern syntax is the same on every system. A pattern without a / is compared with the file name at any depth. A pattern with a / is compared from the start of the path. * matches any text inside one folder name, and ** matches any number of folders. Core\IO::walk uses the same syntax in its skip option.

<?nvs
class Show
{
// A rendered path carries the platform's separator; print it as `/` either way.
public static function slash(string $p): string
{
return Core\Str::replace($p, Core\Path::SEPARATOR, '/');
}
}
string $p = '/var/www/html/index.php';
echo Core\Path::fileName($p), ' ', Core\Path::fileName($p, { withoutExtension: true }), ' ', Core\Path::extension($p) ?? 'none', "\n";
echo Show::slash(Core\Path::parent($p)), ' ', Show::slash(Core\Path::parent($p, { levels: 2 })), "\n";
echo Show::slash(Core\Path::withExtension($p, 'html')), "\n";
echo Show::slash(Core\Path::join('/var/www', 'html', '/index.php')), "\n";
echo Show::slash(Core\Path::normalize('/var/www/../log/./app.log')), "\n";
echo Show::slash(Core\Path::relativeTo('/var/log/app.log', '/var/www') ?? 'none'), "\n";
echo Core\Str::join(Core\Path::split('etc//hosts/'), ','), ' ', Core\Path::isAbsolute('C:/tmp') ? 'abs' : 'rel', ' ', Core\Path::isAbsolute('tmp') ? 'abs' : 'rel', "\n";
echo Core\Path::extension('Makefile') ?? 'none', "\n";
echo Core\Path::matches('*.jpg', 'photos/2024/beach.jpg') ? 'yes' : 'no', ' ', Core\Path::matches('src/*.nvs', 'lib/src/app.nvs') ? 'yes' : 'no', "\n";
Output
index.php index php
/var/www/html /var/www
/var/www/html/index.html
/var/www/html/index.php
/var/log/app.log
../log/app.log
etc,hosts abs rel
none
yes no

Members

13 members.

MemberSignature
fileNamefileName(string $path, { withoutExtension?: bool }): string
parentparent(string $path, { levels?: uint }): string
extensionextension(string $path): ?string
withExtensionwithExtension(string $path, ?string $extension): string
joinjoin(string $base, string ...$segments): string
splitsplit(string $path): array<string>
normalizenormalize(string $path): string
isAbsoluteisAbsolute(string $path): bool
matchesmatches(string $pattern, string $path): bool
relativeTorelativeTo(string $path, string $base): ?string
fromWorkingDirectoryfromWorkingDirectory(string $path): string
thisFilethisFile(): string
thisDirectorythisDirectory(?string $join = null): string

Constants

  • Core\Path::SEPARATORThe path separator of this platform. It is \ on Windows and / on every other platform. Every method writes it, and every method accepts both / and \.