34 lines
697 B
Text
34 lines
697 B
Text
|
|
{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
|