Herbert Sitz
Herbert Sitz

Reputation: 22226

Is this a bug in Vim's string expression evaluation?

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) .

Upvotes: 3

Views: 562

Answers (1)

freitass
freitass

Reputation: 6694

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()*.

Upvotes: 1

Related Questions