wininet/tests: Add tests for a prematurely closed connection.
[wine] / dlls / jscript / jsstr.h
1 /*
2  * Copyright 2012 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 struct _jsstr_t {
20     unsigned length_flags;
21     unsigned ref;
22 };
23
24 #define JSSTR_LENGTH_SHIFT 4
25 #define JSSTR_MAX_LENGTH (1 << (32-JSSTR_LENGTH_SHIFT))
26 #define JSSTR_FLAGS_MASK ((1 << JSSTR_LENGTH_SHIFT)-1)
27
28 #define JSSTR_FLAG_NULLBSTR 1
29
30 static inline unsigned jsstr_length(jsstr_t *str)
31 {
32     return str->length_flags >> JSSTR_LENGTH_SHIFT;
33 }
34
35 typedef struct {
36     jsstr_t str;
37     WCHAR buf[1];
38 } jsstr_inline_t;
39
40 jsstr_t *jsstr_alloc_len(const WCHAR*,unsigned) DECLSPEC_HIDDEN;
41 WCHAR *jsstr_alloc_buf(unsigned,jsstr_t**) DECLSPEC_HIDDEN;
42
43 static inline jsstr_t *jsstr_alloc(const WCHAR *str)
44 {
45     return jsstr_alloc_len(str, strlenW(str));
46 }
47
48 static inline void jsstr_release(jsstr_t *str)
49 {
50     if(!--str->ref)
51         heap_free(str);
52 }
53
54 static inline jsstr_t *jsstr_addref(jsstr_t *str)
55 {
56     str->ref++;
57     return str;
58 }
59
60 static inline jsstr_inline_t *jsstr_as_inline(jsstr_t *str)
61 {
62     return CONTAINING_RECORD(str, jsstr_inline_t, str);
63 }
64
65 /* This will be failable in the future. */
66 static inline const WCHAR *jsstr_flatten(jsstr_t *str)
67 {
68     return jsstr_as_inline(str)->buf;
69 }
70
71 static inline BOOL jsstr_eq(jsstr_t *str1, jsstr_t *str2)
72 {
73     unsigned len = jsstr_length(str1);
74     return len == jsstr_length(str2) && !memcmp(jsstr_as_inline(str1)->buf, jsstr_as_inline(str2)->buf, len*sizeof(WCHAR));
75 }
76
77 static inline void jsstr_extract(jsstr_t *str, unsigned off, unsigned len, WCHAR *buf)
78 {
79     memcpy(buf, jsstr_as_inline(str)->buf+off, len*sizeof(WCHAR));
80 }
81
82 static inline unsigned jsstr_flush(jsstr_t *str, WCHAR *buf)
83 {
84     unsigned len = jsstr_length(str);
85     memcpy(buf, jsstr_as_inline(str)->buf, len*sizeof(WCHAR));
86     return len;
87 }
88
89 static inline jsstr_t *jsstr_substr(jsstr_t *str, unsigned off, unsigned len)
90 {
91     jsstr_t *ret;
92     WCHAR *ptr;
93
94     ptr = jsstr_alloc_buf(len, &ret);
95     if(ptr)
96         jsstr_extract(str, off, len, ptr);
97     return ret;
98 }
99
100 int jsstr_cmp(jsstr_t*,jsstr_t*) DECLSPEC_HIDDEN;
101 jsstr_t *jsstr_concat(jsstr_t*,jsstr_t*) DECLSPEC_HIDDEN;
102
103 jsstr_t *jsstr_nan(void) DECLSPEC_HIDDEN;
104 jsstr_t *jsstr_empty(void) DECLSPEC_HIDDEN;
105 jsstr_t *jsstr_undefined(void) DECLSPEC_HIDDEN;
106
107 BOOL init_strings(void) DECLSPEC_HIDDEN;
108 void free_strings(void) DECLSPEC_HIDDEN;
109
110 const char *debugstr_jsstr(jsstr_t*) DECLSPEC_HIDDEN;