diff --git a/Doc/Manual/Cpp0x.html b/Doc/Manual/Cpp0x.html index 83b54414b..b89b74ad0 100644 --- a/Doc/Manual/Cpp0x.html +++ b/Doc/Manual/Cpp0x.html @@ -460,23 +460,22 @@ arguments defined in the original template<> block:

7.2.18 New string literals

-

SWIG fully supports custom delimiters and unicode string -constants.

+

SWIG fully supports unicode string constants and raw string literals.

 // New string literals
-char      *a = "ABC";
-wstring wide = L"ABC";
-char      *b = u8"ABC";
-char16_t  *c = u"ABC";
-char32_t  *d = U"ABC";
+wstring         aa =  L"Wide string";
+const char     *bb = u8"UTF-8 string";
+const char16_t *cc =  u"UTF-16 string";
+const char32_t *dd =  U"UTF-32 string";
 
-// Custom String delimiter
-char       *e = R"XXX[to be or "not" to be [these are parenthesis], this is the question!]XXX";
-wstring wide2 = LR"XXX[to be or "not" to be [these are parenthesis], this is the question!]XXX";
-char       *f = u8R"XXX[to be or "not" to be [these are parenthesis], this is the question!]XXX";
-char16_t   *g = uR"XXX[to be or "not" to be [these are parenthesis], this is the question!]XXX";
-char32_t   *h = UR"XXX[to be or "not" to be [these are parenthesis], this is the question!]XXX";
+// Raw string literals
+const char      *xx =        ")I'm an \"ascii\" \\ string.";
+const char      *ee =   R"XXX()I'm an "ascii" \ string.)XXX"; // same as xx
+wstring          ff =  LR"XXX(I'm a "raw wide" \ string.)XXX";
+const char      *gg = u8R"XXX(I'm a "raw UTF-8" \ string.)XXX";
+const char16_t  *hh =  uR"XXX(I'm a "raw UTF-16" \ string.)XXX";
+const char32_t  *ii =  UR"XXX(I'm a "raw UTF-32" \ string.)XXX";
 

Note: SWIG currently incorrectly parses the odd number of double quotes diff --git a/Examples/test-suite/cpp0x_raw_string_literals.i b/Examples/test-suite/cpp0x_raw_string_literals.i index f5e804b78..612b2baea 100644 --- a/Examples/test-suite/cpp0x_raw_string_literals.i +++ b/Examples/test-suite/cpp0x_raw_string_literals.i @@ -8,14 +8,16 @@ This module also tests whether Swig correctly parses custom string delimiters. */ %module cpp0x_raw_string_literals -%warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) aa; %warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) bb; %warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) ee; -%warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) ff; +%warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) gg; +%warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) xx; %warnfilter(SWIGWARN_TYPEMAP_SWIGTYPELEAK_MSG) cc; %warnfilter(SWIGWARN_TYPEMAP_SWIGTYPELEAK_MSG) dd; -%warnfilter(SWIGWARN_TYPEMAP_SWIGTYPELEAK_MSG) gg; %warnfilter(SWIGWARN_TYPEMAP_SWIGTYPELEAK_MSG) hh; +%warnfilter(SWIGWARN_TYPEMAP_SWIGTYPELEAK_MSG) ii; + +%include %inline %{ #include @@ -38,27 +40,20 @@ struct URStruct { static const int UR = 100; }; -const char *aa = "ABC"; -wstring wide = L"ABC"; -const char *bb = u8"ABC"; -const char16_t *cc = u"ABC"; -const char32_t *dd = U"ABC"; +// New string literals +wstring aa = L"Wide string"; +const char *bb = u8"UTF-8 string"; +const char16_t *cc = u"UTF-16 string"; +const char32_t *dd = U"UTF-32 string"; %} /* Raw string literals */ - -#warning TODO: change SWIG support from old R"[ ... ]" to new R"( ... )" - -const char *ee = R"XXX[to be or "not" to be [square parenthesis] (round parenthesis), that is the question!]XXX"; -wstring wide2 = LR"XXX[to be or "not" to be [square parenthesis] (round parenthesis), that is the question!]XXX"; -const char *ff = u8R"XXX[to be or "not" to be [square parenthesis] (round parenthesis), that is the question!]XXX"; -const char16_t *gg = uR"XXX[to be or "not" to be [square parenthesis] (round parenthesis), that is the question!]XXX"; -const char32_t *hh = UR"XXX[to be or "not" to be [square parenthesis] (round parenthesis), that is the question!]XXX"; -%{ -const char *ee = R"XXX(to be or "not" to be [square parenthesis] (round parenthesis), that is the question!)XXX"; -wstring wide2 = LR"XXX(to be or "not" to be [square parenthesis] (round parenthesis), that is the question!)XXX"; -const char *ff = u8R"XXX(to be or "not" to be [square parenthesis] (round parenthesis), that is the question!)XXX"; -const char16_t *gg = uR"XXX(to be or "not" to be [square parenthesis] (round parenthesis), that is the question!)XXX"; -const char32_t *hh = UR"XXX(to be or "not" to be [square parenthesis] (round parenthesis), that is the question!)XXX"; +%inline %{ +const char *xx = ")I'm an \"ascii\" \\ string."; +const char *ee = R"XXX()I'm an "ascii" \ string.)XXX"; +wstring ff = LR"XXX(I'm a "raw wide" \ string.)XXX"; +const char *gg = u8R"XXX(I'm a "raw UTF-8" \ string.)XXX"; +const char16_t *hh = uR"XXX(I'm a "raw UTF-16" \ string.)XXX"; +const char32_t *ii = UR"XXX(I'm a "raw UTF-32" \ string.)XXX"; %} diff --git a/Examples/test-suite/python/cpp0x_raw_string_literals_runme.py b/Examples/test-suite/python/cpp0x_raw_string_literals_runme.py new file mode 100644 index 000000000..ea7d16550 --- /dev/null +++ b/Examples/test-suite/python/cpp0x_raw_string_literals_runme.py @@ -0,0 +1,50 @@ +from cpp0x_raw_string_literals import * + +if cvar.L != 100: + raise RuntimeError + +if cvar.u8 != 100: + raise RuntimeError + +if cvar.u != 100: + raise RuntimeError + +if UStruct.U != 100: + raise RuntimeError + + +if cvar.R != 100: + raise RuntimeError + +if cvar.LR != 100: + raise RuntimeError + +if cvar.u8R != 100: + raise RuntimeError + +if cvar.uR != 100: + raise RuntimeError + +if URStruct.UR != 100: + raise RuntimeError + + +if cvar.aa != "Wide string": + raise RuntimeError + +if cvar.bb != "UTF-8 string": + raise RuntimeError, cvar.wide + +if cvar.xx != ")I'm an \"ascii\" \\ string.": + raise RuntimeError, cvar.xx + +if cvar.ee != ")I'm an \"ascii\" \\ string.": + raise RuntimeError, cvar.ee + +if cvar.ff != "I'm a \"raw wide\" \\ string.": + raise RuntimeError, cvar.ff + +if cvar.gg != "I'm a \"raw UTF-8\" \\ string.": + raise RuntimeError, cvar.gg + + diff --git a/Source/Swig/scanner.c b/Source/Swig/scanner.c index 3f9f63a0f..ed849bf4d 100644 --- a/Source/Swig/scanner.c +++ b/Source/Swig/scanner.c @@ -673,7 +673,7 @@ static int look(Scanner * s) { Swig_error(cparse_file, cparse_start_line, "Unterminated string\n"); return SWIG_TOKEN_ERROR; } - else if (c == '[') { + else if (c == '(') { state = 20; } else { @@ -698,8 +698,8 @@ static int look(Scanner * s) { Delitem(s->text, DOH_END); get_escape(s); } - } else { /* Custom delimiter string: R"XXXX[value]XXXX" */ - if (c==']') { + } else { /* Custom delimiter string: R"XXXX(value)XXXX" */ + if (c==')') { int i=0; String *end_delimiter = NewStringEmpty(); while ((c = nextchar(s)) != 0 && c!='\"') { @@ -710,7 +710,7 @@ static int look(Scanner * s) { } if (Strcmp( str_delimiter, end_delimiter )==0) { - Delete( end_delimiter ); /* Correct end delimiter ]XXXX" occured */ + Delete( end_delimiter ); /* Correct end delimiter )XXXX" occured */ Delete( str_delimiter ); str_delimiter = 0; return SWIG_TOKEN_STRING;