]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1144: no way to create raw strings from a blob v9.1.1144
authorBakudankun <bakudankun@gmail.com>
Sun, 23 Feb 2025 19:29:21 +0000 (20:29 +0100)
committerChristian Brabandt <cb@256bit.org>
Sun, 23 Feb 2025 19:29:21 +0000 (20:29 +0100)
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>
runtime/doc/builtin.txt
src/strings.c
src/testdir/test_functions.vim
src/version.c

index 344dfdfd8866d6d6ab61e07aeebdf89bb81fe3f1..80b8a9cc4ae36199192ac41c92040f104270c10e 100644 (file)
@@ -1,4 +1,4 @@
-*builtin.txt*  For Vim version 9.1.  Last change: 2025 Feb 17
+*builtin.txt*  For Vim version 9.1.  Last change: 2025 Feb 23
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -1309,10 +1309,14 @@ blob2str({blob} [, {options}])                          *blob2str()*
                items:
                    encoding    Decode the bytes in {blob} using this
                                encoding.  The value is a |String|.  See
-                               |encoding-names| for the supported values.
+                               |encoding-names| for the supported values
+                               (plus the special value "none").
                                                        *E1515*
-               An error is given and an empty List is returned if
-               an invalid byte sequence is encountered in {blob},
+               When current 'encoding' is "utf-8", an error is given and an
+               empty List is returned if an invalid byte sequence is
+               encountered in {blob}.  To suppress this validation and get
+               potentially invalid string, set "encoding" in {options} to
+               "none".
 
                Returns an empty List if blob is empty.
 
@@ -10645,7 +10649,8 @@ str2blob({list} [, {options}])                                  *str2blob()*
 
                The argument {options} is a |Dict| and supports the following
                items:
-                   encoding    Encode the characters using this encoding.
+                   encoding    Convert the characters using this encoding
+                               before making the Blob.
                                The value is a |String|.  See |encoding-names|
                                for the supported values.
 
index 5de3162bb0dd39d3a64ce050cdeeb3dc5d6afd01..9e39c45acbb19372b4aaae25276356254042dd8b 100644 (file)
@@ -1289,7 +1289,7 @@ f_blob2str(typval_T *argvars, typval_T *rettv)
     blob_T     *blob;
     int                blen;
     long       idx;
-    int                utf8_inuse = FALSE;
+    int                validate_utf8 = FALSE;
 
     if (check_for_blob_arg(argvars, 0) == FAIL
            || check_for_opt_dict_arg(argvars, 1) == FAIL)
@@ -1316,7 +1316,14 @@ f_blob2str(typval_T *argvars, typval_T *rettv)
     }
 
     if (STRCMP(p_enc, "utf-8") == 0 || STRCMP(p_enc, "utf8") == 0)
-       utf8_inuse = TRUE;
+       validate_utf8 = TRUE;
+
+    if (from_encoding != NULL && STRCMP(from_encoding, "none") == 0)
+    {
+       validate_utf8 = FALSE;
+       vim_free(from_encoding);
+       from_encoding = NULL;
+    }
 
     idx = 0;
     while (idx < blen)
@@ -1340,7 +1347,7 @@ f_blob2str(typval_T *argvars, typval_T *rettv)
            }
        }
 
-       if (utf8_inuse)
+       if (validate_utf8)
        {
            if (!utf_valid_string(converted_str, NULL))
            {
index 8ecc3a67106fc01199e0c0f83ab46c5e7cea307c..6928cda8d4ba5d0a1e88912a61760bdae933ed7b 100644 (file)
@@ -4446,6 +4446,9 @@ func Test_blob2str()
     call assert_equal(['a'], blob2str(0z61, test_null_dict()))
     call assert_equal(['a'], blob2str(0z61, {'encoding': test_null_string()}))
 
+    call assert_equal(["\x80"], blob2str(0z80, {'encoding': 'none'}))
+    call assert_equal(['a', "\x80"], blob2str(0z610A80, {'encoding': 'none'}))
+
     #" Invalid encoding
     call assert_fails("call blob2str(0z80)", "E1515: Unable to convert from 'utf-8' encoding")
     call assert_fails("call blob2str(0z610A80)", "E1515: Unable to convert from 'utf-8' encoding")
index 07bede551ada6f289ab32fd5892745a04205726c..5652fc34d34ae4c969db0633c6e01714e8a4832e 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1144,
 /**/
     1143,
 /**/