When wrapping C++ code, generate code which uses

std::string::assign(PTR, LEN) rather than assigning
std::string(PTR, LEN).  Using assign generates more efficient code
(tested with GCC 4.1.2).


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9936 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Olly Betts 2007-09-17 20:10:57 +00:00
commit c836c81acb
11 changed files with 46 additions and 45 deletions

View file

@ -2,7 +2,8 @@ Version 1.3.32 (in progress)
============================ ============================
09/17/2007: olly 09/17/2007: olly
[PHP] Use std::string::assign(PTR, LEN) rather than assigning When wrapping C++ code, generate code which uses
std::string::assign(PTR, LEN) rather than assigning
std::string(PTR, LEN). Using assign generates more efficient code std::string(PTR, LEN). Using assign generates more efficient code
(tested with GCC 4.1.2). (tested with GCC 4.1.2).

View file

@ -97,13 +97,13 @@ namespace std {
// automatically convert constant std::strings to cl:strings // automatically convert constant std::strings to cl:strings
%typemap(ctype) string "char *"; %typemap(ctype) string "char *";
%typemap(in) string "$1 = string($input);"; %typemap(in) string "$1.assign($input);";
%typemap(out) string "$result = (char *)(&$1)->c_str();"; %typemap(out) string "$result = (char *)(&$1)->c_str();";
%typemap(lisptype) string "cl:string"; %typemap(lisptype) string "cl:string";
%typemap(lout) string "(cl::setq ACL_ffresult $body)"; %typemap(lout) string "(cl::setq ACL_ffresult $body)";
%typemap(ctype) wstring "wchar_t *"; %typemap(ctype) wstring "wchar_t *";
%typemap(in) wstring "$1 = string($input);"; %typemap(in) wstring "$1.assign($input);";
%typemap(out) wstring "$result = (wchar_t *)(&$1)->c_str();"; %typemap(out) wstring "$result = (wchar_t *)(&$1)->c_str();";
%typemap(lisptype) wstring "cl:string"; %typemap(lisptype) wstring "cl:string";
%typemap(lout) wstring "(cl::setq ACL_ffresult (excl:native-to-string $body %typemap(lout) wstring "(cl::setq ACL_ffresult (excl:native-to-string $body
@ -112,7 +112,7 @@ namespace std {
/* Overloading check */ /* Overloading check */
// %typemap(in) string { // %typemap(in) string {
// if (caml_ptr_check($input)) // if (caml_ptr_check($input))
// $1 = std::string((char *)caml_ptr_val($input,0), // $1.assign((char *)caml_ptr_val($input,0),
// caml_string_len($input)); // caml_string_len($input));
// else // else
// SWIG_exception(SWIG_TypeError, "string expected"); // SWIG_exception(SWIG_TypeError, "string expected");
@ -120,7 +120,7 @@ namespace std {
// %typemap(in) const string & (std::string temp) { // %typemap(in) const string & (std::string temp) {
// if (caml_ptr_check($input)) { // if (caml_ptr_check($input)) {
// temp = std::string((char *)caml_ptr_val($input,0), // temp.assign((char *)caml_ptr_val($input,0),
// caml_string_len($input)); // caml_string_len($input));
// $1 = &temp; // $1 = &temp;
// } else { // } else {
@ -130,7 +130,7 @@ namespace std {
// %typemap(in) string & (std::string temp) { // %typemap(in) string & (std::string temp) {
// if (caml_ptr_check($input)) { // if (caml_ptr_check($input)) {
// temp = std::string((char *)caml_ptr_val($input,0), // temp.assign((char *)caml_ptr_val($input,0),
// caml_string_len($input)); // caml_string_len($input));
// $1 = &temp; // $1 = &temp;
// } else { // } else {
@ -158,7 +158,7 @@ namespace std {
// } // }
// %typemap(directorout) string { // %typemap(directorout) string {
// $result = std::string((char *)caml_ptr_val($input,0), // $result.assign((char *)caml_ptr_val($input,0),
// caml_string_len($input)); // caml_string_len($input));
// } // }

View file

@ -35,14 +35,14 @@ namespace std {
%typemap(in) string (char* tempptr) { %typemap(in) string (char* tempptr) {
if ($input == C_SCHEME_FALSE) { if ($input == C_SCHEME_FALSE) {
$1 = std::string(); $1.resize(0);
} else { } else {
if (!C_swig_is_string ($input)) { if (!C_swig_is_string ($input)) {
swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE,
"Argument #$argnum is not a string"); "Argument #$argnum is not a string");
} }
tempptr = SWIG_MakeString($input); tempptr = SWIG_MakeString($input);
$1 = std::string(tempptr); $1.assign(tempptr);
if (tempptr) SWIG_free(tempptr); if (tempptr) SWIG_free(tempptr);
} }
} }
@ -51,7 +51,7 @@ namespace std {
char* tempptr) { char* tempptr) {
if ($input == C_SCHEME_FALSE) { if ($input == C_SCHEME_FALSE) {
temp = std::string(); temp.resize(0);
$1 = &temp; $1 = &temp;
} else { } else {
if (!C_swig_is_string ($input)) { if (!C_swig_is_string ($input)) {
@ -59,7 +59,7 @@ namespace std {
"Argument #$argnum is not a string"); "Argument #$argnum is not a string");
} }
tempptr = SWIG_MakeString($input); tempptr = SWIG_MakeString($input);
temp = std::string(tempptr); temp.assign(tempptr);
if (tempptr) SWIG_free(tempptr); if (tempptr) SWIG_free(tempptr);
$1 = &temp; $1 = &temp;
} }
@ -79,7 +79,7 @@ namespace std {
%typemap(varin) string { %typemap(varin) string {
if ($input == C_SCHEME_FALSE) { if ($input == C_SCHEME_FALSE) {
$1 = std::string(); $1.resize(0);
} else { } else {
char *tempptr; char *tempptr;
if (!C_swig_is_string ($input)) { if (!C_swig_is_string ($input)) {
@ -87,7 +87,7 @@ namespace std {
"Argument #$argnum is not a string"); "Argument #$argnum is not a string");
} }
tempptr = SWIG_MakeString($input); tempptr = SWIG_MakeString($input);
$1 = std::string(tempptr); $1.assign(tempptr);
if (tempptr) SWIG_free(tempptr); if (tempptr) SWIG_free(tempptr);
} }
} }

View file

@ -35,7 +35,7 @@ class string;
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null string", 0); SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null string", 0);
return $null; return $null;
} }
$1 = std::string($input); %} $1.assign($input); %}
%typemap(out) string %{ $result = SWIG_csharp_string_callback($1.c_str()); %} %typemap(out) string %{ $result = SWIG_csharp_string_callback($1.c_str()); %}
%typemap(directorout, canthrow=1) string %typemap(directorout, canthrow=1) string
@ -43,7 +43,7 @@ class string;
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null string", 0); SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null string", 0);
return $null; return $null;
} }
$result = std::string($input); %} $result.assign($input); %}
%typemap(directorin) string %{ $input = SWIG_csharp_string_callback($1.c_str()); %} %typemap(directorin) string %{ $input = SWIG_csharp_string_callback($1.c_str()); %}

View file

@ -36,7 +36,7 @@ class wstring;
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0); SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0);
return $null; return $null;
} }
$1 = std::wstring($input); %} $1.assign($input); %}
%typemap(out) wstring %{ $result = SWIG_csharp_wstring_callback($1.c_str()); %} %typemap(out) wstring %{ $result = SWIG_csharp_wstring_callback($1.c_str()); %}
%typemap(directorout, canthrow=1) wstring %typemap(directorout, canthrow=1) wstring
@ -44,7 +44,7 @@ class wstring;
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0); SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0);
return $null; return $null;
} }
$result = std::wstring($input); %} $result.assign($input); %}
%typemap(directorin) wstring %{ $input = SWIG_csharp_wstring_callback($1.c_str()); %} %typemap(directorin) wstring %{ $input = SWIG_csharp_wstring_callback($1.c_str()); %}

View file

@ -32,7 +32,7 @@ namespace std {
%typemap(in) string (char* tempptr) { %typemap(in) string (char* tempptr) {
if (gh_string_p($input)) { if (gh_string_p($input)) {
tempptr = SWIG_scm2str($input); tempptr = SWIG_scm2str($input);
$1 = std::string(tempptr); $1.assign(tempptr);
if (tempptr) SWIG_free(tempptr); if (tempptr) SWIG_free(tempptr);
} else { } else {
SWIG_exception(SWIG_TypeError, "string expected"); SWIG_exception(SWIG_TypeError, "string expected");
@ -43,7 +43,7 @@ namespace std {
char* tempptr) { char* tempptr) {
if (gh_string_p($input)) { if (gh_string_p($input)) {
tempptr = SWIG_scm2str($input); tempptr = SWIG_scm2str($input);
temp = std::string(tempptr); temp.assign(tempptr);
if (tempptr) SWIG_free(tempptr); if (tempptr) SWIG_free(tempptr);
$1 = &temp; $1 = &temp;
} else { } else {
@ -76,7 +76,7 @@ namespace std {
%typemap(varin) string { %typemap(varin) string {
if (gh_string_p($input)) { if (gh_string_p($input)) {
char *tempptr = SWIG_scm2str($input); char *tempptr = SWIG_scm2str($input);
$1 = std::string(tempptr); $1.assign(tempptr);
if (tempptr) SWIG_free(tempptr); if (tempptr) SWIG_free(tempptr);
} else { } else {
SWIG_exception(SWIG_TypeError, "string expected"); SWIG_exception(SWIG_TypeError, "string expected");

View file

@ -36,7 +36,7 @@ class string;
} }
const char *$1_pstr = (const char *)jenv->GetStringUTFChars($input, 0); const char *$1_pstr = (const char *)jenv->GetStringUTFChars($input, 0);
if (!$1_pstr) return $null; if (!$1_pstr) return $null;
$1 = std::string($1_pstr); $1.assign($1_pstr);
jenv->ReleaseStringUTFChars($input, $1_pstr); %} jenv->ReleaseStringUTFChars($input, $1_pstr); %}
%typemap(directorout) string %typemap(directorout) string
@ -46,7 +46,7 @@ class string;
} }
const char *$1_pstr = (const char *)jenv->GetStringUTFChars($input, 0); const char *$1_pstr = (const char *)jenv->GetStringUTFChars($input, 0);
if (!$1_pstr) return $null; if (!$1_pstr) return $null;
$result = std::string($1_pstr); $result.assign($1_pstr);
jenv->ReleaseStringUTFChars($input, $1_pstr); %} jenv->ReleaseStringUTFChars($input, $1_pstr); %}
%typemap(directorin,descriptor="Ljava/lang/String;") string %typemap(directorin,descriptor="Ljava/lang/String;") string

View file

@ -41,7 +41,7 @@ class wstring;
for (jsize i = 0; i < $1_len; ++i) { for (jsize i = 0; i < $1_len; ++i) {
conv_buf[i] = $1_pstr[i]; conv_buf[i] = $1_pstr[i];
} }
$1 = std::wstring(conv_buf, $1_len); $1.assign(conv_buf, $1_len);
delete [] conv_buf; delete [] conv_buf;
} }
jenv->ReleaseStringChars($input, $1_pstr); jenv->ReleaseStringChars($input, $1_pstr);
@ -60,7 +60,7 @@ class wstring;
for (jsize i = 0; i < $1_len; ++i) { for (jsize i = 0; i < $1_len; ++i) {
conv_buf[i] = $1_pstr[i]; conv_buf[i] = $1_pstr[i];
} }
$result = std::wstring(conv_buf, $1_len); $result.assign(conv_buf, $1_len);
delete [] conv_buf; delete [] conv_buf;
} }
jenv->ReleaseStringChars($input, $1_pstr); jenv->ReleaseStringChars($input, $1_pstr);
@ -112,7 +112,7 @@ class wstring;
for (jsize i = 0; i < $1_len; ++i) { for (jsize i = 0; i < $1_len; ++i) {
conv_buf[i] = $1_pstr[i]; conv_buf[i] = $1_pstr[i];
} }
$1_str = std::wstring(conv_buf, $1_len); $1_str.assign(conv_buf, $1_len);
delete [] conv_buf; delete [] conv_buf;
} }
$1 = &$1_str; $1 = &$1_str;
@ -133,7 +133,7 @@ class wstring;
for (jsize i = 0; i < $1_len; ++i) { for (jsize i = 0; i < $1_len; ++i) {
conv_buf[i] = $1_pstr[i]; conv_buf[i] = $1_pstr[i];
} }
$1_str = std::wstring(conv_buf, $1_len); $1_str.assign(conv_buf, $1_len);
delete [] conv_buf; delete [] conv_buf;
} }
$result = &$1_str; $result = &$1_str;

View file

@ -33,14 +33,14 @@ namespace std {
%typemap(in) string { %typemap(in) string {
if (SCHEME_STRINGP($input)) if (SCHEME_STRINGP($input))
$1 = std::string(SCHEME_STR_VAL($input)); $1.assign(SCHEME_STR_VAL($input));
else else
SWIG_exception(SWIG_TypeError, "string expected"); SWIG_exception(SWIG_TypeError, "string expected");
} }
%typemap(in) const string & (std::string temp) { %typemap(in) const string & (std::string temp) {
if (SCHEME_STRINGP($input)) { if (SCHEME_STRINGP($input)) {
temp = std::string(SCHEME_STR_VAL($input)); temp.assign(SCHEME_STR_VAL($input));
$1 = &temp; $1 = &temp;
} else { } else {
SWIG_exception(SWIG_TypeError, "string expected"); SWIG_exception(SWIG_TypeError, "string expected");

View file

@ -85,7 +85,7 @@ namespace std {
/* Overloading check */ /* Overloading check */
%typemap(in) string { %typemap(in) string {
if (caml_ptr_check($input)) if (caml_ptr_check($input))
$1 = std::string((char *)caml_ptr_val($input,0), $1.assign((char *)caml_ptr_val($input,0),
caml_string_len($input)); caml_string_len($input));
else else
SWIG_exception(SWIG_TypeError, "string expected"); SWIG_exception(SWIG_TypeError, "string expected");
@ -93,7 +93,7 @@ namespace std {
%typemap(in) const string & (std::string temp) { %typemap(in) const string & (std::string temp) {
if (caml_ptr_check($input)) { if (caml_ptr_check($input)) {
temp = std::string((char *)caml_ptr_val($input,0), temp.assign((char *)caml_ptr_val($input,0),
caml_string_len($input)); caml_string_len($input));
$1 = &temp; $1 = &temp;
} else { } else {
@ -103,7 +103,7 @@ namespace std {
%typemap(in) string & (std::string temp) { %typemap(in) string & (std::string temp) {
if (caml_ptr_check($input)) { if (caml_ptr_check($input)) {
temp = std::string((char *)caml_ptr_val($input,0), temp.assign((char *)caml_ptr_val($input,0),
caml_string_len($input)); caml_string_len($input));
$1 = &temp; $1 = &temp;
} else { } else {
@ -131,7 +131,7 @@ namespace std {
} }
%typemap(directorout) string { %typemap(directorout) string {
$result = std::string((char *)caml_ptr_val($input,0), $result.assign((char *)caml_ptr_val($input,0),
caml_string_len($input)); caml_string_len($input));
} }

View file

@ -25,13 +25,13 @@ namespace std {
%typemap(in, pikedesc="tStr") string { %typemap(in, pikedesc="tStr") string {
if ($input.type != T_STRING) if ($input.type != T_STRING)
Pike_error("Bad argument: Expected a string.\n"); Pike_error("Bad argument: Expected a string.\n");
$1 = std::string(STR0($input.u.string)); $1.assign(STR0($input.u.string));
} }
%typemap(in, pikedesc="tStr") const string & (std::string temp) { %typemap(in, pikedesc="tStr") const string & (std::string temp) {
if ($input.type != T_STRING) if ($input.type != T_STRING)
Pike_error("Bad argument: Expected a string.\n"); Pike_error("Bad argument: Expected a string.\n");
temp = std::string(STR0($input.u.string)); temp.assign(STR0($input.u.string));
$1 = &temp; $1 = &temp;
} }
@ -45,14 +45,14 @@ namespace std {
%typemap(directorout) string { %typemap(directorout) string {
if ($input.type == T_STRING) if ($input.type == T_STRING)
$result = std::string(STR0($input.u.string)); $result.assign(STR0($input.u.string));
else else
throw Swig::DirectorTypeMismatchException("string expected"); throw Swig::DirectorTypeMismatchException("string expected");
} }
%typemap(directorout) const string & (std::string temp) { %typemap(directorout) const string & (std::string temp) {
if ($input.type == T_STRING) { if ($input.type == T_STRING) {
temp = std::string(STR0($input.u.string)); temp.assign(STR0($input.u.string));
$result = &temp; $result = &temp;
} else { } else {
throw Swig::DirectorTypeMismatchException("string expected"); throw Swig::DirectorTypeMismatchException("string expected");