- 浏览: 3019438 次
- 性别:
- 来自: 海外
文章分类
- 全部博客 (430)
- Programming Languages (23)
- Compiler (20)
- Virtual Machine (57)
- Garbage Collection (4)
- HotSpot VM (26)
- Mono (2)
- SSCLI Rotor (1)
- Harmony (0)
- DLR (19)
- Ruby (28)
- C# (38)
- F# (3)
- Haskell (0)
- Scheme (1)
- Regular Expression (5)
- Python (4)
- ECMAScript (2)
- JavaScript (18)
- ActionScript (7)
- Squirrel (2)
- C (6)
- C++ (10)
- D (2)
- .NET (13)
- Java (86)
- Scala (1)
- Groovy (3)
- Optimization (6)
- Data Structure and Algorithm (3)
- Books (4)
- WPF (1)
- Game Engines (7)
- 吉里吉里 (12)
- UML (1)
- Reverse Engineering (11)
- NSIS (4)
- Utilities (3)
- Design Patterns (1)
- Visual Studio (9)
- Windows 7 (3)
- x86 Assembler (1)
- Android (2)
- School Assignment / Test (6)
- Anti-virus (1)
- REST (1)
- Profiling (1)
- misc (39)
- NetOA (12)
- rant (6)
- anime (5)
- Links (12)
- CLR (7)
- GC (1)
- OpenJDK (2)
- JVM (4)
- KVM (0)
- Rhino (1)
- LINQ (2)
- JScript (0)
- Nashorn (0)
- Dalvik (1)
- DTrace (0)
- LLVM (0)
- MSIL (0)
最新评论
-
mldxs:
虽然很多还是看不懂,写的很好!
虚拟机随谈(一):解释器,树遍历解释器,基于栈与基于寄存器,大杂烩 -
HanyuKing:
Java的多维数组 -
funnyone:
Java 8的default method与method resolution -
ljs_nogard:
Xamarin workbook - .Net Core 中不 ...
LINQ的恶搞…… -
txm119161336:
allocatestlye1 顺序为 // Fields o ...
最近做的两次Java/JVM分享的概要
Ruby每周一测 - 中英文混合字符串截取
Quake Wang发的这个测试相当有趣,值得一看。我也算是被Ruby的字符编码问题困扰了好段时间了,这次果然又中招了。
老庄的解法:
看到老庄的解法,我的第一直觉是:UTF-8中CJK应该是三字节的啊,这样unpack之后算出来的值不是不对了么?然后看看RDoc怎么说的:
看到这个文档我还以为是把UTF-8的字符串拆成字节,结果原来是每个字符对应一个整型数字。
可是这个对应关系到底是怎样的……String#unpack的实现在pack.c里:
然后单个字符的转换函数是:
先确定UTF-8字符的长度(字节数),然后在while循环里编码……但是那几个magic number到底是什么意思我还是没弄明白,主要是那个0x3f和6。回去翻翻UTF-8的说明再看看……
Quake Wang发的这个测试相当有趣,值得一看。我也算是被Ruby的字符编码问题困扰了好段时间了,这次果然又中招了。
老庄的解法:
庄表伟 写道
def truncate_u(text, length = 30, truncate_string = "...") l=0 char_array=text.unpack("U*") char_array.each_with_index do |c,i| l = l+ (c<127 ? 0.5 : 1) if l>=length return char_array[0..i].pack("U*")+(i<char_array.length-1 ? truncate_string : "") end end return text end
看到老庄的解法,我的第一直觉是:UTF-8中CJK应该是三字节的啊,这样unpack之后算出来的值不是不对了么?然后看看RDoc怎么说的:
-------+---------+----------------------------------------- U | Integer | UTF-8 characters as unsigned integers -------+---------+-----------------------------------------
看到这个文档我还以为是把UTF-8的字符串拆成字节,结果原来是每个字符对应一个整型数字。
可是这个对应关系到底是怎样的……String#unpack的实现在pack.c里:
case 'U': if (len > send - s) len = send - s; while (len > 0 && s < send) { long alen = send - s; unsigned long l; l = utf8_to_uv(s, &alen); s += alen; len--; rb_ary_push(ary, ULONG2NUM(l)); } break;
然后单个字符的转换函数是:
static unsigned long utf8_to_uv(p, lenp) char *p; long *lenp; { int c = *p++ & 0xff; unsigned long uv = c; long n; if (!(uv & 0x80)) { *lenp = 1; return uv; } if (!(uv & 0x40)) { *lenp = 1; rb_raise(rb_eArgError, "malformed UTF-8 character"); } if (!(uv & 0x20)) { n = 2; uv &= 0x1f; } else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; } else if (!(uv & 0x08)) { n = 4; uv &= 0x07; } else if (!(uv & 0x04)) { n = 5; uv &= 0x03; } else if (!(uv & 0x02)) { n = 6; uv &= 0x01; } else { *lenp = 1; rb_raise(rb_eArgError, "malformed UTF-8 character"); } if (n > *lenp) { rb_raise(rb_eArgError, "malformed UTF-8 character (expected %d bytes, given %d bytes)", n, *lenp); } *lenp = n--; if (n != 0) { while (n--) { c = *p++ & 0xff; if ((c & 0xc0) != 0x80) { *lenp -= n + 1; rb_raise(rb_eArgError, "malformed UTF-8 character"); } else { c &= 0x3f; uv = uv << 6 | c; } } } n = *lenp - 1; if (uv < utf8_limits[n]) { rb_raiserb_eArgError, "redundant UTF-8 sequence"); } return uv; }
先确定UTF-8字符的长度(字节数),然后在while循环里编码……但是那几个magic number到底是什么意思我还是没弄明白,主要是那个0x3f和6。回去翻翻UTF-8的说明再看看……
发表评论
-
字符串的一般封装方式的内存布局 (0): 拿在手上的是什么
2013-11-04 18:22 21381(Disclaimer:未经许可请 ... -
字符串的一般封装方式的内存布局
2013-11-01 12:55 0(Disclaimer:未经许可请 ... -
RubyConf notes
2011-11-08 19:10 0symmetric coroutine Fiber#trans ... -
ShanghaiOnRails第八次线下活动——你不需要知道的Ruby实现
2010-11-12 15:39 2795上个月底正好赶上参加了ShanghaiOnRails 第八次线 ... -
JRuby的运行模式
2010-11-01 11:21 0// 老的JRuby还是用org.jruby.evaluato ... -
你不需要知道的Ruby草稿
2010-10-27 11:25 0一些Ruby实现 Ruby 1.8 树遍历解释 Ruby 1 ... -
Ruby里的fiber/coroutine例子
2010-01-26 18:33 0Ruby 1.9开始支持fiber。与“fiber”一词的一般 ... -
JRuby使用技巧收集
2009-12-28 09:35 0java.lang.Thread.new { puts &qu ... -
特殊类型的eigenclass
2009-12-17 03:39 0Fixnum的实例没有eigenclass true、fals ... -
奇怪的参数
2009-12-08 02:25 0默认参数与闭包的组合 真正的问题不是只是默认参数看起来很诡异, ... -
MacRuby的执行模式
2009-12-07 07:41 0mailing-list macruby-devel http ... -
Rubinius的执行模型
2009-12-05 15:22 0Rubinius wants to help YOU make ... -
Ruby 1.8和1.9中String#hash的实现
2009-11-22 18:23 01.8 string.c int rb_str_hash(s ... -
To囧:拿你来测测Watir...
2009-11-21 22:38 3400iaimstar 写道@RednaxelaFX 你最近ruby ... -
[标题党] MagLev中GC类的真相……
2009-11-21 14:46 0注意到本文的标题:我是说“GC类”的真相,不是说GC的真相哦~ ... -
小试rubyzip的一个脚本
2009-11-17 20:42 3412呼,今天开始3天都是新人培训,总算可以抽点时间发一帖。 现在在 ... -
爬一下Google和百度看口碑对它们做的SEO效果如何
2009-11-09 00:27 0#!/usr/bin/env ruby require ... -
把Mechanize的html_parser改回到Hpricot
2009-11-08 14:45 3062记得我最初开始用Nokogir ... -
使用新的RubyInstaller
2009-11-07 02:37 0Hpricot的安装需要编译,需要devkit -
Ruby metaprogramming tech notes
2009-09-28 15:39 0class Builder def self.build ...
相关推荐
资源分类:Python库 所属语言:Python 资源全名:v8unpack-0.8.2-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源来自pypi官网。 资源全名:v8unpack-0.6.19-py3-none-any.whl
资源分类:Python库 所属语言:Python 资源全名:fmrib_unpack-1.3.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
mp-unpack mp-unpack
sap_me_pack_unpack_how_to_guide_en
This test case unpacks the compressed code for the MochiKit, jQuery, Dojo and Prototype JavaScript libraries.
a file about function of soemthing in dota 2
Download an archive for your operating system and unpack the content to a place accessible from command line. The ethminer is ready to go. | Builds | Release | Date | | ------ | ------- | ---- | | ...
主要介绍了Lua UnPack函数用法实例,unpack它接受一个数组(table)作为参数,并默认从下标1开始返回数组的所有元素,需要的朋友可以参考下
1. Unpack Download1 'DOSBox_S60v3_(Marcin-prv)_S60cn.rar' archiwe to your Memory Card 1. 解压下载地址1的DOSBox_S60v3_(Marcin-prv)_S60cn.rar 并复制到存储卡 2. Install ALL aplication from 'Install' ...
前端开源库-rsa-unpackrsa解包,从pem字符串解包rsa字段
1) Unpack the archive where you would like to store the binaries, eg: Unix-based operating systems (Linux, Solaris and Mac OS X) tar zxvf apache-maven-3.x.y.tar.gz Windows unzip apache-maven-3.x...
New: Support for Unicode UTF-8 format. 捋? 圹? - Bitmap viewer in Lister, additional formats through Irfanview 捋? 圹? - HTML- and Unicode-Viewer in Lister. 捋? 圹? - Parallel port transfer ...
用于GNS3中模拟ASA防火墙,希望对大家有帮助
Python bootimg.py supported arguments: --cpio-list --repack-565 --repack-bootimg --repack-ramdisk --repack-rle --repack-updata --unpack-565 --unpack-bootimg ... --unpack-yaffs2
webpack解压 从webpack生成的包中提取模块 ...npm install webpack-unpack 用法 管道捆绑到它: $ webpack - unpack (t){console.log(t)},exports.boop n,r,o=module.exports n=0,r>=0;r--){var /*..snip
python库。 资源全名:v8unpack-0.5.1.tar.gz