fix issue in gset.Pop; improve internal/debug

This commit is contained in:
john 2019-08-09 09:51:52 +08:00
parent c7c6e2866e
commit 3af3fd1428
10 changed files with 33 additions and 23 deletions

View File

@ -297,7 +297,7 @@ func (set *Set) Sum() (sum int) {
} }
// Pops randomly pops an item from set. // Pops randomly pops an item from set.
func (set *Set) Pop(size int) interface{} { func (set *Set) Pop() interface{} {
set.mu.RLock() set.mu.RLock()
defer set.mu.RUnlock() defer set.mu.RUnlock()
for k, _ := range set.m { for k, _ := range set.m {

View File

@ -291,7 +291,7 @@ func (set *IntSet) Sum() (sum int) {
} }
// Pops randomly pops an item from set. // Pops randomly pops an item from set.
func (set *IntSet) Pop(size int) int { func (set *IntSet) Pop() int {
set.mu.RLock() set.mu.RLock()
defer set.mu.RUnlock() defer set.mu.RUnlock()
for k, _ := range set.m { for k, _ := range set.m {

View File

@ -292,7 +292,7 @@ func (set *StringSet) Sum() (sum int) {
} }
// Pops randomly pops an item from set. // Pops randomly pops an item from set.
func (set *StringSet) Pop(size int) string { func (set *StringSet) Pop() string {
set.mu.RLock() set.mu.RLock()
defer set.mu.RUnlock() defer set.mu.RUnlock()
for k, _ := range set.m { for k, _ := range set.m {

View File

@ -39,7 +39,7 @@ func PrintStack(skip ...int) {
// Stack returns a formatted stack trace of the goroutine that calls it. // Stack returns a formatted stack trace of the goroutine that calls it.
// It calls runtime.Stack with a large enough buffer to capture the entire trace. // It calls runtime.Stack with a large enough buffer to capture the entire trace.
func Stack(skip ...int) string { func Stack(skip ...int) string {
return StackWithFilter(gFILTER_KEY, skip...) return StackWithFilter("", skip...)
} }
// StackWithFilter returns a formatted stack trace of the goroutine that calls it. // StackWithFilter returns a formatted stack trace of the goroutine that calls it.
@ -83,33 +83,39 @@ func StackWithFilter(filter string, skip ...int) string {
return buffer.String() return buffer.String()
} }
// CallerPath returns the absolute file path along with its line number of the caller. // CallerPath returns the function name and the absolute file path along with its line number of the caller.
func Caller(skip ...int) (path string, line int) { func Caller(skip ...int) (function string, path string, line int) {
return CallerWithFilter("", skip...) return CallerWithFilter("", skip...)
} }
// CallerPathWithFilter returns the absolute file path along with its line number of the caller. // CallerPathWithFilter returns the function name and the absolute file path along with its line number of the caller.
// //
// The parameter <filter> is used to filter the path of the caller. // The parameter <filter> is used to filter the path of the caller.
func CallerWithFilter(filter string, skip ...int) (path string, line int) { func CallerWithFilter(filter string, skip ...int) (function string, path string, line int) {
number := 0 number := 0
if len(skip) > 0 { if len(skip) > 0 {
number = skip[0] number = skip[0]
} }
for i := callerFromIndex(filter) + number; i < gMAX_DEPTH; i++ { for i := callerFromIndex(filter) + number; i < gMAX_DEPTH; i++ {
if _, file, line, ok := runtime.Caller(i); ok { if pc, file, line, ok := runtime.Caller(i); ok {
if filter != "" && strings.Contains(file, filter) { if filter != "" && strings.Contains(file, filter) {
continue continue
} }
if strings.Contains(file, gFILTER_KEY) { if strings.Contains(file, gFILTER_KEY) {
continue continue
} }
return file, line function := ""
if fn := runtime.FuncForPC(pc); fn == nil {
function = "unknown"
} else {
function = fn.Name()
}
return function, file, line
} else { } else {
break break
} }
} }
return "", -1 return "", "", -1
} }
// callerFromIndex returns the caller position exclusive of the debug package. // callerFromIndex returns the caller position exclusive of the debug package.

View File

@ -169,7 +169,7 @@ func (s *Server) setHandler(pattern string, handler *handlerItem) {
if _, ok := s.routesMap[regKey]; !ok { if _, ok := s.routesMap[regKey]; !ok {
s.routesMap[regKey] = make([]registeredRouteItem, 0) s.routesMap[regKey] = make([]registeredRouteItem, 0)
} }
file, line := debug.CallerWithFilter(gFILTER_KEY) _, file, line := debug.CallerWithFilter(gFILTER_KEY)
s.routesMap[regKey] = append(s.routesMap[regKey], registeredRouteItem{ s.routesMap[regKey] = append(s.routesMap[regKey], registeredRouteItem{
file: fmt.Sprintf(`%s:%d`, file, line), file: fmt.Sprintf(`%s:%d`, file, line),
handler: handler, handler: handler,

View File

@ -16,14 +16,14 @@ import (
) )
const ( const (
gPATH_FILTER_KEY = "/gf/os/gfile/gfile_source.go" gPATH_FILTER_KEY = "/gf/os/gfile/gfile"
) )
// SourcePath returns absolute file path of the current source file path. // SourcePath returns absolute file path of the current source file path.
// //
// Note that it's only available in develop environment. // Note that it's only available in develop environment.
func SourcePath(skip ...int) string { func SourcePath(skip ...int) string {
path, _ := debug.CallerWithFilter(gPATH_FILTER_KEY, skip...) _, path, _ := debug.CallerWithFilter(gPATH_FILTER_KEY, skip...)
return path return path
} }

View File

@ -244,11 +244,11 @@ func (l *Logger) print(std io.Writer, lead string, value ...interface{}) {
// Caller path. // Caller path.
callerPath := "" callerPath := ""
if l.flags&F_FILE_LONG > 0 { if l.flags&F_FILE_LONG > 0 {
path, line := debug.CallerWithFilter(gPATH_FILTER_KEY, l.stSkip) _, path, line := debug.CallerWithFilter(gPATH_FILTER_KEY, l.stSkip)
callerPath = fmt.Sprintf(`%s:%d: `, path, line) callerPath = fmt.Sprintf(`%s:%d: `, path, line)
} }
if l.flags&F_FILE_SHORT > 0 { if l.flags&F_FILE_SHORT > 0 {
path, line := debug.CallerWithFilter(gPATH_FILTER_KEY, l.stSkip) _, path, line := debug.CallerWithFilter(gPATH_FILTER_KEY, l.stSkip)
callerPath = fmt.Sprintf(`%s:%d: `, gfile.Basename(path), line) callerPath = fmt.Sprintf(`%s:%d: `, gfile.Basename(path), line)
} }
if len(callerPath) > 0 { if len(callerPath) > 0 {

View File

@ -14,12 +14,12 @@ import (
func Test_Unsafe(t *testing.T) { func Test_Unsafe(t *testing.T) {
gtest.Case(t, func() { gtest.Case(t, func() {
s := "John爱中国" s := "I love 小泽玛利亚"
gtest.AssertEQ(gconv.UnsafeStrToBytes(s), []byte(s)) gtest.AssertEQ(gconv.UnsafeStrToBytes(s), []byte(s))
}) })
gtest.Case(t, func() { gtest.Case(t, func() {
b := []byte("John爱中国") b := []byte("I love 小泽玛利亚")
gtest.AssertEQ(gconv.UnsafeBytesToStr(b), string(b)) gtest.AssertEQ(gconv.UnsafeBytesToStr(b), string(b))
}) })
} }

View File

@ -23,6 +23,7 @@ func MeetProb(prob float32) bool {
} }
// N returns a random int between min and max - [min, max]. // N returns a random int between min and max - [min, max].
// The <min> and <max> also support negative numbers.
func N(min, max int) int { func N(min, max int) int {
if min >= max { if min >= max {
return min return min
@ -31,14 +32,14 @@ func N(min, max int) int {
// Because Intn dose not support negative number, // Because Intn dose not support negative number,
// so we should first shift the value to left, // so we should first shift the value to left,
// then call Intn to produce the random number, // then call Intn to produce the random number,
// and finally shift the result to right. // and finally shift the result back to right.
return Intn(max-(min-0)+1) + (min - 0) return Intn(max-(min-0)+1) + (min - 0)
} }
if min < 0 { if min < 0 {
// Because Intn dose not support negative number, // Because Intn dose not support negative number,
// so we should first shift the value to right, // so we should first shift the value to right,
// then call Intn to produce the random number, // then call Intn to produce the random number,
// and finally shift the result to left. // and finally shift the result back to left.
return Intn(max+(0-min)+1) - (0 - min) return Intn(max+(0-min)+1) - (0 - min)
} }
return 0 return 0
@ -76,7 +77,6 @@ func Digits(n int) string {
b[i] = digits[Intn(10)] b[i] = digits[Intn(10)]
} }
return string(b) return string(b)
} }
// Deprecated. // Deprecated.
@ -101,7 +101,7 @@ func RandLetters(n int) string {
return Letters(n) return Letters(n)
} }
// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n). // Perm returns, as a slice of n int numbers, a pseudo-random permutation of the integers [0,n).
func Perm(n int) []int { func Perm(n int) []int {
m := make([]int, n) m := make([]int, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {

View File

@ -13,9 +13,13 @@ import (
) )
const ( const (
gFILTER_KEY = "/gf/util/gutil/gutil_debug.go" gFILTER_KEY = "/gf/util/gutil/gutil"
) )
func Package() string {
}
// PrintStack prints to standard error the stack trace returned by runtime.Stack. // PrintStack prints to standard error the stack trace returned by runtime.Stack.
func PrintStack(skip ...int) { func PrintStack(skip ...int) {
fmt.Print(Stack(skip...)) fmt.Print(Stack(skip...))