`String.getOrNul(index: Int): Char` where `nul` is...
# stdlib
e
String.getOrNul(index: Int): Char
where
nul
is
\u0000
e
Just use
getOrNull(index) ?: "\u0000"
? Makes it much clearer what is going on. No need for a function on the stdlib.
e
simply more verbose
e
Then write your own extension function
e
sure, but it'd be better to have it in the stdlib
e
Standard library is for code that is commonly needed by many people
e
everyone else working with native strings
e
Which is 0.0001% of developers
e
just let me suggest you the existance of kotlin-native
g
too narrow use case for stdlib imo Especially getting into account a lot of possible confusion between getOrNull and getOrNul
and if you rename to getOrNullTerminator or something like this, a bit more obvious, it becomes not much shorter
m
UTF-16
Char
and most of functions accepting or returning it are useless and should be deprecated.
e
confusion may be still be minimized by the fact they get listed consecutively when a suggestion is requested (alignment makes it easy to spot the difs)
@miha-x64 in favour of? utf32? utf8?
d
in favor of unicode codepoints, represented as
Int
(an
inline class
for those would be nice in the stdlib though maybe).
e
this has to come from oracle first though, you know
d
java.lang.String
already provides an API to deal with code points.
e
however @diesieben07 representing unicode using an
Int
essentially means utf32
which can be quite expensive for most of the simple scenarios
(even in comparison to utf8, utf16 can be expensive, this is the very reason they introduced utf8 strings in Java under the hood now)
d
I am not saying to store strings as an
IntArray
, which would be utf32. What I am saying is that when working with a
String
object you shouldn't have to deal with the intricacies of the underlying storage format.
e
ah, got it
d
So for example
someString.length
should be the number of code points, not the number of utf 16 chars. its the same issue you have in languages that represent strings as byte arrays, where you now have to deal with the byte lenght of a string in utf-8 instead of its actual length.
👍 1
m
The Swift team has put a lot of effort into working out a solid String API. It operates on code points. It offers UTF-8 and UTF-16 access through “views” on a String. https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html#ID301
👍 2