diff --git a/.gitignore b/.gitignore index a945302..d1c59f0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ *.test *.report report/ -vendor/ \ No newline at end of file +vendor/ +tt.go diff --git a/.travis.yml b/.travis.yml index 1e5ba85..b2bcb6a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,13 +2,16 @@ sudo: false language: go dist: trusty go: + - 1.12.x + - 1.11.x + - 1.10.x - 1.9.x - 1.8.x - 1.7.x - 1.6.x - 1.5.x - 1.4.x - - tip +# - tip before_install: - go get github.com/mattn/goveralls - go get golang.org/x/tools/cmd/cover diff --git a/Makefile b/Makefile index 42f1071..946aa78 100644 --- a/Makefile +++ b/Makefile @@ -33,12 +33,13 @@ CMD_ECHO :=$(shell which echo) CMD_NM :=$(shell which nm) CMD_GO :=$(shell which go) CMD_GOLINT :=$(shell which golint) -CMD_GOMETALINTER :=$(shell which gometalinter) CMD_GOIMPORTS :=$(shell which goimport) CMD_MAKE2HELP :=$(shell which make2help) CMD_GLIDE :=$(shell which glide) CMD_GOVER :=$(shell which gover) CMD_GOVERALLS :=$(shell which goveralls) +CMD_CILINT :=$(shell which golangci-lint) +CMD_CURL :=$(shell which curl) PATH_REPORT=report PATH_RACE_REPORT=$(PKG_NAME).race.report @@ -50,42 +51,48 @@ PATH_PROF_MUTEX=$(PKG_NAME).mutex.prof VER_GOLANG=$(shell go version | awk '{print $$3}' | sed -e "s/go//;s/\.//g") GOLANGV18_OVER=$(shell [ "$(VER_GOLANG)" -ge "180" ] && echo 1 || echo 0) +GOMOD_FOUND=$(shell go --help 2>&1 | fgrep "module maintenance" | awk '{print $$1}') +GOMOD_SUPPORT=$(shell [ "$(GOMOD_FOUND)" = "mod" ] && echo 1 || echo 0) all: clean setup ## Setup Build Environment setup:: - @$(CMD_ECHO) -e "\033[1;40;32mSetup Build Environment.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;32mSetup Build Environment.\033[01;m\x1b[0m" +ifeq ($(GOMOD_SUPPORT),1) + @$(CMD_GO) mod tidy + @$(CMD_GO) mod verify +else @$(CMD_GO) get github.com/Masterminds/glide @$(CMD_GO) get github.com/Songmu/make2help/cmd/make2help @$(CMD_GO) get github.com/davecgh/go-spew/spew @$(CMD_GO) get github.com/k0kubun/pp - @$(CMD_GO) get github.com/alecthomas/gometalinter @$(CMD_GO) get github.com/mattn/goveralls @$(CMD_GO) get golang.org/x/tools/cmd/cover @$(CMD_GO) get github.com/modocache/gover @$(CMD_GO) get github.com/dustin/go-humanize - @$(CMD_GO) get github.com/golang/lint/golint + @$(CMD_GO) get golang.org/x/lint/golint @$(CMD_GO) get github.com/awalterschulze/gographviz - @$(CMD_GO) get github.com/golang/dep/cmd/dep - @$(CMD_GOMETALINTER) install + @GO111MODULE=off $(CMD_GO) get github.com/golang/dep/cmd/dep +endif + @$(CMD_CURL) -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.16.0 @$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m" ## Build the go-strutil build:: - @$(CMD_ECHO) -e "\033[1;40;32mBuild the go-strutil.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;32mBuild the go-strutil.\033[01;m\x1b[0m" @$(CMD_GO) build @$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m" ## Build the go-strutil for development devbuild:: - @$(CMD_ECHO) -e "\033[1;40;32mBuild the go-strutil.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;32mBuild the go-strutil.\033[01;m\x1b[0m" @$(CMD_GO) build -x -v -gcflags="-N -l" @$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m" ## Run a LintChecker (Normal) lint: setup - @$(CMD_ECHO) -e "\033[1;40;32mRun a LintChecker (Normal).\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;32mRun a LintChecker (Normal).\033[01;m\x1b[0m" @$(CMD_GO) vet $$($(CMD_GLIDE) novendor) @for pkg in $$($(CMD_GLIDE) novendor -x); do \ $(CMD_GOLINT) -set_exit_status $$pkg || exit $$?; \ @@ -94,13 +101,13 @@ lint: setup ## Run a LintChecker (Strict) strictlint: setup - @$(CMD_ECHO) -e "\033[1;40;32mRun a LintChecker (Strict).\033[01;m\x1b[0m" - @$(CMD_GOMETALINTER) $$($(CMD_GLIDE) novendor) + @$(CMD_ECHO) -e "\033[1;40;32mRun a LintChecker (Strict).\033[01;m\x1b[0m" + @$(CMD_CILINT) run $$($(CMD_GLIDE) novendor) @$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m" ## Run Go Test with Data Race Detection testassert: clean - @$(CMD_ECHO) -e "\033[1;40;32mRun Go Test.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;32mRun Go Test.\033[01;m\x1b[0m" @$(CMD_GO) test -v -test.parallel 4 -race -run Test_strutils_Assert* @$(CMD_ECHO) -e "\033[1;40;36mGenerated a report of data race detection in $(PATH_REPORT)/doc/$(PATH_RACE_REPORT).pid\033[01;m\x1b[0m" @$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m" @@ -108,7 +115,7 @@ testassert: clean ## Run Go Test with Data Race Detection test: clean @$(CMD_MKDIR) -p $(PATH_REPORT)/raw/ $(PATH_REPORT)/doc/ - @$(CMD_ECHO) -e "\033[1;40;32mRun Go Test.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;32mRun Go Test.\033[01;m\x1b[0m" @GORACE="log_path=$(PATH_REPORT)/doc/$(PATH_RACE_REPORT)" $(CMD_GO) test -tags unittest -v -test.parallel 4 -race -coverprofile=$(PATH_REPORT)/raw/$(PATH_CONVER_PROFILE) @$(CMD_ECHO) -e "\033[1;40;36mGenerated a report of data race detection in $(PATH_REPORT)/doc/$(PATH_RACE_REPORT).pid\033[01;m\x1b[0m" @$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m" @@ -116,13 +123,13 @@ test: clean ## Send a report of coverage profile to coveralls.io coveralls:: @$(CMD_GO) get github.com/mattn/goveralls - @$(CMD_ECHO) -e "\033[1;40;32mSend a report of coverage profile to coveralls.io.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;32mSend a report of coverage profile to coveralls.io.\033[01;m\x1b[0m" @$(CMD_GOVERALLS) -coverprofile=$(PATH_REPORT)/raw/$(PATH_CONVER_PROFILE) -service=travis-ci @$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m" ## Generate a report about coverage cover: test - @$(CMD_ECHO) -e "\033[1;40;32mGenerate a report about coverage.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;32mGenerate a report about coverage.\033[01;m\x1b[0m" @$(CMD_GO) tool cover -func=$(PATH_CONVER_PROFILE) -o $(PATH_CONVER_PROFILE).txt @$(CMD_GO) tool cover -html=$(PATH_CONVER_PROFILE) -o $(PATH_CONVER_PROFILE).html @$(CMD_ECHO) -e "\033[1;40;36mGenerated a report file : $(PATH_CONVER_PROFILE).html\033[01;m\x1b[0m" @@ -131,15 +138,15 @@ cover: test ## Profiling pprof: clean @$(CMD_MKDIR) -p $(PATH_REPORT)/raw/ $(PATH_REPORT)/doc/ - @$(CMD_ECHO) -e "\033[1;40;32mGenerate profiles.\033[01;m\x1b[0m" - @$(CMD_ECHO) -e "\033[1;40;33mGenerate a CPU profile.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;32mGenerate profiles.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;33mGenerate a CPU profile.\033[01;m\x1b[0m" @$(CMD_GO) test -tags unittest -test.parallel 4 -bench . -benchmem -cpuprofile=$(PATH_REPORT)/raw/$(PATH_PROF_CPU) - @$(CMD_ECHO) -e "\033[1;40;33mGenerate a Memory profile.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;33mGenerate a Memory profile.\033[01;m\x1b[0m" @$(CMD_GO) test -tags unittest -test.parallel 4 -bench . -benchmem -memprofile=$(PATH_REPORT)/raw/$(PATH_PROF_MEM) - @$(CMD_ECHO) -e "\033[1;40;33mGenerate a Block profile.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;33mGenerate a Block profile.\033[01;m\x1b[0m" @$(CMD_GO) test -tags unittest -test.parallel 4 -bench . -benchmem -blockprofile=$(PATH_REPORT)/raw/$(PATH_PROF_BLOCK) ifeq ($(GOLANGV18_OVER),1) - @$(CMD_ECHO) -e "\033[1;40;33mGenerate a Mutex profile.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;33mGenerate a Mutex profile.\033[01;m\x1b[0m" @$(CMD_GO) test -tags unittest -test.parallel 4 -bench . -benchmem -mutexprofile=$(PATH_REPORT)/raw/$(PATH_PROF_MUTEX) endif @$(CMD_MV) -f *.test $(PATH_REPORT)/raw/ @@ -148,14 +155,14 @@ endif ## Generate report fo profiling report: pprof @$(CMD_MKDIR) -p $(PATH_REPORT)/raw/ $(PATH_REPORT)/doc/ - @$(CMD_ECHO) -e "\033[1;40;33mGenerate all report in text format.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;33mGenerate all report in text format.\033[01;m\x1b[0m" @$(CMD_GO) tool pprof -text $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_CPU) > $(PATH_REPORT)/doc/$(PATH_PROF_CPU).txt @$(CMD_GO) tool pprof -text $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_MEM) > $(PATH_REPORT)/doc/$(PATH_PROF_MEM).txt @$(CMD_GO) tool pprof -text $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_BLOCK) > $(PATH_REPORT)/doc/$(PATH_PROF_BLOCK).txt ifeq ($(GOLANGV18_OVER),1) @$(CMD_GO) tool pprof -text $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_MUTEX) > $(PATH_REPORT)/doc/$(PATH_PROF_MUTEX).txt endif - @$(CMD_ECHO) -e "\033[1;40;33mGenerate all report in pdf format.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;33mGenerate all report in pdf format.\033[01;m\x1b[0m" @$(CMD_GO) tool pprof -pdf $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_CPU) > $(PATH_REPORT)/doc/$(PATH_PROF_CPU).pdf @$(CMD_GO) tool pprof -pdf $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_MEM) > $(PATH_REPORT)/doc/$(PATH_PROF_MEM).pdf @$(CMD_GO) tool pprof -pdf $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_BLOCK) > $(PATH_REPORT)/doc/$(PATH_PROF_BLOCK).pdf @@ -170,7 +177,7 @@ help:: ## Clean-up clean:: - @$(CMD_ECHO) -e "\033[1;40;32mClean-up.\033[01;m\x1b[0m" + @$(CMD_ECHO) -e "\033[1;40;32mClean-up.\033[01;m\x1b[0m" @$(CMD_RM) -rfv *.coverprofile *.swp *.core *.html *.prof *.test *.report ./$(PATH_REPORT)/* @$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m" diff --git a/README.md b/README.md index 40c4168..58e9f25 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ README.md haven't contain all the examples. Please refer to the the XXXtest.go f - [ReverseUnicode](#reverseunicode) - [FileMD5Hash](#filemd5hash) - [MD5Hash](#md5hash) + - [RegExpNamedGroups](#RegExpNamedGroups) - [Validation Methods](#validation-methods) - [IsValidEmail](#isvalidemail) - [IsValidDomain](#isvaliddomain) @@ -784,6 +785,35 @@ The above example will output: 15f764f21d09b11102eb015fc8824d00 ``` + +### RegExpNamedGroups + +RegExpNamedGroups is Captures the text matched by regex into the group name + +```go +func (s *StringProc) RegExpNamedGroups(regex *regexp.Regexp, val string) (map[string]string, error) +``` + +Example: + +```go +strproc := strutils.NewStringProc() + +regexGoVersion := regexp.MustCompile(`go(?P([0-9]{1,3}))\.(?P([0-9]{1,3}))(\.(?P([0-9]{1,3})))?`) +retval, err := getGroupMatched(regexGoVersion, runtime.Version()) +if err != nil { + return 0, err +} + +fmt.Println(retval) +``` + +The above example will output: + +```bash +map[major:1 minor:11 rev:5] +``` + ---- ## Validation Methods diff --git a/go.mod b/go.mod index 0c8ad13..8be5e58 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/torden/go-strutil -require github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4 +require github.com/dustin/go-humanize v1.0.0 diff --git a/go.sum b/go.sum index 897e6f7..4d39dd1 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4 h1:qk/FSDDxo05wdJH28W+p5yivv7LuLYLRXPPD8KQCtZs= -github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= diff --git a/stringproc.go b/stringproc.go index fdf3cee..1de1aab 100644 --- a/stringproc.go +++ b/stringproc.go @@ -1156,4 +1156,34 @@ func (s *StringProc) closeFd(fd *os.File) { if err != nil { fmt.Printf("Error : %+v\n", err) } + +} + +// RegExpNamedGroups is Captures the text matched by regex into the group name +// NOTE : Not Support the Multiple Groups with The Same Name +func (s *StringProc) RegExpNamedGroups(regex *regexp.Regexp, val string) (map[string]string, error) { + + ok := false + err := errors.New("not all success patterns were matched") + + retval := map[string]string{} + extractSubExpNames := regex.SubexpNames() + + ret := regex.FindStringSubmatch(val) + if len(ret) > 0 { + for no, val := range ret { + if no != 0 && val != "" { + if extractSubExpNames[no] != "" { + retval[extractSubExpNames[no]] = val + ok = true + } + } + } + } + + if ok { + err = nil + } + + return retval, err } diff --git a/stringproc_test.go b/stringproc_test.go index c30fcab..dc57957 100644 --- a/stringproc_test.go +++ b/stringproc_test.go @@ -5,6 +5,8 @@ import ( "math" "net/url" "os" + "regexp" + "runtime" "strings" "testing" "time" @@ -1501,3 +1503,22 @@ func Test_strutils_MD5Hash(t *testing.T) { assert.AssertEquals(t, retval, v, "Return Value mismatch.\nExpected: %v\nActual: %v", retval, v) } } + +func Test_strutils_RegExpNamedGroups(t *testing.T) { + + t.Parallel() + + var ok bool + + //refer : https://golang.org/doc/devel/release.html#policy + var regexGoVersion = regexp.MustCompile(`go(?P([0-9]{1,3}))\.(?P([0-9]{1,3}))(\.(?P([0-9]{1,3})))?`) + + verdic, err := strproc.RegExpNamedGroups(regexGoVersion, runtime.Version()) + assert.AssertNil(t, err, "Error : %v", err) + + _, ok = verdic["major"] + assert.AssertTrue(t, ok, "Not Exists Major ver. in Return Value") + + _, ok = verdic["minor"] + assert.AssertTrue(t, ok, "Not Exists Minor ver. in Return Value") +}