summaryrefslogtreecommitdiff
path: root/libguile
diff options
context:
space:
mode:
authorMark H Weaver <mhw@netris.org>2015-01-22 01:22:19 -0500
committerAndy Wingo <wingo@pobox.com>2016-05-22 19:03:37 +0200
commite390e5760b8811be04665e160d0eb79d3721c453 (patch)
treeddec7d4924e6a888645b6f5de5d2f0b7b1d72a26 /libguile
parent751a55e3552547c84cc3cc0ad69fc6f26bd7251e (diff)
downloadguile-e390e5760b8811be04665e160d0eb79d3721c453.tar.gz
Implement 'string-utf8-length' and 'scm_c_string_utf8_length'.
* libguile/strings.c (utf8_length, scm_c_string_utf8_length) (scm_string_utf8_length): New functions. * libguile/strings.h (scm_c_string_utf8_length, scm_string_utf8_length): New prototypes. * doc/ref/api-data.texi (Bytevectors as Strings): Add docs. * doc/ref/guile.texi: Update manual copyright date to 2015. * test-suite/tests/strings.test (string-utf8-length): Add tests.
Diffstat (limited to 'libguile')
-rw-r--r--libguile/strings.c34
-rw-r--r--libguile/strings.h5
2 files changed, 37 insertions, 2 deletions
diff --git a/libguile/strings.c b/libguile/strings.c
index 2e5647e6d..dc2e4f5fe 100644
--- a/libguile/strings.c
+++ b/libguile/strings.c
@@ -1,5 +1,5 @@
/* Copyright (C) 1995, 1996, 1998, 2000, 2001, 2004, 2006,
- * 2008-2015 Free Software Foundation, Inc.
+ * 2008-2016 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
@@ -2065,6 +2065,38 @@ u32_u8_length_in_bytes (const scm_t_uint32 *str, size_t len)
return ret;
}
+static size_t
+utf8_length (SCM str)
+{
+ if (scm_i_is_narrow_string (str))
+ return latin1_u8_strlen ((scm_t_uint8 *) scm_i_string_chars (str),
+ scm_i_string_length (str));
+ else
+ return u32_u8_length_in_bytes
+ ((scm_t_uint32 *) scm_i_string_wide_chars (str),
+ scm_i_string_length (str));
+}
+
+size_t
+scm_c_string_utf8_length (SCM string)
+#define FUNC_NAME "scm_c_string_utf8_length"
+{
+ SCM_VALIDATE_STRING (1, string);
+ return utf8_length (string);
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_string_utf8_length, "string-utf8-length", 1, 0, 0,
+ (SCM string),
+ "Returns the number of bytes in the UTF-8 representation of "
+ "@var{string}.")
+#define FUNC_NAME s_scm_string_utf8_length
+{
+ SCM_VALIDATE_STRING (1, string);
+ return scm_from_size_t (utf8_length (string));
+}
+#undef FUNC_NAME
+
char *
scm_to_utf8_stringn (SCM str, size_t *lenp)
#define FUNC_NAME "scm_to_utf8_stringn"
diff --git a/libguile/strings.h b/libguile/strings.h
index 24471cd69..882e7ce64 100644
--- a/libguile/strings.h
+++ b/libguile/strings.h
@@ -3,7 +3,8 @@
#ifndef SCM_STRINGS_H
#define SCM_STRINGS_H
-/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2004, 2005, 2006, 2008, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
+/* Copyright (C) 1995-1998, 2000, 2001, 2004-2006, 2008-2011, 2013,
+ * 2015-2016 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
@@ -107,6 +108,7 @@ SCM_API SCM scm_string_p (SCM x);
SCM_API SCM scm_string (SCM chrs);
SCM_API SCM scm_make_string (SCM k, SCM chr);
SCM_API SCM scm_string_length (SCM str);
+SCM_API SCM scm_string_utf8_length (SCM str);
SCM_API SCM scm_string_bytes_per_char (SCM str);
SCM_API SCM scm_string_ref (SCM str, SCM k);
SCM_API SCM scm_string_set_x (SCM str, SCM k, SCM chr);
@@ -120,6 +122,7 @@ SCM_API SCM scm_from_stringn (const char *str, size_t len, const char *encoding,
scm_t_string_failed_conversion_handler handler);
SCM_API SCM scm_c_make_string (size_t len, SCM chr);
SCM_API size_t scm_c_string_length (SCM str);
+SCM_API size_t scm_c_string_utf8_length (SCM str);
SCM_API size_t scm_c_symbol_length (SCM sym);
SCM_API SCM scm_c_string_ref (SCM str, size_t pos);
SCM_API void scm_c_string_set_x (SCM str, size_t pos, SCM chr);