2012-10-11 10:16:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Jacek Caban for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "jscript.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
const char *debugstr_jsstr(jsstr_t *str)
|
|
|
|
{
|
|
|
|
return debugstr_wn(str->str, jsstr_length(str));
|
|
|
|
}
|
|
|
|
|
2013-03-25 11:28:38 +00:00
|
|
|
WCHAR *jsstr_alloc_buf(unsigned len, jsstr_t **r)
|
2012-10-11 10:16:01 +00:00
|
|
|
{
|
|
|
|
jsstr_t *ret;
|
|
|
|
|
|
|
|
if(len > JSSTR_MAX_LENGTH)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = heap_alloc(FIELD_OFFSET(jsstr_t, str[len+1]));
|
|
|
|
if(!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret->length_flags = len << JSSTR_LENGTH_SHIFT;
|
|
|
|
ret->ref = 1;
|
|
|
|
ret->str[len] = 0;
|
2013-03-25 11:28:38 +00:00
|
|
|
*r = ret;
|
|
|
|
return ret->str;
|
2012-10-11 10:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jsstr_t *jsstr_alloc_len(const WCHAR *buf, unsigned len)
|
|
|
|
{
|
|
|
|
jsstr_t *ret;
|
2013-03-25 11:28:38 +00:00
|
|
|
WCHAR *ptr;
|
2012-10-11 10:16:01 +00:00
|
|
|
|
2013-03-25 11:28:38 +00:00
|
|
|
ptr = jsstr_alloc_buf(len, &ret);
|
|
|
|
if(ptr)
|
|
|
|
memcpy(ptr, buf, len*sizeof(WCHAR));
|
2012-10-11 10:16:01 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int jsstr_cmp(jsstr_t *str1, jsstr_t *str2)
|
|
|
|
{
|
|
|
|
int len1 = jsstr_length(str1);
|
|
|
|
int len2 = jsstr_length(str2);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = memcmp(str1->str, str2->str, min(len1, len2)*sizeof(WCHAR));
|
|
|
|
if(!ret)
|
|
|
|
ret = len1 - len2;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-18 10:16:24 +00:00
|
|
|
jsstr_t *jsstr_concat(jsstr_t *str1, jsstr_t *str2)
|
|
|
|
{
|
|
|
|
unsigned len1, len2;
|
|
|
|
jsstr_t *ret;
|
2013-03-25 11:28:38 +00:00
|
|
|
WCHAR *ptr;
|
2012-12-18 10:16:24 +00:00
|
|
|
|
|
|
|
len1 = jsstr_length(str1);
|
2012-12-18 10:16:34 +00:00
|
|
|
if(!len1)
|
|
|
|
return jsstr_addref(str2);
|
|
|
|
|
2012-12-18 10:16:24 +00:00
|
|
|
len2 = jsstr_length(str2);
|
2012-12-18 10:16:34 +00:00
|
|
|
if(!len2)
|
|
|
|
return jsstr_addref(str1);
|
2012-12-18 10:16:24 +00:00
|
|
|
|
2013-03-25 11:28:38 +00:00
|
|
|
ptr = jsstr_alloc_buf(len1+len2, &ret);
|
2012-12-18 10:16:24 +00:00
|
|
|
if(!ret)
|
|
|
|
return NULL;
|
|
|
|
|
2013-03-25 11:28:38 +00:00
|
|
|
jsstr_flush(str1, ptr);
|
|
|
|
jsstr_flush(str2, ptr+len1);
|
2012-12-18 10:16:24 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-08 10:37:56 +00:00
|
|
|
static jsstr_t *empty_str, *nan_str, *undefined_str;
|
2012-10-11 10:16:01 +00:00
|
|
|
|
|
|
|
jsstr_t *jsstr_nan(void)
|
|
|
|
{
|
|
|
|
return jsstr_addref(nan_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
jsstr_t *jsstr_empty(void)
|
|
|
|
{
|
|
|
|
return jsstr_addref(empty_str);
|
|
|
|
}
|
|
|
|
|
2013-03-08 10:37:56 +00:00
|
|
|
jsstr_t *jsstr_undefined(void)
|
|
|
|
{
|
|
|
|
return jsstr_addref(undefined_str);
|
|
|
|
}
|
|
|
|
|
2012-10-11 10:16:01 +00:00
|
|
|
BOOL init_strings(void)
|
|
|
|
{
|
|
|
|
static const WCHAR NaNW[] = { 'N','a','N',0 };
|
2013-03-08 10:37:56 +00:00
|
|
|
static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
|
2012-10-11 10:16:01 +00:00
|
|
|
|
2013-03-25 11:28:38 +00:00
|
|
|
if(!jsstr_alloc_buf(0, &empty_str))
|
2012-10-11 10:16:01 +00:00
|
|
|
return FALSE;
|
|
|
|
if(!(nan_str = jsstr_alloc(NaNW)))
|
|
|
|
return FALSE;
|
2013-03-08 10:37:56 +00:00
|
|
|
if(!(undefined_str = jsstr_alloc(undefinedW)))
|
|
|
|
return FALSE;
|
2012-10-11 10:16:01 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_strings(void)
|
|
|
|
{
|
|
|
|
jsstr_release(empty_str);
|
|
|
|
jsstr_release(nan_str);
|
2013-03-08 10:37:56 +00:00
|
|
|
jsstr_release(undefined_str);
|
2012-10-11 10:16:01 +00:00
|
|
|
}
|