好吧,我现在知道为什么会这样了。。。在
>;服务器端
尽管这两个版本都运行在python2.6之上,但awseb Python版本是使用UCS4支持编译的,而本地(macosx)python2.6是使用UCS2编译的。More info about UCS here。在AWS EB EC2:>>> import sys
>>> print sys.maxunicode
1114111本地Python 2.6.8安装:
^{pr2}$
最后,我决定我们的项目最好使用支持UCS4的Python 2.6,因此我必须更新Python安装(Mac OS X 10.9.4):
下载并安装Python2.6.8(与EC2实例相同):$ curl -O https://www.python.org/ftp/python/2.6.8/Python-2.6.8.tgz
$ tar xzvf Python-2.6.8.tgz
$ cd Python-2.6.8
$ ./configure disable-framework disable-toolbox-glue OPT="-fast -arch x86_64 -Wall -Wstrict-prototypes -fno-common -fPIC" enable-unicode=ucs4 LDFLAGS="-arch x86_64"
$ make
$ sudo make install
创建新的virtualenv和安装依赖项:$ virtualenv -p /usr/local/bin/python2.6 venv_ayf_eb_26
$ . venv_ayf_eb_26/bin/activate
$ pip install -r requirements.txt
>;客户端
现在在客户端(Javascript)中,我们需要更新循环字符串的方式,因为ECMAScript 5-使用UCS2。在
因此,要读取“实际字符串/符号长度”,我们使用:String.prototype.getSymbols = function() {
var length = this.length;
var index = -1;
var output = [];
var character;
var charCode;
while (++index < length) {
character = this.charAt(index);
charCode = character.charCodeAt(0);
if (charCode >= 0xD800 && charCode <= 0xDBFF) {
// note: this doesn’t account for lone high surrogates
output.push(character + this.charAt(++index));
} else {
output.push(character);
}
}
return output;
};
String.prototype.realLength = function() {
return this.getSymbols().length;
};
循环:// GET original_text over REST API
text = original_text.getSymbols();
for ( var i=0; i
参考文献