[Go] #2245 Handle NULL pointers for string* conversions.

Rearrange generation of director methods and rename
receiver argument from p to swig_p.

Fixes #2245
This commit is contained in:
Ian Lance Taylor 2022-07-05 17:00:21 -07:00
commit 87cbf8c341
5 changed files with 194 additions and 162 deletions

View file

@ -93,17 +93,22 @@ class string;
%typemap(in) string * (string temp)
%{
temp.assign($input->p, $input->n);
$1 = &temp;
if ($input) {
temp.assign($input->p, $input->n);
$1 = &temp;
} else
$1 = 0;
%}
%typemap(godirectorout) string *
%{
{
if $input != nil {
p := Swig_malloc(len(*$input))
s := (*[1<<30]byte)(unsafe.Pointer(p))[:len(*$input)]
copy(s, *$input)
$result = (*string)(unsafe.Pointer(&s))
} else {
$result = nil
}
%}
@ -125,17 +130,27 @@ class string;
%typemap(directorin,fragment="AllocateString") string * (_gostring_ temp)
%{
temp = Swig_AllocateString($1->data(), $1->length());
$input = &temp;
if ($1) {
temp = Swig_AllocateString($1->data(), $1->length());
$input = &temp;
} else
$input = 0;
%}
%typemap(godirectorin,fragment="CopyString") string *
%{ *$result = swigCopyString(*$input); %}
%typemap(argout,fragment="AllocateString") string *
%{ *$input = Swig_AllocateString($1->data(), $1->length()); %}
%{
if ($1)
*$input = Swig_AllocateString($1->data(), $1->length());
%}
%typemap(goargout,fragment="CopyString") string *
%{ *$input = swigCopyString(*$1) %}
%{
if $input != nil {
*$1 = swigCopyString(*$input)
}
%}
}