Skip to content

Instantly share code, notes, and snippets.

@neelsmith
Last active June 16, 2020 07:28
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 neelsmith/07cfcc045174fccb6487afcc25ad9d20 to your computer and use it in GitHub Desktop.
Save neelsmith/07cfcc045174fccb6487afcc25ad9d20 to your computer and use it in GitHub Desktop.
scala for String<->Vector of code points
// Compute Vector of code point values from String
def strToCps(s: String, cpVector: Vector[Int] = Vector.empty[Int], idx : Int = 0) : Vector[Int] = {
if (idx >= s.length) {
cpVector
} else {
val cp = s.codePointAt(idx)
strToCps(s, cpVector :+ cp, idx + java.lang.Character.charCount(cp))
}
}
// Compose a String from a Vector of code point values
def cpsToString(v: Vector[Int]) = {
val chs = v.map { cp =>
new String(Character.toChars(cp))
}
chs.mkString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment