diff options
author | Mark H Weaver <mhw@netris.org> | 2012-04-04 18:58:44 -0400 |
---|---|---|
committer | Mark H Weaver <mhw@netris.org> | 2012-04-04 18:58:44 -0400 |
commit | bbb9f000ad52282ee1a0518b65437baf20c3d17c (patch) | |
tree | 270f1ab225107ae8cb2a58fe5e8259877d6ab5ea /libguile/strings.c | |
parent | 51853eee69ca5811ae0661eb91868121c6ad1d74 (diff) | |
download | guile-bbb9f000ad52282ee1a0518b65437baf20c3d17c.tar.gz |
Fix scm_to_utf8_stringn once and for all; optimize; add tests
* libguile/strings.c (scm_to_utf8_stringn): Fix another new bug in this
recent comedy of errors: pass the size of the preallocated buffer to
u32_to_u8. Arrange to call 'scm_i_string_wide_chars' and
'scm_i_string_length' only once each. Rename local variables for
improved code clarity.
* test-suite/standalone/test-conversion.c (test_to_utf8_stringn): New
function to test scm_to_utf8_stringn.
Diffstat (limited to 'libguile/strings.c')
-rw-r--r-- | libguile/strings.c | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/libguile/strings.c b/libguile/strings.c index ec55c501b..07356fcaf 100644 --- a/libguile/strings.c +++ b/libguile/strings.c @@ -1929,28 +1929,28 @@ scm_to_utf8_stringn (SCM str, size_t *lenp) NULL, lenp); else { + scm_t_uint32 *chars = (scm_t_uint32 *) scm_i_string_wide_chars (str); scm_t_uint8 *buf, *ret; - size_t predicted_len, actual_len; /* length in bytes */ + size_t num_chars = scm_i_string_length (str); + size_t num_bytes_predicted, num_bytes_actual; - predicted_len = u32_u8_length_in_bytes - ((scm_t_uint32 *) scm_i_string_wide_chars (str), - scm_i_string_length (str)); + num_bytes_predicted = u32_u8_length_in_bytes (chars, num_chars); if (lenp) { - *lenp = predicted_len; - buf = scm_malloc (predicted_len); + *lenp = num_bytes_predicted; + buf = scm_malloc (num_bytes_predicted); } else { - buf = scm_malloc (predicted_len + 1); - buf[predicted_len] = 0; + buf = scm_malloc (num_bytes_predicted + 1); + buf[num_bytes_predicted] = 0; } - ret = u32_to_u8 ((scm_t_uint32 *) scm_i_string_wide_chars (str), - scm_i_string_length (str), buf, &actual_len); + num_bytes_actual = num_bytes_predicted; + ret = u32_to_u8 (chars, num_chars, buf, &num_bytes_actual); - if (SCM_LIKELY (ret == buf && actual_len == predicted_len)) + if (SCM_LIKELY (ret == buf && num_bytes_actual == num_bytes_predicted)) return (char *) ret; /* An error: a bad codepoint. */ |