diff options
author | Kevin Ryde <user42@zip.com.au> | 2005-06-24 21:43:36 +0000 |
---|---|---|
committer | Kevin Ryde <user42@zip.com.au> | 2005-06-24 21:43:36 +0000 |
commit | 48ddf0d9a9728607e0bab7a431a1b3c51c3f46ec (patch) | |
tree | cb56b5518dbc0a87f7c33d906f4bce4364247294 /libguile/strings.c | |
parent | 9247329fe1b9491daa994e212b786a4c6825ac1d (diff) | |
download | guile-48ddf0d9a9728607e0bab7a431a1b3c51c3f46ec.tar.gz |
(scm_take_locale_stringn): Use realloc to make room for
null-terminator, rather than mallocing a whole new block.
(scm_take_locale_string): Use scm_take_locale_stringn len==-1.
Diffstat (limited to 'libguile/strings.c')
-rw-r--r-- | libguile/strings.c | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/libguile/strings.c b/libguile/strings.c index b990575a8..9a2656b80 100644 --- a/libguile/strings.c +++ b/libguile/strings.c @@ -829,34 +829,34 @@ scm_from_locale_string (const char *str) SCM scm_take_locale_stringn (char *str, size_t len) { + SCM buf, res; + if (len == (size_t)-1) - return scm_take_locale_string (str); + len = strlen (str); else { - /* STR might not be zero terminated and we are not allowed to - look at str[len], so we have to make a new one... - */ - SCM res = scm_from_locale_stringn (str, len); - free (str); - return res; + /* Ensure STR is null terminated. A realloc for 1 extra byte should + often be satisfied from the alignment padding after the block, with + no actual data movement. */ + str = scm_realloc (str, len+1); + str[len] = '\0'; } -} - -SCM -scm_take_locale_string (char *str) -{ - size_t len = strlen (str); - SCM buf, res; buf = scm_double_cell (STRINGBUF_TAG, (scm_t_bits) str, - (scm_t_bits) len, (scm_t_bits) 0); + (scm_t_bits) len, (scm_t_bits) 0); res = scm_double_cell (STRING_TAG, - SCM_UNPACK (buf), - (scm_t_bits) 0, (scm_t_bits) len); + SCM_UNPACK (buf), + (scm_t_bits) 0, (scm_t_bits) len); scm_gc_register_collectable_memory (str, len+1, "string"); return res; } +SCM +scm_take_locale_string (char *str) +{ + return scm_take_locale_stringn (str, -1); +} + char * scm_to_locale_stringn (SCM str, size_t *lenp) { |