Substr基于字节而不是字符数

时间:2012-04-18 11:33:42

标签: javascript byte

我正在创建一个输入系统,其中字段最大值只能是200个字节。我通过使用以下内容来计算剩余的字节数(这种方法可能会引起争议!):

var totalBytes = 200;
var $newVal = $(this).val();
var m = encodeURIComponent($newVal).match(/%[89ABab]/g);
var bytesLeft = totalBytes - ($newVal.length + (m ? m.length : 0));

这似乎运行良好,但是如果有人要粘贴大量数据,我希望能够对输入进行切片并仅显示200个字节。我猜在psuedo-code中看起来像:

$newText = substrBytes($string, 0, 200);

任何帮助或指导都将不胜感激。

编辑:这里发生的一切都是UTF-8顺便说一句:)

编辑2 :我知道我可以循环每个角色并进行评估,我想我希望可能会有一些更优雅的东西来处理这个问题。

谢谢!

2 个答案:

答案 0 :(得分:3)

Google搜索产生了a blog article,并附带了一个自己动手的输入框。我在这里复制代码是因为SO喜欢明确的答案而不是链接,但是归功于McDowell

/**
 * codePoint - an integer containing a Unicode code point
 * return - the number of bytes required to store the code point in UTF-8
 */
function utf8Len(codePoint) {
  if(codePoint >= 0xD800 && codePoint <= 0xDFFF)
    throw new Error("Illegal argument: "+codePoint);
  if(codePoint < 0) throw new Error("Illegal argument: "+codePoint);
  if(codePoint <= 0x7F) return 1;
  if(codePoint <= 0x7FF) return 2;
  if(codePoint <= 0xFFFF) return 3;
  if(codePoint <= 0x1FFFFF) return 4;
  if(codePoint <= 0x3FFFFFF) return 5;
  if(codePoint <= 0x7FFFFFFF) return 6;
  throw new Error("Illegal argument: "+codePoint);
}

function isHighSurrogate(codeUnit) {
  return codeUnit >= 0xD800 && codeUnit <= 0xDBFF;
}

function isLowSurrogate(codeUnit) {
  return codeUnit >= 0xDC00 && codeUnit <= 0xDFFF;
}

/**
 * Transforms UTF-16 surrogate pairs to a code point.
 * See RFC2781
 */
function toCodepoint(highCodeUnit, lowCodeUnit) {
  if(!isHighSurrogate(highCodeUnit)) throw new Error("Illegal argument: "+highCodeUnit);
  if(!isLowSurrogate(lowCodeUnit)) throw new Error("Illegal argument: "+lowCodeUnit);
  highCodeUnit = (0x3FF & highCodeUnit) << 10;
  var u = highCodeUnit | (0x3FF & lowCodeUnit);
  return u + 0x10000;
}

/**
 * Counts the length in bytes of a string when encoded as UTF-8.
 * str - a string
 * return - the length as an integer
 */
function utf8ByteCount(str) {
  var count = 0;
  for(var i=0; i<str.length; i++) {
    var ch = str.charCodeAt(i);
    if(isHighSurrogate(ch)) {
      var high = ch;
      var low = str.charCodeAt(++i);
      count += utf8Len(toCodepoint(high, low));
    } else {
      count += utf8Len(ch);
    }
  }
  return count;
}

答案 1 :(得分:1)

JavaScript中的字符串在内部以UTF-16表示,因此每个字符实际上占用两个字节。所以你的问题更像是“以UTF-8获取str的字节长度”。

您几乎不需要符号的一半,因此可能会减少198或199个字节。

这是两种不同的解决方案:

// direct byte size counting
function cutInUTF8(str, n) {
    var len = Math.min(n, str.length);
    var i, cs, c = 0, bytes = 0;
    for (i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        cs = 1;
        if (c >= 128) cs++;
        if (c >= 2048) cs++;
        if (c >= 0xD800 && c < 0xDC00) {
            c = str.charCodeAt(++i);
            if (c >= 0xDC00 && c < 0xE000) {
                cs++;
            } else {
                // you might actually want to throw an error
                i--;
            }
        }
        if (n < (bytes += cs)) break;
    }
    return str.substr(0, i);
}

// using internal functions, but is not very fast due to try/catch
function cutInUTF8(str, n) {
    var encoded = unescape(encodeURIComponent(str)).substr(0, n);
    while (true) {
        try {
            str = decodeURIComponent(escape(encoded));
            return str;
        } catch(e) {
            encoded = encoded.substr(0, encoded.length-1);
        }
    }
}