summaryrefslogtreecommitdiffstats
path: root/ffi.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2017-05-10 06:41:50 -0700
committerKaz Kylheku <kaz@kylheku.com>2017-05-10 06:41:50 -0700
commit471149c262613e4b69fc14b9599fe541106084e4 (patch)
treee784850dd3089f325d37482621aa5d3ae7c2c045 /ffi.c
parentfe81a856d6a1db346c519897581925d1797913ad (diff)
downloadtxr-471149c262613e4b69fc14b9599fe541106084e4.tar.gz
txr-471149c262613e4b69fc14b9599fe541106084e4.tar.bz2
txr-471149c262613e4b69fc14b9599fe541106084e4.zip
ffi: adjust semantics of zarray of characters.
We want to be able to extract null-terminated UTF-8 strings from arrays, without trailing junk, yet retain the ability to extract the entire array including embedded nulls. The natural way is to use the array/zarray distinction. * ffi.c (ffi_array_in, ffi_array_get): Don't try to guess whether the array is null terminated; just rely on the null_term flag, and treat accordingly. * txr.1: Doc updated.
Diffstat (limited to 'ffi.c')
-rw-r--r--ffi.c18
1 files changed, 8 insertions, 10 deletions
diff --git a/ffi.c b/ffi.c
index 5918933d..8c99f3d9 100644
--- a/ffi.c
+++ b/ffi.c
@@ -1008,7 +1008,7 @@ static val ffi_array_in(struct txr_ffi_type *tft, int copy, mem_t *src,
str = null_string;
} else {
const char *chptr = coerce(const char *, src);
- if (chptr[tft->size - 1] == 0) {
+ if (tft->null_term) {
str = string_utf8(chptr);
} else {
wchar_t *wch = utf8_dup_from_buf(chptr, tft->size);
@@ -1022,13 +1022,12 @@ static val ffi_array_in(struct txr_ffi_type *tft, int copy, mem_t *src,
if (nelem == 0) {
str = null_string;
} else {
- cnum nchar = tft->size / sizeof (wchar_t);
const wchar_t *wchptr = coerce(const wchar_t *, src);
- if (wchptr[nchar - 1] == 0) {
+ if (tft->null_term) {
str = string(wchptr);
} else {
- val ustr = mkustring(num_fast(nchar));
+ val ustr = mkustring(num_fast(nelem));
str = init_str(ustr, wchptr);
}
}
@@ -1040,7 +1039,7 @@ static val ffi_array_in(struct txr_ffi_type *tft, int copy, mem_t *src,
str = null_string;
} else {
const unsigned char *chptr = coerce(const unsigned char *, src);
- if (chptr[tft->size - 1] == 0)
+ if (tft->null_term)
str = string_8bit(chptr);
else
str = string_8bit_size(chptr, tft->size);
@@ -1129,7 +1128,7 @@ static val ffi_array_get(struct txr_ffi_type *tft, mem_t *src, val self)
return null_string;
} else {
const char *chptr = coerce(const char *, src);
- if (chptr[tft->size - 1] == 0) {
+ if (tft->null_term) {
return string_utf8(chptr);
} else {
wchar_t *wch = utf8_dup_from_buf(chptr, tft->size);
@@ -1140,13 +1139,12 @@ static val ffi_array_get(struct txr_ffi_type *tft, mem_t *src, val self)
if (nelem == 0) {
return null_string;
} else {
- cnum nchar = tft->size / sizeof (wchar_t);
const wchar_t *wchptr = coerce(const wchar_t *, src);
- if (wchptr[nchar - 1] == 0) {
+ if (tft->null_term) {
return string(wchptr);
} else {
- val ustr = mkustring(num_fast(nchar));
+ val ustr = mkustring(num_fast(nelem));
return init_str(ustr, wchptr);
}
}
@@ -1155,7 +1153,7 @@ static val ffi_array_get(struct txr_ffi_type *tft, mem_t *src, val self)
return null_string;
} else {
const unsigned char *chptr = coerce(const unsigned char *, src);
- if (chptr[tft->size - 1] == 0)
+ if (tft->null_term)
return string_8bit(chptr);
else
return string_8bit_size(chptr, tft->size);