Scoop API

scoop:scoop.std:0.1.0

src/text/search.scoop

Selected production source · src/text/search.scoop

1package scoop.text
3private fun requireTextIndex(index: Long, length: Long) {
4 if (index < 0L || index > length)
5 throw IndexOutOfBoundsException()
6}
8/**
9 * Returns the first matching Unicode-scalar index at or after startIndex, or -1. The starting
10 * index must be in 0..charLength.
11 */
12public fun String.indexOf(value: String, startIndex: Long = 0L): Long {
13 requireTextIndex(startIndex, charLength)
14 val pattern = TextPattern(value)
15 if (pattern.size == 0L)
16 return startIndex
17 var index = 0L
18 var matched = 0L
19 for (character in this) {
20 if (index >= startIndex) {
21 matched = pattern.advance(matched, character)
22 if (matched == pattern.size)
23 return index - pattern.size + 1L
24 }
25 index += 1L
26 }
27 return -1L
30/**
31 * Returns the last matching Unicode-scalar start index no greater than startIndex, or -1. The
32 * starting index must be in 0..charLength.
33 */
34public fun String.lastIndexOf(value: String, startIndex: Long = this.charLength): Long {
35 val length = this.charLength
36 requireTextIndex(startIndex, length)
37 val pattern = TextPattern(value)
38 if (pattern.size == 0L)
39 return startIndex
40 val limit =
41 if (pattern.size > length - startIndex)
42 length
43 else
44 startIndex + pattern.size
45 var result = -1L
46 var index = 0L
47 var matched = 0L
48 for (character in this) {
49 if (index == limit)
50 break
51 matched = pattern.advance(matched, character)
52 if (matched == pattern.size)
53 result = index - pattern.size + 1L
54 index += 1L
55 }
56 return result
59/**
60 * Returns the first matching Unicode-scalar index at or after startIndex, or -1. The starting
61 * index must be in 0..charLength.
62 */
63public fun String.indexOf(value: Char, startIndex: Long = 0L): Long {
64 if (startIndex < 0L)
65 throw IndexOutOfBoundsException()
66 var index = 0L
67 for (character in this) {
68 if (index >= startIndex && character == value)
69 return index
70 index += 1L
71 }
72 requireTextIndex(startIndex, index)
73 return -1L
76/**
77 * Returns the last matching Unicode-scalar start index no greater than startIndex, or -1. The
78 * starting index must be in 0..charLength.
79 */
80public fun String.lastIndexOf(value: Char, startIndex: Long = this.charLength): Long {
81 if (startIndex < 0L)
82 throw IndexOutOfBoundsException()
83 var index = 0L
84 var result = -1L
85 for (character in this) {
86 if (index > startIndex)
87 return result
88 if (character == value)
89 result = index
90 index += 1L
91 }
92 requireTextIndex(startIndex, index)
93 return result
96/**
97 * Tests for an exact literal occurrence, without case folding.
98 */
99public operator fun String.contains(value: String): Boolean = indexOf(value) >= 0L
101/**
102 * Tests for an exact literal occurrence, without case folding.
103 */
104public operator fun String.contains(value: Char): Boolean = indexOf(value) >= 0L
106private fun String.matchesAt(value: String, start: Long): Boolean {
107 val source = iterator()
108 var skip = start
109 while (skip > 0L) {
110 source.next();
111 skip -= 1L
112 }
113 for (character in value) {
114 if (source.next() != Some(character))
115 return false
116 }
117 return true
120/**
121 * Tests for an exact literal prefix, without case folding.
122 */
123public fun String.startsWith(prefix: String): Boolean = matchesAt(prefix, 0L)
125/**
126 * Tests for an exact literal prefix, without case folding.
127 */
128public fun String.startsWith(prefix: Char): Boolean = byteLength != 0L && getCharAt(0L) == prefix
130/**
131 * Tests for an exact literal suffix, without case folding.
132 */
133public fun String.endsWith(suffix: String): Boolean {
134 val start = charLength - suffix.charLength
135 return start >= 0L && matchesAt(suffix, start)
138/**
139 * Tests for an exact literal suffix, without case folding.
140 */
141public fun String.endsWith(suffix: Char): Boolean {
142 var last: Char? = None
143 for (character in this)
144 last = Some(character)
145 return last == Some(suffix)