diff --git a/CHANGES.current b/CHANGES.current index 206b6bd68..77c628e9b 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,6 +5,16 @@ See the RELEASENOTES file for a summary of changes in each release. Version 3.0.9 (in progress) =========================== +2016-03-01: wsfulton + Add rstrip encoder for use in %rename. This is like the strip encoder but + strips the symbol's suffix instead of the prefix. The example below + will rename SomeThingCls to SomeThing and AnotherThingCls to AnotherThing: + + %rename("%(rstrip:[Cls])s") ""; + + class SomeThingCls {}; + struct AnotherThingCls {}; + 2016-02-07: kwwette [Octave] recognise various unary functions * Use __float__() for numeric conversions, e.g. when calling double() diff --git a/Doc/Manual/SWIG.html b/Doc/Manual/SWIG.html index 178247e42..bf1705d20 100644 --- a/Doc/Manual/SWIG.html +++ b/Doc/Manual/SWIG.html @@ -1888,6 +1888,13 @@ and a more descriptive one, but the two functions are otherwise equivalent: literally, e.g. %rename("strip:[wx]") wxPrintPrint + + rstrip:[suffix] + String without the given suffix or the original string if it doesn't + end with this suffix. Note that square brackets should be used + literally, e.g. %rename("rstrip:[Cls]") + PrintClsPrint + regex:/pattern/subst/ String after (Perl-like) regex substitution operation. This function diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 8b86ae0e7..9e626cb30 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -344,6 +344,7 @@ CPP_TEST_CASES += \ rename2 \ rename3 \ rename4 \ + rename_rstrip_encoder \ rename_scope \ rename_simple \ rename_strip_encoder \ diff --git a/Examples/test-suite/python/rename_rstrip_encoder_runme.py b/Examples/test-suite/python/rename_rstrip_encoder_runme.py new file mode 100644 index 000000000..84072f7f2 --- /dev/null +++ b/Examples/test-suite/python/rename_rstrip_encoder_runme.py @@ -0,0 +1,5 @@ +from rename_rstrip_encoder import * + +s = SomeThing() +a = AnotherThing() +a.DoClsX() diff --git a/Examples/test-suite/rename_rstrip_encoder.i b/Examples/test-suite/rename_rstrip_encoder.i new file mode 100644 index 000000000..4ecbe129f --- /dev/null +++ b/Examples/test-suite/rename_rstrip_encoder.i @@ -0,0 +1,15 @@ +%module rename_rstrip_encoder + +// strip the Cls suffix from all identifiers +%rename("%(rstrip:[Cls])s") ""; + +%inline %{ + +class SomeThingCls { +}; + +struct AnotherThingCls { + void DoClsXCls() {} +}; + +%} diff --git a/Source/Swig/misc.c b/Source/Swig/misc.c index df51a6b4e..95113f9d1 100644 --- a/Source/Swig/misc.c +++ b/Source/Swig/misc.c @@ -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 */