maxiaoshan 3 лет назад
Родитель
Сommit
8e3bc09a0e
1 измененных файлов с 29 добавлено и 0 удалено
  1. 29 0
      src/spider/script.go

+ 29 - 0
src/spider/script.go

@@ -703,6 +703,35 @@ func (s *Script) LoadScript(code, script_file string, newstate bool) string {
 		S.Push(lua.LBool(isMatch))
 		return 2
 	}))
+	//截取
+	s.L.SetGlobal("stringSub", s.L.NewFunction(func(S *lua.LState) int {
+		text := S.ToString(-3)
+		start := S.ToInt(-2)
+		end := S.ToInt(-1)
+		result := ""
+		if len(text) > 0 {
+			textRune := []rune(text)
+			textLen := len(textRune)
+			if end == -1 {
+				if start >= 1 { //正向截取到结尾
+					result = string(textRune[start-1:])
+				} else if start < 0 && textLen+start >= 0 { //反向截取后缀
+					result = string(textRune[textLen+start:])
+				}
+			} else if start >= 1 && end <= textLen { //从第start个截取到第end个
+				result = string(textRune[start-1 : end])
+			}
+		}
+		S.Push(lua.LString(result))
+		return 1
+	}))
+	//长度
+	s.L.SetGlobal("stringLen", s.L.NewFunction(func(S *lua.LState) int {
+		text := S.ToString(-1)
+		textLen := len([]rune(text))
+		S.Push(lua.LNumber(textLen))
+		return 1
+	}))
 	//去除特殊标签中间内容
 	s.L.SetGlobal("getPureContent", s.L.NewFunction(func(S *lua.LState) int {
 		con := S.ToString(-1)