Rename FormatOptions.prefix to root_sep

main
copygirl 1 year ago
parent ab48fa440b
commit 8e4a99bfe5
  1. 32
      src/path.zig

@ -34,21 +34,21 @@ pub const Path = struct {
/// An empty `fmt` results in the `default` format being used, which can be
/// changed at runtime.
pub const FormatOptions = struct {
/// The prefix used for an absolute `Path`, if any.
/// The root separator used for an absolute `Path`, if any.
///
/// If set to `null`, absolute paths can't be represented using strings.
/// In this case, an absolue path and relative path with identical parts
/// will be indistinguishable.
prefix: ?[]const u8,
root_sep: ?[]const u8,
/// The separator used between parts that make up a `Path`.
sep: []const u8,
/// The format used by Flecs' C API. For example `flecs.core`.
pub const flecs_c = FormatOptions{ .prefix = null, .sep = "." };
pub const flecs_c = FormatOptions{ .root_sep = null, .sep = "." };
/// The format used by Flecs' C++ API. For example `::flecs::core`.
pub const flecs_cpp = FormatOptions{ .prefix = "::", .sep = "::" };
pub const flecs_cpp = FormatOptions{ .root_sep = "::", .sep = "::" };
/// Unix-like format. For example `/flecs/core`.
pub const unix = FormatOptions{ .prefix = "/", .sep = "/" };
pub const unix = FormatOptions{ .root_sep = "/", .sep = "/" };
/// The default format used when none is specified. Can be changed at runtime.
pub var default = flecs_c;
@ -99,9 +99,9 @@ pub const Path = struct {
/// Parses a string as a `Path` using the `FormatOptions` specified,
/// or `FormatOptions.default` if the argument is `null`.
///
/// If the string starts with the specified prefix (if any), the resulting
/// path will be absolute. The rest of the string will be split by the
/// specified seperator, becoming its parts.
/// If the string starts with the specified root separator (if any), the
/// resulting path will be absolute. The rest of the string will be split
/// by the specified seperator, becoming its parts.
///
/// The parts array will be allocated with the specified `Allocator` and is
/// owned by the resulting path. `deinit()` must be called to free it.
@ -112,8 +112,8 @@ pub const Path = struct {
var remaining = path;
var absolute = false;
// If prefix is defined and path starts with it, the path is absolute.
if (opt.prefix) |p| {
// If `root_sep` is set and path starts with it, the path is absolute.
if (opt.root_sep) |p| {
if (std.mem.startsWith(u8, remaining, p)) {
remaining = remaining[p.len..];
absolute = true;
@ -274,12 +274,12 @@ pub const Path = struct {
fn calculateLength(self: Path, opt: FormatOptions) usize {
// Separators.
var result = opt.sep.len * (self.parts.len - 1);
// Prefix.
// Root separator.
if (self.absolute) {
if (opt.prefix) |p|
if (opt.root_sep) |p|
result += p.len;
}
// Parts.
// Parts themselves.
for (self.parts) |part|
result += switch (part) {
.name => |name| name.len,
@ -289,9 +289,9 @@ pub const Path = struct {
}
fn write(self: Path, opt: FormatOptions, writer: anytype) !void {
// Write prefix (if applicable).
// Write root separator (if applicable).
if (self.absolute) {
if (opt.prefix) |p|
if (opt.root_sep) |p|
try writer.writeAll(p);
}
// Write the first part.
@ -412,7 +412,7 @@ test Path {
// They can also be turned directly into strings, which
// allows you to use entirely custom `FormatOptions`s:
const absolute1_str = try absolute1.toString(.{ .prefix = "= ", .sep = " + " }, alloc);
const absolute1_str = try absolute1.toString(.{ .root_sep = "= ", .sep = " + " }, alloc);
defer alloc.free(absolute1_str);
try expectEqualStrings("= I'm + absolute!", absolute1_str);

Loading…
Cancel
Save