mirror of
https://github.com/vim/vim.git
synced 2025-09-24 03:44:06 -04:00
patch 8.1.0765: string format of a Blob can't be parsed back
Problem: String format of a Blob can't be parsed back. Solution: Use 0z format.
This commit is contained in:
37
src/blob.c
37
src/blob.c
@@ -168,7 +168,7 @@ write_blob(FILE *fd, blob_T *blob)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Convert a blob to a readable form: "[0x11,0x34]"
|
* Convert a blob to a readable form: "0z00112233.44556677.8899"
|
||||||
*/
|
*/
|
||||||
char_u *
|
char_u *
|
||||||
blob2string(blob_T *blob, char_u **tofree, char_u *numbuf)
|
blob2string(blob_T *blob, char_u **tofree, char_u *numbuf)
|
||||||
@@ -179,20 +179,19 @@ blob2string(blob_T *blob, char_u **tofree, char_u *numbuf)
|
|||||||
if (blob == NULL)
|
if (blob == NULL)
|
||||||
{
|
{
|
||||||
*tofree = NULL;
|
*tofree = NULL;
|
||||||
return (char_u *)"[]";
|
return (char_u *)"0z";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store bytes in the growarray.
|
// Store bytes in the growarray.
|
||||||
ga_init2(&ga, 1, 4000);
|
ga_init2(&ga, 1, 4000);
|
||||||
ga_append(&ga, '[');
|
ga_concat(&ga, (char_u *)"0z");
|
||||||
for (i = 0; i < blob_len(blob); i++)
|
for (i = 0; i < blob_len(blob); i++)
|
||||||
{
|
{
|
||||||
if (i > 0)
|
if (i > 0 && (i & 3) == 0)
|
||||||
ga_concat(&ga, (char_u *)",");
|
ga_concat(&ga, (char_u *)".");
|
||||||
vim_snprintf((char *)numbuf, NUMBUFLEN, "0x%02X", (int)blob_get(blob, i));
|
vim_snprintf((char *)numbuf, NUMBUFLEN, "%02X", (int)blob_get(blob, i));
|
||||||
ga_concat(&ga, numbuf);
|
ga_concat(&ga, numbuf);
|
||||||
}
|
}
|
||||||
ga_append(&ga, ']');
|
|
||||||
*tofree = ga.ga_data;
|
*tofree = ga.ga_data;
|
||||||
return *tofree;
|
return *tofree;
|
||||||
}
|
}
|
||||||
@@ -207,24 +206,20 @@ string2blob(char_u *str)
|
|||||||
blob_T *blob = blob_alloc();
|
blob_T *blob = blob_alloc();
|
||||||
char_u *s = str;
|
char_u *s = str;
|
||||||
|
|
||||||
if (*s != '[')
|
if (s[0] != '0' || (s[1] != 'z' && s[1] != 'Z'))
|
||||||
goto failed;
|
goto failed;
|
||||||
s = skipwhite(s + 1);
|
s += 2;
|
||||||
while (*s != ']')
|
while (vim_isxdigit(*s))
|
||||||
{
|
{
|
||||||
if (s[0] != '0' || s[1] != 'x'
|
if (!vim_isxdigit(s[1]))
|
||||||
|| !vim_isxdigit(s[2]) || !vim_isxdigit(s[3]))
|
|
||||||
goto failed;
|
|
||||||
ga_append(&blob->bv_ga, (hex2nr(s[2]) << 4) + hex2nr(s[3]));
|
|
||||||
s += 4;
|
|
||||||
if (*s == ',')
|
|
||||||
s = skipwhite(s + 1);
|
|
||||||
else if (*s != ']')
|
|
||||||
goto failed;
|
goto failed;
|
||||||
|
ga_append(&blob->bv_ga, (hex2nr(s[0]) << 4) + hex2nr(s[1]));
|
||||||
|
s += 2;
|
||||||
|
if (*s == '.' && vim_isxdigit(s[1]))
|
||||||
|
++s;
|
||||||
}
|
}
|
||||||
s = skipwhite(s + 1);
|
if (*skipwhite(s) != NUL)
|
||||||
if (*s != NUL)
|
goto failed; // text after final digit
|
||||||
goto failed; // text after final ']'
|
|
||||||
|
|
||||||
++blob->bv_refcount;
|
++blob->bv_refcount;
|
||||||
return blob;
|
return blob;
|
||||||
|
@@ -4258,6 +4258,8 @@ eval7(
|
|||||||
if (blob != NULL)
|
if (blob != NULL)
|
||||||
ga_append(&blob->bv_ga,
|
ga_append(&blob->bv_ga,
|
||||||
(hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
|
(hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
|
||||||
|
if (bp[2] == '.' && vim_isxdigit(bp[3]))
|
||||||
|
++bp;
|
||||||
}
|
}
|
||||||
if (blob != NULL)
|
if (blob != NULL)
|
||||||
rettv_blob_set(rettv, blob);
|
rettv_blob_set(rettv, blob);
|
||||||
|
@@ -26,6 +26,12 @@ func Test_blob_create()
|
|||||||
call assert_fails('let b = 0z12345', 'E973:')
|
call assert_fails('let b = 0z12345', 'E973:')
|
||||||
|
|
||||||
call assert_equal(0z, test_null_blob())
|
call assert_equal(0z, test_null_blob())
|
||||||
|
|
||||||
|
let b = 0z001122.33445566.778899.aabbcc.dd
|
||||||
|
call assert_equal(0z00112233445566778899aabbccdd, b)
|
||||||
|
call assert_fails('let b = 0z1.1')
|
||||||
|
call assert_fails('let b = 0z.')
|
||||||
|
call assert_fails('let b = 0z001122.')
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
" assignment to a blob
|
" assignment to a blob
|
||||||
@@ -91,10 +97,13 @@ func Test_blob_get()
|
|||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
func Test_blob_to_string()
|
func Test_blob_to_string()
|
||||||
let b = 0zDEADBEEF
|
let b = 0z00112233445566778899aabbccdd
|
||||||
call assert_equal('[0xDE,0xAD,0xBE,0xEF]', string(b))
|
call assert_equal('0z00112233.44556677.8899AABB.CCDD', string(b))
|
||||||
|
call assert_equal(b, eval(string(b)))
|
||||||
|
call remove(b, 4, -1)
|
||||||
|
call assert_equal('0z00112233', string(b))
|
||||||
call remove(b, 0, 3)
|
call remove(b, 0, 3)
|
||||||
call assert_equal('[]', string(b))
|
call assert_equal('0z', string(b))
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
func Test_blob_compare()
|
func Test_blob_compare()
|
||||||
|
@@ -791,6 +791,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
765,
|
||||||
/**/
|
/**/
|
||||||
764,
|
764,
|
||||||
/**/
|
/**/
|
||||||
|
Reference in New Issue
Block a user