From ff79c4fe779efc2ff2981517ece446d59cb58ba7 Mon Sep 17 00:00:00 2001 From: HIQ Society <51492452+hiqsociety@users.noreply.github.com> Date: Fri, 22 Dec 2023 06:21:09 -0500 Subject: [PATCH] Update strings.go --- strings.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/strings.go b/strings.go index 4275353..cd2b769 100644 --- a/strings.go +++ b/strings.go @@ -8,3 +8,33 @@ func TrimStringFromQuery(s, substr string) string { } return s } + +func Strpos(haystack, needle string, offset int) int { + length := len(haystack) + if length == 0 || offset >= length || -offset > length { + return -1 + } + + if offset < 0 { + offset += length + } + + for i := offset; i <= length-len(needle); i++ { + if haystack[i:i+len(needle)] == needle { + return i + } + } + + return -1 +} + + +func Substr(str string, start uint, length int) string { + if int(start) >= len(str) || length == 0 { + return "" + } + if length < 0 || int(start)+length > len(str) { + return str[start:] + } + return str[start : start+uint(length)] +}