Path
Cross-platform path manipulation utilities optimized for Ceramic.
This class provides a convenient way of working with file and directory paths across different platforms. It is a modified version of haxe.io.Path that doesn't depend on regular expressions (EReg), making it more efficient for frequent path operations in game development.
Supports common path formats:
- Unix/Mac/Linux:
directory1/directory2/filename.extension
- Windows:
directory1\directory2\filename.extension
- Windows absolute:
C:\directory\file.ext
- Network paths:
\\server\share\file.ext
Example usage:
// Parse a path
var path = new Path("assets/images/player.png");
trace(path.dir); // "assets/images"
trace(path.file); // "player"
trace(path.ext); // "png"
// Manipulate paths
var newPath = Path.withExtension("image.jpg", "png"); // "image.png"
var joined = Path.join(["assets", "sounds", "music.ogg"]); // "assets/sounds/music.ogg"
var normalized = Path.normalize("/usr/local/../lib"); // "/usr/lib"
// Check path properties
if (Path.isAbsolute("/home/user")) {
trace("Absolute path");
}
Static Members
Removes the file extension from a path string.
Example:
Path.withoutExtension("image.png"); // "image"
Path.withoutExtension("path/to/file.txt"); // "path/to/file"
Name | Type | Description |
---|---|---|
path |
String | The path to process |
Returns | Description |
---|---|
String | The path without its extension |
Extracts only the filename and extension from a path.
Example:
Path.withoutDirectory("/home/user/file.txt"); // "file.txt"
Path.withoutDirectory("assets/image.png"); // "image.png"
Name | Type | Description |
---|---|---|
path |
String | The path to process |
Returns | Description |
---|---|
String | The filename with extension, without directory |
Extracts the directory portion of a path.
Example:
Path.directory("/home/user/file.txt"); // "/home/user"
Path.directory("file.txt"); // ""
Name | Type | Description |
---|---|---|
path |
String | The path to process |
Returns | Description |
---|---|
String | The directory portion, or empty string if none |
Extracts the file extension from a path.
Example:
Path.extension("image.png"); // "png"
Path.extension("archive.tar.gz"); // "gz"
Path.extension("README"); // ""
Name | Type | Description |
---|---|---|
path |
String | The path to process |
Returns | Description |
---|---|
String | The extension without dot, or empty string if none |
Changes or adds a file extension to a path.
Example:
Path.withExtension("image.jpg", "png"); // "image.png"
Path.withExtension("document", "pdf"); // "document.pdf"
Name | Type | Description |
---|---|---|
path |
String | The path to modify |
ext |
String | The new extension (without dot) |
Returns | Description |
---|---|
String | The path with the new extension |
Joins multiple path segments into a single path.
Automatically adds separators between segments and normalizes the result. Empty segments are filtered out.
Example:
Path.join(["assets", "images", "player.png"]); // "assets/images/player.png"
Path.join(["/home", "user", "docs"]); // "/home/user/docs"
Name | Type | Description |
---|---|---|
paths |
Array<String> | Array of path segments to join |
Returns | Description |
---|---|
String | The joined and normalized path |
Normalizes a path by resolving relative segments and cleaning separators.
Operations performed:
- Converts all backslashes to forward slashes
- Resolves
.
(current directory) and..
(parent directory) segments - Removes duplicate slashes (except after colons for Windows drives)
- Preserves absolute path indicators
Example:
Path.normalize("/usr/local/../lib"); // "/usr/lib"
Path.normalize("./assets//images/."); // "assets/images"
Path.normalize("C:\\Users\\..\\Windows"); // "C:/Windows"
Name | Type | Description |
---|---|---|
path |
String | The path to normalize |
Returns | Description |
---|---|
String | The normalized path |
Ensures a path ends with a directory separator.
The type of separator added matches the existing separators in the path:
- If the last separator is a backslash, adds a backslash
- Otherwise, adds a forward slash
- Empty string becomes "/"
Example:
Path.addTrailingSlash("dir"); // "dir/"
Path.addTrailingSlash("C:\\Windows"); // "C:\\Windows\\"
Path.addTrailingSlash("dir/"); // "dir/" (unchanged)
Name | Type | Description |
---|---|---|
path |
String | The path to process |
Returns | Description |
---|---|
String | The path with a trailing separator |
Removes all trailing directory separators from a path.
Strips any combination of trailing forward slashes and backslashes.
Example:
Path.removeTrailingSlashes("dir/"); // "dir"
Path.removeTrailingSlashes("C:\\Windows\\\\"); // "C:\\Windows"
Path.removeTrailingSlashes("file.txt"); // "file.txt" (unchanged)
Name | Type | Description |
---|---|---|
path |
String | The path to process |
Returns | Description |
---|---|
String | The path without trailing separators |
Determines if a path is absolute or relative.
A path is considered absolute if it:
- Starts with
/
(Unix/Mac/Linux) - Has a drive letter like
C:
(Windows) - Starts with
\\\\
(Windows network path)
Example:
Path.isAbsolute("/home/user"); // true
Path.isAbsolute("C:\\Windows"); // true
Path.isAbsolute("\\\\server\\share"); // true
Path.isAbsolute("relative/path"); // false
Path.isAbsolute("./file.txt"); // false
Name | Type | Description |
---|---|---|
path |
String | The path to check |
Returns | Description |
---|---|
Bool | True if the path is absolute, false if relative |
Instance Members
dir: String
The directory portion of the path.
This is the leading part of the path that is not part of the file name
and the extension. Does not include a trailing /
or \
separator.
Examples:
- Path "dir/file.txt" -> dir = "dir"
- Path "file.txt" -> dir = null
- Path "/home/user/file.txt" -> dir = "/home/user"
- Path "C:\Windows\file.txt" -> dir = "C:\Windows"
file: String
The file name without extension.
This is the part of the path between the directory and the extension. For files that start with a dot (like .htaccess) or paths ending with a separator, the value is an empty string "".
Examples:
- Path "dir/file.txt" -> file = "file"
- Path ".htaccess" -> file = ""
- Path "/dir/" -> file = ""
- Path "document.tar.gz" -> file = "document.tar"
ext: String
The file extension without the leading dot.
The extension is the part after the last dot in the filename. The separating dot is not included in the extension value.
Examples:
- Path "file.txt" -> ext = "txt"
- Path "archive.tar.gz" -> ext = "gz"
- Path "file" -> ext = null
- Path ".htaccess" -> ext = "htaccess"
backslash: Bool
Indicates the type of directory separator used in the original path.
True if the last directory separator found was a backslash (\\
),
false if it was a forward slash (/
) or if no separator was found.
This helps preserve the original path style when converting back to string.
toString(): String
Reconstructs the path string from its components.
The directory separator used depends on the backslash
property:
- If true, uses backslash (
\\
) as separator - If false, uses forward slash (
/
) as separator
Null components are treated as empty strings.
Returns | Description |
---|---|
String | The reconstructed path string |
Creates a new Path instance by parsing the given path string.
The path is split into its components (directory, filename, extension) which can be accessed through the corresponding properties. Handles both forward slash and backslash separators.
Special cases:
- "." and ".." are treated as directories with empty filenames
- Paths ending with separators have empty filenames
Name | Type | Description |
---|---|---|
path |
String | The path string to parse |