From 70457118603d7ed762f9ff3cccdf3365d3621e3c Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 13 Feb 2025 08:55:41 -0500 Subject: [PATCH] Convert test_str into a rune slice for better unicode compatibility, it also fixed the bug where all unicode characters wouldn't be colored --- cmd/main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 82c5748..06b9a58 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -129,6 +129,8 @@ func main() { matchIndices = regComp.FindAllSubmatch(test_str) } + test_str_runes := []rune(test_str) // Converting to runes preserves unicode characters + if *printMatchesFlag { // if we are in single line mode, print the line on which // the matches occur @@ -158,10 +160,10 @@ func main() { oldIndices := indicesToPrint.values() indicesToPrint = new_uniq_arr[int]() // Explanation: - // Find all numbers from 0 to len(test_str) that are NOT in oldIndices. + // Find all numbers from 0 to len(test_str_runes) that are NOT in oldIndices. // These are the values we want to print, now that we have inverted the match. // Re-initialize indicesToPrint and add all of these values to it. - indicesToPrint.add(setDifference(genRange(0, len(test_str)), oldIndices)...) + indicesToPrint.add(setDifference(genRange(0, len(test_str_runes)), oldIndices)...) } // If lineFlag is enabled, we should only print something if: @@ -182,7 +184,7 @@ func main() { // the corresponding end index. // 3. If not, just print the character. if substituteFlagEnabled { - for i := range test_str { + for i := range test_str_runes { inMatchIndex := false for _, m := range matchIndices { if i == m[0].StartIdx { @@ -193,11 +195,11 @@ func main() { } } if !inMatchIndex { - fmt.Fprintf(out, "%c", test_str[i]) + fmt.Fprintf(out, "%c", test_str_runes[i]) } } } else { - for i, c := range test_str { + for i, c := range test_str_runes { if indicesToPrint.contains(i) { color.New(color.FgRed).Fprintf(out, "%c", c) // Newline after every match - only if -o is enabled and -v is disabled.