scoop:scoop.regex:0.1.0
src/results.scoop
Selected production source · src/results.scoop
1
package scoop.regex3
import scoop.collections.toList5
/** A half-open Unicode scalar range in the original input. */6
public struct MatchRange(7
/** The inclusive start position. */ val start: Long,8
/** The exclusive end position. */ val endExclusive: Long9
)11
/** An independently copied capture and its range. An empty capture is still a participating group. */12
public struct MatchGroup(13
/** The captured text. */ val value: String,14
/** Its range in the original input. */ val range: MatchRange15
)17
/** A snapshot of one successful match, usable after subsequent searches or Regex.close. */18
public class MatchResult internal constructor(19
/** All capture slots, including group zero. */ public val groups: MatchGroups,20
internal val byteStart: Long,21
internal val byteEnd: Long22
) {23
/** The text of group zero. */24
public val value: String get() = groups[0L]!!.value25
/** The range of group zero. */26
public val range: MatchRange get() = groups[0L]!!.range27
}29
internal struct RegexName(val name: String, val group: Long)31
/** Immutable numbered and named capture slots. None denotes a group that did not participate. */32
public 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.size39
/** 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 = false48
for (entry in names) {49
if (entry.name == name) {50
known = true51
val value = values[entry.group]52
if (value != None) return value53
}54
}55
if (!known) throw IllegalArgumentException(Some("unknown capture name: " + name))56
return None57
}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
}66
}68
internal 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 error81
}82
}84
@Unsafe85
internal 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 None91
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)98
}