Scoop API

scoop:scoop.regex:0.1.0

src/regex.scoop

Selected production source · src/regex.scoop

1package scoop.regex
3import scoop.collections.*
4import scoop.sequences.Sequence
6/**
7 * A reusable PCRE2 UTF-8 pattern. Close it deterministically with close/use. Matching calls can
8 * share a compiled pattern, but callers must serialize close against every use of that pattern.
9 */
10public class Regex public constructor(
11 /** The original pattern. */ public val pattern: String,
12 options: Set<RegexOption> = emptySet()
13) : Closeable {
14 /** An independent snapshot of the constructor options. Inline pattern options are separate. */
15 public val options: Set<RegexOption> = options.toSet()
16 private var handle: ULong = compileRegex(pattern, this.options)
17 private val names: Array<RegexName> = copyRegexNames(handle)
19 /** Whether close has released the compiled pattern. */
20 public val isClosed: Boolean get() = handle == 0UL
22 /** Tests anchored execution from the beginning through the end of input. */
23 public fun matches(input: String): Boolean = booleanMatch(input, 2)
25 /** Tests whether a match exists anywhere in the input, without copying captures. */
26 public fun containsMatchIn(input: String): Boolean = booleanMatch(input, 0)
28 /** Matches from the beginning through the end. A native \K can reset the returned start. */
29 public fun matchEntire(input: String): MatchResult? = firstMatch(input, 0L, 2)
31 /** Matches at a Unicode scalar index, without searching subsequent start positions. */
32 public fun matchAt(input: String, index: Long): MatchResult? = firstMatch(input, index, 1)
34 /** Searches from a Unicode scalar index, preserving the complete input's context. */
35 public fun find(input: String, startIndex: Long = 0L): MatchResult? = firstMatch(input, startIndex, 0)
37 /** Returns independent lazy traversals. Keep this Regex open while matching during traversal. */
38 public fun findAll(input: String, startIndex: Long = 0L): Sequence<MatchResult> {
39 checkedHandle()
40 return RegexMatches(this, input, RegexInput(input).byteOffset(startIndex))
41 }
43 internal fun checkedHandle(): ULong {
44 if (handle == 0UL) throw IllegalStateException(Some("regex is closed"))
45 return handle
46 }
48 private fun booleanMatch(input: String, mode: Int): Boolean {
49 val owned = checkedHandle()
50 @Unsafe {
51 return input.withUtf8Bytes { bytes, count ->
52 @Unsafe {
53 val matched = regexMatch(owned, bytes, count, 0L, 0u, mode, false)
54 if (matched.code == -1) return@withUtf8Bytes false
55 if (matched.code < 0) throw matchFailure(matched.code)
56 regexMatchFree(matched.data)
57 return@withUtf8Bytes true
58 }
59 }
60 }
61 }
63 private fun firstMatch(text: String, startIndex: Long, mode: Int): MatchResult? {
64 checkedHandle()
65 val input = RegexInput(text)
66 val start = input.byteOffset(startIndex)
67 return search(input, start, 0u, mode)?.result
68 }
70 internal fun search(input: RegexInput, start: Long, flags: UInt, mode: Int = 0): RegexStep? {
71 val owned = checkedHandle()
72 @Unsafe {
73 return input.text.withUtf8Bytes { bytes, count ->
74 @Unsafe {
75 val matched = regexMatch(owned, bytes, count, start, flags, mode, true)
76 if (matched.code == -1) return@withUtf8Bytes None
77 if (matched.code < 0) throw matchFailure(matched.code)
78 try {
79 val result = copyRegexMatch(matched.data, input, bytes, count, names)
80 return@withUtf8Bytes Some(RegexStep(result, matched.nextStart,
81 matched.nextOptions, matched.hasNext))
82 } finally { regexMatchFree(matched.data) }
83 }
84 }
85 }
86 }
88 /** Releases the compiled pattern. Repeated closes are harmless. */
89 public override fun close() {
90 val owned = handle
91 handle = 0UL
92 if (owned != 0UL) @Unsafe { regexFree(owned) }
93 }
95 release { if (handle != 0UL) @Unsafe { regexFree(handle) } }
97 public companion object {
98 /** Quotes literal text for a pattern, including embedded quote terminators. */
99 public fun escape(literal: String): String = escapeRegexLiteral(literal)
100 }
103internal struct RegexStep(val result: MatchResult, val start: Long, val flags: UInt, val hasNext: Boolean)
105private fun compileRegex(pattern: String, options: Set<RegexOption>): ULong {
106 var flags = 0u
107 for (option in options) flags = flags or option.mask
108 val compileFlags = flags
109 @Unsafe {
110 return pattern.withUtf8Bytes { bytes, count ->
111 @Unsafe {
112 val compiled = regexCompile(bytes, count, compileFlags)
113 if (compiled.handle != 0UL) return@withUtf8Bytes compiled.handle
114 if (compiled.code < 0) throw matchFailure(compiled.code)
115 val offset = RegexInput(pattern).scalarOffset(compiled.offset, bytes, count)
116 throw RegexSyntaxException(pattern, offset, compiled.code, regexDetail(compiled.code))
117 }
118 }
119 }