Skip to content

Commit

Permalink
Merge pull request #729 from twpayne/strings-builder
Browse files Browse the repository at this point in the history
Use strings.Builder instead of bytes.Buffer
  • Loading branch information
twpayne authored May 4, 2020
2 parents 0bc0257 + 610701e commit b962e36
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
8 changes: 4 additions & 4 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"bytes"
"errors"
"strings"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -30,7 +30,7 @@ func init() {
}

func (c *Config) runCompletion(cmd *cobra.Command, args []string) error {
output := &bytes.Buffer{}
output := &strings.Builder{}
switch args[0] {
case "bash":
if err := rootCmd.GenBashCompletion(output); err != nil {
Expand All @@ -49,8 +49,8 @@ func (c *Config) runCompletion(cmd *cobra.Command, args []string) error {
}

if c.completion.output == "" {
_, err := c.Stdout.Write(output.Bytes())
_, err := c.Stdout.Write([]byte(output.String()))
return err
}
return c.fs.WriteFile(c.completion.output, output.Bytes(), 0o666)
return c.fs.WriteFile(c.completion.output, []byte(output.String()), 0o666)
}
7 changes: 3 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -230,11 +229,11 @@ func (c *Config) autoCommit(vcs VCS) error {
if err != nil {
return err
}
b := &bytes.Buffer{}
if err := commitMessageTmpl.Execute(b, status); err != nil {
sb := &strings.Builder{}
if err := commitMessageTmpl.Execute(sb, status); err != nil {
return err
}
commitArgs := vcs.CommitArgs(b.String())
commitArgs := vcs.CommitArgs(sb.String())
return c.run(c.SourceDir, c.SourceVCS.Command, commitArgs...)
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/executetemplate.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cmd

import (
"bytes"
"io/ioutil"
"strconv"
"strings"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -46,7 +46,7 @@ func (c *Config) runExecuteTemplateCmd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
output := &bytes.Buffer{}
output := &strings.Builder{}
switch len(args) {
case 0:
data, err := ioutil.ReadAll(c.Stdin)
Expand All @@ -73,8 +73,8 @@ func (c *Config) runExecuteTemplateCmd(cmd *cobra.Command, args []string) error
}

if c.executeTemplate.output == "" {
_, err = c.Stdout.Write(output.Bytes())
_, err = c.Stdout.Write([]byte(output.String()))
return err
}
return c.fs.WriteFile(c.executeTemplate.output, output.Bytes(), 0o666)
return c.fs.WriteFile(c.executeTemplate.output, []byte(output.String()), 0o666)
}
6 changes: 3 additions & 3 deletions internal/chezmoi/targetstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,11 @@ func (ts *TargetState) ExecuteTemplateData(name string, data []byte) ([]byte, er
return nil, err
}
}
output := &bytes.Buffer{}
if err = tmpl.ExecuteTemplate(output, name, ts.TemplateData); err != nil {
sb := &strings.Builder{}
if err = tmpl.ExecuteTemplate(sb, name, ts.TemplateData); err != nil {
return nil, err
}
return output.Bytes(), nil
return []byte(sb.String()), nil
}

// Get returns the state of the given target, or nil if no such target is found.
Expand Down
15 changes: 8 additions & 7 deletions internal/generate-assets/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go/format"
"io/ioutil"
"os"
"strings"
"text/template"
)

Expand All @@ -32,14 +33,14 @@ func init() {
)

func printMultiLineString(s []byte) string {
b := &bytes.Buffer{}
sb := &strings.Builder{}
for i, line := range bytes.Split(s, []byte{'\n'}) {
if i != 0 {
b.WriteString(" +\n")
sb.WriteString(" +\n")
}
b.WriteString(fmt.Sprintf("%q", append(line, '\n')))
sb.WriteString(fmt.Sprintf("%q", append(line, '\n')))
}
return b.String()
return sb.String()
}

func run() error {
Expand All @@ -54,8 +55,8 @@ func run() error {
}
}

source := &bytes.Buffer{}
if err := outputTemplate.Execute(source, struct {
sb := &strings.Builder{}
if err := outputTemplate.Execute(sb, struct {
Tags string
Assets map[string][]byte
}{
Expand All @@ -65,7 +66,7 @@ func run() error {
return err
}

formattedSource, err := format.Source(source.Bytes())
formattedSource, err := format.Source([]byte(sb.String()))
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions internal/generate-helps/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"bytes"
"flag"
"fmt"
"go/format"
Expand Down Expand Up @@ -209,8 +208,8 @@ func run() error {
InputFile: *inputFile,
OutputFile: *outputFile,
}
buf := &bytes.Buffer{}
if err := outputTemplate.ExecuteTemplate(buf, "output", data); err != nil {
sb := &strings.Builder{}
if err := outputTemplate.ExecuteTemplate(sb, "output", data); err != nil {
return err
}

Expand All @@ -226,7 +225,7 @@ func run() error {
w = fw
}

output, err := format.Source(buf.Bytes())
output, err := format.Source([]byte(sb.String()))
if err != nil {
return err
}
Expand Down

0 comments on commit b962e36

Please sign in to comment.