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

34 lines
697 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
{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