Skip to content

Commit 2770a97

Browse files
committed
updated for version 7.3.377
Problem: No support for bitwise AND, OR, XOR and invert. Solution: Add add(), or(), invert() and xor() functions.
1 parent c0974a9 commit 2770a97

File tree

5 files changed

+129
-5
lines changed

5 files changed

+129
-5
lines changed

runtime/doc/eval.txt

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,12 @@ expr6 . expr6 .. String concatenation *expr-.*
801801
For |Lists| only "+" is possible and then both expr6 must be a list. The
802802
result is a new list with the two lists Concatenated.
803803

804-
expr7 * expr7 .. number multiplication *expr-star*
805-
expr7 / expr7 .. number division *expr-/*
806-
expr7 % expr7 .. number modulo *expr-%*
804+
expr7 * expr7 .. Number multiplication *expr-star*
805+
expr7 / expr7 .. Number division *expr-/*
806+
expr7 % expr7 .. Number modulo *expr-%*
807807

808808
For all, except ".", Strings are converted to Numbers.
809+
For bitwise operators see |and()|, |or()| and |xor()|.
809810

810811
Note the difference between "+" and ".":
811812
"123" + "456" = 579
@@ -1687,6 +1688,7 @@ USAGE RESULT DESCRIPTION ~
16871688
abs( {expr}) Float or Number absolute value of {expr}
16881689
acos( {expr}) Float arc cosine of {expr}
16891690
add( {list}, {item}) List append {item} to |List| {list}
1691+
and( {expr}, {expr}) Number bitwise AND
16901692
append( {lnum}, {string}) Number append {string} below line {lnum}
16911693
append( {lnum}, {list}) Number append lines {list} below line {lnum}
16921694
argc() Number number of files in the argument list
@@ -1825,6 +1827,7 @@ inputrestore() Number restore typeahead
18251827
inputsave() Number save and clear typeahead
18261828
inputsecret( {prompt} [, {text}]) String like input() but hiding the text
18271829
insert( {list}, {item} [, {idx}]) List insert {item} in {list} [before {idx}]
1830+
invert( {expr}) Number bitwise invert
18281831
isdirectory( {directory}) Number TRUE if {directory} is a directory
18291832
islocked( {expr}) Number TRUE if {expr} is locked
18301833
items( {dict}) List key-value pairs in {dict}
@@ -1864,6 +1867,7 @@ mode( [expr]) String current editing mode
18641867
mzeval( {expr}) any evaluate |MzScheme| expression
18651868
nextnonblank( {lnum}) Number line nr of non-blank line >= {lnum}
18661869
nr2char( {expr}) String single char with ASCII value {expr}
1870+
or( {expr}, {expr}) Number bitwise OR
18671871
pathshorten( {expr}) String shorten directory names in a path
18681872
pow( {x}, {y}) Float {x} to the power of {y}
18691873
prevnonblank( {lnum}) Number line nr of non-blank line <= {lnum}
@@ -1987,6 +1991,7 @@ winsaveview() Dict save view of current window
19871991
winwidth( {nr}) Number width of window {nr}
19881992
writefile( {list}, {fname} [, {binary}])
19891993
Number write list of lines to file {fname}
1994+
xor( {expr}, {expr}) Number bitwise XOR
19901995

19911996
abs({expr}) *abs()*
19921997
Return the absolute value of {expr}. When {expr} evaluates to
@@ -2026,6 +2031,13 @@ add({list}, {expr}) *add()*
20262031
Use |insert()| to add an item at another position.
20272032

20282033

2034+
and({expr}, {expr}) *and()*
2035+
Bitwise AND on the two arguments. The arguments are converted
2036+
to a number. A List, Dict or Float argument causes an error.
2037+
Example: >
2038+
:let flag = and(bits, 0x80)
2039+
2040+
20292041
append({lnum}, {expr}) *append()*
20302042
When {expr} is a |List|: Append each item of the |List| as a
20312043
text line below line {lnum} in the current buffer.
@@ -3782,6 +3794,11 @@ insert({list}, {item} [, {idx}]) *insert()*
37823794
Note that when {item} is a |List| it is inserted as a single
37833795
item. Use |extend()| to concatenate |Lists|.
37843796

3797+
invert({expr}) *invert()*
3798+
Bitwise invert. The argument is converted to a number. A
3799+
List, Dict or Float argument causes an error. Example: >
3800+
:let bits = invert(bits)
3801+
37853802
isdirectory({directory}) *isdirectory()*
37863803
The result is a Number, which is non-zero when a directory
37873804
with the name {directory} exists. If {directory} doesn't
@@ -4347,6 +4364,13 @@ getpos({expr}) Get the position for {expr}. For possible values of {expr}
43474364
call setpos('.', save_cursor)
43484365
< Also see |setpos()|.
43494366

4367+
or({expr}, {expr}) *or()*
4368+
Bitwise OR on the two arguments. The arguments are converted
4369+
to a number. A List, Dict or Float argument causes an error.
4370+
Example: >
4371+
:let bits = or(bits, 0x80)
4372+
4373+
43504374
pathshorten({expr}) *pathshorten()*
43514375
Shorten directory names in the path {expr} and return the
43524376
result. The tail, the file name, is kept as-is. The other
@@ -6121,7 +6145,15 @@ writefile({list}, {fname} [, {binary}])
61216145
To copy a file byte for byte: >
61226146
:let fl = readfile("foo", "b")
61236147
:call writefile(fl, "foocopy", "b")
6124-
<
6148+
6149+
6150+
xor({expr}, {expr}) *xor()*
6151+
Bitwise XOR on the two arguments. The arguments are converted
6152+
to a number. A List, Dict or Float argument causes an error.
6153+
Example: >
6154+
:let bits = xor(bits, 0x80)
6155+
6156+
61256157
61266158
*feature-list*
61276159
There are three types of features:

src/eval.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
474474
static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
475475
#endif
476476
static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
477+
static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
477478
static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
478479
static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
479480
static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
@@ -602,6 +603,7 @@ static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
602603
static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
603604
static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
604605
static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
606+
static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
605607
static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
606608
static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
607609
static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
@@ -640,6 +642,7 @@ static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640642
#endif
641643
static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642644
static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
645+
static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
643646
static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
644647
#ifdef FEAT_FLOAT
645648
static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
@@ -751,6 +754,7 @@ static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
751754
static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
752755
static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
753756
static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
757+
static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
754758

755759
static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
756760
static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
@@ -7715,6 +7719,7 @@ static struct fst
77157719
{"acos", 1, 1, f_acos}, /* WJMc */
77167720
#endif
77177721
{"add", 2, 2, f_add},
7722+
{"and", 2, 2, f_and},
77187723
{"append", 2, 2, f_append},
77197724
{"argc", 0, 0, f_argc},
77207725
{"argidx", 0, 0, f_argidx},
@@ -7850,6 +7855,7 @@ static struct fst
78507855
{"inputsave", 0, 0, f_inputsave},
78517856
{"inputsecret", 1, 2, f_inputsecret},
78527857
{"insert", 2, 3, f_insert},
7858+
{"invert", 1, 1, f_invert},
78537859
{"isdirectory", 1, 1, f_isdirectory},
78547860
{"islocked", 1, 1, f_islocked},
78557861
{"items", 1, 1, f_items},
@@ -7888,6 +7894,7 @@ static struct fst
78887894
#endif
78897895
{"nextnonblank", 1, 1, f_nextnonblank},
78907896
{"nr2char", 1, 1, f_nr2char},
7897+
{"or", 2, 2, f_or},
78917898
{"pathshorten", 1, 1, f_pathshorten},
78927899
#ifdef FEAT_FLOAT
78937900
{"pow", 2, 2, f_pow},
@@ -7999,6 +8006,7 @@ static struct fst
79998006
{"winsaveview", 0, 0, f_winsaveview},
80008007
{"winwidth", 1, 1, f_winwidth},
80018008
{"writefile", 2, 3, f_writefile},
8009+
{"xor", 2, 2, f_xor},
80028010
};
80038011

80048012
#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
@@ -8571,6 +8579,18 @@ f_add(argvars, rettv)
85718579
EMSG(_(e_listreq));
85728580
}
85738581

8582+
/*
8583+
* "and(expr, expr)" function
8584+
*/
8585+
static void
8586+
f_and(argvars, rettv)
8587+
typval_T *argvars;
8588+
typval_T *rettv;
8589+
{
8590+
rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8591+
& get_tv_number_chk(&argvars[1], NULL);
8592+
}
8593+
85748594
/*
85758595
* "append(lnum, string/list)" function
85768596
*/
@@ -12957,6 +12977,17 @@ f_insert(argvars, rettv)
1295712977
}
1295812978
}
1295912979

12980+
/*
12981+
* "invert(expr)" function
12982+
*/
12983+
static void
12984+
f_invert(argvars, rettv)
12985+
typval_T *argvars;
12986+
typval_T *rettv;
12987+
{
12988+
rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
12989+
}
12990+
1296012991
/*
1296112992
* "isdirectory()" function
1296212993
*/
@@ -14107,6 +14138,18 @@ f_nr2char(argvars, rettv)
1410714138
rettv->vval.v_string = vim_strsave(buf);
1410814139
}
1410914140

14141+
/*
14142+
* "or(expr, expr)" function
14143+
*/
14144+
static void
14145+
f_or(argvars, rettv)
14146+
typval_T *argvars;
14147+
typval_T *rettv;
14148+
{
14149+
rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14150+
| get_tv_number_chk(&argvars[1], NULL);
14151+
}
14152+
1411014153
/*
1411114154
* "pathshorten()" function
1411214155
*/
@@ -18393,6 +18436,19 @@ f_writefile(argvars, rettv)
1839318436
rettv->vval.v_number = ret;
1839418437
}
1839518438

18439+
/*
18440+
* "xor(expr, expr)" function
18441+
*/
18442+
static void
18443+
f_xor(argvars, rettv)
18444+
typval_T *argvars;
18445+
typval_T *rettv;
18446+
{
18447+
rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18448+
^ get_tv_number_chk(&argvars[1], NULL);
18449+
}
18450+
18451+
1839618452
/*
1839718453
* Translate a String variable into a position.
1839818454
* Returns NULL when there is an error.

src/testdir/test65.in

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Test for floating point.
1+
Test for floating point and logical operators.
22

33
STARTTEST
44
:so small.vim
@@ -72,6 +72,23 @@ STARTTEST
7272
:$put ='float2nr'
7373
:$put =float2nr(123.456)
7474
:$put =float2nr(-123.456)
75+
:$put ='AND'
76+
:$put =and(127, 127)
77+
:$put =and(127, 16)
78+
:$put =and(127, 128)
79+
:$put ='OR'
80+
:$put =or(16, 7)
81+
:$put =or(8, 7)
82+
:$put =or(0, 123)
83+
:$put ='XOR'
84+
:$put =xor(127, 127)
85+
:$put =xor(127, 16)
86+
:$put =xor(127, 128)
87+
:$put ='invert'
88+
:$put =and(invert(127), 65535)
89+
:$put =and(invert(16), 65535)
90+
:$put =and(invert(128), 65535)
91+
:$put =invert(1.0)
7592
:/^Results/,$wq! test.out
7693
ENDTEST
7794

src/testdir/test65.ok

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,20 @@ trunc
5454
float2nr
5555
123
5656
-123
57+
AND
58+
127
59+
16
60+
0
61+
OR
62+
23
63+
15
64+
123
65+
XOR
66+
0
67+
111
68+
255
69+
invert
70+
65408
71+
65519
72+
65407
73+
0

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@ static char *(features[]) =
714714

715715
static int included_patches[] =
716716
{ /* Add new patch number below this line */
717+
/**/
718+
377,
717719
/**/
718720
376,
719721
/**/

0 commit comments

Comments
 (0)