Skip to content

Commit b3854bf

Browse files
Bakudankunchrisbra
authored andcommitted
patch 9.1.1144: no way to create raw strings from a blob
Problem: no way to create raw strings from a blob Solution: support the "encoding": "none" option to create raw strings (which may be invalid!) (Bakudankun) closes: #16666 Signed-off-by: Bakudankun <bakudankun@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent e0029da commit b3854bf

4 files changed

Lines changed: 25 additions & 8 deletions

File tree

runtime/doc/builtin.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*builtin.txt* For Vim version 9.1. Last change: 2025 Feb 17
1+
*builtin.txt* For Vim version 9.1. Last change: 2025 Feb 23
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1309,10 +1309,14 @@ blob2str({blob} [, {options}]) *blob2str()*
13091309
items:
13101310
encoding Decode the bytes in {blob} using this
13111311
encoding. The value is a |String|. See
1312-
|encoding-names| for the supported values.
1312+
|encoding-names| for the supported values
1313+
(plus the special value "none").
13131314
*E1515*
1314-
An error is given and an empty List is returned if
1315-
an invalid byte sequence is encountered in {blob},
1315+
When current 'encoding' is "utf-8", an error is given and an
1316+
empty List is returned if an invalid byte sequence is
1317+
encountered in {blob}. To suppress this validation and get
1318+
potentially invalid string, set "encoding" in {options} to
1319+
"none".
13161320

13171321
Returns an empty List if blob is empty.
13181322

@@ -10645,7 +10649,8 @@ str2blob({list} [, {options}]) *str2blob()*
1064510649

1064610650
The argument {options} is a |Dict| and supports the following
1064710651
items:
10648-
encoding Encode the characters using this encoding.
10652+
encoding Convert the characters using this encoding
10653+
before making the Blob.
1064910654
The value is a |String|. See |encoding-names|
1065010655
for the supported values.
1065110656

src/strings.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ f_blob2str(typval_T *argvars, typval_T *rettv)
12891289
blob_T *blob;
12901290
int blen;
12911291
long idx;
1292-
int utf8_inuse = FALSE;
1292+
int validate_utf8 = FALSE;
12931293

12941294
if (check_for_blob_arg(argvars, 0) == FAIL
12951295
|| check_for_opt_dict_arg(argvars, 1) == FAIL)
@@ -1316,7 +1316,14 @@ f_blob2str(typval_T *argvars, typval_T *rettv)
13161316
}
13171317

13181318
if (STRCMP(p_enc, "utf-8") == 0 || STRCMP(p_enc, "utf8") == 0)
1319-
utf8_inuse = TRUE;
1319+
validate_utf8 = TRUE;
1320+
1321+
if (from_encoding != NULL && STRCMP(from_encoding, "none") == 0)
1322+
{
1323+
validate_utf8 = FALSE;
1324+
vim_free(from_encoding);
1325+
from_encoding = NULL;
1326+
}
13201327

13211328
idx = 0;
13221329
while (idx < blen)
@@ -1340,7 +1347,7 @@ f_blob2str(typval_T *argvars, typval_T *rettv)
13401347
}
13411348
}
13421349

1343-
if (utf8_inuse)
1350+
if (validate_utf8)
13441351
{
13451352
if (!utf_valid_string(converted_str, NULL))
13461353
{

src/testdir/test_functions.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4446,6 +4446,9 @@ func Test_blob2str()
44464446
call assert_equal(['a'], blob2str(0z61, test_null_dict()))
44474447
call assert_equal(['a'], blob2str(0z61, {'encoding': test_null_string()}))
44484448

4449+
call assert_equal(["\x80"], blob2str(0z80, {'encoding': 'none'}))
4450+
call assert_equal(['a', "\x80"], blob2str(0z610A80, {'encoding': 'none'}))
4451+
44494452
#" Invalid encoding
44504453
call assert_fails("call blob2str(0z80)", "E1515: Unable to convert from 'utf-8' encoding")
44514454
call assert_fails("call blob2str(0z610A80)", "E1515: Unable to convert from 'utf-8' encoding")

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1144,
707709
/**/
708710
1143,
709711
/**/

0 commit comments

Comments
 (0)