annotate src/blob.c @ 24450:3e1886f1e875 v8.2.2765

patch 8.2.2765: Vim9: not all blob operations work Commit: https://github.com/vim/vim/commit/0e3ff1919603ee4c4a347fdf761dbdbdeb068015 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Apr 14 20:35:23 2021 +0200 patch 8.2.2765: Vim9: not all blob operations work Problem: Vim9: not all blob operations work. Solution: Run more tests also with Vim9 script and :def functions. Fix what doesn't work.
author Bram Moolenaar <Bram@vim.org>
date Wed, 14 Apr 2021 20:45:04 +0200
parents 602e528a8e43
children 53216e87f21c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2 *
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 *
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 * blob.c: Blob support by Yasuhiro Matsumoto
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 #include "vim.h"
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 #if defined(FEAT_EVAL) || defined(PROTO)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 * Allocate an empty blob.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20 * Caller should take care of the reference count.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 blob_T *
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23 blob_alloc(void)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 {
16825
ce04ebdf26b8 patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents: 16034
diff changeset
25 blob_T *blob = ALLOC_CLEAR_ONE(blob_T);
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 if (blob != NULL)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 ga_init2(&blob->bv_ga, 1, 100);
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 return blob;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 * Allocate an empty blob for a return value, with reference count set.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 * Returns OK or FAIL.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 int
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 rettv_blob_alloc(typval_T *rettv)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 blob_T *b = blob_alloc();
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 if (b == NULL)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 return FAIL;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 rettv_blob_set(rettv, b);
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 return OK;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49 * Set a blob as the return value.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 void
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 rettv_blob_set(typval_T *rettv, blob_T *b)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 rettv->v_type = VAR_BLOB;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 rettv->vval.v_blob = b;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 if (b != NULL)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 ++b->bv_refcount;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59
15581
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
60 int
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
61 blob_copy(blob_T *from, typval_T *to)
15581
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
62 {
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
63 int ret = OK;
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
64
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
65 to->v_type = VAR_BLOB;
17344
a0eb2ff0f297 patch 8.1.1671: copying a blob may result in it being locked
Bram Moolenaar <Bram@vim.org>
parents: 16825
diff changeset
66 to->v_lock = 0;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
67 if (from == NULL)
15581
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
68 to->vval.v_blob = NULL;
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
69 else if (rettv_blob_alloc(to) == FAIL)
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
70 ret = FAIL;
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
71 else
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
72 {
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
73 int len = from->bv_ga.ga_len;
15581
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
74
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
75 if (len > 0)
15589
44ea60ca593b patch 8.1.0802: negative index doesn't work for Blob
Bram Moolenaar <Bram@vim.org>
parents: 15581
diff changeset
76 {
15581
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
77 to->vval.v_blob->bv_ga.ga_data =
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
78 vim_memsave(from->bv_ga.ga_data, len);
15589
44ea60ca593b patch 8.1.0802: negative index doesn't work for Blob
Bram Moolenaar <Bram@vim.org>
parents: 15581
diff changeset
79 if (to->vval.v_blob->bv_ga.ga_data == NULL)
44ea60ca593b patch 8.1.0802: negative index doesn't work for Blob
Bram Moolenaar <Bram@vim.org>
parents: 15581
diff changeset
80 len = 0;
44ea60ca593b patch 8.1.0802: negative index doesn't work for Blob
Bram Moolenaar <Bram@vim.org>
parents: 15581
diff changeset
81 }
15581
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
82 to->vval.v_blob->bv_ga.ga_len = len;
22635
9e249065aeac patch 8.2.1866: Vim9: appending to pushed blob gives wrong result
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
83 to->vval.v_blob->bv_ga.ga_maxlen = len;
15581
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
84 }
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
85 return ret;
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
86 }
c2382f0d1279 patch 8.1.0798: changing a blob while iterating over it works strangely
Bram Moolenaar <Bram@vim.org>
parents: 15515
diff changeset
87
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 void
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 blob_free(blob_T *b)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91 ga_clear(&b->bv_ga);
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 vim_free(b);
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 * Unreference a blob: decrement the reference count and free it when it
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 * becomes zero.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 void
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 blob_unref(blob_T *b)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 if (b != NULL && --b->bv_refcount <= 0)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 blob_free(b);
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107 * Get the length of data.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 long
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 blob_len(blob_T *b)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 if (b == NULL)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113 return 0L;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 return b->bv_ga.ga_len;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 * Get byte "idx" in blob "b".
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 * Caller must check that "idx" is valid.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120 */
15589
44ea60ca593b patch 8.1.0802: negative index doesn't work for Blob
Bram Moolenaar <Bram@vim.org>
parents: 15581
diff changeset
121 int
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
122 blob_get(blob_T *b, int idx)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124 return ((char_u*)b->bv_ga.ga_data)[idx];
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128 * Store one byte "c" in blob "b" at "idx".
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 * Caller must make sure that "idx" is valid.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 void
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132 blob_set(blob_T *b, int idx, char_u c)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134 ((char_u*)b->bv_ga.ga_data)[idx] = c;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138 * Return TRUE when two blobs have exactly the same values.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
140 int
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 blob_equal(
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142 blob_T *b1,
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
143 blob_T *b2)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
144 {
15456
f01eb1aed348 patch 8.1.0736: code for Blob not sufficiently tested
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
145 int i;
f01eb1aed348 patch 8.1.0736: code for Blob not sufficiently tested
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
146 int len1 = blob_len(b1);
f01eb1aed348 patch 8.1.0736: code for Blob not sufficiently tested
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
147 int len2 = blob_len(b2);
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148
15456
f01eb1aed348 patch 8.1.0736: code for Blob not sufficiently tested
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
149 // empty and NULL are considered the same
f01eb1aed348 patch 8.1.0736: code for Blob not sufficiently tested
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
150 if (len1 == 0 && len2 == 0)
f01eb1aed348 patch 8.1.0736: code for Blob not sufficiently tested
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
151 return TRUE;
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 if (b1 == b2)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153 return TRUE;
15456
f01eb1aed348 patch 8.1.0736: code for Blob not sufficiently tested
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
154 if (len1 != len2)
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 return FALSE;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157 for (i = 0; i < b1->bv_ga.ga_len; i++)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
158 if (blob_get(b1, i) != blob_get(b2, i)) return FALSE;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
159 return TRUE;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
161
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 * Read "blob" from file "fd".
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 * Return OK or FAIL.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166 int
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 read_blob(FILE *fd, blob_T *blob)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 struct stat st;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 if (fstat(fileno(fd), &st) < 0)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172 return FAIL;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173 if (ga_grow(&blob->bv_ga, st.st_size) == FAIL)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 return FAIL;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 blob->bv_ga.ga_len = st.st_size;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 if (fread(blob->bv_ga.ga_data, 1, blob->bv_ga.ga_len, fd)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 < (size_t)blob->bv_ga.ga_len)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 return FAIL;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 return OK;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182 /*
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
183 * Write "blob" to file "fd".
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 * Return OK or FAIL.
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 */
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 int
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 write_blob(FILE *fd, blob_T *blob)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 if (fwrite(blob->bv_ga.ga_data, 1, blob->bv_ga.ga_len, fd)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190 < (size_t)blob->bv_ga.ga_len)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15466
diff changeset
192 emsg(_(e_write));
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 return FAIL;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195 return OK;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
196 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197
15466
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
198 /*
15515
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
199 * Convert a blob to a readable form: "0z00112233.44556677.8899"
15466
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
200 */
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
201 char_u *
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
202 blob2string(blob_T *blob, char_u **tofree, char_u *numbuf)
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
203 {
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
204 int i;
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
205 garray_T ga;
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
206
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
207 if (blob == NULL)
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
208 {
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
209 *tofree = NULL;
15515
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
210 return (char_u *)"0z";
15466
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
211 }
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
212
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
213 // Store bytes in the growarray.
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
214 ga_init2(&ga, 1, 4000);
15515
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
215 ga_concat(&ga, (char_u *)"0z");
15466
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
216 for (i = 0; i < blob_len(blob); i++)
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
217 {
15515
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
218 if (i > 0 && (i & 3) == 0)
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
219 ga_concat(&ga, (char_u *)".");
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
220 vim_snprintf((char *)numbuf, NUMBUFLEN, "%02X", (int)blob_get(blob, i));
15466
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
221 ga_concat(&ga, numbuf);
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
222 }
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
223 *tofree = ga.ga_data;
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
224 return *tofree;
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
225 }
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
226
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
227 /*
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
228 * Convert a string variable, in the format of blob2string(), to a blob.
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
229 * Return NULL when conversion failed.
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
230 */
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
231 blob_T *
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
232 string2blob(char_u *str)
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
233 {
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
234 blob_T *blob = blob_alloc();
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
235 char_u *s = str;
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
236
16034
27f9f4c1400b patch 8.1.1022: may use NULL pointer when out of memory
Bram Moolenaar <Bram@vim.org>
parents: 15589
diff changeset
237 if (blob == NULL)
27f9f4c1400b patch 8.1.1022: may use NULL pointer when out of memory
Bram Moolenaar <Bram@vim.org>
parents: 15589
diff changeset
238 return NULL;
15515
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
239 if (s[0] != '0' || (s[1] != 'z' && s[1] != 'Z'))
15466
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
240 goto failed;
15515
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
241 s += 2;
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
242 while (vim_isxdigit(*s))
15466
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
243 {
15515
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
244 if (!vim_isxdigit(s[1]))
15466
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
245 goto failed;
15515
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
246 ga_append(&blob->bv_ga, (hex2nr(s[0]) << 4) + hex2nr(s[1]));
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
247 s += 2;
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
248 if (*s == '.' && vim_isxdigit(s[1]))
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
249 ++s;
15466
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
250 }
15515
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
251 if (*skipwhite(s) != NUL)
99a4cc4782ac patch 8.1.0765: string format of a Blob can't be parsed back
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
252 goto failed; // text after final digit
15466
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
253
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
254 ++blob->bv_refcount;
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
255 return blob;
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
256
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
257 failed:
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
258 blob_free(blob);
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
259 return NULL;
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
260 }
435fcefd2c8e patch 8.1.0741: viminfo with Blob is not tested
Bram Moolenaar <Bram@vim.org>
parents: 15456
diff changeset
261
24432
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
262 int
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
263 blob_slice_or_index(
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
264 blob_T *blob,
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
265 int is_range,
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
266 varnumber_T n1,
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
267 varnumber_T n2,
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
268 int exclusive,
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
269 typval_T *rettv)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
270 {
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
271 long len = blob_len(blob);
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
272
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
273 if (is_range)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
274 {
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
275 // The resulting variable is a sub-blob. If the indexes
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
276 // are out of range the result is empty.
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
277 if (n1 < 0)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
278 {
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
279 n1 = len + n1;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
280 if (n1 < 0)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
281 n1 = 0;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
282 }
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
283 if (n2 < 0)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
284 n2 = len + n2;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
285 else if (n2 >= len)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
286 n2 = len - (exclusive ? 0 : 1);
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
287 if (exclusive)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
288 --n2;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
289 if (n1 >= len || n2 < 0 || n1 > n2)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
290 {
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
291 clear_tv(rettv);
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
292 rettv->v_type = VAR_BLOB;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
293 rettv->vval.v_blob = NULL;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
294 }
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
295 else
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
296 {
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
297 blob_T *new_blob = blob_alloc();
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
298 long i;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
299
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
300 if (new_blob != NULL)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
301 {
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
302 if (ga_grow(&new_blob->bv_ga, n2 - n1 + 1) == FAIL)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
303 {
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
304 blob_free(new_blob);
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
305 return FAIL;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
306 }
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
307 new_blob->bv_ga.ga_len = n2 - n1 + 1;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
308 for (i = n1; i <= n2; i++)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
309 blob_set(new_blob, i - n1, blob_get(blob, i));
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
310
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
311 clear_tv(rettv);
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
312 rettv_blob_set(rettv, new_blob);
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
313 }
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
314 }
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
315 }
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
316 else
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
317 {
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
318 // The resulting variable is a byte value.
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
319 // If the index is too big or negative that is an error.
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
320 if (n1 < 0)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
321 n1 = len + n1;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
322 if (n1 < len && n1 >= 0)
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
323 {
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
324 int v = blob_get(blob, n1);
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
325
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
326 clear_tv(rettv);
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
327 rettv->v_type = VAR_NUMBER;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
328 rettv->vval.v_number = v;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
329 }
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
330 else
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
331 {
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
332 semsg(_(e_blobidx), n1);
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
333 return FAIL;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
334 }
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
335 }
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
336 return OK;
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
337 }
aa150abca273 patch 8.2.2756: Vim9: blob index and slice not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 22635
diff changeset
338
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
339 /*
24450
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
340 * Check if "n1"- is a valid index for a blobl with length "bloblen".
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
341 */
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
342 int
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
343 check_blob_index(long bloblen, varnumber_T n1, int is_range, int quiet)
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
344 {
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
345 if (n1 < 0 || n1 > bloblen)
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
346 {
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
347 if (!quiet)
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
348 semsg(_(e_blobidx), n1);
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
349 return FAIL;
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
350 }
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
351 return OK;
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
352 }
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
353
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
354 /*
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
355 * Check if "n1"-"n2" is a valid range for a blob with length "bloblen".
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
356 */
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
357 int
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
358 check_blob_range(long bloblen, varnumber_T n1, varnumber_T n2, int quiet)
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
359 {
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
360 if (n2 < 0 || n2 >= bloblen || n2 < n1)
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
361 {
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
362 if (!quiet)
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
363 semsg(_(e_blobidx), n2);
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
364 return FAIL;
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
365 }
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
366 return OK;
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
367 }
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
368
3e1886f1e875 patch 8.2.2765: Vim9: not all blob operations work
Bram Moolenaar <Bram@vim.org>
parents: 24434
diff changeset
369 /*
24434
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
370 * Set bytes "n1" to "n2" (inclusive) in "dest" to the value of "src".
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
371 * Caller must make sure "src" is a blob.
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
372 * Returns FAIL if the number of bytes does not match.
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
373 */
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
374 int
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
375 blob_set_range(blob_T *dest, long n1, long n2, typval_T *src)
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
376 {
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
377 int il, ir;
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
378
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
379 if (n2 - n1 + 1 != blob_len(src->vval.v_blob))
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
380 {
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
381 emsg(_("E972: Blob value does not have the right number of bytes"));
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
382 return FAIL;
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
383 }
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
384
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
385 ir = 0;
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
386 for (il = n1; il <= n2; il++)
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
387 blob_set(dest, il, blob_get(src->vval.v_blob, ir++));
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
388 return OK;
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
389 }
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
390
602e528a8e43 patch 8.2.2757: Vim9: blob tests for legacy and Vim9 script are separate
Bram Moolenaar <Bram@vim.org>
parents: 24432
diff changeset
391 /*
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
392 * "remove({blob})" function
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
393 */
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
394 void
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
395 blob_remove(typval_T *argvars, typval_T *rettv)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
396 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
397 int error = FALSE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
398 long idx = (long)tv_get_number_chk(&argvars[1], &error);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
399 long end;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
400
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
401 if (!error)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
402 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
403 blob_T *b = argvars[0].vval.v_blob;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
404 int len = blob_len(b);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
405 char_u *p;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
406
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
407 if (idx < 0)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
408 // count from the end
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
409 idx = len + idx;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
410 if (idx < 0 || idx >= len)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
411 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
412 semsg(_(e_blobidx), idx);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
413 return;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
414 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
415 if (argvars[2].v_type == VAR_UNKNOWN)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
416 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
417 // Remove one item, return its value.
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
418 p = (char_u *)b->bv_ga.ga_data;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
419 rettv->vval.v_number = (varnumber_T) *(p + idx);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
420 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
421 --b->bv_ga.ga_len;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
422 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
423 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
424 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
425 blob_T *blob;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
426
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
427 // Remove range of items, return list with values.
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
428 end = (long)tv_get_number_chk(&argvars[2], &error);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
429 if (error)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
430 return;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
431 if (end < 0)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
432 // count from the end
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
433 end = len + end;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
434 if (end >= len || idx > end)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
435 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
436 semsg(_(e_blobidx), end);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
437 return;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
438 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
439 blob = blob_alloc();
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
440 if (blob == NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
441 return;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
442 blob->bv_ga.ga_len = end - idx + 1;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
443 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
444 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
445 vim_free(blob);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
446 return;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
447 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
448 p = (char_u *)b->bv_ga.ga_data;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
449 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
450 (size_t)(end - idx + 1));
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
451 ++blob->bv_refcount;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
452 rettv->v_type = VAR_BLOB;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
453 rettv->vval.v_blob = blob;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
454
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
455 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
456 b->bv_ga.ga_len -= end - idx + 1;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
457 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
458 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
459 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17344
diff changeset
460
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17530
diff changeset
461 #endif // defined(FEAT_EVAL)