Add rstrip encoder for use in %rename.

This is like the strip encoder but strips the symbol's suffix instead
of the prefix.
This commit is contained in:
William S Fulton 2016-03-01 20:42:12 +00:00
commit 4a3e1fd44c
6 changed files with 72 additions and 0 deletions

View file

@ -1147,6 +1147,39 @@ String *Swig_string_strip(String *s) {
return ns;
}
/* -----------------------------------------------------------------------------
* Swig_string_rstrip()
*
* Strip given suffix from identifiers
*
* Printf(stderr,"%(rstrip:[Cls])s","HelloCls") -> Hello
* ----------------------------------------------------------------------------- */
String *Swig_string_rstrip(String *s) {
String *ns;
int len = Len(s);
if (!len) {
ns = NewString(s);
} else {
const char *cs = Char(s);
const char *ce = Strchr(cs, ']');
if (*cs != '[' || !ce) {
ns = NewString(s);
} else {
String *fmt = NewStringf("%%.%ds", ce-cs-1);
String *suffix = NewStringf(fmt, cs+1);
int suffix_len = Len(suffix);
if (0 == Strncmp(cs+len-suffix_len, suffix, suffix_len)) {
int copy_len = len-suffix_len-(ce+1-cs);
ns = NewStringWithSize(ce+1, copy_len);
} else {
ns = NewString(ce+1);
}
}
}
return ns;
}
/* -----------------------------------------------------------------------------
* Swig_offset_string()
*
@ -1403,6 +1436,7 @@ void Swig_init() {
DohEncoding("command", Swig_string_command);
DohEncoding("schemify", Swig_string_schemify);
DohEncoding("strip", Swig_string_strip);
DohEncoding("rstrip", Swig_string_rstrip);
DohEncoding("regex", Swig_string_regex);
/* aliases for the case encoders */