-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMultiDecl.kt
113 lines (49 loc) · 1.55 KB
/
MultiDecl.kt
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
package extensions.muli
import java.awt.Point
import java.util.regex.Pattern
import java.util.ArrayList
fun main(args : Array<String>) {
val (x, y) = Point(1, 2)
println("($x, $y)")
// testMultiReturn()
// testIndexedIteration()
}
operator fun Point.component1() = x
operator fun Point.component2() = y
fun testMultiReturn() {
val (file, line) = parseFileNameAndLine("Foo12.java:12")
println("Line $line in $file")
}
data class Location(val fileName: String, val line: Int)
fun parseFileNameAndLine(str: String): Location {
val m = str.toGroupsByRegex("([\\w\\.]+):(\\d+)")
if (m == null) {
throw IllegalArgumentException("String format is wrong: '$str', must be <file-name>:<line>")
}
return Location(m[1], m[2].toInt())
}
fun testIndexedIteration() {
for ((i, c) in "abcd".indexed) {
println("s[$i] = $c")
}
}
val String.indexed: StringIteratorWithIndices
get() = StringIteratorWithIndices(this)
class StringIteratorWithIndices(val str: String) : Iterator<Pair<Int, Char>> {
var i = 0
public override fun next(): Pair<Int, Char> {
val result = Pair(i, str[i])
i++
return result
}
public override fun hasNext(): Boolean = i < str.length
}
fun String.toGroupsByRegex(regex: String): List<String>? {
val m = Pattern.compile(regex).matcher(this)
if (!m.matches()) return null
val result = ArrayList<String>()
for (g in 0..m.groupCount()) {
result.add(m.group(g)!!)
}
return result
}