这是 Vim 字符串表达式求值中的错误吗?

发布于 2024-11-08 16:54:53 字数 838 浏览 0 评论 0原文

我正在调试 Arch Linux 上 Vim73 中运行的插件的问题,它似乎源于字符串表达式求值中的错误。

在此 Vim 安装中,表达式 'xxx' > '' 的计算结果为 0(假),而在所有其他 Vims 中,我看到该表达式的计算结果(正如它应该的那样)为 1(真)。

有谁知道这的解释吗? Arch Linux Vim 编译时并没有内置很多功能,但是真的有一些功能可以改变字符串表达式的计算吗?

是否有一些 Vim 设置(编码?)可能改变了这个字符串比较的结果?这是 Vim 的简单安装(vimrc 中没有任何值得注意的内容),给出了糟糕的结果,没有看到任何可以更改设置的地方,即使有一些设置会影响这个结果。

感谢您提供任何信息。

更新: 事实证明,这个问题是由最近版本的 64 位 Vim 中设置 Vim 标志“ignorecase”时字符串比较函数中的错误引起的。无论是否忽略大小写,非空字符串都应该大于空字符串,但 Vim 返回 false。错误报告在这里: http://groups.google.com/group/vim_dev/browse_thread/thread/313bc7c46a19cd40

解决方法是:(1) 使用强制进行“matchcase”比较的比较运算符,例如 mystring_var ># '' 或 (2) 使用 !empty(mystring_var)< /代码> .

I was debugging a problem with a plugin running in Vim73 on Arch Linux and it seems to stem from an error in string expression evaluation.

In this Vim installation the expression 'xxx' > '' evaluates to 0 (false) while in all other Vims I've seen the expression evaluates (as it should) to 1 (true).

Does anyone know the explanation for this? The Arch Linux Vim was not compiled with lots of features built in, but could there really be some feature that changes the evaluation of string expressions?

Is there some Vim setting (encoding?) that might have changed the result of this string comparison? It was a plain-jane install of Vim (nothing of note in vimrc) giving the bad result, didn't see anwhere a setting could have been changed even if there is some setting that affects this result.

Thanks for any info.

UPDATE:
It turns out this problem was caused by a bug in the string comparison function in recent version of 64-bit Vim when the Vim flag 'ignorecase' is set. A non-empty string should be greater than an empty string regardless of whether case is ignored, but Vim was returning false. Bug report is here:
http://groups.google.com/group/vim_dev/browse_thread/thread/313bc7c46a19cd40

Workarounds would be: (1) use comparison operator that forces 'matchcase' comparison, e.g., mystring_var ># '' or (2) use !empty(mystring_var) .

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

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

发布评论

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

评论(1

寄人书 2024-11-15 16:54:53

要知道这个问题的答案,您必须查看文档。这是 *41.4* 条件 的引用部分:

逻辑运算符适用于数字和字符串。当比较两个
字符串,使用数学差异。这比较字节值,
这可能不适合某些语言。

当比较字符串和数字时,字符串首先被转换为
数字。这有点棘手,因为当一个字符串看起来不像
number,使用数字零。示例:

:if 0 == "一"
:回显“是”
:结束

这将回显“是”,因为“一”看起来不像数字,因此它是
转换为数字零。

显然,vim 不保证您尝试执行的操作的结果,您不应该依赖它。如果你想比较字符串的长度,请查看 *strlen()*

To know the answer for this question you have to take a look at the documentation. Here is a quote of the *41.4* Conditionals section:

The logic operators work both for numbers and strings. When comparing two
strings, the mathematical difference is used. This compares byte values,
which may not be right for some languages.

When comparing a string with a number, the string is first converted to a
number. This is a bit tricky, because when a string doesn't look like a
number, the number zero is used. Example:

:if 0 == "one"
:  echo "yes"
:endif

This will echo "yes", because "one" doesn't look like a number, thus it is
converted to the number zero.

Apparently, vim does not guarantee the result for the operation you are trying to perform and you shouldn't rely on it. If you want to compare the length of the strings, take a look at *strlen()*.

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