aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/string.go14
-rw-r--r--utils/string_test.go24
2 files changed, 38 insertions, 0 deletions
diff --git a/utils/string.go b/utils/string.go
new file mode 100644
index 0000000..b3cbb17
--- /dev/null
+++ b/utils/string.go
@@ -0,0 +1,14 @@
+package utils
+
+import (
+ "fmt"
+ "strings"
+)
+
+func Unprefix(path string, prefix string) (string, error) {
+ if !strings.HasPrefix(path, prefix) {
+ return path, fmt.Errorf("%q is not prefixed by %q", path, prefix)
+ } else {
+ return path[len(prefix):], nil
+ }
+}
diff --git a/utils/string_test.go b/utils/string_test.go
new file mode 100644
index 0000000..5d42c24
--- /dev/null
+++ b/utils/string_test.go
@@ -0,0 +1,24 @@
+package utils_test
+
+import (
+ "testing"
+
+ "github.com/n-peugnet/dna-backup/utils"
+)
+
+func TestUnprefix(t *testing.T) {
+ str, err := utils.Unprefix("foo/bar", "foo/")
+ if str != "bar" {
+ t.Errorf("expected: %q, actual: %q", "bar", str)
+ }
+ if err != nil {
+ t.Error(err)
+ }
+ str, err = utils.Unprefix("foo/bar", "baz")
+ if str != "foo/bar" {
+ t.Errorf("expected: %q, actual: %q", "foo/bar", str)
+ }
+ if err == nil {
+ t.Error("err should not be nil")
+ }
+}