Skip to content

Instantly share code, notes, and snippets.

@mattn

mattn/evalfunc.c Secret

Last active November 25, 2016 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/e4d83314ff8dcf935642a169af10994f to your computer and use it in GitHub Desktop.
Save mattn/e4d83314ff8dcf935642a169af10994f to your computer and use it in GitHub Desktop.
/*
* "trim()" function
*/
static void
f_trim(typval_T *argvars, typval_T *rettv)
{
char_u *s = get_tv_string_chk(&argvars[0]);
char_u *mask, *head, *trail, *last = NULL, *p;
if (argvars[1].v_type == VAR_STRING)
mask = get_tv_string_chk(&argvars[1]);
else
mask = (char_u*) " \t\v\r\n";
head = s;
while (*head)
{
#ifndef FEAT_MBYTE
p = vim_strchr(mask, *head);
if (!p)
break;
#else
int c1 = (*mb_ptr2char)(head), c2;
p = mask;
while (*p)
{
c2 = (*mb_ptr2char)(p);
if (c1 == c2)
break;
mb_ptr_adv(p);
}
if (!*p)
break;
#endif
mb_ptr_adv(head);
}
rettv->v_type = VAR_STRING;
if (!*head)
{
rettv->vval.v_string = vim_strsave((char_u*)"");
return;
}
trail = head;
while (*trail)
{
#ifndef FEAT_MBYTE
p = vim_strchr(mask, *trail);
mb_ptr_adv(trail);
if (p)
last = trail;
#else
int c1 = (*mb_ptr2char)(trail), c2;
p = mask;
while (*p)
{
c2 = (*mb_ptr2char)(p);
if (c1 == c2)
break;
mb_ptr_adv(p);
}
mb_ptr_adv(trail);
if (!*p)
{
last = trail;
p = NULL;
}
#endif
}
if (last)
rettv->vval.v_string = vim_strnsave(head, last - head);
else
rettv->vval.v_string = vim_strsave(head);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment