From 43b172b24e4aa20f1b7de8b82a82056d642c00ec Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Sun, 7 May 2023 09:58:29 +0300 Subject: [PATCH] docs: added separate list of methods & update readme --- MIRROR_FUNCS.md | 141 +++++++++++++++++++++++++++++++++ readme.md | 203 +++++++++++++----------------------------------- 2 files changed, 195 insertions(+), 149 deletions(-) create mode 100644 MIRROR_FUNCS.md diff --git a/MIRROR_FUNCS.md b/MIRROR_FUNCS.md new file mode 100644 index 0000000..917589b --- /dev/null +++ b/MIRROR_FUNCS.md @@ -0,0 +1,141 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
func (b *bufio.Writer) Write(p []byte) (int, error)func (b *bufio.Writer) WriteString(s string) (int, error)
func (b *bytes.Buffer) Write(p []byte) (int, error)func (b *bytes.Buffer) WriteString(s string) (int, error)
func bytes.Compare(a, b []byte) intfunc strings.Compare(a, b string) int
func bytes.Contains(b, subslice []byte) boolfunc strings.Contains(s, substr string) bool
func bytes.ContainsAny(b []byte, chars string) boolfunc strings.ContainsAny(s, chars string) bool
func bytes.ContainsRune(b []byte, r rune) boolfunc strings.ContainsRune(s string, r rune) bool
func bytes.Count(s, sep []byte) intfunc strings.Count(s, substr string) int
func bytes.EqualFold(s, t []byte) boolfunc strings.EqualFold(s, t string) bool
func bytes.HasPrefix(s, prefix []byte) boolfunc strings.HasPrefix(s, prefix string) bool
func bytes.HasSuffix(s, suffix []byte) boolfunc strings.HasSuffix(s, suffix string) bool
func bytes.Index(s, sep []byte) intfunc strings.Index(s, substr string) int
func bytes.IndexAny(s []byte, chars string) intfunc strings.IndexAny(s, chars string) int
func bytes.IndexByte(b []byte, c byte) intfunc strings.IndexByte(s string, c byte) int
func bytes.IndexFunc(s []byte, f func(r rune) bool) intfunc strings.IndexFunc(s string, f func(rune) bool) int
func bytes.IndexRune(s []byte, r rune) intfunc strings.IndexRune(s string, r rune) int
func bytes.LastIndex(s, sep []byte) intfunc strings.LastIndex(s, sep string) int
func bytes.LastIndexAny(s []byte, chars string) intfunc strings.LastIndexAny(s, chars string) int
func bytes.LastIndexByte(s []byte, c byte) intfunc strings.LastIndexByte(s string, c byte) int
func bytes.LastIndexFunc(s []byte, f func(r rune) bool) intfunc strings.LastIndexFunc(s string, f func(rune) bool) int
bytes.NewBufferStringbytes.NewBuffer
func (h *hash/maphash.Hash) Write(b []byte) (int, error)func (h *hash/maphash.Hash) WriteString(s string) (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 (f *os.File) Write(b []byte) (n int, err error)func (f *os.File) WriteString(s string) (n int, err error)
func regexp.Match(pattern string, b []byte) (bool, error)func regexp.MatchString(pattern string, s string) (bool, error)
func (re *regexp.Regexp) FindAllIndex(b []byte, n int) [][]intfunc (re *regexp.Regexp) FindAllStringIndex(s string, n int) [][]int
func (re *regexp.Regexp) FindAllSubmatch(b []byte, n int) [][][]bytefunc (re *regexp.Regexp) FindAllStringSubmatch(s string, n int) [][]string
func (re *regexp.Regexp) FindIndex(b []byte) (loc []int)func (re *regexp.Regexp) FindStringIndex(s string) (loc []int)
func (re *regexp.Regexp) FindSubmatchIndex(b []byte) []intfunc (re *regexp.Regexp) FindStringSubmatchIndex(s string) []int
func (re *regexp.Regexp) Match(b []byte) boolfunc (re *regexp.Regexp) MatchString(s string) bool
func (b *strings.Builder) Write(p []byte) (int, error)func (b *strings.Builder) WriteByte(c byte) error
func utf8.Valid(p []byte) boolfunc utf8.ValidString(s string) bool
func utf8.FullRune(p []byte) boolfunc utf8.FullRuneInString(s string) bool
func utf8.RuneCount(p []byte) intfunc utf8.RuneCountInString(s string) (n int)
func utf8.DecodeLastRune(p []byte) (rune, int)func utf8.DecodeLastRuneInString(s string) (rune, int)
func utf8.DecodeRune(p []byte) (rune, int)func utf8.DecodeRuneInString(s string) (une, int)
diff --git a/readme.md b/readme.md index e770d56..31bf7f3 100644 --- a/readme.md +++ b/readme.md @@ -1,171 +1,76 @@ # `mirror` [![Code Coverage](https://coveralls.io/repos/github/butuzov/mirror/badge.svg?branch=main)](https://coveralls.io/github/butuzov/mirror?branch=main) [![build status](https://github.com/butuzov/mirror/actions/workflows/main.yaml/badge.svg?branch=main)]() -`mirror` help to suggest use of alternative methods, in order to avoid additional convertion of `[]byte`/`string`. +`mirror` suggests use of alternative functions/methods in order to gain performance boosts by avoiding unnecessary `[]byte/string` convertion calls. See [MIRROR_FUNCS.md](MIRROR_FUNCS.md) list of mirror functions you can use in go's stdlib. ## πŸ‡ΊπŸ‡¦ PLEASE HELP ME πŸ‡ΊπŸ‡¦ -Fundrise for scout drone **DJI Matrice 30T** See more details at [butuzov/README.md](https://github.com/butuzov/butuzov/) +Fundrise for scout drone **DJI Matrice 30T** for my squad (Ukrainian Forces). See more details at [butuzov/README.md](https://github.com/butuzov/butuzov/) -## Examples +## Linter Use Cases - - - - -
BadGood
+### `github.com/argoproj/argo-cd` ```go -regexp.Match("foo", []byte("foobar1")) +// Before +func IsValidHostname(hostname string, fqdn bool) bool { + if !fqdn { + return validHostNameRegexp.Match([]byte(hostname)) || validIPv6Regexp.Match([]byte(hostname)) + } else { + return validFQDNRegexp.Match([]byte(hostname)) + } +} + +// After: With alternative method (and lost `else` case) +func IsValidHostname(hostname string, fqdn bool) bool { + if !fqdn { + return validHostNameRegexp.MatchString(hostname) || validIPv6Regexp.MatchString(hostname) + } + + return validFQDNRegexp.MatchString(hostname) +} ``` - +## Install -```go -regexp.MatchString("foo", "foobar1") +``` +go install github.com/butuzov/mirrror/cmd/mirror@latest ``` -
- -## Supported Checks - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
func (b *bufio.Writer) Write(p []byte) (int, error)func (b *bufio.Writer) WriteString(s string) (int, error)
func (b *bytes.Buffer) Write(p []byte) (int, error)func (b *bytes.Buffer) WriteString(s string) (int, error)
func bytes.Compare(a, b []byte) intfunc strings.Compare(a, b string) int
func bytes.Contains(b, subslice []byte) boolfunc strings.Contains(s, substr string) bool
func bytes.ContainsAny(b []byte, chars string) boolfunc strings.ContainsAny(s, chars string) bool
func bytes.ContainsRune(b []byte, r rune) boolfunc strings.ContainsRune(s string, r rune) bool
func bytes.Count(s, sep []byte) intfunc strings.Count(s, substr string) int
func bytes.EqualFold(s, t []byte) boolfunc strings.EqualFold(s, t string) bool
func bytes.HasPrefix(s, prefix []byte) boolfunc strings.HasPrefix(s, prefix string) bool
func bytes.HasSuffix(s, suffix []byte) boolfunc strings.HasSuffix(s, suffix string) bool
func bytes.Index(s, sep []byte) intfunc strings.Index(s, substr string) int
func bytes.IndexAny(s []byte, chars string) intfunc strings.IndexAny(s, chars string) int
func bytes.IndexByte(b []byte, c byte) intfunc strings.IndexByte(s string, c byte) int
func bytes.IndexFunc(s []byte, f func(r rune) bool) intfunc strings.IndexFunc(s string, f func(rune) bool) int
func bytes.IndexRune(s []byte, r rune) intfunc strings.IndexRune(s string, r rune) int
func bytes.LastIndex(s, sep []byte) intfunc strings.LastIndex(s, sep string) int
func bytes.LastIndexAny(s []byte, chars string) intfunc strings.LastIndexAny(s, chars string) int
func bytes.LastIndexByte(s []byte, c byte) intfunc strings.LastIndexByte(s string, c byte) int
func bytes.LastIndexFunc(s []byte, f func(r rune) bool) intfunc strings.LastIndexFunc(s string, f func(rune) bool) int
bytes.NewBufferStringbytes.NewBuffer
func (h *maphash.Hash) Write(b []byte) (int, error)func (h *maphash.Hash) WriteString(s string) (int, error)
func (rw *httptest.ResponseRecorder) Write(buf []byte) (int, error)func (rw *httptest.ResponseRecorder) WriteString(str string) (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 regexp.Match(pattern string, b []byte) (bool, error)func regexp.MatchString(pattern string, s string) (bool, error)
func (re *regexp.Regexp) FindAllIndex(b []byte, n int) [][]intfunc (re *regexp.Regexp) FindAllStringIndex(s string, n int) [][]int
func (re *regexp.Regexp) FindAllSubmatch(b []byte, n int) [][][]bytefunc (re *regexp.Regexp) FindAllStringSubmatch(s string, n int) [][]string
func (re *regexp.Regexp) FindIndex(b []byte) (loc []int)func (re *regexp.Regexp) FindStringIndex(s string) (loc []int)
func (re *regexp.Regexp) FindSubmatchIndex(b []byte) []intfunc (re *regexp.Regexp) FindStringSubmatchIndex(s string) []int
func (re *regexp.Regexp) Match(b []byte) boolfunc (re *regexp.Regexp) MatchString(s string) bool
func (b *strings.Builder) Write(p []byte) (int, error)func (b *strings.Builder) WriteByte(c byte) error
func utf8.Valid(p []byte) boolfunc utf8.ValidString(s string) bool
+Or with [`golangci-lint`](https://github.com/golangci/golangci-lint/issue/NNNNN) -## Install -### Compile From Source +## How to use -You can get `mirror` with `go install` command. +You run `mirror` with [`go vet`](https://pkg.go.dev/cmd/vet): + +``` +go vet -vettool=$(which mirror) ./... +# github.com/jcmoraisjr/haproxy-ingress/pkg/common/net/ssl +pkg/common/net/ssl/ssl.go:64:11: avoid allocations with (*os.File).WriteString +pkg/common/net/ssl/ssl.go:161:12: avoid allocations with (*os.File).WriteString +pkg/common/net/ssl/ssl.go:166:3: avoid allocations with (*os.File).WriteString +``` -```shell -go install github.com/butuzov/mirror/cmd/mirror@latest +Can be called directly: +``` +mirror ./... +# https://github.com/cosmtrek/air +/air/runner/util.go:149:6: avoid allocations with (*regexp.Regexp).MatchString +/air/runner/util.go:173:14: avoid allocations with (*os.File).WriteString ``` -## Usage +With [`golangci-lint`](https://github.com/golangci/golangci-lint) -```shell -# include test files reports -mirror --with-tests ./... ``` +// TODO +``` + + +## Command line +- You can add checks for `_test.go` files with cli option `--with-tests` + +### `golangci-lint` + +```yaml +# TODO: add mirror to golangci-ling +```