jscript: Moved string concatenation to helper function.
[wine] / dlls / jscript / jsstr.c
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 #include "jscript.h"
20
21 #include "wine/debug.h"
22
23 const char *debugstr_jsstr(jsstr_t *str)
24 {
25     return debugstr_wn(str->str, jsstr_length(str));
26 }
27
28 jsstr_t *jsstr_alloc_buf(unsigned len)
29 {
30     jsstr_t *ret;
31
32     if(len > JSSTR_MAX_LENGTH)
33         return NULL;
34
35     ret = heap_alloc(FIELD_OFFSET(jsstr_t, str[len+1]));
36     if(!ret)
37         return NULL;
38
39     ret->length_flags = len << JSSTR_LENGTH_SHIFT;
40     ret->ref = 1;
41     ret->str[len] = 0;
42     return ret;
43 }
44
45 jsstr_t *jsstr_alloc_len(const WCHAR *buf, unsigned len)
46 {
47     jsstr_t *ret;
48
49     ret = jsstr_alloc_buf(len);
50     if(ret)
51         memcpy(ret->str, buf, len*sizeof(WCHAR));
52
53     return ret;
54 }
55
56 int jsstr_cmp(jsstr_t *str1, jsstr_t *str2)
57 {
58     int len1 = jsstr_length(str1);
59     int len2 = jsstr_length(str2);
60     int ret;
61
62     ret = memcmp(str1->str, str2->str, min(len1, len2)*sizeof(WCHAR));
63     if(!ret)
64         ret = len1 - len2;
65
66     return ret;
67 }
68
69 jsstr_t *jsstr_concat(jsstr_t *str1, jsstr_t *str2)
70 {
71     unsigned len1, len2;
72     jsstr_t *ret;
73
74     len1 = jsstr_length(str1);
75     len2 = jsstr_length(str2);
76
77     ret = jsstr_alloc_buf(len1+len2);
78     if(!ret)
79         return NULL;
80
81     if(len1)
82         memcpy(ret->str, str1->str, len1*sizeof(WCHAR));
83     if(len2)
84         memcpy(ret->str+len1, str2->str, len2*sizeof(WCHAR));
85     return ret;
86 }
87
88 static jsstr_t *empty_str, *nan_str;
89
90 jsstr_t *jsstr_nan(void)
91 {
92     return jsstr_addref(nan_str);
93 }
94
95 jsstr_t *jsstr_empty(void)
96 {
97     return jsstr_addref(empty_str);
98 }
99
100 BOOL init_strings(void)
101 {
102     static const WCHAR NaNW[] = { 'N','a','N',0 };
103
104     if(!(empty_str = jsstr_alloc_buf(0)))
105         return FALSE;
106     if(!(nan_str = jsstr_alloc(NaNW)))
107         return FALSE;
108     return TRUE;
109 }
110
111 void free_strings(void)
112 {
113     jsstr_release(empty_str);
114     jsstr_release(nan_str);
115 }