From 6b24b1dbd795f51345adc7a55229fdd87d9ef084 Mon Sep 17 00:00:00 2001
From: Johan de Klerk <johan@shiplogic.com>
Date: Mon, 22 Jan 2024 15:33:00 +0200
Subject: [PATCH] Check for unmatched index

---
 logs/logs_test.go            | 5 +++++
 string_utils/string_utils.go | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/logs/logs_test.go b/logs/logs_test.go
index 2eb9ff7..4abfb86 100644
--- a/logs/logs_test.go
+++ b/logs/logs_test.go
@@ -30,3 +30,8 @@ func TestLogs(t *testing.T) {
 	// api_logs.Errorf("Debugging %d!", 456)
 	// api_logs.Error("Info")
 }
+
+func TestSanitiseLogs(t *testing.T) {
+	logString := "{\n \"emailVerificationStatus\": true,\n \"keyValueMetadata\": [],\n \"stepUp\": null,\n \"riskAdvice\": null\n}"
+	logs.SanitiseLogs(logString)
+}
diff --git a/string_utils/string_utils.go b/string_utils/string_utils.go
index efa8731..a5721d7 100644
--- a/string_utils/string_utils.go
+++ b/string_utils/string_utils.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"encoding/json"
 	"fmt"
+	"github.com/samber/lo"
 	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/number_utils"
 	"regexp"
 	"strconv"
@@ -65,6 +66,10 @@ func ReplaceAllRegexStringSubmatch(re *regexp.Regexp, s string, replaceWith stri
 	lastIndex := 0
 
 	for _, v := range re.FindAllSubmatchIndex([]byte(s), -1) {
+		if lo.Contains(v, -1) {
+			continue
+		}
+
 		if len(v) == regexIndexSubmatchEnd+1 {
 			// One submatch - replace the submatch with replaceWith
 			result += s[lastIndex:v[regexIndexSubmatchStart]] + replaceWith + s[v[regexIndexSubmatchEnd]:v[regexIndexMatchEnd]]
-- 
GitLab