summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorJohn Mark Bell <jmb@netsurf-browser.org>2012-02-06 18:00:39 +0000
committerJohn Mark Bell <jmb@netsurf-browser.org>2012-02-06 18:00:39 +0000
commit2558d1626468cfae7c9406509315dfbe10c16e83 (patch)
tree89c68720e620ebbf59b8cafc3f5b3d09dbb9d8e0 /src/core
parentebd543e0bc08a7df3c0c9a4bbb575f2dcafd270b (diff)
downloadlibdom-2558d1626468cfae7c9406509315dfbe10c16e83.tar.gz
libdom-2558d1626468cfae7c9406509315dfbe10c16e83.tar.bz2
Add constructor which auto-interns data
svn path=/trunk/libdom/; revision=13427
Diffstat (limited to 'src/core')
-rw-r--r--src/core/string.c45
-rw-r--r--src/core/string.h4
2 files changed, 28 insertions, 21 deletions
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 <dom/core/string.h>
-/* 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);