comparison src/userfunc.c @ 26721:9c9b8d95b05f v8.2.3889

patch 8.2.3889: duplicate code for translating script-local function name Commit: https://github.com/vim/vim/commit/e7f4abd38b6e05100c699900c8f87281e363beb2 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Fri Dec 24 20:47:38 2021 +0000 patch 8.2.3889: duplicate code for translating script-local function name Problem: Duplicate code for translating script-local function name. Solution: Move the code to get_scriptlocal_funcname(). (Yegappan Lakshmanan, closes #9393)
author Bram Moolenaar <Bram@vim.org>
date Fri, 24 Dec 2021 22:00:03 +0100
parents fac6673086df
children a8a4e1e7b111
comparison
equal deleted inserted replaced
26720:7d4b0da6a26b 26721:9c9b8d95b05f
3874 } 3874 }
3875 return NULL; 3875 return NULL;
3876 } 3876 }
3877 3877
3878 /* 3878 /*
3879 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
3880 * current script ID and returns the expanded function name. The caller should
3881 * free the returned name. If not called from a script context or the function
3882 * name doesn't start with these prefixes, then returns NULL.
3883 * This doesn't check whether the script-local function exists or not.
3884 */
3885 char_u *
3886 get_scriptlocal_funcname(char_u *funcname)
3887 {
3888 char sid_buf[25];
3889 int off;
3890 char_u *newname;
3891
3892 if (funcname == NULL)
3893 return NULL;
3894
3895 if (STRNCMP(funcname, "s:", 2) != 0
3896 && STRNCMP(funcname, "<SID>", 5) != 0)
3897 // The function name is not a script-local function name
3898 return NULL;
3899
3900 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3901 {
3902 emsg(_(e_using_sid_not_in_script_context));
3903 return NULL;
3904 }
3905 // Expand s: prefix into <SNR>nr_<name>
3906 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
3907 (long)current_sctx.sc_sid);
3908 off = *funcname == 's' ? 2 : 5;
3909 newname = alloc(STRLEN(sid_buf) + STRLEN(funcname + off) + 1);
3910 if (newname == NULL)
3911 return NULL;
3912 STRCPY(newname, sid_buf);
3913 STRCAT(newname, funcname + off);
3914
3915 return newname;
3916 }
3917
3918 /*
3879 * Call trans_function_name(), except that a lambda is returned as-is. 3919 * Call trans_function_name(), except that a lambda is returned as-is.
3880 * Returns the name in allocated memory. 3920 * Returns the name in allocated memory.
3881 */ 3921 */
3882 char_u * 3922 char_u *
3883 save_function_name( 3923 save_function_name(