Scoop API

scoop:scoop.regex:0.1.0

src/results.scoop

Selected production source · src/results.scoop

1package scoop.regex
3import scoop.collections.toList
5/** A half-open Unicode scalar range in the original input. */
6public struct MatchRange(
7 /** The inclusive start position. */ val start: Long,
8 /** The exclusive end position. */ val endExclusive: Long
9)
11/** An independently copied capture and its range. An empty capture is still a participating group. */
12public struct MatchGroup(
13 /** The captured text. */ val value: String,
14 /** Its range in the original input. */ val range: MatchRange
17/** A snapshot of one successful match, usable after subsequent searches or Regex.close. */
18public class MatchResult internal constructor(
19 /** All capture slots, including group zero. */ public val groups: MatchGroups,
20 internal val byteStart: Long,
21 internal val byteEnd: Long
22) {
23 /** The text of group zero. */
24 public val value: String get() = groups[0L]!!.value
25 /** The range of group zero. */
26 public val range: MatchRange get() = groups[0L]!!.range
29internal struct RegexName(val name: String, val group: Long)
31/** Immutable numbered and named capture slots. None denotes a group that did not participate. */
32public class MatchGroups internal constructor(
33 private val values: Array<MatchGroup?>,
34 private val names: Array<RegexName>
35) {
36 /** The number of slots, including group zero. */
37 public val size: Long get() = values.size
39 /** Returns a slot; out-of-range indices throw IndexOutOfBoundsException. */
40 public operator fun get(index: Long): MatchGroup? {
41 if (index < 0L || index >= size) throw IndexOutOfBoundsException()
42 return values[index]
43 }
45 /** Returns the first participating group with this name, or None; unknown names throw. */
46 public operator fun get(name: String): MatchGroup? {
47 var known = false
48 for (entry in names) {
49 if (entry.name == name) {
50 known = true
51 val value = values[entry.group]
52 if (value != None) return value
53 }
54 }
55 if (!known) throw IllegalArgumentException(Some("unknown capture name: " + name))
56 return None
57 }
59 /** Returns all slots with the name in group-number order, preserving unparticipating slots. */
60 public fun getAll(name: String): List<MatchGroup?> {
61 val result = ArrayList<MatchGroup?>()
62 for (entry in names) { if (entry.name == name) result.add(values[entry.group]) }
63 if (result.size == 0L) throw IllegalArgumentException(Some("unknown capture name: " + name))
64 return result.toList()
65 }
68internal fun copyRegexNames(handle: ULong): Array<RegexName> {
69 try {
70 @Unsafe {
71 return Array<RegexName>(regexNameCount(handle)) { index ->
72 @Unsafe {
73 val entry = regexName(handle, index)
74 return@Array RegexName(String.fromUtf8(entry.text, entry.length), entry.group)
75 }
76 }
77 }
78 } catch (error: Throwable) {
79 @Unsafe { regexFree(handle) }
80 throw error
81 }
84@Unsafe
85internal fun copyRegexMatch(data: ULong, input: RegexInput, bytes: Ptr<UInt8>, count: Long,
86 names: Array<RegexName>): MatchResult {
87 val groups = Array<MatchGroup?>(regexGroupCount(data)) { index ->
88 @Unsafe {
89 val range = regexGroup(data, index)
90 if (range.start < 0L) return@Array None
91 val value = String.fromUtf8(bytes + range.start, range.end - range.start)
92 return@Array Some(MatchGroup(value, MatchRange(
93 input.scalarOffset(range.start, bytes, count), input.scalarOffset(range.end, bytes, count))))
94 }
95 }
96 val whole = regexGroup(data, 0L)
97 return MatchResult(MatchGroups(groups, names), whole.start, whole.end)