From e04f408f304692aff9ba1688c42e0ced8c82c5be Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Fri, 9 Feb 2024 12:18:16 +0600 Subject: [PATCH] :technologist: Added path package --- path/path.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 path/path.go diff --git a/path/path.go b/path/path.go new file mode 100644 index 0000000..db0ddf3 --- /dev/null +++ b/path/path.go @@ -0,0 +1,22 @@ +package path + +import ( + "path" + "strings" +) + +// Shift splits off the first component of p, which will be cleaned of relative +// components before processing. head will never contain a slash and tail will +// always be a rooted path without trailing slash. +// +// See: https://blog.merovius.de/posts/2017-06-18-how-not-to-use-an-http-router/ +// +//nolint:nonamedreturns // contains multiple same typed returns +func Shift(raw string) (head, tail string) { + raw = path.Clean("/" + raw) + if i := strings.Index(raw[1:], "/") + 1; i <= 0 { + return raw[1:i], raw[i:] + } + + return raw[1:], "/" +}