comparison src/eval.c @ 9589:bf204ab1ce7d v7.4.2072

commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd Author: Bram Moolenaar <Bram@vim.org> Date: Tue Jul 19 19:10:51 2016 +0200 patch 7.4.2072 Problem: substitute() does not support a Funcref argument. Solution: Support a Funcref like it supports a string starting with "\=".
author Christian Brabandt <cb@256bit.org>
date Tue, 19 Jul 2016 19:15:06 +0200
parents 05a56bbe34a1
children 172131507c85
comparison
equal deleted inserted replaced
9588:58eb7e194286 9589:bf204ab1ce7d
9767 sub = vim_strnsave(s, (int)(p - s)); 9767 sub = vim_strnsave(s, (int)(p - s));
9768 str = vim_strnsave(*fnamep, *fnamelen); 9768 str = vim_strnsave(*fnamep, *fnamelen);
9769 if (sub != NULL && str != NULL) 9769 if (sub != NULL && str != NULL)
9770 { 9770 {
9771 *usedlen = (int)(p + 1 - src); 9771 *usedlen = (int)(p + 1 - src);
9772 s = do_string_sub(str, pat, sub, flags); 9772 s = do_string_sub(str, pat, sub, NULL, flags);
9773 if (s != NULL) 9773 if (s != NULL)
9774 { 9774 {
9775 *fnamep = s; 9775 *fnamep = s;
9776 *fnamelen = (int)STRLEN(s); 9776 *fnamelen = (int)STRLEN(s);
9777 vim_free(*bufp); 9777 vim_free(*bufp);
9811 return valid; 9811 return valid;
9812 } 9812 }
9813 9813
9814 /* 9814 /*
9815 * Perform a substitution on "str" with pattern "pat" and substitute "sub". 9815 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
9816 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
9816 * "flags" can be "g" to do a global substitute. 9817 * "flags" can be "g" to do a global substitute.
9817 * Returns an allocated string, NULL for error. 9818 * Returns an allocated string, NULL for error.
9818 */ 9819 */
9819 char_u * 9820 char_u *
9820 do_string_sub( 9821 do_string_sub(
9821 char_u *str, 9822 char_u *str,
9822 char_u *pat, 9823 char_u *pat,
9823 char_u *sub, 9824 char_u *sub,
9825 typval_T *expr,
9824 char_u *flags) 9826 char_u *flags)
9825 { 9827 {
9826 int sublen; 9828 int sublen;
9827 regmatch_T regmatch; 9829 regmatch_T regmatch;
9828 int i; 9830 int i;
9871 * into. It will contain: 9873 * into. It will contain:
9872 * - The text up to where the match is. 9874 * - The text up to where the match is.
9873 * - The substituted text. 9875 * - The substituted text.
9874 * - The text after the match. 9876 * - The text after the match.
9875 */ 9877 */
9876 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE); 9878 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
9877 if (ga_grow(&ga, (int)((end - tail) + sublen - 9879 if (ga_grow(&ga, (int)((end - tail) + sublen -
9878 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL) 9880 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9879 { 9881 {
9880 ga_clear(&ga); 9882 ga_clear(&ga);
9881 break; 9883 break;
9883 9885
9884 /* copy the text up to where the match is */ 9886 /* copy the text up to where the match is */
9885 i = (int)(regmatch.startp[0] - tail); 9887 i = (int)(regmatch.startp[0] - tail);
9886 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i); 9888 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9887 /* add the substituted text */ 9889 /* add the substituted text */
9888 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data 9890 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
9889 + ga.ga_len + i, TRUE, TRUE, FALSE); 9891 + ga.ga_len + i, TRUE, TRUE, FALSE);
9890 ga.ga_len += i + sublen - 1; 9892 ga.ga_len += i + sublen - 1;
9891 tail = regmatch.endp[0]; 9893 tail = regmatch.endp[0];
9892 if (*tail == NUL) 9894 if (*tail == NUL)
9893 break; 9895 break;
9904 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data); 9906 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
9905 ga_clear(&ga); 9907 ga_clear(&ga);
9906 if (p_cpo == empty_option) 9908 if (p_cpo == empty_option)
9907 p_cpo = save_cpo; 9909 p_cpo = save_cpo;
9908 else 9910 else
9909 /* Darn, evaluating {sub} expression changed the value. */ 9911 /* Darn, evaluating {sub} expression or {expr} changed the value. */
9910 free_string_option(save_cpo); 9912 free_string_option(save_cpo);
9911 9913
9912 return ret; 9914 return ret;
9913 } 9915 }
9914 9916