1 /*
2  * This file is part of libdom.
3  * Licensed under the MIT License,
4  *                http://www.opensource.org/licenses/mit-license.php
5  * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
6  */
7 
8 #ifndef dom_string_h_
9 #define dom_string_h_
10 
11 #include <inttypes.h>
12 #include <stddef.h>
13 #include <libwapcaplet/libwapcaplet.h>
14 
15 #include <dom/functypes.h>
16 #include <dom/core/exceptions.h>
17 
18 typedef struct dom_string dom_string;
19 struct dom_string {
20 	uint32_t refcnt;
21 };
22 
23 
24 /* Claim a reference on a DOM string */
dom_string_ref(dom_string * str)25 static inline dom_string *dom_string_ref(dom_string *str)
26 {
27 	if (str != NULL)
28 		str->refcnt++;
29 	return str;
30 }
31 
32 /* Destroy a DOM string */
33 void dom_string_destroy(dom_string *str);
34 
35 /* Release a reference on a DOM string */
dom_string_unref(dom_string * str)36 static inline void dom_string_unref(dom_string *str)
37 {
38 	if ((str != NULL) && (--(str->refcnt) == 0)) {
39 		dom_string_destroy(str);
40 	}
41 }
42 
43 /* Create a DOM string from a string of characters */
44 dom_exception dom_string_create(const uint8_t *ptr, size_t len,
45 		dom_string **str);
46 dom_exception dom_string_create_interned(const uint8_t *ptr, size_t len,
47 		dom_string **str);
48 
49 /* Obtain an interned representation of a dom string */
50 dom_exception dom_string_intern(dom_string *str,
51 		struct lwc_string_s **lwcstr);
52 
53 /* Case sensitively compare two DOM strings */
54 bool dom_string_isequal(const dom_string *s1, const dom_string *s2);
55 /* Case insensitively compare two DOM strings */
56 bool dom_string_caseless_isequal(const dom_string *s1, const dom_string *s2);
57 
58 /* Case sensitively compare DOM string and lwc_string */
59 bool dom_string_lwc_isequal(const dom_string *s1, lwc_string *s2);
60 /* Case insensitively compare DOM string and lwc_string */
61 bool dom_string_caseless_lwc_isequal(const dom_string *s1, lwc_string *s2);
62 
63 /* Get the index of the first occurrence of a character in a dom string */
64 uint32_t dom_string_index(dom_string *str, uint32_t chr);
65 /* Get the index of the last occurrence of a character in a dom string */
66 uint32_t dom_string_rindex(dom_string *str, uint32_t chr);
67 
68 /* Get the length, in characters, of a dom string */
69 uint32_t dom_string_length(dom_string *str);
70 
71 /**
72  * Get the raw character data of the dom_string.
73  * @note: This function is just provided for the convenience of accessing the
74  * raw C string character, no change on the result string is allowed.
75  */
76 const char *dom_string_data(const dom_string *str);
77 
78 /* Get the byte length of this dom_string */
79 size_t dom_string_byte_length(const dom_string *str);
80 
81 /* Get the UCS-4 character at position index, the index should be in
82  * [0, length), and length can be get by calling dom_string_length
83  */
84 dom_exception dom_string_at(dom_string *str, uint32_t index,
85 		uint32_t *ch);
86 
87 /* Concatenate two dom strings */
88 dom_exception dom_string_concat(dom_string *s1, dom_string *s2,
89 		dom_string **result);
90 
91 /* Extract a substring from a dom string */
92 dom_exception dom_string_substr(dom_string *str,
93 		uint32_t i1, uint32_t i2, dom_string **result);
94 
95 /* Insert data into a dom string at the given location */
96 dom_exception dom_string_insert(dom_string *target,
97 		dom_string *source, uint32_t offset,
98 		dom_string **result);
99 
100 /* Replace a section of a dom string */
101 dom_exception dom_string_replace(dom_string *target,
102 		dom_string *source, uint32_t i1, uint32_t i2,
103 		dom_string **result);
104 
105 /* Generate an uppercase version of the given string */
106 dom_exception dom_string_toupper(dom_string *source, bool ascii_only,
107 				 dom_string **upper);
108 
109 /* Generate an lowercase version of the given string */
110 dom_exception dom_string_tolower(dom_string *source, bool ascii_only,
111 				 dom_string **lower);
112 
113 /* Calculate a hash value from a dom string */
114 uint32_t dom_string_hash(dom_string *str);
115 
116 #endif
117