From 01b6268b303919739f15a8681b898e2e65b84cc9 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Mon, 24 Apr 2023 11:49:23 +1200 Subject: Make typemaps consistently use string::data() vs c_str() Use c_str() only when we need a terminating zero byte and data() when we don't (as we already did in most cases). The two methods in fact do the same thing as of C++11 (and in practice did for all C++98 implementations I'm aware of) but it's useful to make clear when we need a terminating zero byte and when we don't, for example for adding string_view support (string_view supports data(), but can't support c_str() (since it could be a slice from the middle of a std::string). --- Lib/csharp/std_wstring.i | 12 ++++++------ Lib/ocaml/std_string.i | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Lib/csharp/std_wstring.i b/Lib/csharp/std_wstring.i index 1d10ca808..c7fef41a5 100644 --- a/Lib/csharp/std_wstring.i +++ b/Lib/csharp/std_wstring.i @@ -63,7 +63,7 @@ class wstring; return $null; } $1 = Swig_csharp_UTF16ToWString($input); %} -%typemap(out) wstring %{ $result = SWIG_csharp_wstring_with_length_callback($1.c_str(), (int)$1.size()); %} +%typemap(out) wstring %{ $result = SWIG_csharp_wstring_with_length_callback($1.data(), (int)$1.size()); %} %typemap(directorout, canthrow=1) wstring %{ if (!$input) { @@ -72,7 +72,7 @@ class wstring; } $result = Swig_csharp_UTF16ToWString($input); %} -%typemap(directorin) wstring %{ $input = SWIG_csharp_wstring_with_length_callback($1.c_str(), (int)$1.size()); %} +%typemap(directorin) wstring %{ $input = SWIG_csharp_wstring_with_length_callback($1.data(), (int)$1.size()); %} %typemap(csin) wstring "$csinput" %typemap(csout, excode=SWIGEXCODE) wstring { @@ -83,7 +83,7 @@ class wstring; %typemap(typecheck) wstring = wchar_t *; %typemap(throws, canthrow=1) wstring -%{ SWIG_csharp_ApplicationException_callback($1.c_str(), (int)$1.size()); +%{ SWIG_csharp_ApplicationException_callback($1.data(), (int)$1.size()); return $null; %} // const wstring & @@ -106,7 +106,7 @@ class wstring; } std::wstring $1_str(Swig_csharp_UTF16ToWString($input)); $1 = &$1_str; %} -%typemap(out) const wstring & %{ $result = SWIG_csharp_wstring_with_length_callback($1->c_str(), (int)$1->size()); %} +%typemap(out) const wstring & %{ $result = SWIG_csharp_wstring_with_length_callback($1->data(), (int)$1->size()); %} %typemap(csin) const wstring & "$csinput" %typemap(csout, excode=SWIGEXCODE) const wstring & { @@ -124,7 +124,7 @@ class wstring; $1_str = Swig_csharp_UTF16ToWString($input); $result = &$1_str; %} -%typemap(directorin) const wstring & %{ $input = SWIG_csharp_wstring_with_length_callback($1.c_str(), (int)$1.size()); %} +%typemap(directorin) const wstring & %{ $input = SWIG_csharp_wstring_with_length_callback($1.data(), (int)$1.size()); %} %typemap(csvarin, excode=SWIGEXCODE2) const wstring & %{ set { @@ -139,7 +139,7 @@ class wstring; %typemap(typecheck) const wstring & = wchar_t *; %typemap(throws, canthrow=1) const wstring & -%{ SWIG_csharp_ApplicationException_callback($1.c_str(), (int)$1.size()); +%{ SWIG_csharp_ApplicationException_callback($1.data(), (int)$1.size()); return $null; %} } diff --git a/Lib/ocaml/std_string.i b/Lib/ocaml/std_string.i index 08e304911..6cf918c6f 100644 --- a/Lib/ocaml/std_string.i +++ b/Lib/ocaml/std_string.i @@ -51,11 +51,11 @@ class wstring; } %typemap(out) const string & { - $result = caml_val_string_len((*$1).c_str(), (*$1).size()); + $result = caml_val_string_len((*$1).data(), (*$1).size()); } %typemap(directorin) string { - swig_result = caml_val_string_len($1.c_str(), $1.size()); + swig_result = caml_val_string_len($1.data(), $1.size()); args = caml_list_append(args, swig_result); } @@ -64,19 +64,19 @@ class wstring; } %typemap(out) string { - $result = caml_val_string_len($1.c_str(),$1.size()); + $result = caml_val_string_len($1.data(),$1.size()); } %typemap(varout) string { - $result = caml_val_string_len($1.c_str(),$1.size()); + $result = caml_val_string_len($1.data(),$1.size()); } %typemap(out) string * { - $result = caml_val_string_len((*$1).c_str(),(*$1).size()); + $result = caml_val_string_len((*$1).data(),(*$1).size()); } %typemap(varout) string * { - $result = caml_val_string_len((*$1).c_str(),(*$1).size()); + $result = caml_val_string_len((*$1).data(),(*$1).size()); } %typemap(typecheck) string, const string & = char *; @@ -87,7 +87,7 @@ class wstring; %typemap(in, numinputs=0) string &OUTPUT ($*1_ltype temp) %{ $1 = &temp; %} %typemap(argout) string &OUTPUT { - swig_result = caml_list_append(swig_result, caml_val_string_len((*$1).c_str(), (*$1).size())); + swig_result = caml_list_append(swig_result, caml_val_string_len((*$1).data(), (*$1).size())); } %typemap(in) string &INOUT = const string &; %typemap(argout) string &INOUT = string &OUTPUT; -- cgit v1.2.1