Skip to content

Commit 08fa1c7

Browse files
committed
pkg/regexp: reorder for ease of maintenance
The Go regexp package defines set of functions/methods that follow a pattern. Maintaining the CUE equivalent in alphabetical order helps to see variations. Signed-off-by: Paul Jolly <paul@myitcv.io> Change-Id: I007660c47344855862a5c7492aea3d4298bb3640 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/532822 Unity-Result: CUEcueckoo <cueckoo@cuelang.org> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org> Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
1 parent fe20132 commit 08fa1c7

File tree

2 files changed

+58
-58
lines changed

2 files changed

+58
-58
lines changed

pkg/regexp/manual.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ import (
2222

2323
var errNoMatch = errors.New("no match")
2424

25-
// Valid reports whether the given regular expression
26-
// is valid.
27-
func Valid(pattern string) (bool, error) {
28-
_, err := regexp.Compile(pattern)
29-
return err == nil, err
30-
}
31-
3225
// Find returns a string holding the text of the leftmost match in s of
3326
// the regular expression. It returns bottom if there was no match.
3427
func Find(pattern, s string) (string, error) {
@@ -61,25 +54,37 @@ func FindAll(pattern, s string, n int) ([]string, error) {
6154
return m, nil
6255
}
6356

64-
// FindSubmatch returns a list of strings holding the text of the leftmost match
65-
// of the regular expression in s and the matches, if any, of its
66-
// subexpressions. Submatches are matches of parenthesized subexpressions (also
67-
// known as capturing groups) within the regular expression, numbered from left
68-
// to right in order of opening parenthesis. Submatch 0 is the match of the
69-
// entire expression, submatch 1 the match of the first parenthesized
70-
// subexpression, and so on. It returns bottom for no match.
71-
func FindSubmatch(pattern, s string) ([]string, error) {
57+
// FindAllNamedSubmatch is like FindAllSubmatch, but returns a map with the
58+
// named used in capturing groups. See FindNamedSubmatch for an example on
59+
// how to use named groups.
60+
func FindAllNamedSubmatch(pattern, s string, n int) ([]map[string]string, error) {
7261
re, err := regexp.Compile(pattern)
7362
if err != nil {
7463
return nil, err
7564
}
76-
m := re.FindStringSubmatch(s)
65+
names := re.SubexpNames()
66+
if len(names) == 0 {
67+
return nil, errNoNamedGroup
68+
}
69+
m := re.FindAllStringSubmatch(s, n)
7770
if m == nil {
7871
return nil, errNoMatch
7972
}
80-
return m, nil
73+
result := make([]map[string]string, len(m))
74+
for i, m := range m {
75+
r := make(map[string]string, len(names)-1)
76+
for k, name := range names {
77+
if name != "" {
78+
r[name] = m[k]
79+
}
80+
}
81+
result[i] = r
82+
}
83+
return result, nil
8184
}
8285

86+
var errNoNamedGroup = errors.New("no named groups")
87+
8388
// FindAllSubmatch finds successive matches as returned by FindSubmatch,
8489
// observing the rules of FindAll. It returns bottom for no match.
8590
func FindAllSubmatch(pattern, s string, n int) ([][]string, error) {
@@ -94,8 +99,6 @@ func FindAllSubmatch(pattern, s string, n int) ([][]string, error) {
9499
return m, nil
95100
}
96101

97-
var errNoNamedGroup = errors.New("no named groups")
98-
99102
// FindNamedSubmatch is like FindSubmatch, but returns a map with the names used
100103
// in capturing groups.
101104
//
@@ -126,31 +129,28 @@ func FindNamedSubmatch(pattern, s string) (map[string]string, error) {
126129
return r, nil
127130
}
128131

129-
// FindAllNamedSubmatch is like FindAllSubmatch, but returns a map with the
130-
// named used in capturing groups. See FindNamedSubmatch for an example on
131-
// how to use named groups.
132-
func FindAllNamedSubmatch(pattern, s string, n int) ([]map[string]string, error) {
132+
// FindSubmatch returns a list of strings holding the text of the leftmost match
133+
// of the regular expression in s and the matches, if any, of its
134+
// subexpressions. Submatches are matches of parenthesized subexpressions (also
135+
// known as capturing groups) within the regular expression, numbered from left
136+
// to right in order of opening parenthesis. Submatch 0 is the match of the
137+
// entire expression, submatch 1 the match of the first parenthesized
138+
// subexpression, and so on. It returns bottom for no match.
139+
func FindSubmatch(pattern, s string) ([]string, error) {
133140
re, err := regexp.Compile(pattern)
134141
if err != nil {
135142
return nil, err
136143
}
137-
names := re.SubexpNames()
138-
if len(names) == 0 {
139-
return nil, errNoNamedGroup
140-
}
141-
m := re.FindAllStringSubmatch(s, n)
144+
m := re.FindStringSubmatch(s)
142145
if m == nil {
143146
return nil, errNoMatch
144147
}
145-
result := make([]map[string]string, len(m))
146-
for i, m := range m {
147-
r := make(map[string]string, len(names)-1)
148-
for k, name := range names {
149-
if name != "" {
150-
r[name] = m[k]
151-
}
152-
}
153-
result[i] = r
154-
}
155-
return result, nil
148+
return m, nil
149+
}
150+
151+
// Valid reports whether the given regular expression
152+
// is valid.
153+
func Valid(pattern string) (bool, error) {
154+
_, err := regexp.Compile(pattern)
155+
return err == nil, err
156156
}

pkg/regexp/pkg.go

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)