For grapheme cluster compt

This commit is contained in:
cutls 2019-11-11 01:20:51 +09:00
parent 85ccbd1382
commit 8e663391b9
2 changed files with 14 additions and 24 deletions

View File

@ -152,33 +152,21 @@ $.isSurrogatePear = function(upper, lower) {
return 0xd800 <= upper && upper <= 0xdbff && 0xdc00 <= lower && lower <= 0xdfff return 0xd800 <= upper && upper <= 0xdbff && 0xdc00 <= lower && lower <= 0xdfff
} }
$.mb_strlen = function(str) { $.mb_strlen = function(str) {
var ret = 0 var splitter = new GraphemeSplitter()
for (var i = 0; i < str.length; i++, ret++) { var arr = splitter.splitGraphemes(str)
var upper = str.charCodeAt(i) return arr.length
var lower = str.length > i + 1 ? str.charCodeAt(i + 1) : 0
if ($.isSurrogatePear(upper, lower)) {
i++
}
}
return ret
} }
$.mb_substr = function(str, begin, end) { $.mb_substr = function(str, begin, end) {
var ret = '' //配列にする
for (var i = 0, len = 0; i < str.length; i++, len++) { var splitter = new GraphemeSplitter()
var upper = str.charCodeAt(i) var arr = splitter.splitGraphemes(str)
var lower = str.length > i + 1 ? str.charCodeAt(i + 1) : 0 var newarr = []
var s = '' for(var i = 0; i < arr.length; i++){
if ($.isSurrogatePear(upper, lower)) { if(i >= begin && i <= end){
i++ newarr.push(arr[i])
s = String.fromCharCode(upper, lower)
} else {
s = String.fromCharCode(upper)
}
if (begin <= len && len < end) {
ret += s
} }
} }
return ret return newarr.join('')
} }
//ソートするやつ //ソートするやつ
function object_array_sort(data, key, order, fn) { function object_array_sort(data, key, order, fn) {

View File

@ -1,5 +1,6 @@
//オブジェクトパーサー(トゥート) //オブジェクトパーサー(トゥート)
function parse(obj, mix, acct_id, tlid, popup, mutefilter, type) { function parse(obj, mix, acct_id, tlid, popup, mutefilter, type) {
var splitter = new GraphemeSplitter()
var templete = '' var templete = ''
if (obj[0]) { if (obj[0]) {
if (tlid === 1) { if (tlid === 1) {
@ -500,9 +501,10 @@ function parse(obj, mix, acct_id, tlid, popup, mutefilter, type) {
} else { } else {
var ct = ct2 var ct = ct2
} }
if ( if (
(sent < ct && $.mb_strlen($.strip_tags(content)) > 5) || (sent < ct && $.mb_strlen($.strip_tags(content)) > 5) ||
($.strip_tags(content).length > ltr && $.mb_strlen($.strip_tags(content)) > 5) ($.mb_strlen($.strip_tags(content)) > ltr && $.mb_strlen($.strip_tags(content)) > 5)
) { ) {
var content = `<span class="gray">${lang.lang_parse_fulltext}</span><br>` + content var content = `<span class="gray">${lang.lang_parse_fulltext}</span><br>` + content
var spoil = `<span class="cw-long-${toot.id}">${$.mb_substr( var spoil = `<span class="cw-long-${toot.id}">${$.mb_substr(