Created
January 12, 2023 15:54
-
-
Save yongjhih/f517b2f88b36f6f6acc9890f010ec362 to your computer and use it in GitHub Desktop.
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
fun CharSequence.indicesOf(pattern: String): Sequence<Int> = sequence { | |
var startIndex = 0 | |
while (true) { | |
startIndex = indexOf(pattern, startIndex) | |
if (startIndex < 0) break | |
yield(startIndex) | |
startIndex += pattern.length | |
} | |
} | |
fun CharSequence.matches(pattern: String): Sequence<IntRange> = | |
indicesOf(pattern).map { it until it + pattern.length } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment