RosettaCodeData/Task/String-length/Lambdatalk/string-length.lambdatalk

34 lines
697 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{script
LAMBDATALK.DICT["W.unicodeLength"] = function() {
function countCodePoints(str) {
var point,
index,
width = 0,
len = 0;
for (index = 0; index < str.length;) {
point = str.codePointAt(index);
width = 0;
while (point) {
width += 1;
point = point >> 8;
}
index += Math.round(width/2);
len += 1;
}
return len;
}
var args = arguments[0].trim();
return countCodePoints(args)
};
}
Testing:
{W.length Hello, World!} -> 13
{W.length José} -> 4
{W.unicodeLength José} -> 4
{W.length 𝔘𝔫𝔦𝔠𝔬𝔡𝔢} -> 14
{W.unicodeLength 𝔘𝔫𝔦𝔠𝔬𝔡𝔢} -> 7