From 0ffe17ee3d200689f7bdb51c1b3dfd460272253d Mon Sep 17 00:00:00 2001 From: John Date: Thu, 11 Apr 2019 09:26:52 +0800 Subject: [PATCH] revert SearchArray/InArray functions for gstr --- g/internal/cmdenv/cmdenv.go | 27 +++++++++++++++++++++------ g/text/gstr/gstr.go | 17 +++++++++++++++++ g/text/gstr/gstr_z_unit_basic_test.go | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/g/internal/cmdenv/cmdenv.go b/g/internal/cmdenv/cmdenv.go index 31e7475bc..9dd9a8d08 100644 --- a/g/internal/cmdenv/cmdenv.go +++ b/g/internal/cmdenv/cmdenv.go @@ -7,12 +7,27 @@ package cmdenv import ( - "github.com/gogf/gf/g/container/gvar" - "github.com/gogf/gf/g/os/gcmd" - "github.com/gogf/gf/g/os/genv" - "strings" + "github.com/gogf/gf/g/container/gvar" + "os" + "regexp" + "strings" ) +var ( + // Console options. + cmdOptions = make(map[string]string) +) + +func init() { + reg := regexp.MustCompile(`\-\-{0,1}(.+?)=(.+)`) + for i := 0; i < len(os.Args); i++ { + result := reg.FindStringSubmatch(os.Args[i]) + if len(result) > 1 { + cmdOptions[result[1]] = result[2] + } + } +} + // 获取指定名称的命令行参数,当不存在时获取环境变量参数,皆不存在时,返回给定的默认值。 // 规则: // 1、命令行参数以小写字母格式,使用: gf.包名.变量名 传递; @@ -22,11 +37,11 @@ func Get(key string, def...interface{}) gvar.VarRead { if len(def) > 0 { value = def[0] } - if v := gcmd.Option.Get(key); v != "" { + if v, ok := cmdOptions[key]; ok { value = v } else { key = strings.ToUpper(strings.Replace(key, ".", "_", -1)) - if v := genv.Get(key); v != "" { + if v := os.Getenv(key); v != "" { value = v } } diff --git a/g/text/gstr/gstr.go b/g/text/gstr/gstr.go index fc03ce3bc..b1dad21fd 100644 --- a/g/text/gstr/gstr.go +++ b/g/text/gstr/gstr.go @@ -594,4 +594,21 @@ func QuoteMeta(str string) string { buf.WriteRune(char) } return buf.String() +} + +// SearchArray searches string in string slice case-sensitively, +// returns its index in . +// If is not found in , it returns -1. +func SearchArray (a []string, s string) int { + for i, v := range a { + if s == v { + return i + } + } + return -1 +} + +// InArray checks whether string in slice . +func InArray (a []string, s string) bool { + return SearchArray(a, s) != -1 } \ No newline at end of file diff --git a/g/text/gstr/gstr_z_unit_basic_test.go b/g/text/gstr/gstr_z_unit_basic_test.go index b69939236..d15e312db 100644 --- a/g/text/gstr/gstr_z_unit_basic_test.go +++ b/g/text/gstr/gstr_z_unit_basic_test.go @@ -383,3 +383,22 @@ func Test_ContainsAny(t *testing.T) { }) } +func Test_SearchArray(t *testing.T) { + gtest.Case(t, func() { + a := g.SliceStr{"a", "b", "c"} + gtest.AssertEQ(gstr.SearchArray(a, "a"), 0) + gtest.AssertEQ(gstr.SearchArray(a, "b"), 1) + gtest.AssertEQ(gstr.SearchArray(a, "c"), 2) + gtest.AssertEQ(gstr.SearchArray(a, "d"), -1) + }) +} + +func Test_InArray(t *testing.T) { + gtest.Case(t, func() { + a := g.SliceStr{"a", "b", "c"} + gtest.AssertEQ(gstr.InArray(a, "a"), true) + gtest.AssertEQ(gstr.InArray(a, "b"), true) + gtest.AssertEQ(gstr.InArray(a, "c"), true) + gtest.AssertEQ(gstr.InArray(a, "d"), false) + }) +}