]> git repos ~mattia - go-utility.git/commitdiff
+ String utils + URI v0.0.7
authorMattia Cabrini <dev@mattiacabrini.com>
Sun, 3 Mar 2024 13:38:53 +0000 (14:38 +0100)
committerMattia Cabrini <dev@mattiacabrini.com>
Sun, 3 Mar 2024 13:38:53 +0000 (14:38 +0100)
net/uri.go [new file with mode: 0644]
reflect.go
strings.go [new file with mode: 0644]

diff --git a/net/uri.go b/net/uri.go
new file mode 100644 (file)
index 0000000..eb0e39c
--- /dev/null
@@ -0,0 +1,36 @@
+// Copyright (c) 2023 Mattia Cabrini
+// SPDX-License-Identifier: MIT
+
+package net
+
+import "strings"
+
+type URI struct {
+       comp []string
+       path string
+
+       stackIx int
+}
+
+func InitURI(uri string) (u URI) {
+       u.path = strings.Split(uri, "?")[0]
+       u.comp = strings.Split(u.path, "/")[1:]
+       return
+}
+
+func (u *URI) Pop() (part string) {
+       if u.stackIx < len(u.comp) {
+               part = u.comp[u.stackIx]
+               u.stackIx++
+       }
+
+       return
+}
+
+func (u *URI) StackCount() int {
+       return len(u.comp) - u.stackIx
+}
+
+func (u *URI) ResetStack() {
+       u.stackIx = 0
+}
index 142ca84a0b4f0a5b735d7781c28a5e03a2c625c5..25bee9e0dd7b3bac1bc13b6ff949bf3ca3033831 100644 (file)
@@ -70,8 +70,8 @@ func GetMethod(obj interface{}, name string, suffix string) *Method {
        return nil
 }
 
-// Giveng an obj interface{}, returns an interface{} which is a propery value of that onj.
-// GetProperty seeks a property that has name equal to name + suffix.
+// Giveng an obj interface{}, returns an interface{} which is a propery value of that obj.
+// GetProperty seeks a property that has name equal to name + suffix and all given tags equal to true.
 // `suffix` might just be an empty string.
 func GetProperty(obj interface{}, name string, suffix string, tags ...string) interface{} {
        to := reflect.TypeOf(obj)
diff --git a/strings.go b/strings.go
new file mode 100644 (file)
index 0000000..70f5f3b
--- /dev/null
@@ -0,0 +1,39 @@
+// Copyright (c) 2023 Mattia Cabrini
+// SPDX-License-Identifier: MIT
+
+package utility
+
+// Returns str without last n characters
+func ButLastN(str string, n int) string {
+       lenStr := len(str)
+
+       if lenStr < n {
+               return ""
+       }
+
+       return str[:lenStr-n]
+}
+
+// Returns true is str ends with any of the provided suffixes; False otherwise
+// If true is returned, the matching suffix is also returned; Empty string otherwise
+func EndsWithAny(str string, suffixesList ...string) (bool, string) {
+       for _, suffix := range suffixesList {
+               if EndsWith(str, suffix) {
+                       return true, suffix
+               }
+       }
+
+       return false, ""
+}
+
+// Returns true is str ends with suffix; False otherwise
+func EndsWith(str string, suffix string) bool {
+       lenStr := len(str)
+       lenSuffix := len(suffix)
+
+       if lenStr < lenSuffix {
+               return false
+       }
+
+       return str[lenStr-lenSuffix:] == suffix
+}