Skip to content

Commit

Permalink
Use strings.Builder over concatenation (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c authored Jan 1, 2019
1 parent f7383ea commit 11e9e2d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
9 changes: 6 additions & 3 deletions internal/text/char.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package text

import (
"strings"
"unicode"
"unicode/utf8"

Expand All @@ -10,12 +11,14 @@ import (
// Char represents a character. Whatever that means.
type Char []rune

func (char Char) String() (str string) {
func (char Char) String() string {
var builder strings.Builder

for _, r := range char {
str += string(r)
builder.WriteRune(r)
}

return
return builder.String()
}

// SplitString splits a string into a slice of characters.
Expand Down
24 changes: 14 additions & 10 deletions internal/text/transform/spaced.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transform

import (
"errors"
"strings"

"github.com/72636c/hyperspaced/internal/text"
)
Expand All @@ -19,23 +20,26 @@ func SpacedN(str string, n int) string {

chars := text.SplitString(str)
sep := toSeparator(n)
str = "" // lol variable reassignment

for _, char := range chars {
str += char.String() + sep
}
var builder strings.Builder

for index, char := range chars {
builder.WriteString(char.String())

if len(str) == 0 {
return ""
if index < len(chars)-1 {
builder.WriteString(sep)
}
}

return str[:len(str)-len(sep)]
return builder.String()
}

func toSeparator(length int) (sep string) {
func toSeparator(length int) string {
var builder strings.Builder

for index := 0; index < length; index++ {
sep += " "
builder.WriteString(" ")
}

return
return builder.String()
}

0 comments on commit 11e9e2d

Please sign in to comment.