Last active
June 16, 2020 07:28
-
-
Save neelsmith/07cfcc045174fccb6487afcc25ad9d20 to your computer and use it in GitHub Desktop.
scala for String<->Vector of code points
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
// 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