From 2558d1626468cfae7c9406509315dfbe10c16e83 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Mon, 6 Feb 2012 18:00:39 +0000 Subject: Add constructor which auto-interns data svn path=/trunk/libdom/; revision=13427 --- src/core/string.c | 45 ++++++++++++++++++++++++++++----------------- src/core/string.h | 4 ---- 2 files changed, 28 insertions(+), 21 deletions(-) (limited to 'src/core') diff --git a/src/core/string.c b/src/core/string.c index be0053e..a9659cb 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -141,35 +141,46 @@ dom_exception dom_string_create(const uint8_t *ptr, size_t len, } /** - * Create a dom_string from a lwc_string - * - * \param str The lwc_string - * \param ret The new dom_string + * Create an interned DOM string from a string of characters + * + * \param ptr Pointer to string of characters + * \param len Length, in bytes, of string of characters + * \param str Pointer to location to receive result * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion + * + * The returned string will already be referenced, so there is no need + * to explicitly reference it. + * + * The string of characters passed in will be copied for use by the + * returned DOM string. */ -dom_exception _dom_string_create_from_lwcstring(lwc_string *str, - dom_string **ret) +dom_exception dom_string_create_interned(const uint8_t *ptr, size_t len, + dom_string **str) { - dom_string *r; + dom_string *ret; - if (str == NULL) { - *ret = NULL; - return DOM_NO_ERR; + if (ptr == NULL || len == 0) { + ptr = (const uint8_t *) ""; + len = 0; } - r = malloc(sizeof(dom_string)); - if (r == NULL) + ret = malloc(sizeof(dom_string)); + if (ret == NULL) return DOM_NO_MEM_ERR; - r->data.intern = lwc_string_ref(str); + if (lwc_intern_string((const char *) ptr, len, + &ret->data.intern) != lwc_error_ok) { + free(ret); + return DOM_NO_MEM_ERR; + } + + ret->refcnt = 1; - r->refcnt = 1; + ret->type = DOM_STRING_INTERNED; - r->type = DOM_STRING_INTERNED; + *str = ret; - *ret = r; return DOM_NO_ERR; - } /** diff --git a/src/core/string.h b/src/core/string.h index 796a2ea..cbf7d36 100644 --- a/src/core/string.h +++ b/src/core/string.h @@ -11,10 +11,6 @@ #include -/* Create a DOM string from a lwc_string */ -dom_exception _dom_string_create_from_lwcstring(struct lwc_string_s *str, - dom_string **ret); - /* Map the lwc_error to dom_exception */ dom_exception _dom_exception_from_lwc_error(lwc_error err); -- cgit v1.2.3