Change the length of strings created from fixed-size buffers.
Use the usual C rule for NUL-terminated strings instead of discarding all the trailing NUL characters. This was unexpected (as buffers in C code are not necessarily always padded with NULs to their full length) and also inconsistent among languages as this was only done for those of them using typemaps/strings.swg but not for C# or Java, for example, which terminated the string at the first NUL even before this change. Notice that this patch couldn't use strlen() or wcslen() with possibly not NUL-terminated strings, so we had to add [our own equivalents of] strnlen() and wcsnlen() and use them instead. This required adding yet another parameter to string typemap macros, so update the example using them accordingly too.
This commit is contained in:
parent
ad02cb98e6
commit
88a0e228a9
8 changed files with 75 additions and 20 deletions
|
|
@ -17,8 +17,19 @@ SWIG_pchar_descriptor(void)
|
|||
}
|
||||
}
|
||||
|
||||
%fragment("SWIG_strnlen","header",fragment="SWIG_FromCharPtrAndSize") {
|
||||
size_t
|
||||
SWIG_strnlen(const char* s, size_t maxlen)
|
||||
{
|
||||
const char *p;
|
||||
for (p = s; maxlen-- && *p; p++)
|
||||
;
|
||||
return p - s;
|
||||
}
|
||||
}
|
||||
|
||||
%include <typemaps/strings.swg>
|
||||
%typemaps_string(%checkcode(STRING), %checkcode(CHAR),
|
||||
char, Char, SWIG_AsCharPtrAndSize, SWIG_FromCharPtrAndSize, strlen,
|
||||
char, Char, SWIG_AsCharPtrAndSize, SWIG_FromCharPtrAndSize,
|
||||
strlen, SWIG_strnlen,
|
||||
"<limits.h>", CHAR_MIN, CHAR_MAX)
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
SWIG_AsCharPtrAndSize,
|
||||
SWIG_FromCharPtrAndSize,
|
||||
SWIG_CharPtrLen,
|
||||
SWIG_CharBufLen,
|
||||
SWIG_AsCharPtr,
|
||||
SWIG_FromCharPtr,
|
||||
SWIG_AsCharArray,
|
||||
|
|
@ -276,12 +277,13 @@
|
|||
}
|
||||
%typemap(freearg) const Char (&)[ANY] "";
|
||||
|
||||
%typemap(out,fragment=#SWIG_FromCharPtrAndSize)
|
||||
%typemap(out,fragment=#SWIG_FromCharPtrAndSize,fragment=#SWIG_CharBufLen)
|
||||
Char [ANY], const Char[ANY]
|
||||
{
|
||||
size_t size = $1_dim0;
|
||||
%#ifndef SWIG_PRESERVE_CARRAY_SIZE
|
||||
while (size && ($1[size - 1] == '\0')) --size;
|
||||
size_t size = SWIG_CharBufLen($1, $1_dim0);
|
||||
%#else
|
||||
size_t size = $1_dim0;
|
||||
%#endif
|
||||
%set_output(SWIG_FromCharPtrAndSize($1, size));
|
||||
}
|
||||
|
|
@ -298,23 +300,25 @@
|
|||
|
||||
/* varout */
|
||||
|
||||
%typemap(varout,noblock=1,fragment=#SWIG_FromCharPtrAndSize)
|
||||
%typemap(varout,noblock=1,fragment=#SWIG_CharBufLen)
|
||||
Char [ANY], const Char [ANY] {
|
||||
size_t size = $1_dim0;
|
||||
%#ifndef SWIG_PRESERVE_CARRAY_SIZE
|
||||
while (size && ($1[size - 1] == '\0')) --size;
|
||||
size_t size = SWIG_CharBufLen($1, $1_dim0);
|
||||
%#else
|
||||
size_t size = $1_dim0;
|
||||
%#endif
|
||||
%set_varoutput(SWIG_FromCharPtrAndSize($1, size));
|
||||
}
|
||||
|
||||
/* constant */
|
||||
|
||||
%typemap(constcode,fragment=#SWIG_FromCharPtrAndSize)
|
||||
%typemap(constcode,fragment=#SWIG_CharBufLen)
|
||||
Char [ANY], const Char [ANY]
|
||||
{
|
||||
size_t size = $value_dim0;
|
||||
%#ifndef SWIG_PRESERVE_CARRAY_SIZE
|
||||
while (size && ($value[size - 1] == '\0')) --size;
|
||||
size_t size = SWIG_CharBufLen($1, $1_dim0);
|
||||
%#else
|
||||
size_t size = $value_dim0;
|
||||
%#endif
|
||||
%set_constant("$symname", SWIG_FromCharPtrAndSize($value,size));
|
||||
}
|
||||
|
|
@ -323,12 +327,13 @@
|
|||
#if defined(SWIG_DIRECTOR_TYPEMAPS)
|
||||
|
||||
/* directorin */
|
||||
%typemap(directorin,fragment=#SWIG_FromCharPtrAndSize)
|
||||
%typemap(directorin,fragment=#SWIG_CharBufLen)
|
||||
Char [ANY], const Char [ANY]
|
||||
{
|
||||
size_t size = $1_dim0;
|
||||
%#ifndef SWIG_PRESERVE_CARRAY_SIZE
|
||||
while (size && ($1[size - 1] == '\0')) --size;
|
||||
size_t size = SWIG_CharBufLen($1, $1_dim0);
|
||||
%#else
|
||||
size_t size = $1_dim0;
|
||||
%#endif
|
||||
$input = SWIG_FromCharPtrAndSize($1, size);
|
||||
}
|
||||
|
|
@ -360,12 +365,13 @@
|
|||
|
||||
/* throws */
|
||||
|
||||
%typemap(throws,fragment=#SWIG_FromCharPtrAndSize)
|
||||
%typemap(throws,fragment=#SWIG_CharBufLen)
|
||||
Char [ANY], const Char[ANY]
|
||||
{
|
||||
size_t size = $1_dim0;
|
||||
%#ifndef SWIG_PRESERVE_CARRAY_SIZE
|
||||
while (size && ($1[size - 1] == '\0')) --size;
|
||||
size_t size = SWIG_CharBufLen($1, $1_dim0);
|
||||
%#else
|
||||
size_t size = $1_dim0;
|
||||
%#endif
|
||||
%raise(SWIG_FromCharPtrAndSize($1, size), "$type", 0);
|
||||
}
|
||||
|
|
@ -499,6 +505,7 @@
|
|||
SWIG_AsCharPtrAndSize,
|
||||
SWIG_FromCharPtrAndSize,
|
||||
SWIG_CharPtrLen,
|
||||
SWIG_CharBufLen,
|
||||
SWIG_NewCopyCharArray,
|
||||
SWIG_DeleteCharArray,
|
||||
FragLimits, CHAR_MIN, CHAR_MAX)
|
||||
|
|
@ -586,6 +593,7 @@ SWIG_AsVal_dec(Char)(SWIG_Object obj, Char *val)
|
|||
SWIG_AsCharPtrAndSize,
|
||||
SWIG_FromCharPtrAndSize,
|
||||
SWIG_CharPtrLen,
|
||||
SWIG_CharBufLen,
|
||||
SWIG_As##CharName##Ptr,
|
||||
SWIG_From##CharName##Ptr,
|
||||
SWIG_As##CharName##Array,
|
||||
|
|
@ -604,12 +612,14 @@ SWIG_AsVal_dec(Char)(SWIG_Object obj, Char *val)
|
|||
SWIG_AsCharPtrAndSize,
|
||||
SWIG_FromCharPtrAndSize,
|
||||
SWIG_CharPtrLen,
|
||||
SWIG_CharBufLen,
|
||||
FragLimits, CHAR_MIN, CHAR_MAX)
|
||||
%_typemap2_string(StringCode, CharCode,
|
||||
Char, CharName,
|
||||
SWIG_AsCharPtrAndSize,
|
||||
SWIG_FromCharPtrAndSize,
|
||||
SWIG_CharPtrLen,
|
||||
SWIG_CharBufLen,
|
||||
%new_copy_array,
|
||||
%delete_array,
|
||||
FragLimits, CHAR_MIN, CHAR_MAX)
|
||||
|
|
@ -624,6 +634,7 @@ SWIG_AsVal_dec(Char)(SWIG_Object obj, Char *val)
|
|||
SWIG_AsCharPtrAndSize,
|
||||
SWIG_FromCharPtrAndSize,
|
||||
SWIG_CharPtrLen,
|
||||
SWIG_CharBufLen,
|
||||
SWIG_NewCopyCharArray,
|
||||
SWIG_DeleteCharArray,
|
||||
FragLimits, CHAR_MIN, CHAR_MAX)
|
||||
|
|
@ -632,6 +643,7 @@ SWIG_AsVal_dec(Char)(SWIG_Object obj, Char *val)
|
|||
SWIG_AsCharPtrAndSize,
|
||||
SWIG_FromCharPtrAndSize,
|
||||
SWIG_CharPtrLen,
|
||||
SWIG_CharBufLen,
|
||||
SWIG_NewCopyCharArray,
|
||||
SWIG_DeleteCharArray,
|
||||
FragLimits, CHAR_MIN, CHAR_MAX)
|
||||
|
|
|
|||
|
|
@ -18,8 +18,20 @@ SWIG_pwchar_descriptor()
|
|||
}
|
||||
}
|
||||
|
||||
%fragment("SWIG_wcsnlen","header",fragment="SWIG_FromWCharPtrAndSize") {
|
||||
size_t
|
||||
SWIG_wcsnlen(const wchar_t* s, size_t maxlen)
|
||||
{
|
||||
const wchar_t *p;
|
||||
for (p = s; maxlen-- && *p; p++)
|
||||
;
|
||||
return p - s;
|
||||
}
|
||||
}
|
||||
|
||||
%include <typemaps/strings.swg>
|
||||
%typemaps_string(%checkcode(UNISTRING), %checkcode(UNICHAR),
|
||||
wchar_t, WChar, SWIG_AsWCharPtrAndSize, SWIG_FromWCharPtrAndSize, wcslen,
|
||||
wchar_t, WChar, SWIG_AsWCharPtrAndSize, SWIG_FromWCharPtrAndSize,
|
||||
wcslen, SWIG_wcsnlen,
|
||||
"<wchar.h>", WCHAR_MIN, WCHAR_MAX)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue