Skip to content

Commit

Permalink
Update textutils.PrintHeader header length logic
Browse files Browse the repository at this point in the history
Swap out use of `len` (performs bytes length calculation) with
`utf8.RuneCountInString` (char length calculation). While the
two provide identical results for *most* cases (where ASCII
characters are given), using `utf8.RuneCountInString` looks to
be the correct approach to use here (where non-ASCII characters
*could* be provided at some point in the future).
  • Loading branch information
atc0005 committed Dec 16, 2024
1 parent d04d779 commit 51516a0
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/textutils/textutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"strconv"
"strings"
"unicode/utf8"
)

// InList is a helper function to emulate Python's `if "x" in list:`
Expand Down Expand Up @@ -78,7 +79,8 @@ func LowerCaseStringSlice(xs []string) []string {
// PrintHeader prints a section header with liberal leading and trailing
// newlines to help separate otherwise potentially dense blocks of text.
func PrintHeader(headerText string) {
headerBorderStr := strings.Repeat("=", len(headerText))
// headerBorderStr := strings.Repeat("=", len(headerText))
headerBorderStr := strings.Repeat("=", utf8.RuneCountInString(headerText))
fmt.Printf(
"\n\n%s\n%s\n%s\n\n",
headerBorderStr,
Expand Down

0 comments on commit 51516a0

Please sign in to comment.