Advertisement
vkazar

Untitled

Jan 26th, 2024
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.76 KB | None | 0 0
  1. fun String.unpack(): String {
  2.     val builder = StringBuilder()
  3.     var i = 0
  4.     while (i < length) {
  5.         if (this[i].isDigit()) {
  6.  
  7.             val repeatCount = this[i].digitToInt()
  8.             i += 2
  9.             val subStringStartIndex = i
  10.             var openedBraces = 1
  11.             while (openedBraces > 0) {
  12.                 when (this[i]) {
  13.                     '[' -> ++openedBraces
  14.                     ']' -> --openedBraces
  15.                 }
  16.                 ++i
  17.             }
  18.             val stringToRepeat = substring(subStringStartIndex until i - 1).unpack()
  19.  
  20.             builder.append(stringToRepeat.repeat(repeatCount))
  21.         } else {
  22.             builder.append(this[i])
  23.             ++i
  24.         }
  25.     }
  26.     return builder.toString()
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement