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
parent ef4ca158de
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
[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
(tested with GCC 4.1.2).

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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