Skip to content

Commit 3bf2ff5

Browse files
committed
improve code point-safe truncate
1 parent 541280f commit 3bf2ff5

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

compiler/src/dotty/tools/repl/Rendering.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,12 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
7070
* then this bug will surface, so perhaps better not?
7171
* https://github.com/scala/bug/issues/12337
7272
*/
73-
private[repl] def truncate(str: String): String =
74-
def adjust(s: String): String = // to not cut a Unicode character in half
75-
if s.nonEmpty && s.last.isUnicodeIdentifierStart then s.dropRight(1)
76-
else s
77-
78-
if str.length > MaxStringElements then adjust(str.take(MaxStringElements - 3)) + "..."
73+
private[repl] def truncate(str: String): String = {
74+
val ncp = str.codePointCount(0, str.length) // to not cut inside code point
75+
if ncp > MaxStringElements && ncp > 3 then
76+
str.substring(0, str.offsetByCodePoints(0, MaxStringElements - 3)) + "..."
7977
else str
80-
end truncate
78+
}
8179

8280
/** Return a String representation of a value we got from `classLoader()`. */
8381
private[repl] def replStringOf(value: Object)(using Context): String = {

0 commit comments

Comments
 (0)