From 4d3681b71c2c3a6047d6822a4f68b2cb38313228 Mon Sep 17 00:00:00 2001 From: me-cs <49519858+me-cs@users.noreply.github.com> Date: Tue, 20 May 2025 19:36:02 +0800 Subject: [PATCH] Optimize slicing operations (#4877) --- tools/goctl/api/javagen/gencomponents.go | 8 +- tools/goctl/api/spec/fn.go | 8 +- tools/goctl/migrate/mod.go | 4 +- tools/goctl/util/stringx/string_test.go | 128 +++++++++++++++++++++++ 4 files changed, 138 insertions(+), 10 deletions(-) diff --git a/tools/goctl/api/javagen/gencomponents.go b/tools/goctl/api/javagen/gencomponents.go index 0f78df270..6d8b5903e 100644 --- a/tools/goctl/api/javagen/gencomponents.go +++ b/tools/goctl/api/javagen/gencomponents.go @@ -8,10 +8,10 @@ import ( "fmt" "io" "path" + "slices" "strings" "text/template" - "github.com/zeromicro/go-zero/core/stringx" "github.com/zeromicro/go-zero/tools/goctl/api/spec" apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util" "github.com/zeromicro/go-zero/tools/goctl/internal/version" @@ -96,13 +96,13 @@ func (c *componentsContext) createComponent(dir, packetName string, ty spec.Type for _, item := range c.responseTypes { if item.Name() == defineStruct.Name() { superClassName = "HttpResponseData" - if !stringx.Contains(c.imports, httpResponseData) { + if !slices.Contains(c.imports, httpResponseData) { c.imports = append(c.imports, httpResponseData) } break } } - if superClassName == "HttpData" && !stringx.Contains(c.imports, httpData) { + if superClassName == "HttpData" && !slices.Contains(c.imports, httpData) { c.imports = append(c.imports, httpData) } @@ -266,7 +266,7 @@ func (c *componentsContext) genGetSet(writer io.Writer, indent int) error { tyString := javaType decorator := "" javaPrimitiveType := []string{"int", "long", "boolean", "float", "double", "short"} - if !stringx.Contains(javaPrimitiveType, javaType) { + if !slices.Contains(javaPrimitiveType, javaType) { if member.IsOptional() || member.IsOmitEmpty() { decorator = "@Nullable " } else { diff --git a/tools/goctl/api/spec/fn.go b/tools/goctl/api/spec/fn.go index 1032b3293..441f27a21 100644 --- a/tools/goctl/api/spec/fn.go +++ b/tools/goctl/api/spec/fn.go @@ -3,9 +3,9 @@ package spec import ( "errors" "path" + "slices" "strings" - "github.com/zeromicro/go-zero/core/stringx" "github.com/zeromicro/go-zero/tools/goctl/util" ) @@ -64,7 +64,7 @@ func (m Member) IsOptional() bool { tag := m.Tags() for _, item := range tag { if item.Key == bodyTagKey || item.Key == formTagKey { - if stringx.Contains(item.Options, "optional") { + if slices.Contains(item.Options, "optional") { return true } } @@ -81,7 +81,7 @@ func (m Member) IsOmitEmpty() bool { tag := m.Tags() for _, item := range tag { if item.Key == bodyTagKey { - if stringx.Contains(item.Options, "omitempty") { + if slices.Contains(item.Options, "omitempty") { return true } } @@ -93,7 +93,7 @@ func (m Member) IsOmitEmpty() bool { func (m Member) GetPropertyName() (string, error) { tags := m.Tags() for _, tag := range tags { - if stringx.Contains(definedKeys, tag.Key) { + if slices.Contains(definedKeys, tag.Key) { if tag.Name == "-" { return util.Untitle(m.Name), nil } diff --git a/tools/goctl/migrate/mod.go b/tools/goctl/migrate/mod.go index 74a54b7ba..8a28662f2 100644 --- a/tools/goctl/migrate/mod.go +++ b/tools/goctl/migrate/mod.go @@ -4,9 +4,9 @@ import ( "errors" "fmt" "os" + "slices" "time" - "github.com/zeromicro/go-zero/core/stringx" "github.com/zeromicro/go-zero/tools/goctl/rpc/execx" "github.com/zeromicro/go-zero/tools/goctl/util/console" "github.com/zeromicro/go-zero/tools/goctl/util/ctx" @@ -37,7 +37,7 @@ func editMod(version string, verbose bool) error { return err } - if !stringx.Contains(latest, version) { + if !slices.Contains(latest, version) { return fmt.Errorf("release version %q is not found", version) } diff --git a/tools/goctl/util/stringx/string_test.go b/tools/goctl/util/stringx/string_test.go index 1ed1ee7c0..346eb4e9a 100644 --- a/tools/goctl/util/stringx/string_test.go +++ b/tools/goctl/util/stringx/string_test.go @@ -192,3 +192,131 @@ func TestUntitle(t *testing.T) { assert.Equal(t, c.want, ret) } } + +func TestContainsAny(t *testing.T) { + type args struct { + s string + runes []rune + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "runes is empty", + args: args{ + s: "test", + runes: []rune{}, + }, + want: true, + }, + { + name: "s is empty and runes is not empty", + args: args{ + s: "", + runes: []rune{'a', 'b', 'c'}, + }, + want: false, + }, + { + name: "s contains runes", + args: args{ + s: "hello", + runes: []rune{'e', 'f'}, + }, + want: true, + }, + { + name: "s does not contain runes", + args: args{ + s: "hello", + runes: []rune{'x', 'y'}, + }, + want: false, + }, + { + name: "s and runes both have one matching character", + args: args{ + s: "a", + runes: []rune{'a'}, + }, + want: true, + }, + { + name: "s and runes both have one non-matching character", + args: args{ + s: "a", + runes: []rune{'b'}, + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.want, ContainsAny(tt.args.s, tt.args.runes...), "ContainsAny(%v, %v)", tt.args.s, tt.args.runes) + }) + } +} + +func TestContainsWhiteSpace(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "contains space", + args: args{s: "hello world"}, + want: true, + }, + { + name: "contains newline", + args: args{s: "hello\nworld"}, + want: true, + }, + { + name: "contains tab", + args: args{s: "hello\tworld"}, + want: true, + }, + { + name: "contains form feed", + args: args{s: "hello\fworld"}, + want: true, + }, + { + name: "contains vertical tab", + args: args{s: "hello\vworld"}, + want: true, + }, + { + name: "no whitespace", + args: args{s: "helloworld"}, + want: false, + }, + { + name: "empty string", + args: args{s: ""}, + want: false, + }, + { + name: "only whitespace", + args: args{s: " \t\n\f\v"}, + want: true, + }, + { + name: "contains non-standard whitespace", + args: args{s: "hello\u00A0world"}, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.want, ContainsWhiteSpace(tt.args.s), "ContainsWhiteSpace(%v)", tt.args.s) + }) + } +}