Skip to content

Commit

Permalink
refactor: use strings.Builder over bytes.Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Jan 4, 2024
1 parent 7315aaa commit 1061abf
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cmd/debug.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"bytes"
"os"
"strings"

"github.com/goccy/go-json"
"github.com/spf13/cobra"
Expand All @@ -22,7 +22,7 @@ var inspectCmd = &cobra.Command{
if err != nil {
return err
}
var buf bytes.Buffer
var buf strings.Builder
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
_ = enc.Encode(resp)
Expand Down
3 changes: 1 addition & 2 deletions editor/editor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package editor

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -85,7 +84,7 @@ func (ed *editor) substituteArgs(result *lang.GenerateResult) ([]string, error)
if err != nil {
return nil, err
}
var s bytes.Buffer
var s strings.Builder
err = tmpl.Execute(&s, data)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions lang/base.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lang

import (
"bytes"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -417,7 +416,7 @@ func (l baseLang) generateCodeContent(
NeedsDefinition: needsDefinition(code),
Version: fmt.Sprintf("%s: %s", constants.CmdName, constants.Version),
}
var buf bytes.Buffer
var buf strings.Builder
err = tmpl.Execute(&buf, data)
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions lang/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ func (g golang) HasInitialized(outDir string) (bool, error) {

func (g golang) Initialize(outDir string) error {
const modPath = "leetcode-solutions"
var stderr bytes.Buffer
var stderr strings.Builder
cmd := exec.Command("go", "mod", "init", modPath)
log.Info("go mod init", "cmd", cmd.String())
cmd.Dir = outDir
cmd.Stdout = os.Stdout
cmd.Stderr = io.MultiWriter(os.Stderr, &stderr)
err := cmd.Run()
if err != nil && !bytes.Contains(stderr.Bytes(), []byte("go.mod already exists")) {
if err != nil && !strings.Contains(stderr.String(), "go.mod already exists") {
return err
}

Expand Down
5 changes: 2 additions & 3 deletions lang/test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lang

import (
"bytes"
"context"
"fmt"
"os/exec"
Expand Down Expand Up @@ -128,7 +127,7 @@ func buildTest(_ *leetcode.QuestionData, genResult *GenerateResult, args []strin
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

buf := new(bytes.Buffer)
buf := new(strings.Builder)
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
cmd.Dir = genResult.OutDir
cmd.Stdout = buf
Expand Down Expand Up @@ -187,7 +186,7 @@ func runTest(q *leetcode.QuestionData, genResult *GenerateResult, args []string,
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

outputBuf := new(bytes.Buffer)
outputBuf := new(strings.Builder)
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
cmd.Dir = genResult.OutDir
cmd.Stdin = strings.NewReader(c.InputString())
Expand Down
5 changes: 2 additions & 3 deletions lang/testcase.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lang

import (
"bytes"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -110,7 +109,7 @@ func (tc *TestCases) Contains(c TestCase) bool {
}

func (tc *TestCases) String() string {
buf := new(bytes.Buffer)
buf := new(strings.Builder)
for i, c := range tc.Cases {
buf.WriteString(testCaseInputMark + "\n")
buf.WriteString(c.InputString())
Expand All @@ -124,7 +123,7 @@ func (tc *TestCases) String() string {
}

func (tc *TestCases) InputString() string {
buf := new(bytes.Buffer)
buf := new(strings.Builder)
for _, c := range tc.Cases {
buf.WriteString(c.InputString())
}
Expand Down
3 changes: 1 addition & 2 deletions leetcode/question.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package leetcode

import (
"bytes"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -611,7 +610,7 @@ func (q *QuestionData) GetFormattedFilename(lang string, filenameTemplate string
return "", err
}

var buf bytes.Buffer
var buf strings.Builder
err = tmpl.Execute(&buf, data)
if err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion scripts/update_readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"strings"

"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -47,7 +48,7 @@ func updateUsage(readme []byte) []byte {
}

func updateConfig(readme []byte) []byte {
buf := new(bytes.Buffer)
buf := new(strings.Builder)
_ = config.Get().Write(buf, true)
configStr := buf.String()
configStr = "\n```yaml\n" + configStr + "```\n"
Expand Down

0 comments on commit 1061abf

Please sign in to comment.