From 20339de8b4bf22b18f64c1f5d56b8ebb623d5c2a Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Thu, 4 May 2006 04:45:41 +0000 Subject: [PATCH] more fixes/tests for empty strings git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9083 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/test-suite/li_std_string.i | 20 ++++++++++++++ .../test-suite/perl5/li_std_string_runme.pl | 26 +++++++++++++++++++ .../test-suite/python/li_std_string_runme.py | 11 ++++++++ .../test-suite/ruby/li_std_string_runme.rb | 20 ++++++++++++++ Lib/perl5/perlstrings.swg | 4 +-- Lib/typemaps/std_strings.swg | 2 +- 6 files changed, 80 insertions(+), 3 deletions(-) diff --git a/Examples/test-suite/li_std_string.i b/Examples/test-suite/li_std_string.i index 9916e7255..f0e7568fb 100644 --- a/Examples/test-suite/li_std_string.i +++ b/Examples/test-suite/li_std_string.i @@ -127,3 +127,23 @@ public: }; %} + +%inline %{ + std::string empty() { + return std::string(); + } + + char *c_empty() { + return ""; + } + + char *c_null() { + return 0; + } + + const char *get_null(const char *a) { + return a == 0 ? a : "non-null"; + } + + +%} diff --git a/Examples/test-suite/perl5/li_std_string_runme.pl b/Examples/test-suite/perl5/li_std_string_runme.pl index ec309bf4d..5cf90d26b 100644 --- a/Examples/test-suite/perl5/li_std_string_runme.pl +++ b/Examples/test-suite/perl5/li_std_string_runme.pl @@ -118,3 +118,29 @@ if ($gen1->testl(9234567890121111113) ne 9234567890121111114) { die ("ulonglong"); } + +if (li_std_string::empty() ne "") { + die ("empty"); +} + +if (li_std_string::c_empty() ne "") { + die ("c_empty"); +} + + +if (li_std_string::c_null() ne undef) { + die ("c_empty"); +} + + +if (li_std_string::get_null(li_std_string::c_null()) ne undef) { + die ("c_empty"); +} + +if (li_std_string::get_null(li_std_string::c_empty()) ne "non-null") { + die ("c_empty"); +} + +if (li_std_string::get_null(li_std_string::empty()) ne "non-null") { + die ("c_empty"); +} diff --git a/Examples/test-suite/python/li_std_string_runme.py b/Examples/test-suite/python/li_std_string_runme.py index 1360cacb5..ed79718d2 100644 --- a/Examples/test-suite/python/li_std_string_runme.py +++ b/Examples/test-suite/python/li_std_string_runme.py @@ -119,3 +119,14 @@ if li_std_string.test_reference_input("hello") != "hello": s = li_std_string.test_reference_inout("hello") if s != "hellohello": raise RuntimeError + + +if li_std_string.empty() != "": + raise RuntimeError + + +if li_std_string.c_empty() != "": + raise RuntimeError + +if li_std_string.c_null() != None: + raise RuntimeError diff --git a/Examples/test-suite/ruby/li_std_string_runme.rb b/Examples/test-suite/ruby/li_std_string_runme.rb index e9617f9bf..9e485b606 100644 --- a/Examples/test-suite/ruby/li_std_string_runme.rb +++ b/Examples/test-suite/ruby/li_std_string_runme.rb @@ -111,3 +111,23 @@ s = test_reference_inout("hello") if (s != "hellohello") raise RuntimeError end + + +if (empty() != "") + raise RuntimeError +end + +if (c_empty() != "") + raise RuntimeError +end + + +if (c_null() != nil) + raise RuntimeError +end + + +if (get_null(c_null()) != nil) + raise RuntimeError +end + diff --git a/Lib/perl5/perlstrings.swg b/Lib/perl5/perlstrings.swg index b3d1a9eb0..2d51317ff 100644 --- a/Lib/perl5/perlstrings.swg +++ b/Lib/perl5/perlstrings.swg @@ -43,8 +43,8 @@ SWIGINTERNINLINE SV * SWIG_FromCharPtrAndSize(const char* carray, size_t size) { SV *obj = sv_newmortal(); - if (size && carray) { - if (carray[size - 1] == 0) { + if (carray) { + if (size && (carray[size - 1] == 0)) { sv_setpv(obj, carray); } else { char *tmp = %new_array(size + 1, char); diff --git a/Lib/typemaps/std_strings.swg b/Lib/typemaps/std_strings.swg index 59211418f..b7e8f6d52 100644 --- a/Lib/typemaps/std_strings.swg +++ b/Lib/typemaps/std_strings.swg @@ -43,7 +43,7 @@ SWIG_From_dec(String)(const String& s) if (s.size()) { return SWIG_FromCharPtrAndSize(s.data(), s.size()); } else { - return SWIG_FromCharPtrAndSize(s.c_str(), 1); + return SWIG_FromCharPtrAndSize(s.c_str(), 0); } } }