util.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package util
  2. import "strconv"
  3. var msgHandler map[string](chan interface{})
  4. func GetMsgHandler(msgPipe string) chan interface{} {
  5. hdr, has := msgHandler[msgPipe]
  6. if !has {
  7. hdr = make(chan interface{})
  8. msgHandler[msgPipe] = hdr
  9. }
  10. return hdr
  11. }
  12. func PostMessage(msgPipe string, msg interface{}) {
  13. hdr, has := msgHandler[msgPipe]
  14. if !has {
  15. hdr = make(chan interface{})
  16. msgHandler[msgPipe] = hdr
  17. }
  18. hdr <- msg
  19. }
  20. const (
  21. NUMBER_ALL = iota
  22. NUMBER_CONTINUE
  23. )
  24. func NumberFilter(in string, mode int) string {
  25. sbyte := make([]byte, 0)
  26. bnumbered := false
  27. for _, ss := range []byte(in) {
  28. if (ss >= '0' && ss <= '9') || ss == '.' || ss == '-' || ss == '+' {
  29. sbyte = append(sbyte, ss)
  30. bnumbered = true
  31. } else if mode != NUMBER_ALL {
  32. if bnumbered {
  33. break
  34. }
  35. }
  36. }
  37. return string(sbyte)
  38. }
  39. func init() {
  40. msgHandler = make(map[string]chan interface{})
  41. }
  42. func DebugLevel() int {
  43. return 1
  44. }
  45. func ToInt(source interface{}) int {
  46. res := -1
  47. switch val := source.(type) {
  48. case int:
  49. res = val
  50. case int16:
  51. res = int(val)
  52. case int32:
  53. res = int(val)
  54. case float32:
  55. res = int(val)
  56. case float64:
  57. res = int(val)
  58. case string:
  59. res = int(StringToInt64(val, 0))
  60. }
  61. return res
  62. }
  63. func StringToInt64(str string, dval int64) int64 {
  64. res, e := strconv.ParseInt(str, 10, 64)
  65. if e != nil {
  66. res = dval
  67. }
  68. return res
  69. }