summaryrefslogtreecommitdiff
path: root/include/dom/core/string.h
blob: f2a6122aee7b59900de9bf92d091e2e8eafabb8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
 */

#ifndef dom_string_h_
#define dom_string_h_

#include <inttypes.h>
#include <stddef.h>
#include <libwapcaplet/libwapcaplet.h>

#include <dom/functypes.h>
#include <dom/core/exceptions.h>

typedef struct dom_string dom_string;
struct dom_string {
	uint32_t refcnt;
} _ALIGNED;


/* Claim a reference on a DOM string */
static inline dom_string *dom_string_ref(dom_string *str)
{
	if (str != NULL)
		str->refcnt++;
	return str;
}

/* Destroy a DOM string */
void dom_string_destroy(dom_string *str);

/* Release a reference on a DOM string */
static inline void dom_string_unref(dom_string *str) 
{
	if ((str != NULL) && (--(str->refcnt) == 0)) {
		dom_string_destroy(str);
	}
}

/* Create a DOM string from a string of characters */
dom_exception dom_string_create(const uint8_t *ptr, size_t len, 
		dom_string **str);
dom_exception dom_string_create_interned(const uint8_t *ptr, size_t len,
		dom_string **str);

/* Obtain an interned representation of a dom string */
dom_exception dom_string_intern(dom_string *str, 
		struct lwc_string_s **lwcstr);

/* Case sensitively compare two DOM strings */
bool dom_string_isequal(const dom_string *s1, const dom_string *s2);
/* Case insensitively compare two DOM strings */
bool dom_string_caseless_isequal(const dom_string *s1, const dom_string *s2);

/* Case sensitively compare DOM string and lwc_string */
bool dom_string_lwc_isequal(const dom_string *s1, lwc_string *s2);
/* Case insensitively compare DOM string and lwc_string */
bool dom_string_caseless_lwc_isequal(const dom_string *s1, lwc_string *s2);

/* Get the index of the first occurrence of a character in a dom string */
uint32_t dom_string_index(dom_string *str, uint32_t chr);
/* Get the index of the last occurrence of a character in a dom string */
uint32_t dom_string_rindex(dom_string *str, uint32_t chr);

/* Get the length, in characters, of a dom string */
uint32_t dom_string_length(dom_string *str);

/**
 * Get the raw character data of the dom_string.
 * @note: This function is just provided for the convenience of accessing the 
 * raw C string character, no change on the result string is allowed.
 */
const char *dom_string_data(const dom_string *str);

/* Get the byte length of this dom_string */
size_t dom_string_byte_length(const dom_string *str);

/* Get the UCS-4 character at position index, the index should be in 
 * [0, length), and length can be get by calling dom_string_length
 */
dom_exception dom_string_at(dom_string *str, uint32_t index, 
		uint32_t *ch);

/* Concatenate two dom strings */
dom_exception dom_string_concat(dom_string *s1, dom_string *s2,
		dom_string **result);

/* Extract a substring from a dom string */
dom_exception dom_string_substr(dom_string *str, 
		uint32_t i1, uint32_t i2, dom_string **result);

/* Insert data into a dom string at the given location */
dom_exception dom_string_insert(dom_string *target,
		dom_string *source, uint32_t offset,
		dom_string **result);

/* Replace a section of a dom string */
dom_exception dom_string_replace(dom_string *target,
		dom_string *source, uint32_t i1, uint32_t i2,
		dom_string **result);

/* Generate an uppercase version of the given string */
dom_exception dom_string_toupper(dom_string *source, bool ascii_only,
				 dom_string **upper);

/* Generate an lowercase version of the given string */
dom_exception dom_string_tolower(dom_string *source, bool ascii_only,
				 dom_string **lower);

/* Calculate a hash value from a dom string */
uint32_t dom_string_hash(dom_string *str);

#endif