-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This makes it easier to detect jumps and toggle show-line-number
- Loading branch information
1 parent
5961cef
commit ccde85e
Showing
14 changed files
with
558 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/PeekingIterator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2024 Romain Guy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.romainguy.kotlin.explorer | ||
|
||
/** Based on Guava PeekingIterator */ | ||
class PeekingIterator<E : Any>(private val iterator: Iterator<E>) : Iterator<E> { | ||
private var peekedElement: E? = null | ||
|
||
override fun hasNext(): Boolean { | ||
return peekedElement != null || iterator.hasNext() | ||
} | ||
|
||
override fun next(): E { | ||
val element = peekedElement ?: return iterator.next() | ||
peekedElement = null | ||
return element | ||
} | ||
|
||
fun peek(): E { | ||
return peekedElement.takeIf { it != null } ?: iterator.next().also { peekedElement = it } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/String.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.romainguy.kotlin.explorer | ||
|
||
fun Iterator<String>.consumeUntil(prefix: String): Boolean { | ||
while (hasNext()) { | ||
val line = next() | ||
if (line.trim().startsWith(prefix)) return true | ||
} | ||
return false | ||
} | ||
|
||
fun Iterator<String>.consumeUntil(regex: Regex): MatchResult? { | ||
while (hasNext()) { | ||
val line = next() | ||
val match = regex.matchEntire(line) | ||
if (match != null) { | ||
return match | ||
} | ||
} | ||
return null | ||
} |
Oops, something went wrong.