patch 8.2.3818: cannot filter or map characters in a string

Problem:    Cannot filter or map characters in a string.
Solution:   Make filter() and map() work on a string. (Naruhiko Nishino,
            closes #9327)
diff --git a/src/errors.h b/src/errors.h
index 452ff45..156e194 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -758,3 +758,5 @@
 	INIT(= N_("E1248: Closure called from invalid context"));
 EXTERN char e_highlight_group_name_too_long[]
 	INIT(= N_("E1249: Highlight group name too long"));
+EXTERN char e_argument_of_str_must_be_list_string_dictionary_or_blob[]
+	INIT(= N_("E1250: Argument of %s must be a List, String, Dictionary or Blob"));
diff --git a/src/list.c b/src/list.c
index ca80e41..ff22de6 100644
--- a/src/list.c
+++ b/src/list.c
@@ -2329,9 +2329,15 @@
 			    && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
 	    goto theend;
     }
+    else if (argvars[0].v_type == VAR_STRING)
+    {
+	rettv->v_type = VAR_STRING;
+	rettv->vval.v_string = NULL;
+    }
     else
     {
-	semsg(_(e_listdictblobarg), func_name);
+	semsg(_(e_argument_of_str_must_be_list_string_dictionary_or_blob),
+								    func_name);
 	goto theend;
     }
 
@@ -2480,6 +2486,66 @@
 		++idx;
 	    }
 	}
+	else if (argvars[0].v_type == VAR_STRING)
+	{
+	    char_u	*p;
+	    typval_T	tv;
+	    garray_T	ga;
+	    char_u	buf[MB_MAXBYTES + 1];
+	    int		len;
+
+	    // set_vim_var_nr() doesn't set the type
+	    set_vim_var_type(VV_KEY, VAR_NUMBER);
+
+	    ga_init2(&ga, (int)sizeof(char), 80);
+	    for (p = tv_get_string(&argvars[0]); *p != NUL; p += len)
+	    {
+	        typval_T newtv;
+
+		if (has_mbyte)
+		    len = mb_ptr2len(p);
+		else
+		    len = 1;
+
+		STRNCPY(buf, p, len);
+		buf[len] = NUL;
+
+		tv.v_type = VAR_STRING;
+		tv.vval.v_string = vim_strsave(buf);
+
+		set_vim_var_nr(VV_KEY, idx);
+		if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
+								   || did_emsg)
+		    break;
+		if (did_emsg)
+		{
+		    clear_tv(&newtv);
+		    clear_tv(&tv);
+		    break;
+		}
+		else if (filtermap != FILTERMAP_FILTER)
+		{
+		    if (newtv.v_type != VAR_STRING)
+		    {
+			clear_tv(&newtv);
+			clear_tv(&tv);
+			emsg(_(e_stringreq));
+			break;
+		    }
+		    else
+			ga_concat(&ga, newtv.vval.v_string);
+		}
+		else if (!rem)
+		    ga_concat(&ga, tv.vval.v_string);
+
+		clear_tv(&newtv);
+		clear_tv(&tv);
+
+	        ++idx;
+	    }
+	    ga_append(&ga, NUL);
+	    rettv->vval.v_string = ga.ga_data;
+	}
 	else // argvars[0].v_type == VAR_LIST
 	{
 	    int	    prev_lock = l->lv_lock;
diff --git a/src/testdir/test_filter_map.vim b/src/testdir/test_filter_map.vim
index a9ff35e..947e6a2 100644
--- a/src/testdir/test_filter_map.vim
+++ b/src/testdir/test_filter_map.vim
@@ -93,8 +93,6 @@
 func Test_map_filter_fails()
   call assert_fails('call map([1], "42 +")', 'E15:')
   call assert_fails('call filter([1], "42 +")', 'E15:')
-  call assert_fails("let l = map('abc', '\"> \" . v:val')", 'E896:')
-  call assert_fails("let l = filter('abc', '\"> \" . v:val')", 'E896:')
   call assert_fails("let l = filter([1, 2, 3], '{}')", 'E728:')
   call assert_fails("let l = filter({'k' : 10}, '{}')", 'E728:')
   call assert_fails("let l = filter([1, 2], {})", 'E731:')
@@ -145,4 +143,68 @@
   call assert_equal(0z129956, bout)
 endfunc
 
+func Test_filter_map_string()
+  let s = "abc"
+
+  " filter()
+  call filter(s, '"b" != v:val')
+  call assert_equal(s, s)
+  call assert_equal('ac', filter('abc', '"b" != v:val'))
+  call assert_equal('あいうえお', filter('あxいxうxえxお', '"x" != v:val'))
+  call assert_equal('あa😊💕💕b💕', filter('あxax😊x💕💕b💕x', '"x" != v:val'))
+  call assert_equal('xxxx', filter('あxax😊x💕💕b💕x', '"x" == v:val'))
+  let t = "%),:;>?]}’”†‡…‰,‱‼⁇⁈⁉℃℉,、。〉》」,』】〕〗〙〛,!),.:,;?,]}"
+  let u = "%):;>?]}’”†‡…‰‱‼⁇⁈⁉℃℉、。〉》」』】〕〗〙〛!),.:;?]}"
+  call assert_equal(u, filter(t, '"," != v:val'))
+  call assert_equal('', filter('abc', '0'))
+  call assert_equal('ac', filter('abc', { i, x -> "b" != x }))
+  call assert_equal('あいうえお', filter('あxいxうxえxお', { i, x -> "x" != x }))
+  call assert_equal('', filter('abc', { i, x -> v:false }))
+
+  " map()
+  call map(s, 'nr2char(char2nr(v:val) + 2)')
+  call assert_equal(s, s)
+  call assert_equal('cde', map('abc', 'nr2char(char2nr(v:val) + 2)'))
+  call assert_equal('[あ][i][う][え][お]', map('あiうえお', '"[" .. v:val .. "]"'))
+  call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', map('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"'))
+  call assert_equal('', map('abc', '""'))
+  call assert_equal('cde', map('abc', { i, x -> nr2char(char2nr(x) + 2) }))
+  call assert_equal('[あ][i][う][え][お]', map('あiうえお', { i, x -> '[' .. x .. ']' }))
+  call assert_equal('', map('abc', { i, x -> '' }))
+
+  " mapnew()
+  call mapnew(s, 'nr2char(char2nr(v:val) + 2)')
+  call assert_equal(s, s)
+  call assert_equal('cde', mapnew('abc', 'nr2char(char2nr(v:val) + 2)'))
+  call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', '"[" .. v:val .. "]"'))
+  call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', mapnew('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"'))
+  call assert_equal('', mapnew('abc', '""'))
+  call assert_equal('cde', mapnew('abc', { i, x -> nr2char(char2nr(x) + 2) }))
+  call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', { i, x -> '[' .. x .. ']' }))
+  call assert_equal('', mapnew('abc', { i, x -> '' }))
+
+  " map() and filter()
+  call assert_equal('[あ][⁈][a][😊][⁉][💕][💕][b][💕]', map(filter('あx⁈ax😊x⁉💕💕b💕x', '"x" != v:val'), '"[" .. v:val .. "]"'))
+
+  " patterns-composing(\Z)
+  call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', {i,x -> x =~ '\Z' .. nr2char(0x0960) }))
+  call assert_equal('àà', filter('càt,càt', {i,x -> x =~ '\Za' }))
+  call assert_equal('ÅÅ', filter('Åström,Åström', {i,x -> x =~ '\Z' .. nr2char(0xc5) }))
+  call assert_equal('öö', filter('Åström,Åström', {i,x -> x =~ '\Z' .. nr2char(0xf6) }))
+  call assert_equal('ऊ@ॡ', map('ऊॠॡ', {i,x -> x =~ '\Z' .. nr2char(0x0960) ? '@' : x }))
+  call assert_equal('c@t', map('càt', {i,x -> x =~ '\Za' ? '@' : x }))
+  call assert_equal('@ström', map('Åström', {i,x -> x =~ '\Z' .. nr2char(0xc5) ? '@' : x }))
+  call assert_equal('Åstr@m', map('Åström', {i,x -> x =~ '\Z' .. nr2char(0xf6) ? '@' : x }))
+
+  " patterns-composing(\%C)
+  call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', {i,x -> x =~ nr2char(0x0960) .. '\%C' }))
+  call assert_equal('àà', filter('càt,càt', {i,x -> x =~ 'a' .. '\%C' }))
+  call assert_equal('ÅÅ', filter('Åström,Åström', {i,x -> x =~ nr2char(0xc5) .. '\%C' }))
+  call assert_equal('öö', filter('Åström,Åström', {i,x -> x =~ nr2char(0xf6) .. '\%C' }))
+  call assert_equal('ऊ@ॡ', map('ऊॠॡ', {i,x -> x =~ nr2char(0x0960) .. '\%C' ? '@' : x }))
+  call assert_equal('c@t', map('càt', {i,x -> x =~ 'a' .. '\%C' ? '@' : x }))
+  call assert_equal('@ström', map('Åström', {i,x -> x =~ nr2char(0xc5) .. '\%C' ? '@' : x }))
+  call assert_equal('Åstr@m', map('Åström', {i,x -> x =~ nr2char(0xf6) .. '\%C' ? '@' : x }))
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/version.c b/src/version.c
index e9836ef..d48206f 100644
--- a/src/version.c
+++ b/src/version.c
@@ -750,6 +750,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    3818,
+/**/
     3817,
 /**/
     3816,