降价 -> detab 正则表达式中的摊牌错误?

发布于 2024-12-03 06:40:55 字数 706 浏览 3 评论 0原文

我正在查看 Gruber 的原始 Markdown 实现此处 和 Showdown 实现此处

我正在比较每个中的 _Detab 函数。我给出以下每个字符串。

"Where\tis pancakes house?"

Perl 版本的测试和输出位于此处。长度为 26 个字符。

测试和输出的 JavaScript 版本位于此处。长度为 27 个字符。

      123456789012345678901234567
Perl: Where   is pancakes house?
  JS: Where    is pancakes house?

我犯了一个错误吗?这是一个错误,还是有其他目的?

I'm looking at Gruber's original Markdown implementation here and the Showdown implementation here.

I'm comparing the _Detab function in each. I'm giving each the following string

"Where\tis pancakes house?"

The Perl version of the test and output is here. This is 26 characters long.

The JavaScript version of the test and output is here. This is 27 characters long.

      123456789012345678901234567
Perl: Where   is pancakes house?
  JS: Where    is pancakes house?

Have I made a mistake? Is it a bug, or is there some other purpose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷又爱笑 2024-12-10 06:40:55

Showdown 的 detabber 中有几个错误。这就是为什么对于 Stack Overflow 的版本,我重写了它< /a>:

function _Detab(text) {
    if (!/\t/.test(text))
        return text;

    var spaces = ["    ", "   ", "  ", " "],
    skew = 0,
    v;

    return text.replace(/[\n\t]/g, function (match, offset) {
        if (match === "\n") {
            skew = offset + 1;
            return match;
        }
        v = (offset - skew) % 4;
        skew = offset + 1;
        return spaces[v];
    });
}

它正确地 detabs,如果我正确地回忆起我的测量结果,这与旧 IE 版本中的原始版本一样快(可能慢一点),并且在较新的浏览器中快得多

请参阅 http://code.google.com/p/pagedown/wiki/PageDown 完整版本的 Showdown。

There are several bugs in Showdown's detabber. That's why for Stack Overflow's version, I have rewritten it:

function _Detab(text) {
    if (!/\t/.test(text))
        return text;

    var spaces = ["    ", "   ", "  ", " "],
    skew = 0,
    v;

    return text.replace(/[\n\t]/g, function (match, offset) {
        if (match === "\n") {
            skew = offset + 1;
            return match;
        }
        v = (offset - skew) % 4;
        skew = offset + 1;
        return spaces[v];
    });
}

It detabs correctly, and if I recall my measurements correctly, this is about as fast (maybe a little slower) as the original in older IE versions, and much faster in newer browsers.

See http://code.google.com/p/pagedown/wiki/PageDown for our full version of Showdown.

滿滿的愛 2024-12-10 06:40:55

它看起来像是 Showdown 实现中的一个错误。 Markdown 使用 4 个空格制表符,因此在制表符转换为空格后,以制表符结尾的字符串长度应始终是 4 个字符的倍数。 Perl 版本将 "Where\t" 设为 8 个字符,但 JavaScript 版本将其设为 9 个字符。

我怀疑该错误可能不会发生在行首的制表符上,这就是它们通常在 Markdown 中使用的方式,这可以解释为什么它没有被注意到。

It looks like a bug in the Showdown implementation. Markdown uses 4-space tabs, so a string ending in a tab should always be a multiple of 4 characters long after tabs are converted to spaces. The Perl version makes "Where\t" 8 characters, but the JavaScript one makes it 9 characters.

I suspect the bug may not occur with tabs at the beginning of a line, which is how they're normally used in Markdown, which would explain why it hasn't been noticed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文