diff options
Diffstat (limited to 'src/nvim/mbyte.c')
-rw-r--r-- | src/nvim/mbyte.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index ab787524a9..78204b22a8 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -2361,6 +2361,34 @@ static char *iconv_string(const vimconv_T *const vcp, const char *str, size_t sl return result; } +/// iconv() function +void f_iconv(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + vimconv_T vimconv; + + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; + + const char *const str = tv_get_string(&argvars[0]); + char buf1[NUMBUFLEN]; + char *const from = enc_canonize(enc_skip((char *)tv_get_string_buf(&argvars[1], buf1))); + char buf2[NUMBUFLEN]; + char *const to = enc_canonize(enc_skip((char *)tv_get_string_buf(&argvars[2], buf2))); + vimconv.vc_type = CONV_NONE; + convert_setup(&vimconv, from, to); + + // If the encodings are equal, no conversion needed. + if (vimconv.vc_type == CONV_NONE) { + rettv->vval.v_string = xstrdup(str); + } else { + rettv->vval.v_string = string_convert(&vimconv, (char *)str, NULL); + } + + convert_setup(&vimconv, NULL, NULL); + xfree(from); + xfree(to); +} + /// Setup "vcp" for conversion from "from" to "to". /// The names must have been made canonical with enc_canonize(). /// vcp->vc_type must have been initialized to CONV_NONE. |