Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Created January 12, 2023 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongjhih/f517b2f88b36f6f6acc9890f010ec362 to your computer and use it in GitHub Desktop.
Save yongjhih/f517b2f88b36f6f6acc9890f010ec362 to your computer and use it in GitHub Desktop.
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