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.
func (set *Set) Pop(size int) interface{} {
func (set *Set) Pop() interface{} {
set.mu.RLock()
defer set.mu.RUnlock()
for k, _ := range set.m {

View File

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

View File

@ -292,7 +292,7 @@ func (set *StringSet) Sum() (sum int) {
}
// Pops randomly pops an item from set.
func (set *StringSet) Pop(size int) string {
func (set *StringSet) Pop() string {
set.mu.RLock()
defer set.mu.RUnlock()
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.
// It calls runtime.Stack with a large enough buffer to capture the entire trace.
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.
@ -83,33 +83,39 @@ func StackWithFilter(filter string, skip ...int) string {
return buffer.String()
}
// CallerPath returns the absolute file path along with its line number of the caller.
func Caller(skip ...int) (path string, line int) {
// CallerPath returns the function name and the absolute file path along with its line number of the caller.
func Caller(skip ...int) (function string, path string, line int) {
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.
func CallerWithFilter(filter string, skip ...int) (path string, line int) {
func CallerWithFilter(filter string, skip ...int) (function string, path string, line int) {
number := 0
if len(skip) > 0 {
number = skip[0]
}
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) {
continue
}
if strings.Contains(file, gFILTER_KEY) {
continue
}
return file, line
function := ""
if fn := runtime.FuncForPC(pc); fn == nil {
function = "unknown"
} else {
function = fn.Name()
}
return function, file, line
} else {
break
}
}
return "", -1
return "", "", -1
}
// 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 {
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{
file: fmt.Sprintf(`%s:%d`, file, line),
handler: handler,

View File

@ -16,14 +16,14 @@ import (
)
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.
//
// Note that it's only available in develop environment.
func SourcePath(skip ...int) string {
path, _ := debug.CallerWithFilter(gPATH_FILTER_KEY, skip...)
_, path, _ := debug.CallerWithFilter(gPATH_FILTER_KEY, skip...)
return path
}

View File

@ -244,11 +244,11 @@ func (l *Logger) print(std io.Writer, lead string, value ...interface{}) {
// Caller path.
callerPath := ""
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)
}
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)
}
if len(callerPath) > 0 {

View File

@ -14,12 +14,12 @@ import (
func Test_Unsafe(t *testing.T) {
gtest.Case(t, func() {
s := "John爱中国"
s := "I love 小泽玛利亚"
gtest.AssertEQ(gconv.UnsafeStrToBytes(s), []byte(s))
})
gtest.Case(t, func() {
b := []byte("John爱中国")
b := []byte("I love 小泽玛利亚")
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].
// The <min> and <max> also support negative numbers.
func N(min, max int) int {
if min >= max {
return min
@ -31,14 +32,14 @@ func N(min, max int) int {
// Because Intn dose not support negative number,
// so we should first shift the value to left,
// 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)
}
if min < 0 {
// Because Intn dose not support negative number,
// so we should first shift the value to right,
// 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 0
@ -76,7 +77,6 @@ func Digits(n int) string {
b[i] = digits[Intn(10)]
}
return string(b)
}
// Deprecated.
@ -101,7 +101,7 @@ func RandLetters(n int) string {
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 {
m := make([]int, n)
for i := 0; i < n; i++ {

View File

@ -13,9 +13,13 @@ import (
)
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.
func PrintStack(skip ...int) {
fmt.Print(Stack(skip...))