mbonnin
11/25/2021, 6:55 PMfun String.source(): Source
like there is fun File.source(): Source
?
// This works
File(path).source().buffer()
// This doesn't
"Hello World".source().buffer()
mbonnin
11/25/2021, 6:56 PMBuffer().write("Hello World")
works but it breaks the typing flowjw
11/25/2021, 7:22 PMjw
11/25/2021, 7:22 PMmbonnin
11/25/2021, 7:24 PMfun String.source(charset: Charset = Charsets.UTF_8): Source
mbonnin
11/25/2021, 7:26 PMByteString.source()
mbonnin
11/25/2021, 7:50 PMCharset
is not MPP, I see...mbonnin
11/25/2021, 8:04 PMString.utf8Source()
and ByteString.source()
jw
11/25/2021, 11:28 PMmbonnin
11/26/2021, 8:57 AMjessewilson
11/26/2021, 11:43 AMmbonnin
11/26/2021, 11:44 AMjessewilson
11/26/2021, 11:54 AMval s = """1\n2\n3\n"""
return s.toSource()
That's your ideal case, but then we refactor some:
val s = buildString {
for (i in 1..3)
append(i.toString())
append("\n")
}
}
return s.toSource()
In general the 2nd example performs terribly compared to building a Buffer. It allocates lots of intermediate Strings and then needs to UTF-8 transcode to go back into bytes. Optimal here is where the more verbose API leads you:
return Buffer().apply {
for (i in 1..3)
writeDecimalLong(i)
writeUtf8("\n")
}
}
mbonnin
11/26/2021, 11:58 AMmbonnin
11/26/2021, 12:00 PMBufferedSource
mainly and that provides convenience overloads to work with String
and ByteString
and it's a bit painful to define these overloads everywhere...mbonnin
11/26/2021, 12:01 PMjw
11/28/2021, 9:02 PMjw
11/28/2021, 9:02 PMjw
11/28/2021, 9:03 PM