Skip to content

Instantly share code, notes, and snippets.

@bzz0217
Last active May 21, 2016 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzz0217/a36d511b4dd46ee8a69797ef5ef6187c to your computer and use it in GitHub Desktop.
Save bzz0217/a36d511b4dd46ee8a69797ef5ef6187c to your computer and use it in GitHub Desktop.
文字数測定[改]
// 文字数測定
var StrCount = {
//改行
line: 1,
//全角
em: 1,
//半角
em_half: 0.5,
//4バイト文字
surrogate: 1,
//文字列
str: "",
surrogate_count: function(){
//サロゲートベア(4バイト対応)
var count = this.str.split( /[\uD800-\uDBFF][\uDC00-\uDFFF]/g ).length - 1;
if (count > 0){
count = count * this.surrogate;
}
this.str = this.str.replace( /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, '' );
return count;
},
str_count_sum: function(str){
this.str = str;
surrogate_count = this.surrogate_count();
var count = 0;
for (var i = 0, len = this.str.length; i < len; i++) {
var c = this.str.charCodeAt(i);
count += this.charcode(c);
}
count = count + surrogate_count;
return count;
},
charcode: function(c){
count = 0;
if (c == 10 || c == 13){
//改行(\r||\n)
count = this.line;
} else if ((c >= 0x0 && c < 0x81) || (c == 0xf8f0) || (c >= 0xff61 && c < 0xffa0) || (c >= 0xf8f1 && c < 0xf8f4)) {
//半角
count = this.em_half;
} else {
//全角
count = this.em;
}
return count;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment