github.com/PandaGoAdmin/utils@v0.0.0-20211208134815-d5461603a00f/docs/code.md (about) 1 ### 代码片段 2 3 // TimerOnce 一次性定时器 4 TimerOnce struct { 5 Pt *time.Timer // 定时器指针 6 Dr time.Duration // 时间间隔 7 Fn func(args ...interface{}) // 执行函数 8 Args interface{} // 执行函数的参数 9 Count int // 已执行次数 10 running bool // 是否在运行 11 } 12 // TimerCycle 循环定时器 13 TimerCycle struct { 14 Pt *time.Ticker // 定时器指针 15 Dr time.Duration // 时间间隔 16 Fn func(args ...interface{}) // 执行函数 17 Args interface{} // 执行函数的参数 18 Max int // 最大执行次数 19 Count int // 已执行次数 20 LastTime time.Time // 上次执行时间 21 running bool // 是否在运行 22 } 23 // LkkTimers 定时器容器 24 LkkTimers struct { 25 Onces map[int64]*TimerOnce // 一次性定时器字典 26 Cycles map[int64]*TimerCycle // 循环定时器字典 27 OnceRuns uint // 一次性定时器已执行次数 28 CycleRuns uint // 循环定时器已执行次数 29 } 30 31 // KTimer utilities 32 KTimer *LkkTimers 33 34 35 type ITimer interface { 36 SetAfter() 37 ClearAfter() 38 ClearAfterAll() 39 SetInterval() 40 ClearInterval() 41 ClearIntervalAll() 42 Start() 43 Stop() 44 Reset() 45 Clear() 46 CountOnces() 47 CountCycles() 48 CountTimers() 49 } 50 51 func (kt *LkkTime) GetTimer() *LkkTimers { 52 if KTimer == nil { 53 KTimer = &LkkTimers{} 54 } 55 return KTimer 56 } 57 58 emoji表情的处理 59 import "unicode/utf8" 60 61 func FilterEmoji(content string) string { 62 new_content := "" 63 for _, value := range content { 64 _, size := utf8.DecodeRuneInString(string(value)) 65 if size <= 3 { 66 new_content += string(value) 67 } 68 } 69 return new_content 70 } 71 72 // 检查是否存在特殊符号 73 // 1. emoji字符 74 // 2. ascii控制字符 75 // 3. \ " ' 76 // val:待检查的字符串 77 // 返回值: 78 // bool:true:有特殊字符 false:无特殊字符 79 func IfHaveSpecialChar(val string) bool { 80 if len(val) <= 0 { 81 return false 82 } 83 84 // 表情符号过滤 85 // Wide UCS-4 build 86 emojiReg, _ := regexp.Compile("[^\U00000000-\U0000FFFF]+") 87 if emojiReg.Match([]byte(val)) { 88 return true 89 } 90 91 // 排除控制字符和特殊字符 92 for _, charItem := range val { 93 // 排除控制字符 94 if (charItem > 0 && charItem < 0x20) || charItem == 0x7F { 95 return true 96 } 97 98 // 排除部分特殊字符: \ " ' 99 switch charItem { 100 case '\\': 101 fallthrough 102 case '"': 103 fallthrough 104 case '\'': 105 return true 106 } 107 } 108 109 return false 110 } 111 112 // strip tags in html string 113 func StripTags(src string) string { 114 //去除style,script,html tag 115 re := regexp.MustCompile(`(?s)<(?:style|script)[^<>]*>.*?</(?:style|script)>|</?[a-z][a-z0-9]*[^<>]*>|<!--.*?-->`) 116 src = re.ReplaceAllString(src, "") 117 118 //trim all spaces(2+) into \n 119 re = regexp.MustCompile(`\s{2,}`) 120 src = re.ReplaceAllString(src, "\n") 121 122 return strings.TrimSpace(src) 123 } 124