etc.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the GO-LICENSE file.
  4. package regexp // modernc.org/regexp
  5. import (
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "runtime/debug"
  11. "strings"
  12. )
  13. // origin returns caller's short position, skipping skip frames.
  14. func origin(skip int) string {
  15. pc, fn, fl, _ := runtime.Caller(skip)
  16. f := runtime.FuncForPC(pc)
  17. var fns string
  18. if f != nil {
  19. fns = f.Name()
  20. if x := strings.LastIndex(fns, "."); x > 0 {
  21. fns = fns[x+1:]
  22. }
  23. if strings.HasPrefix(fns, "func") {
  24. num := true
  25. for _, c := range fns[len("func"):] {
  26. if c < '0' || c > '9' {
  27. num = false
  28. break
  29. }
  30. }
  31. if num {
  32. return origin(skip + 2)
  33. }
  34. }
  35. }
  36. return fmt.Sprintf("%s:%d:%s", filepath.Base(fn), fl, fns)
  37. }
  38. // todo prints and returns caller's position and an optional message tagged with TODO. Output goes to stderr.
  39. //
  40. //lint:ignore U1000 debug helper
  41. func todo(s string, args ...interface{}) string {
  42. switch {
  43. case s == "":
  44. s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
  45. default:
  46. s = fmt.Sprintf(s, args...)
  47. }
  48. s = fmt.Sprintf("%s\n\tTODO %s", origin(2), s)
  49. // fmt.Fprintf(os.Stderr, "%s\n", s)
  50. // os.Stdout.Sync()
  51. return s
  52. }
  53. // trc prints and returns caller's position and an optional message tagged with TRC. Output goes to stderr.
  54. //
  55. //lint:ignore U1000 debug helper
  56. func trc(s string, args ...interface{}) string {
  57. switch {
  58. case s == "":
  59. s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
  60. default:
  61. s = fmt.Sprintf(s, args...)
  62. }
  63. s = fmt.Sprintf("%s: TRC %s", origin(2), s)
  64. fmt.Fprintf(os.Stderr, "%s\n", s)
  65. os.Stderr.Sync()
  66. return s
  67. }
  68. //lint:ignore U1000 debug helper
  69. func stack() []byte { return debug.Stack() }