From bffce24ea94e7c3f51b68f965df725b3195d551c Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Tue, 30 May 2023 15:35:04 +0300 Subject: [PATCH] docs: Update MIRROR_FUNCS.md (#29) --- MIRROR_FUNCS.md | 81 ++++++++++++++++++++++------------------ checkers_bufio.go | 20 ++++++++++ checkers_bytes.go | 20 +++++----- checkers_strings.go | 20 +++++----- testdata/born-to-rune.go | 13 ++++++- testdata/issue-26.go | 3 +- 6 files changed, 98 insertions(+), 59 deletions(-) diff --git a/MIRROR_FUNCS.md b/MIRROR_FUNCS.md index c7bf070..776816e 100644 --- a/MIRROR_FUNCS.md +++ b/MIRROR_FUNCS.md @@ -1,141 +1,150 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - +
func (b *bufio.Writer) Write(p []byte) (int, error) func (b *bufio.Writer) WriteString(s string) (int, error) + func (b *bufio.Writer) Write(p []byte) (int, error) + func (b *bufio.Writer) WriteRune(r rune) (int, error) +
func (b *bytes.Buffer) Write(p []byte) (int, error) func (b *bytes.Buffer) WriteString(s string) (int, error) + func (b *bytes.Buffer) Write(p []byte) (int, error) + func (b *bytes.Buffer) WriteRune(r rune) (int, error) +
func bytes.Compare(a, b []byte) int func strings.Compare(a, b string) intfunc bytes.Compare(a, b []byte) int
func bytes.Contains(b, subslice []byte) bool func strings.Contains(s, substr string) boolfunc bytes.Contains(b, subslice []byte) bool
func bytes.ContainsAny(b []byte, chars string) bool func strings.ContainsAny(s, chars string) boolfunc bytes.ContainsAny(b []byte, chars string) bool
func bytes.ContainsRune(b []byte, r rune) bool func strings.ContainsRune(s string, r rune) boolfunc bytes.ContainsRune(b []byte, r rune) bool
func bytes.Count(s, sep []byte) int func strings.Count(s, substr string) intfunc bytes.Count(s, sep []byte) int
func bytes.EqualFold(s, t []byte) bool func strings.EqualFold(s, t string) boolfunc bytes.EqualFold(s, t []byte) bool
func bytes.HasPrefix(s, prefix []byte) bool func strings.HasPrefix(s, prefix string) boolfunc bytes.HasPrefix(s, prefix []byte) bool
func bytes.HasSuffix(s, suffix []byte) bool func strings.HasSuffix(s, suffix string) boolfunc bytes.HasSuffix(s, suffix []byte) bool
func bytes.Index(s, sep []byte) int func strings.Index(s, substr string) intfunc bytes.Index(s, sep []byte) int
func bytes.IndexAny(s []byte, chars string) int func strings.IndexAny(s, chars string) intfunc bytes.IndexAny(s []byte, chars string) int
func bytes.IndexByte(b []byte, c byte) int func strings.IndexByte(s string, c byte) intfunc bytes.IndexByte(b []byte, c byte) int
func bytes.IndexFunc(s []byte, f func(r rune) bool) int func strings.IndexFunc(s string, f func(rune) bool) intfunc bytes.IndexFunc(s []byte, f func(r rune) bool) int
func bytes.IndexRune(s []byte, r rune) int func strings.IndexRune(s string, r rune) intfunc bytes.IndexRune(s []byte, r rune) int
func bytes.LastIndex(s, sep []byte) int func strings.LastIndex(s, sep string) intfunc bytes.LastIndex(s, sep []byte) int
func bytes.LastIndexAny(s []byte, chars string) int func strings.LastIndexAny(s, chars string) intfunc bytes.LastIndexAny(s []byte, chars string) int
func bytes.LastIndexByte(s []byte, c byte) int func strings.LastIndexByte(s string, c byte) intfunc bytes.LastIndexByte(s []byte, c byte) int
func bytes.LastIndexFunc(s []byte, f func(r rune) bool) int func strings.LastIndexFunc(s string, f func(rune) bool) intfunc bytes.LastIndexFunc(s []byte, f func(r rune) bool) int
func bytes.NewBuffer(buf []byte *bytes.Buffer func bytes.NewBufferString(s string) *bytes.Bufferfunc bytes.NewBuffer(buf []byte *bytes.Buffer
func (h *hash/maphash.Hash) Write(b []byte) (int, error) func (h *hash/maphash.Hash) WriteString(s string) (int, error)func (h *hash/maphash.Hash) Write(b []byte) (int, error)
func (rw *net/http/httptest.ResponseRecorder) Write(buf []byte) (int, error) func (rw *net/http/httptest.ResponseRecorder) WriteString(str string) (int, error)func (rw *net/http/httptest.ResponseRecorder) Write(buf []byte) (int, error)
func (f *os.File) Write(b []byte) (n int, err error) func (f *os.File) WriteString(s string) (n int, err error)func (f *os.File) Write(b []byte) (n int, err error)
func regexp.Match(pattern string, b []byte) (bool, error) func regexp.MatchString(pattern string, s string) (bool, error)func regexp.Match(pattern string, b []byte) (bool, error)
func (re *regexp.Regexp) FindAllIndex(b []byte, n int) [][]int func (re *regexp.Regexp) FindAllStringIndex(s string, n int) [][]intfunc (re *regexp.Regexp) FindAllIndex(b []byte, n int) [][]int
func (re *regexp.Regexp) FindAllSubmatch(b []byte, n int) [][][]byte func (re *regexp.Regexp) FindAllStringSubmatch(s string, n int) [][]stringfunc (re *regexp.Regexp) FindAllSubmatch(b []byte, n int) [][][]byte
func (re *regexp.Regexp) FindIndex(b []byte) (loc []int) func (re *regexp.Regexp) FindStringIndex(s string) (loc []int)func (re *regexp.Regexp) FindIndex(b []byte) (loc []int)
func (re *regexp.Regexp) FindSubmatchIndex(b []byte) []int func (re *regexp.Regexp) FindStringSubmatchIndex(s string) []intfunc (re *regexp.Regexp) FindSubmatchIndex(b []byte) []int
func (re *regexp.Regexp) Match(b []byte) bool func (re *regexp.Regexp) MatchString(s string) boolfunc (re *regexp.Regexp) Match(b []byte) bool
func (b *strings.Builder) Write(p []byte) (int, error)func (b *strings.Builder) WriteByte(c byte) errorfunc (b *strings.Builder) WriteString(s string) error + func (b *strings.Builder) Write(p []byte) (int, error) + func (b *strings.Builder) WriteRune(r rune) (int, error) +
func utf8.Valid(p []byte) bool func utf8.ValidString(s string) boolfunc utf8.Valid(p []byte) bool
func utf8.FullRune(p []byte) bool func utf8.FullRuneInString(s string) boolfunc utf8.FullRune(p []byte) bool
func utf8.RuneCount(p []byte) int func utf8.RuneCountInString(s string) (n int)func utf8.RuneCount(p []byte) int
func utf8.DecodeLastRune(p []byte) (rune, int) func utf8.DecodeLastRuneInString(s string) (rune, int)func utf8.DecodeLastRune(p []byte) (rune, int)
func utf8.DecodeRune(p []byte) (rune, int) func utf8.DecodeRuneInString(s string) (une, int)func utf8.DecodeRune(p []byte) (rune, int)
diff --git a/checkers_bufio.go b/checkers_bufio.go index ed6d153..292ed26 100644 --- a/checkers_bufio.go +++ b/checkers_bufio.go @@ -33,4 +33,24 @@ var BufioMethods = []checker.Violation{ Returns: 2, }, }, + { // (*bufio.Writer).WriteString -> (*bufio.Writer).WriteRune + Targets: checker.Strings, + Type: checker.Method, + Package: "bufio", + Struct: "Writer", + Caller: "WriteString", + Args: []int{0}, + ArgsType: checker.Rune, + AltCaller: "WriteRune", + }, + // { // (*bufio.Writer).WriteString -> (*bufio.Writer).WriteByte + // Targets: checker.Strings, + // Type: checker.Method, + // Package: "strings", + // Struct: "Builder", + // Caller: "WriteString", + // Args: []int{0}, + // ArgsType: checker.Byte, + // AltCaller: "WriteByte", // byte + // }, } diff --git a/checkers_bytes.go b/checkers_bytes.go index e88da93..c490a37 100644 --- a/checkers_bytes.go +++ b/checkers_bytes.go @@ -312,15 +312,15 @@ var ( ArgsType: checker.Rune, AltCaller: "WriteRune", }, - { // (*bytes.Buffer).WriteString -> (*bytes.Buffer).WriteByte - Targets: checker.Strings, - Type: checker.Method, - Package: "bytes", - Struct: "Buffer", - Caller: "WriteString", - Args: []int{0}, - ArgsType: checker.Byte, - AltCaller: "WriteByte", - }, + // { // (*bytes.Buffer).WriteString -> (*bytes.Buffer).WriteByte + // Targets: checker.Strings, + // Type: checker.Method, + // Package: "bytes", + // Struct: "Buffer", + // Caller: "WriteString", + // Args: []int{0}, + // ArgsType: checker.Byte, + // AltCaller: "WriteByte", + // }, } ) diff --git a/checkers_strings.go b/checkers_strings.go index 46d2e73..ead7e9c 100644 --- a/checkers_strings.go +++ b/checkers_strings.go @@ -285,15 +285,15 @@ var ( ArgsType: checker.Rune, AltCaller: "WriteRune", }, - { // (*strings.Builder).WriteString -> (*strings.Builder).WriteByte - Targets: checker.Strings, - Type: checker.Method, - Package: "strings", - Struct: "Builder", - Caller: "WriteString", - Args: []int{0}, - ArgsType: checker.Byte, - AltCaller: "WriteByte", // byte - }, + // { // (*strings.Builder).WriteString -> (*strings.Builder).WriteByte + // Targets: checker.Strings, + // Type: checker.Method, + // Package: "strings", + // Struct: "Builder", + // Caller: "WriteString", + // Args: []int{0}, + // ArgsType: checker.Byte, + // AltCaller: "WriteByte", // byte + // }, } ) diff --git a/testdata/born-to-rune.go b/testdata/born-to-rune.go index 595d714..dbfb22c 100644 --- a/testdata/born-to-rune.go +++ b/testdata/born-to-rune.go @@ -1,8 +1,10 @@ package main import ( + "bufio" "bytes" "fmt" + "io" "strings" ) @@ -13,6 +15,7 @@ func bornToRune() { var bbuf bytes.Buffer var sbuf strings.Builder + buf := bufio.NewWriter(io.Discard) var r rune = 'п' b := "p"[0] @@ -20,13 +23,19 @@ func bornToRune() { sbuf.WriteString(string('п')) // want `avoid allocations with \(\*strings\.Builder\)\.WriteRune` sbuf.WriteString(string('r')) // want `avoid allocations with \(\*strings\.Builder\)\.WriteRune` sbuf.WriteString(string(`п`)) - sbuf.WriteString(string(b)) // want `avoid allocations with \(\*strings\.Builder\)\.WriteByte` + sbuf.WriteString(string(b)) bbuf.WriteString(string([]byte("foobar"))) // want `avoid allocations with \(\*bytes\.Buffer\)\.Write` bbuf.WriteString(string('п')) // want `avoid allocations with \(\*bytes\.Buffer\)\.WriteRune` bbuf.WriteString(string(r)) // want `avoid allocations with \(\*bytes\.Buffer\)\.WriteRune` bbuf.WriteString(string(`п`)) - bbuf.WriteString(string(b)) // want `avoid allocations with \(\*bytes\.Buffer\)\.WriteByte` + bbuf.WriteString(string(b)) + + buf.WriteString(string([]byte("foobar"))) // want `avoid allocations with \(\*bufio\.Writer\)\.Write` + buf.WriteString(string('п')) // want `avoid allocations with \(\*bufio\.Writer\)\.WriteRune` + buf.WriteString(string(r)) // want `avoid allocations with \(\*bufio\.Writer\)\.WriteRune` + buf.WriteString(string(`п`)) + buf.WriteString(string(b)) fmt.Println("strings.Builder:", sbuf.String()) fmt.Println(" bytes.Buffer:", bbuf.String()) diff --git a/testdata/issue-26.go b/testdata/issue-26.go index 8a1f4ce..8043e07 100644 --- a/testdata/issue-26.go +++ b/testdata/issue-26.go @@ -12,6 +12,7 @@ func foobar_byte() { fmt.Printf("%T\n", b) - strBuilder.WriteString(string(b)) // want `avoid allocations with \(\*strings\.Builder\)\.WriteByte` + // TODO: WriteByte can be added but only if return of this call doesn't checked. + strBuilder.WriteString(string(b)) fmt.Println(strBuilder.String()) }