3 ms·
index += 1; if (index < strlen) { ch = str.charAt(index); } else { ch = 0; } If I was using kotlin, I'd do it even cleaner: inline
by kuschku 6y ago
index += 1;
if (index < strlen) {
ch = str.charAt(index);
} else {
ch = 0;
}
If I was using kotlin, I'd do it even cleaner:
inline fun String.at(index: Int): Char? =
if (index >= 0 && index < length) charAt(index)
else null
[...]
index += 1
ch = str.at(index) ?: 0;
The bytecode is exactly the same, but I’d argue it’s much cleaner
- mjcohen 6y agoI haven't used kotlin, but where is length defined? Should it be a parameter to the function?
- pirocks 6y agoThe above is an extension function(which is basically a member function of class not written in that class), which has an implicit `this` parameter. In other words `length` is actually `this.length`, just as it would be in a regular method.