f



[VIM] change "VARIABLE <relational operator> VALUE" to "VALUE <relational operator> VARIABLE"

In C/C++ the following style of check is preferred :
VALUE <relational operator> VARIABLE

instead of
VARIABLE <relational operator> VALUE


eg: 
if( 5 == data ) is preferred over
if( data == 5

while( CONST_NAME != value ) is preferred over
while( value != CONST_NAME)

I have do the above suggested changes throughout the file.
How do I do it using vim ?
0
qazmlp1209
7/25/2003 2:17:44 PM
comp.editors 4871 articles. 0 followers. Post Follow

2 Replies
535 Views

Similar Articles

[PageSpeed] 20

This is an interesting problem, with several parts. First we'll need a 
r.e matching constants:
  [0-9.]\+\|"[^"]*"
is a first approximation; it misprocesses strings with internal "'s and 
scientific notation numeric constants and doesn't even try for 
non-decimal radix constants.
Then we need an r.e matching a variable name:
[A-Za-z_][A-Za-z_0-9}*
is an approximation. Then we combine these with the three classes of 
relational operators: = & !=; < & <=; and > & >=, separately since they 
must be treated differently:

:%s/\([A-Za-z_][A-Za-z_0-9}* \)[ ^I]*\(!+=\)
[ ^I]*\([0-9.]\+\|"[^"]*"\)/\3\2\1/g

:%s/\([A-Za-z_][A-Za-z_0-9}* \)[ ^I]*<\(=+\)
[ ^I]*\([0-9.]\+\|"[^"]*"\)/\3>\2\1/g

:%s/\([A-Za-z_][A-Za-z_0-9}* \)[ ^I]*>\(=+\)
[ ^I]*\([0-9.]\+\|"[^"]*"\)/\3<\2\1/g

Each of the above pairs of lines represent a single long line to vim. 
Note that all spaces around the relational operators have been taken 
out; if you want a single space on either side them:
  /\3 \2 \1/
will put them there for you.

While this will handle most simple cases, it does not even attempt to 
handle the more complex cases where there are expressions involved (on 
either side).

As usual, this composed extemporaneously, therefore trst carefully bfore 
committing to real data.

qazmlp wrote:

> In C/C++ the following style of check is preferred :
> VALUE <relational operator> VARIABLE
> 
> instead of
> VARIABLE <relational operator> VALUE
> 
> 
> eg: 
> if( 5 == data ) is preferred over
> if( data == 5
> 
> while( CONST_NAME != value ) is preferred over
> while( value != CONST_NAME)
> 
> I have do the above suggested changes throughout the file.
> How do I do it using vim ?

0
Robert
7/25/2003 4:18:44 PM
qazmlp wrote:

 > In C/C++ the following style of check is preferred :
 > VALUE <relational operator> VARIABLE
 > 
 > instead of
 > VARIABLE <relational operator> VALUE
 > 
 > eg: 
 > if( 5 == data ) is preferred over
 > if( data == 5
 > 
 > while( CONST_NAME != value ) is preferred over
 > while( value != CONST_NAME)
 
It is? This is why some people do it:

From the comp.lang.c FAQ

 | Question 17.4
 | Why do some people write if(0 == x) instead of if(x == 0)? 
 | 
 | ----------------------------------------------------------
 | 
 | It's a trick to guard against the common error of writing 
 | 
 | if(x = 0)
 | 
 | If you're in the habit of writing the constant before the
 | ==, the compiler will complain if you accidentally type 
 | 	if(0 = x)
 | 
 | Evidently it can be easier to remember to reverse the
 | test than it is to remember to type the doubled = sign. 

But semantically this is strange. Silly answer:

   :set rl

Antony
0
ad_scriven
7/25/2003 7:59:57 PM
Reply: