command: fast-path for Covers()

This commit is contained in:
Michael Muré
2024-11-12 18:59:02 +01:00
parent e938d64220
commit 98d9cadcbd

View File

@@ -106,6 +106,14 @@ func (c Command) Segments() []string {
// Covers returns true if the command is identical or a parent of the given other command. // Covers returns true if the command is identical or a parent of the given other command.
func (c Command) Covers(other Command) bool { func (c Command) Covers(other Command) bool {
// fast-path, equivalent to the code below (verified with fuzzing)
if !strings.HasPrefix(string(other), string(c)) {
return false
}
return c == separator || len(c) == len(other) || other[len(c)] == separator[0]
/* -------
otherSegments := other.Segments() otherSegments := other.Segments()
if len(otherSegments) < len(c.Segments()) { if len(otherSegments) < len(c.Segments()) {
return false return false
@@ -116,6 +124,8 @@ func (c Command) Covers(other Command) bool {
} }
} }
return true return true
*/
} }
// String returns the composed representation the command. This is also // String returns the composed representation the command. This is also