text_regexp.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package stdlib
  2. import (
  3. "regexp"
  4. "github.com/d5/tengo/v2"
  5. )
  6. func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap {
  7. return &tengo.ImmutableMap{
  8. Value: map[string]tengo.Object{
  9. // match(text) => bool
  10. "match": &tengo.UserFunction{
  11. Value: func(args ...tengo.Object) (
  12. ret tengo.Object,
  13. err error,
  14. ) {
  15. if len(args) != 1 {
  16. err = tengo.ErrWrongNumArguments
  17. return
  18. }
  19. s1, ok := tengo.ToString(args[0])
  20. if !ok {
  21. err = tengo.ErrInvalidArgumentType{
  22. Name: "first",
  23. Expected: "string(compatible)",
  24. Found: args[0].TypeName(),
  25. }
  26. return
  27. }
  28. if re.MatchString(s1) {
  29. ret = tengo.TrueValue
  30. } else {
  31. ret = tengo.FalseValue
  32. }
  33. return
  34. },
  35. },
  36. // find(text) => array(array({text:,begin:,end:}))/undefined
  37. // find(text, maxCount) => array(array({text:,begin:,end:}))/undefined
  38. "find": &tengo.UserFunction{
  39. Value: func(args ...tengo.Object) (
  40. ret tengo.Object,
  41. err error,
  42. ) {
  43. numArgs := len(args)
  44. if numArgs != 1 && numArgs != 2 {
  45. err = tengo.ErrWrongNumArguments
  46. return
  47. }
  48. s1, ok := tengo.ToString(args[0])
  49. if !ok {
  50. err = tengo.ErrInvalidArgumentType{
  51. Name: "first",
  52. Expected: "string(compatible)",
  53. Found: args[0].TypeName(),
  54. }
  55. return
  56. }
  57. if numArgs == 1 {
  58. m := re.FindStringSubmatchIndex(s1)
  59. if m == nil {
  60. ret = tengo.UndefinedValue
  61. return
  62. }
  63. arr := &tengo.Array{}
  64. for i := 0; i < len(m); i += 2 {
  65. arr.Value = append(arr.Value,
  66. &tengo.ImmutableMap{
  67. Value: map[string]tengo.Object{
  68. "text": &tengo.String{
  69. Value: s1[m[i]:m[i+1]],
  70. },
  71. "begin": tengo.Int{
  72. Value: int64(m[i]),
  73. },
  74. "end": tengo.Int{
  75. Value: int64(m[i+1]),
  76. },
  77. }})
  78. }
  79. ret = &tengo.Array{Value: []tengo.Object{arr}}
  80. return
  81. }
  82. i2, ok := tengo.ToInt(args[1])
  83. if !ok {
  84. err = tengo.ErrInvalidArgumentType{
  85. Name: "second",
  86. Expected: "int(compatible)",
  87. Found: args[1].TypeName(),
  88. }
  89. return
  90. }
  91. m := re.FindAllStringSubmatchIndex(s1, i2)
  92. if m == nil {
  93. ret = tengo.UndefinedValue
  94. return
  95. }
  96. arr := &tengo.Array{}
  97. for _, m := range m {
  98. subMatch := &tengo.Array{}
  99. for i := 0; i < len(m); i += 2 {
  100. subMatch.Value = append(subMatch.Value,
  101. &tengo.ImmutableMap{
  102. Value: map[string]tengo.Object{
  103. "text": &tengo.String{
  104. Value: s1[m[i]:m[i+1]],
  105. },
  106. "begin": tengo.Int{
  107. Value: int64(m[i]),
  108. },
  109. "end": tengo.Int{
  110. Value: int64(m[i+1]),
  111. },
  112. }})
  113. }
  114. arr.Value = append(arr.Value, subMatch)
  115. }
  116. ret = arr
  117. return
  118. },
  119. },
  120. // replace(src, repl) => string
  121. "replace": &tengo.UserFunction{
  122. Value: func(args ...tengo.Object) (
  123. ret tengo.Object,
  124. err error,
  125. ) {
  126. if len(args) != 2 {
  127. err = tengo.ErrWrongNumArguments
  128. return
  129. }
  130. s1, ok := tengo.ToString(args[0])
  131. if !ok {
  132. err = tengo.ErrInvalidArgumentType{
  133. Name: "first",
  134. Expected: "string(compatible)",
  135. Found: args[0].TypeName(),
  136. }
  137. return
  138. }
  139. s2, ok := tengo.ToString(args[1])
  140. if !ok {
  141. err = tengo.ErrInvalidArgumentType{
  142. Name: "second",
  143. Expected: "string(compatible)",
  144. Found: args[1].TypeName(),
  145. }
  146. return
  147. }
  148. s, ok := doTextRegexpReplace(re, s1, s2)
  149. if !ok {
  150. return nil, tengo.ErrStringLimit
  151. }
  152. ret = &tengo.String{Value: s}
  153. return
  154. },
  155. },
  156. // split(text) => array(string)
  157. // split(text, maxCount) => array(string)
  158. "split": &tengo.UserFunction{
  159. Value: func(args ...tengo.Object) (
  160. ret tengo.Object,
  161. err error,
  162. ) {
  163. numArgs := len(args)
  164. if numArgs != 1 && numArgs != 2 {
  165. err = tengo.ErrWrongNumArguments
  166. return
  167. }
  168. s1, ok := tengo.ToString(args[0])
  169. if !ok {
  170. err = tengo.ErrInvalidArgumentType{
  171. Name: "first",
  172. Expected: "string(compatible)",
  173. Found: args[0].TypeName(),
  174. }
  175. return
  176. }
  177. var i2 = -1
  178. if numArgs > 1 {
  179. i2, ok = tengo.ToInt(args[1])
  180. if !ok {
  181. err = tengo.ErrInvalidArgumentType{
  182. Name: "second",
  183. Expected: "int(compatible)",
  184. Found: args[1].TypeName(),
  185. }
  186. return
  187. }
  188. }
  189. arr := &tengo.Array{}
  190. for _, s := range re.Split(s1, i2) {
  191. arr.Value = append(arr.Value,
  192. &tengo.String{Value: s})
  193. }
  194. ret = arr
  195. return
  196. },
  197. },
  198. },
  199. }
  200. }
  201. // Size-limit checking implementation of regexp.ReplaceAllString.
  202. func doTextRegexpReplace(re *regexp.Regexp, src, repl string) (string, bool) {
  203. idx := 0
  204. out := ""
  205. for _, m := range re.FindAllStringSubmatchIndex(src, -1) {
  206. var exp []byte
  207. exp = re.ExpandString(exp, repl, src, m)
  208. if len(out)+m[0]-idx+len(exp) > tengo.MaxStringLen {
  209. return "", false
  210. }
  211. out += src[idx:m[0]] + string(exp)
  212. idx = m[1]
  213. }
  214. if idx < len(src) {
  215. if len(out)+len(src)-idx > tengo.MaxStringLen {
  216. return "", false
  217. }
  218. out += src[idx:]
  219. }
  220. return out, true
  221. }