Skip to content

Commit

Permalink
docs: Update MIRROR_FUNCS.md (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
butuzov authored May 30, 2023
1 parent f7af4f6 commit bffce24
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 59 deletions.
81 changes: 45 additions & 36 deletions MIRROR_FUNCS.md
Original file line number Diff line number Diff line change
@@ -1,141 +1,150 @@
<table><tr>
<td><code>func (b *bufio.Writer) Write(p []byte) (int, error)</code></td>
<td><code>func (b *bufio.Writer) WriteString(s string) (int, error)</code></td>
<td>
<code>func (b *bufio.Writer) Write(p []byte) (int, error)</code>
<code>func (b *bufio.Writer) WriteRune(r rune) (int, error)</code>
</td>
</tr>
<tr>
<td><code>func (b *bytes.Buffer) Write(p []byte) (int, error)</code></td>
<td><code>func (b *bytes.Buffer) WriteString(s string) (int, error)</code></td>
<td>
<code>func (b *bytes.Buffer) Write(p []byte) (int, error)</code>
<code>func (b *bytes.Buffer) WriteRune(r rune) (int, error)</code>
</td>
</tr>
<tr>
<td><code>func bytes.Compare(a, b []byte) int</code></td>
<td><code>func strings.Compare(a, b string) int</code></td>
<td><code>func bytes.Compare(a, b []byte) int</code></td>
</tr>
<tr>
<td><code>func bytes.Contains(b, subslice []byte) bool</code></td>
<td><code>func strings.Contains(s, substr string) bool</code></td>
<td><code>func bytes.Contains(b, subslice []byte) bool</code></td>
</tr>
<tr>
<td><code>func bytes.ContainsAny(b []byte, chars string) bool</code></td>
<td><code>func strings.ContainsAny(s, chars string) bool</code></td>
<td><code>func bytes.ContainsAny(b []byte, chars string) bool</code></td>
</tr>
<tr>
<td><code>func bytes.ContainsRune(b []byte, r rune) bool</code></td>
<td><code>func strings.ContainsRune(s string, r rune) bool</code></td>
<td><code>func bytes.ContainsRune(b []byte, r rune) bool</code></td>
</tr>
<tr>
<td><code>func bytes.Count(s, sep []byte) int</code></td>
<td><code>func strings.Count(s, substr string) int</code></td>
<td><code>func bytes.Count(s, sep []byte) int</code></td>
</tr>
<tr>
<td><code>func bytes.EqualFold(s, t []byte) bool</code></td>
<td><code>func strings.EqualFold(s, t string) bool</code></td>
<td><code>func bytes.EqualFold(s, t []byte) bool</code></td>
</tr>
<tr>
<td><code>func bytes.HasPrefix(s, prefix []byte) bool</code></td>
<td><code>func strings.HasPrefix(s, prefix string) bool</code></td>
<td><code>func bytes.HasPrefix(s, prefix []byte) bool</code></td>
</tr>
<tr>
<td><code>func bytes.HasSuffix(s, suffix []byte) bool</code></td>
<td><code>func strings.HasSuffix(s, suffix string) bool</code></td>
<td><code>func bytes.HasSuffix(s, suffix []byte) bool</code></td>
</tr>
<tr>
<td><code>func bytes.Index(s, sep []byte) int</code></td>
<td><code>func strings.Index(s, substr string) int</code></td>
<td><code>func bytes.Index(s, sep []byte) int</code></td>
</tr>
<tr>
<td><code>func bytes.IndexAny(s []byte, chars string) int</code></td>
<td><code>func strings.IndexAny(s, chars string) int</code></td>
<td><code>func bytes.IndexAny(s []byte, chars string) int</code></td>
</tr>
<tr>
<td><code>func bytes.IndexByte(b []byte, c byte) int</code></td>
<td><code>func strings.IndexByte(s string, c byte) int</code></td>
<td><code>func bytes.IndexByte(b []byte, c byte) int</code></td>
</tr>
<tr>
<td><code>func bytes.IndexFunc(s []byte, f func(r rune) bool) int</code></td>
<td><code>func strings.IndexFunc(s string, f func(rune) bool) int</code></td>
<td><code>func bytes.IndexFunc(s []byte, f func(r rune) bool) int</code></td>
</tr>
<tr>
<td><code>func bytes.IndexRune(s []byte, r rune) int</code></td>
<td><code>func strings.IndexRune(s string, r rune) int</code></td>
<td><code>func bytes.IndexRune(s []byte, r rune) int</code></td>
</tr>
<tr>
<td><code>func bytes.LastIndex(s, sep []byte) int</code></td>
<td><code>func strings.LastIndex(s, sep string) int</code></td>
<td><code>func bytes.LastIndex(s, sep []byte) int</code></td>
</tr>
<tr>
<td><code>func bytes.LastIndexAny(s []byte, chars string) int</code></td>
<td><code>func strings.LastIndexAny(s, chars string) int</code></td>
<td><code>func bytes.LastIndexAny(s []byte, chars string) int</code></td>
</tr>
<tr>
<td><code>func bytes.LastIndexByte(s []byte, c byte) int</code></td>
<td><code>func strings.LastIndexByte(s string, c byte) int</code></td>
<td><code>func bytes.LastIndexByte(s []byte, c byte) int</code></td>
</tr>
<tr>
<td><code>func bytes.LastIndexFunc(s []byte, f func(r rune) bool) int</code></td>
<td><code>func strings.LastIndexFunc(s string, f func(rune) bool) int</code></td>
<td><code>func bytes.LastIndexFunc(s []byte, f func(r rune) bool) int</code></td>
</tr>
<tr>
<td><code>func bytes.NewBuffer(buf []byte *bytes.Buffer</code></td>
<td><code>func bytes.NewBufferString(s string) *bytes.Buffer</code></td>
<td><code>func bytes.NewBuffer(buf []byte *bytes.Buffer</code></td>
</tr>
<tr>
<td><code>func (h *hash/maphash.Hash) Write(b []byte) (int, error)</code></td>
<td><code>func (h *hash/maphash.Hash) WriteString(s string) (int, error)</code></td>
<td><code>func (h *hash/maphash.Hash) Write(b []byte) (int, error)</code></td>
</tr>
<tr>
<td><code>func (rw *net/http/httptest.ResponseRecorder) Write(buf []byte) (int, error)</code></td>
<td><code>func (rw *net/http/httptest.ResponseRecorder) WriteString(str string) (int, error)</code></td>
<td><code>func (rw *net/http/httptest.ResponseRecorder) Write(buf []byte) (int, error)</code></td>
</tr>
<tr>
<td><code>func (f *os.File) Write(b []byte) (n int, err error)</code></td>
<td><code>func (f *os.File) WriteString(s string) (n int, err error)</code></td>
<td><code>func (f *os.File) Write(b []byte) (n int, err error)</code></td>
</tr>
<tr>
<td><code>func regexp.Match(pattern string, b []byte) (bool, error)</code></td>
<td><code>func regexp.MatchString(pattern string, s string) (bool, error)</code></td>
<td><code>func regexp.Match(pattern string, b []byte) (bool, error)</code></td>
</tr>
<tr>
<td><code>func (re *regexp.Regexp) FindAllIndex(b []byte, n int) [][]int</code></td>
<td><code>func (re *regexp.Regexp) FindAllStringIndex(s string, n int) [][]int</code></td>
<td><code>func (re *regexp.Regexp) FindAllIndex(b []byte, n int) [][]int</code></td>
</tr>
<tr>
<td><code>func (re *regexp.Regexp) FindAllSubmatch(b []byte, n int) [][][]byte</code></td>
<td><code>func (re *regexp.Regexp) FindAllStringSubmatch(s string, n int) [][]string</code></td>
<td><code>func (re *regexp.Regexp) FindAllSubmatch(b []byte, n int) [][][]byte</code></td>
</tr>
<tr>
<td><code>func (re *regexp.Regexp) FindIndex(b []byte) (loc []int)</code></td>
<td><code>func (re *regexp.Regexp) FindStringIndex(s string) (loc []int)</code></td>
<td><code>func (re *regexp.Regexp) FindIndex(b []byte) (loc []int)</code></td>
</tr>
<tr>
<td><code>func (re *regexp.Regexp) FindSubmatchIndex(b []byte) []int</code></td>
<td><code>func (re *regexp.Regexp) FindStringSubmatchIndex(s string) []int</code></td>
<td><code>func (re *regexp.Regexp) FindSubmatchIndex(b []byte) []int</code></td>
</tr>
<tr>
<td><code>func (re *regexp.Regexp) Match(b []byte) bool</code></td>
<td><code>func (re *regexp.Regexp) MatchString(s string) bool</code></td>
<td><code>func (re *regexp.Regexp) Match(b []byte) bool</code></td>
</tr>
<tr>
<td><code>func (b *strings.Builder) Write(p []byte) (int, error)</code></td>
<td><code>func (b *strings.Builder) WriteByte(c byte) error</code></td>
<td><code>func (b *strings.Builder) WriteString(s string) error</code></td>
<td>
<code>func (b *strings.Builder) Write(p []byte) (int, error)</code>
<code>func (b *strings.Builder) WriteRune(r rune) (int, error)</code>
</td>
</tr>
<tr>
<td><code>func utf8.Valid(p []byte) bool</code></td>
<td><code>func utf8.ValidString(s string) bool</code></td>
<td><code>func utf8.Valid(p []byte) bool</code></td>
</tr>
<tr>
<td><code>func utf8.FullRune(p []byte) bool</code></td>
<td><code>func utf8.FullRuneInString(s string) bool</code></td>
<td><code>func utf8.FullRune(p []byte) bool</code></td>
</tr>
<tr>
<td><code>func utf8.RuneCount(p []byte) int</code></td>
<td><code>func utf8.RuneCountInString(s string) (n int)</code></td>
<td><code>func utf8.RuneCount(p []byte) int</code></td>
</tr>
<tr>
<td><code>func utf8.DecodeLastRune(p []byte) (rune, int)</code></td>
<td><code>func utf8.DecodeLastRuneInString(s string) (rune, int)</code></td>
<td><code>func utf8.DecodeLastRune(p []byte) (rune, int)</code></td>
</tr>
<tr>
<td><code>func utf8.DecodeRune(p []byte) (rune, int)</code></td>
<td><code>func utf8.DecodeRuneInString(s string) (une, int)</code></td>
<td><code>func utf8.DecodeRune(p []byte) (rune, int)</code></td>
</tr>
</table>
20 changes: 20 additions & 0 deletions checkers_bufio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// },
}
20 changes: 10 additions & 10 deletions checkers_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
// },
}
)
20 changes: 10 additions & 10 deletions checkers_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// },
}
)
13 changes: 11 additions & 2 deletions testdata/born-to-rune.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
)

Expand All @@ -13,20 +15,27 @@ func bornToRune() {

var bbuf bytes.Buffer
var sbuf strings.Builder
buf := bufio.NewWriter(io.Discard)
var r rune = 'п'
b := "p"[0]

sbuf.WriteString(string([]byte("foobar"))) // want `avoid allocations with \(\*strings\.Builder\)\.Write`
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())
Expand Down
3 changes: 2 additions & 1 deletion testdata/issue-26.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit bffce24

Please sign in to comment.