summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/nre.nim20
-rw-r--r--test/captures.nim3
2 files changed, 12 insertions, 11 deletions
diff --git a/src/nre.nim b/src/nre.nim
index b6835bd00..92cc7bbf4 100644
--- a/src/nre.nim
+++ b/src/nre.nim
@@ -10,7 +10,7 @@ import optional_t
 # Type definitions {{{
 type
   Regex* = ref object
-    pattern: string  # not nil
+    pattern*: string  # not nil
     pcreObj: ptr pcre.Pcre  # not nil
     pcreExtra: ptr pcre.ExtraData  ## nil
 
@@ -43,18 +43,20 @@ proc getinfo[T](self: Regex, opt: cint): T =
     # XXX Error message that doesn't expose implementation details
     raise newException(FieldError, "Invalid getinfo for $1, errno $2" % [$opt, $retcode])
 
-# Capture accessors {{{
-proc captureCount(self: Regex): int =
-  ## get the maximum number of captures
+# Regex accessors {{{
+proc captureCount*(self: Regex): int =
+  ## Get the maximum number of captures
   ##
   ## Does not return the number of captured captures
   return getinfo[int](self, pcre.INFO_CAPTURECOUNT)
 
-proc captureNames*(self: Regex): seq[string] =
-  result = @[]
-  for key in self.captureNameToId.keys:
-    result.add(key)
+proc captureNameId*(self: Regex): Table[string, int] =
+  ## Returns a map from named capture groups to their numerical
+  ## identifier
+  return self.captureNameToId
+# }}}
 
+# Capture accessors {{{
 proc captureBounds*(self: RegexMatch): CaptureBounds = return CaptureBounds(self)
 
 proc captures*(self: RegexMatch): Captures = return Captures(self)
@@ -107,7 +109,7 @@ proc `[]`*(self: Captures, name: string): string =
   return self.captures[self.pattern.captureNameToId.fget(name)]
 
 template asTableImpl(cond: bool): stmt {.immediate, dirty.} =
-  for key in RegexMatch(self).pattern.captureNames:
+  for key in RegexMatch(self).pattern.captureNameId.keys:
     let nextVal = self[key]
     if cond:
       result[key] = default
diff --git a/test/captures.nim b/test/captures.nim
index e987e9ca4..340c3d638 100644
--- a/test/captures.nim
+++ b/test/captures.nim
@@ -37,8 +37,7 @@ suite "captures":
   test "capture count":
     let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?")
     check(ex1.captureCount == 2)
-    # Don't have sets, do this :<
-    check(ex1.captureNames == @["foo", "bar"] or ex1.captureNames == @["bar", "foo"])
+    check(ex1.captureNameId == {"foo" : 0, "bar" : 1}.toTable())
 
   test "named capture table":
     let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").match("foo").get
committer GitHub <noreply@github.com> 2023-10-11 17:44:14 +0200 NIR: Nim intermediate representation (#22777)' href='/ahoang/Nim/commit/compiler/expanddefaults.nim?h=devel&id=816589b6674e3af281766f1279420758dcacedc4'>816589b66 ^
db603237c ^
816589b66 ^



db603237c ^
816589b66 ^







6ed33b6d6 ^
816589b66 ^





d3af51e3c ^
816589b66 ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131