comparison src/blob.c @ 24432:aa150abca273 v8.2.2756

patch 8.2.2756: Vim9: blob index and slice not implemented yet Commit: https://github.com/vim/vim/commit/cfc3023cb6ce5aaec13f49bc4b821feb05e3fb03 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Apr 11 20:26:34 2021 +0200 patch 8.2.2756: Vim9: blob index and slice not implemented yet Problem: Vim9: blob index and slice not implemented yet. Solution: Implement blob index and slice.
author Bram Moolenaar <Bram@vim.org>
date Sun, 11 Apr 2021 20:30:04 +0200
parents 9e249065aeac
children 602e528a8e43
comparison
equal deleted inserted replaced
24431:8f23b64d87fa 24432:aa150abca273
255 return blob; 255 return blob;
256 256
257 failed: 257 failed:
258 blob_free(blob); 258 blob_free(blob);
259 return NULL; 259 return NULL;
260 }
261
262 int
263 blob_slice_or_index(
264 blob_T *blob,
265 int is_range,
266 varnumber_T n1,
267 varnumber_T n2,
268 int exclusive,
269 typval_T *rettv)
270 {
271 long len = blob_len(blob);
272
273 if (is_range)
274 {
275 // The resulting variable is a sub-blob. If the indexes
276 // are out of range the result is empty.
277 if (n1 < 0)
278 {
279 n1 = len + n1;
280 if (n1 < 0)
281 n1 = 0;
282 }
283 if (n2 < 0)
284 n2 = len + n2;
285 else if (n2 >= len)
286 n2 = len - (exclusive ? 0 : 1);
287 if (exclusive)
288 --n2;
289 if (n1 >= len || n2 < 0 || n1 > n2)
290 {
291 clear_tv(rettv);
292 rettv->v_type = VAR_BLOB;
293 rettv->vval.v_blob = NULL;
294 }
295 else
296 {
297 blob_T *new_blob = blob_alloc();
298 long i;
299
300 if (new_blob != NULL)
301 {
302 if (ga_grow(&new_blob->bv_ga, n2 - n1 + 1) == FAIL)
303 {
304 blob_free(new_blob);
305 return FAIL;
306 }
307 new_blob->bv_ga.ga_len = n2 - n1 + 1;
308 for (i = n1; i <= n2; i++)
309 blob_set(new_blob, i - n1, blob_get(blob, i));
310
311 clear_tv(rettv);
312 rettv_blob_set(rettv, new_blob);
313 }
314 }
315 }
316 else
317 {
318 // The resulting variable is a byte value.
319 // If the index is too big or negative that is an error.
320 if (n1 < 0)
321 n1 = len + n1;
322 if (n1 < len && n1 >= 0)
323 {
324 int v = blob_get(blob, n1);
325
326 clear_tv(rettv);
327 rettv->v_type = VAR_NUMBER;
328 rettv->vval.v_number = v;
329 }
330 else
331 {
332 semsg(_(e_blobidx), n1);
333 return FAIL;
334 }
335 }
336 return OK;
260 } 337 }
261 338
262 /* 339 /*
263 * "remove({blob})" function 340 * "remove({blob})" function
264 */ 341 */