From 227935bf5f9d180273e1015a53cfcea863c395e3 Mon Sep 17 00:00:00 2001 From: Mattia Cabrini Date: Sun, 3 Mar 2024 14:38:53 +0100 Subject: [PATCH] + String utils + URI --- net/uri.go | 36 ++++++++++++++++++++++++++++++++++++ reflect.go | 4 ++-- strings.go | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 net/uri.go create mode 100644 strings.go diff --git a/net/uri.go b/net/uri.go new file mode 100644 index 0000000..eb0e39c --- /dev/null +++ b/net/uri.go @@ -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 +} diff --git a/reflect.go b/reflect.go index 142ca84..25bee9e 100644 --- a/reflect.go +++ b/reflect.go @@ -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 index 0000000..70f5f3b --- /dev/null +++ b/strings.go @@ -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 +} -- 2.43.0