什么是代理对? [英] What is a surrogate pair?

查看:323
本文介绍了什么是代理对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  validator.isLength = function(str,min,max )
//在字符串中匹配代理对,或者声明一个空数组(如果没有在字符串中找到)
var surrogatePairs = str.match(/ [\\\�-\\\�] [\\\�-\ uDFFF] / g)|| [];
//从主字符串长度减去代理对字符串长度
var len = str.length - surrogatePairs.length;
//现在将字符串长度与最小和最大值进行比较...还确保定义max(换句话说,max param对于函数是可选的)
return len> = min&& (typeof max ==='undefined'|| len <= max);
};

据了解,上述代码检查字符串的长度,但不使用代理成对考虑。所以:


  1. 我对代码的理解是否正确?


  2. 什么是代理对?


我迄今为止只知道这与编码有关。 / p>

解决方案

你是否尝试只是google?



最好的描述是 http://unicodebook.readthedocs.io/unicode_encodings.html#surrogates



在UTF-16中,一些字符存储在8位,其他字符存储在16位。



代理对是一个字符代表16位。
某些字符代码被保留为成对的第一个。


I came across this code in a javascript open source project.

validator.isLength = function (str, min, max) 
    // match surrogate pairs in string or declare an empty array if none found in string
    var surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
    // subtract the surrogate pairs string length from main string length
    var len = str.length - surrogatePairs.length;
    // now compare string length with min and max ... also make sure max is defined(in other words, max param is optional for function)
    return len >= min && (typeof max === 'undefined' || len <= max);
};

As far as I understand, the above code is checking the length of the string but not taking the surrogate pairs into account. So:

  1. Is my understanding of the code correct?

  2. What are surrogate pairs?

I have thus far only figured out that this is related to encoding.

解决方案

Did you try to just google it?

The best description is http://unicodebook.readthedocs.io/unicode_encodings.html#surrogates

In UTF-16 some characters are stored in 8 bits and others in 16 bits.

Surrogate pair is a character representation that take 16 bits. Some character codes is reserved to be the first one in such pairs.

这篇关于什么是代理对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆