comparison src/blob.c @ 30531:2650a01b8bbc v9.0.0601

patch 9.0.0601: too much indent Commit: https://github.com/vim/vim/commit/368aa6908862874fdb901c534ee99033ac977e4a Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Tue Sep 27 11:46:48 2022 +0100 patch 9.0.0601: too much indent Problem: Too much indent. Solution: Return out early from a funtion. (Yegappan Lakshmanan, close #11238)
author Bram Moolenaar <Bram@vim.org>
date Tue, 27 Sep 2022 13:00:04 +0200
parents 6c2bbd7d9217
children b3de17181c19
comparison
equal deleted inserted replaced
30530:f501627bf532 30531:2650a01b8bbc
58 } 58 }
59 59
60 int 60 int
61 blob_copy(blob_T *from, typval_T *to) 61 blob_copy(blob_T *from, typval_T *to)
62 { 62 {
63 int ret = OK; 63 int len;
64 64
65 to->v_type = VAR_BLOB; 65 to->v_type = VAR_BLOB;
66 to->v_lock = 0; 66 to->v_lock = 0;
67 if (from == NULL) 67 if (from == NULL)
68 {
68 to->vval.v_blob = NULL; 69 to->vval.v_blob = NULL;
69 else if (rettv_blob_alloc(to) == FAIL) 70 return OK;
70 ret = FAIL; 71 }
71 else 72
72 { 73 if (rettv_blob_alloc(to) == FAIL)
73 int len = from->bv_ga.ga_len; 74 return FAIL;
74 75
75 if (len > 0) 76 len = from->bv_ga.ga_len;
76 { 77 if (len > 0)
77 to->vval.v_blob->bv_ga.ga_data = 78 {
78 vim_memsave(from->bv_ga.ga_data, len); 79 to->vval.v_blob->bv_ga.ga_data =
79 if (to->vval.v_blob->bv_ga.ga_data == NULL) 80 vim_memsave(from->bv_ga.ga_data, len);
80 len = 0; 81 if (to->vval.v_blob->bv_ga.ga_data == NULL)
81 } 82 len = 0;
82 to->vval.v_blob->bv_ga.ga_len = len; 83 }
83 to->vval.v_blob->bv_ga.ga_maxlen = len; 84 to->vval.v_blob->bv_ga.ga_len = len;
84 } 85 to->vval.v_blob->bv_ga.ga_maxlen = len;
85 return ret; 86
87 return OK;
86 } 88 }
87 89
88 void 90 void
89 blob_free(blob_T *b) 91 blob_free(blob_T *b)
90 { 92 {
278 failed: 280 failed:
279 blob_free(blob); 281 blob_free(blob);
280 return NULL; 282 return NULL;
281 } 283 }
282 284
285 /*
286 * Returns a slice of 'blob' from index 'n1' to 'n2' in 'rettv'. The length of
287 * the blob is 'len'. Returns an empty blob if the indexes are out of range.
288 */
289 static int
290 blob_slice(
291 blob_T *blob,
292 long len,
293 varnumber_T n1,
294 varnumber_T n2,
295 int exclusive,
296 typval_T *rettv)
297 {
298 if (n1 < 0)
299 {
300 n1 = len + n1;
301 if (n1 < 0)
302 n1 = 0;
303 }
304 if (n2 < 0)
305 n2 = len + n2;
306 else if (n2 >= len)
307 n2 = len - (exclusive ? 0 : 1);
308 if (exclusive)
309 --n2;
310 if (n1 >= len || n2 < 0 || n1 > n2)
311 {
312 clear_tv(rettv);
313 rettv->v_type = VAR_BLOB;
314 rettv->vval.v_blob = NULL;
315 }
316 else
317 {
318 blob_T *new_blob = blob_alloc();
319 long i;
320
321 if (new_blob != NULL)
322 {
323 if (ga_grow(&new_blob->bv_ga, n2 - n1 + 1) == FAIL)
324 {
325 blob_free(new_blob);
326 return FAIL;
327 }
328 new_blob->bv_ga.ga_len = n2 - n1 + 1;
329 for (i = n1; i <= n2; i++)
330 blob_set(new_blob, i - n1, blob_get(blob, i));
331
332 clear_tv(rettv);
333 rettv_blob_set(rettv, new_blob);
334 }
335 }
336
337 return OK;
338 }
339
340 /*
341 * Return the byte value in 'blob' at index 'idx' in 'rettv'. If the index is
342 * too big or negative that is an error. The length of the blob is 'len'.
343 */
344 static int
345 blob_index(
346 blob_T *blob,
347 int len,
348 varnumber_T idx,
349 typval_T *rettv)
350 {
351 // The resulting variable is a byte value.
352 // If the index is too big or negative that is an error.
353 if (idx < 0)
354 idx = len + idx;
355 if (idx < len && idx >= 0)
356 {
357 int v = blob_get(blob, idx);
358
359 clear_tv(rettv);
360 rettv->v_type = VAR_NUMBER;
361 rettv->vval.v_number = v;
362 }
363 else
364 {
365 semsg(_(e_blob_index_out_of_range_nr), idx);
366 return FAIL;
367 }
368
369 return OK;
370 }
371
283 int 372 int
284 blob_slice_or_index( 373 blob_slice_or_index(
285 blob_T *blob, 374 blob_T *blob,
286 int is_range, 375 int is_range,
287 varnumber_T n1, 376 varnumber_T n1,
288 varnumber_T n2, 377 varnumber_T n2,
289 int exclusive, 378 int exclusive,
290 typval_T *rettv) 379 typval_T *rettv)
291 { 380 {
292 long len = blob_len(blob); 381 long len = blob_len(blob);
293 382
294 if (is_range) 383 if (is_range)
295 { 384 return blob_slice(blob, len, n1, n2, exclusive, rettv);
296 // The resulting variable is a sub-blob. If the indexes
297 // are out of range the result is empty.
298 if (n1 < 0)
299 {
300 n1 = len + n1;
301 if (n1 < 0)
302 n1 = 0;
303 }
304 if (n2 < 0)
305 n2 = len + n2;
306 else if (n2 >= len)
307 n2 = len - (exclusive ? 0 : 1);
308 if (exclusive)
309 --n2;
310 if (n1 >= len || n2 < 0 || n1 > n2)
311 {
312 clear_tv(rettv);
313 rettv->v_type = VAR_BLOB;
314 rettv->vval.v_blob = NULL;
315 }
316 else
317 {
318 blob_T *new_blob = blob_alloc();
319 long i;
320
321 if (new_blob != NULL)
322 {
323 if (ga_grow(&new_blob->bv_ga, n2 - n1 + 1) == FAIL)
324 {
325 blob_free(new_blob);
326 return FAIL;
327 }
328 new_blob->bv_ga.ga_len = n2 - n1 + 1;
329 for (i = n1; i <= n2; i++)
330 blob_set(new_blob, i - n1, blob_get(blob, i));
331
332 clear_tv(rettv);
333 rettv_blob_set(rettv, new_blob);
334 }
335 }
336 }
337 else 385 else
338 { 386 return blob_index(blob, len, n1, rettv);
339 // The resulting variable is a byte value.
340 // If the index is too big or negative that is an error.
341 if (n1 < 0)
342 n1 = len + n1;
343 if (n1 < len && n1 >= 0)
344 {
345 int v = blob_get(blob, n1);
346
347 clear_tv(rettv);
348 rettv->v_type = VAR_NUMBER;
349 rettv->vval.v_number = v;
350 }
351 else
352 {
353 semsg(_(e_blob_index_out_of_range_nr), n1);
354 return FAIL;
355 }
356 }
357 return OK; 387 return OK;
358 } 388 }
359 389
360 /* 390 /*
361 * Check if "n1"- is a valid index for a blobl with length "bloblen". 391 * Check if "n1"- is a valid index for a blobl with length "bloblen".