I have the following `commonPrefix()` function (for whole paths) which works
fine on Linux (and Windows), but wondering if it could be shorter and/or more
efficient?
proc commonPrefix*(paths: openArray[string], sep = "/"): string =
if len(paths) == 0:
return ""
result = paths[0]
for path in paths[1 .. ^1]:
while not path.startsWith(result) and len(result) > 0:
result = result[0 .. ^2]
result = result[0 .. result.rfind(sep)]
Run
Tests:
test "commonPrefix":
check(commonPrefix(["/tmp/test1", "/tmp/data.dat"]) == "/tmp/")
check(commonPrefix(["/home/mark/test1", "/tmp/data.dat"]) == "/")
check(commonPrefix(["/home/mark/test1", "local/data.dat"]) == "")
Run