tiefen_space_app/utils/tools.js

21 lines
484 B
JavaScript

export function utf8Length(str) {
let length = 0;
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
if (code < 0x80) {
// 0xxxxxxx
length += 0.5;
} else if (code < 0x800) {
// 110xxxxx 10xxxxxx
length += 2;
} else if (code < 0x10000) {
// 1110xxxx 10xxxxxx 10xxxxxx
length += 1;
} else if (code <= 0x10ffff) {
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
length += 4;
}
}
return length;
}