Dav*_*son 6 javascript regex v8 newline node.js
我有一个非常漂亮的工具,下划线cli,在打印出帮助/使用信息时,它会得到最奇怪的行为.
在usage()函数中,我这样做是为了缩进文本块(例如,选项):
str.replace(/^/, " ");
Run Code Online (Sandbox Code Playgroud)
除了非常明显之外,这个正则表达式直接来自TJ Hollowaychuk的commander.js代码.正则表达式是正确的.
然而,我在使用文本的中间插入了bizzare空格.像这样:
Commands:
...
values Retrieve all the values of an object's properties.
extend <object> Override properties in the input data.
defaults <object> Fill in missing properties in the input data.
any <exp> Return 'true' if any of the values in the input make the expression true. Expression args: (value, key, list)
all <exp> Return 'true' if all values in the input make the expression true. Expression args: (value, key, list)
isObject Return 'true' if the input data is an object with named properties
isArray Return 'true' if the input data is an array
isString Return 'true' if the input data is a string
...
99%的几率,这是V8中的一个错误.
任何人都知道为什么会发生这种情况,或者最简单的解决方法是什么?
是的,原来这是一个V8的bug,确切地说是1748.这是我在该工具中使用的变通方法:
str.replace(/(^|\n), "$1 ");
Run Code Online (Sandbox Code Playgroud)
这是 V8 中的一个错误(错误 1748):
这是该错误的测试:
function assertEquals(a, b, msg) { if(a !== b) { console.log("'%s' != '%s' %s", a, b, msg); } }
var str = Array(10000).join("X");
str.replace(/^|X/g, function(m, i, s) {
if (i > 0) assertEquals("X", m, "at position 0x" + i.toString(16));
});
Run Code Online (Sandbox Code Playgroud)
我的盒子上印着:
'X'!= ''。在位置 0x100 'X'!= ''。在位置 0x200 'X'!= ''。在位置 0x300 'X'!= ''。在位置 0x400 'X'!= ''。在位置 0x500 'X'!= ''。在位置 0x600 ...
在 jsfiddle 上,它不打印任何内容(我的 Chrome 浏览器中的 V8 版本没有该错误):
错误历史记录:
从V8变更日志来看,该错误在V8-3.6.5(2011-10-05)中得到修复。
从Node.js 变更日志来看,Node-0.6.5 应该使用 V8-3.6.6.11 !?!!?。Node.js 从 V8-3.6.4 更新到 V8-3.7.0 (Node-0.5.10),然后针对 Node-0.6.0 降级到 V8-3.6.6。所以理论上来说,这个bug应该在Node V0.6.0之前就已经被修复了。为什么它仍然在 Node-0.6.5 上重现???奇怪的。
拥有最新版本(Node-0.6.15)的人可以运行上面的测试片段并报告它是否生成错误吗?或者我最终会抽出时间来处理它。
感谢 ZachB 在 Node-0.6.15 上确认了此错误。我针对节点提交了一个问题(issue #3168),并且已应用修复程序(5d69bbf),并且应该包含在 Node-0.6.16 中。:) :) :)
在此之前,解决方法是替换:
str.replace(/^/, indent);
Run Code Online (Sandbox Code Playgroud)
和:
str.replace(/(^|\n)/, "$1" + indent);
Run Code Online (Sandbox Code Playgroud)
更新:只是为了咯咯笑,我在当前的节点版本 v0.8.1 上检查了这一点,并确认该错误确实已修复。我没有费心回去确认该错误是否在 0.6.16 或 v0.8.X 系列之间的某个时间得到修复。
| 归档时间: |
|
| 查看次数: |
642 次 |
| 最近记录: |