Add Path.equals

main
copygirl 1 year ago
parent edadf3e1df
commit ab48fa440b
  1. 20
      src/path.zig

@ -256,6 +256,21 @@ pub const Path = struct {
try self.write(opt, writer); try self.write(opt, writer);
} }
/// Returns whether the contents of the specified `Path`s are equivalent.
///
/// Path equivalency does not imply these paths are or are not referring to
/// the same `Entity`. For example, an entity that is referred to using its
/// entity id has a different path from the same entity referred to by name.
pub fn equals(first: Path, second: Path) bool {
if (first.absolute != second.absolute) return false;
if (first.parts.len != second.parts.len) return false;
for (first.parts, second.parts) |a, b| switch (a) {
.name => |a_name| if (b != .name or !std.mem.eql(u8, a_name, b.name)) return false,
.id => |a_id| if (b != .id or a_id != b.id) return false,
};
return true;
}
fn calculateLength(self: Path, opt: FormatOptions) usize { fn calculateLength(self: Path, opt: FormatOptions) usize {
// Separators. // Separators.
var result = opt.sep.len * (self.parts.len - 1); var result = opt.sep.len * (self.parts.len - 1);
@ -329,6 +344,7 @@ pub const Path = struct {
test Path { test Path {
const alloc = std.testing.allocator; const alloc = std.testing.allocator;
const expect = std.testing.expect;
const expectFmt = std.testing.expectFmt; const expectFmt = std.testing.expectFmt;
const expectEqual = std.testing.expectEqual; const expectEqual = std.testing.expectEqual;
const expectEqualStrings = std.testing.expectEqualStrings; const expectEqualStrings = std.testing.expectEqualStrings;
@ -386,9 +402,7 @@ test Path {
// Numeric ids can also be passed to `buildParts`. // Numeric ids can also be passed to `buildParts`.
const numeric2_parts = Path.buildParts(.{ 100, 101, "bar" }); const numeric2_parts = Path.buildParts(.{ 100, 101, "bar" });
const numeric2 = try Path.fromParts(false, &numeric2_parts); const numeric2 = try Path.fromParts(false, &numeric2_parts);
try expect(numeric1.equals(numeric2));
// TODO: Let's check numeric1 and numeric2 for equality here.
_ = numeric2;
// Paths are formattable. As format specifier you can use options defined // Paths are formattable. As format specifier you can use options defined
// on the `FormatOptions` type, or an empty string to use the default. // on the `FormatOptions` type, or an empty string to use the default.

Loading…
Cancel
Save