diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2017-05-04 20:40:55 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2017-05-04 20:40:55 -0700 |
commit | e5186dace2ce5c13c2a178b67507a3f917f4ed25 (patch) | |
tree | 365929fae624706089d854ee9c50cb7dc3378e5c /lib.c | |
parent | 7246e3eff36f290b207b203c40f90850ddd031e8 (diff) | |
download | txr-e5186dace2ce5c13c2a178b67507a3f917f4ed25.tar.gz txr-e5186dace2ce5c13c2a178b67507a3f917f4ed25.tar.bz2 txr-e5186dace2ce5c13c2a178b67507a3f917f4ed25.zip |
ffi: new bstr type.
The bstr type is like str, but doesn't perform UTF-8
conversion. The C data is assumed to be null terminated byte
strings representing code points U+0000 through U+00FF.
* ffi.c (bstr_s, bstr_d_s): New symbol variables.
(ffi_bstr_put, ffi_bstr_get, ffi_bstr_d_get): New static
functions.
(ffi_init_types): Register bstr and bstr-d types.
(ffi_init): Initialize bstr_s and bstr_d_s.
* ffi.h (bstr_s, bstr_d_s): Declared.
* lib.c (chk_strdup_8bit, string_8bit): New function.
* lib.h (chk_strdup_8bit, string_8bit): Declared.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -2673,6 +2673,21 @@ char *chk_strdup_utf8(const char *str) return copy; } +unsigned char *chk_strdup_8bit(const wchar_t *str) +{ + size_t nchar = wcslen(str) + 1, i; + unsigned char *copy = coerce(unsigned char *, chk_malloc(nchar)); + for (i = 0; i < nchar; i++) { + if (str[i] < 0 || str[i] > 255) { + free(copy); + uw_throwf(error_s, lit("cannot coerce ~s to 8 bit"), + string(str), nao); + } + copy[i] = str[i]; + } + return copy; +} + mem_t *chk_copy_obj(mem_t *orig, size_t size) { mem_t *copy = chk_malloc(size); @@ -3276,6 +3291,15 @@ val string_utf8(const char *str) return obj; } +val string_8bit(const unsigned char *str) +{ + size_t l = strlen(coerce(const char *, str)), i; + wchar_t *wstr = chk_wmalloc(l + 1); + for (i = 0; i <= l; i++) + wstr[i] = str[i]; + return string_own(wstr); +} + val mkstring(val len, val ch) { size_t l = c_num(len); |