Debian Codegraph @

tpl/strings/regexp.go

raw
1 // Copyright 2017 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package strings
15
16 import (
17 "github.com/gohugoio/hugo/common/hstrings"
18 "github.com/spf13/cast"
19 )
20
21 // FindRE returns a list of strings that match the regular expression. By default all matches
22 // will be included. The number of matches can be limited with an optional third parameter.
23 func (ns *Namespace) FindRE(expr string, content any, limit ...any) ([]string, error) {
24 re, err := hstrings.GetOrCompileRegexp(expr)
25 if err != nil {
26 return nil, err
27 }
28
29 conv, err := cast.ToStringE(content)
30 if err != nil {
31 return nil, err
32 }
33
34 if len(limit) == 0 {
35 return re.FindAllString(conv, -1), nil
36 }
37
38 lim, err := cast.ToIntE(limit[0])
39 if err != nil {
40 return nil, err
41 }
42
43 return re.FindAllString(conv, lim), nil
44 }
45
46 // FindRESubmatch returns a slice of all successive matches of the regular
47 // expression in content. Each element is a slice of strings holding the text
48 // of the leftmost match of the regular expression and the matches, if any, of
49 // its subexpressions.
50 //
51 // By default all matches will be included. The number of matches can be
52 // limited with the optional limit parameter. A return value of nil indicates
53 // no match.
54 func (ns *Namespace) FindRESubmatch(expr string, content any, limit ...any) ([][]string, error) {
55 re, err := hstrings.GetOrCompileRegexp(expr)
56 if err != nil {
57 return nil, err
58 }
59
60 conv, err := cast.ToStringE(content)
61 if err != nil {
62 return nil, err
63 }
64 n := -1
65 if len(limit) > 0 {
66 n, err = cast.ToIntE(limit[0])
67 if err != nil {
68 return nil, err
69 }
70 }
71
72 return re.FindAllStringSubmatch(conv, n), nil
73 }
74
75 // ReplaceRE returns a copy of s, replacing all matches of the regular
76 // expression pattern with the replacement text repl. The number of replacements
77 // can be limited with an optional fourth parameter.
78 func (ns *Namespace) ReplaceRE(pattern, repl, s any, n ...any) (_ string, err error) {
79 sp, err := cast.ToStringE(pattern)
80 if err != nil {
81 return
82 }
83
84 sr, err := cast.ToStringE(repl)
85 if err != nil {
86 return
87 }
88
89 ss, err := cast.ToStringE(s)
90 if err != nil {
91 return
92 }
93
94 nn := -1
95 if len(n) > 0 {
96 nn, err = cast.ToIntE(n[0])
97 if err != nil {
98 return
99 }
100 }
101
102 re, err := hstrings.GetOrCompileRegexp(sp)
103 if err != nil {
104 return "", err
105 }
106
107 // Expand against the original string. ReplaceAllStringFunc hands the
108 // callback the match on its own, so a pattern with a zero-width assertion
109 // such as \b would not match again inside it.
110 var b []byte
111 last := 0
112
113 for _, m := range re.FindAllStringSubmatchIndex(ss, nn) {
114 b = append(b, ss[last:m[0]]...)
115 b = re.ExpandString(b, sr, ss, m)
116 last = m[1]
117 }
118
119 return string(b) + ss[last:], nil
120 }