summaryrefslogtreecommitdiff
path: root/locate.go
diff options
context:
space:
mode:
Diffstat (limited to 'locate.go')
-rw-r--r--locate.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/locate.go b/locate.go
new file mode 100644
index 0000000..6498c5f
--- /dev/null
+++ b/locate.go
@@ -0,0 +1,42 @@
+package saggytrousers
+
+import (
+ "strings"
+)
+
+// For Locate, LocateExact
+func preprocess(s string) string {
+ s = strings.ToLower(s)
+ s = strings.ReplaceAll(s, " ", "")
+ s = strings.ReplaceAll(s, "-", "")
+
+ return s
+}
+
+// This function is needed for SelectHeader. So we put it in saggytrousers, and
+// the badtudexo.Locate function calls this.
+// For all other types, including []any, please call LocateExact.
+func Locate(header []string, needle string) int {
+ // Perform preprocessing of header and needle
+ var cneedle string
+ var cheader []string
+
+ cneedle = preprocess(needle)
+
+ for _, v := range header {
+ cheader = append(cheader, preprocess(v))
+ }
+
+ return LocateExact(cheader, cneedle)
+}
+
+// Returns the index where the needle is found, returns -1 if not found;
+// matches exactly.
+func LocateExact[T comparable](header []T, needle T) int {
+ for i := 0; i < len(header); i++ {
+ if header[i] == needle {
+ return i
+ }
+ }
+ return -1
+}