Corrected string conversion slightly

This was SVN commit r2034.
This commit is contained in:
Ykkrosh 2005-03-22 12:09:11 +00:00
parent 6d792365aa
commit 5b11dbb6b0
2 changed files with 24 additions and 23 deletions

View File

@ -7,43 +7,42 @@
#include <jsapi.h>
// Make JS debugging a little easier by automatically naming GC roots
#ifndef NDEBUG
// Don't simply #define NAME_ALL_GC_ROOTS, because jsapi.h is horridly broken
# define JS_AddRoot(cx, rp) JS_AddNamedRoot((cx), (rp), __FILE__)
#endif
#if SDL_BYTE_ORDER == SDL_BIG_ENDIAN
#define ucs2le_to_wchart(ptr) (wchar_t)( (u16) ((u8*)ptr)[0] | (u16) ( ((u8*)ptr)[1] << 8) )
#else
#define ucs2le_to_wchart(ptr) (wchar_t)(*ptr);
#endif
JSString* StringConvert::wstring_to_jsstring(JSContext* cx, const std::wstring& str)
JSString* StringConvert::wchars_to_jsstring(JSContext* cx, const wchar_t* chars, size_t len)
{
size_t len = str.length();
if (len == 0)
return JSVAL_TO_STRING(JS_GetEmptyStringValue(cx));
jschar* data = (jschar*)JS_malloc(cx, len*sizeof(jschar));
if (!data)
return NULL;
// Copy the string data, doing wchar_t->jschar conversion (since they're not equal in GCC)
for (size_t i=0; i<len; ++i)
data[i] = str[i];
data[i] = chars[i];
return JS_NewUCString(cx, data, len);
}
JSString* StringConvert::wchars_to_jsstring(JSContext* cx, const wchar_t* chars)
{
size_t len = wcslen(chars);
if( !len )
return( JSVAL_TO_STRING( JS_GetEmptyStringValue( cx ) ) );
jschar* data = (jschar*)JS_malloc(cx, len*sizeof(jschar));
if (!data)
return NULL;
for (size_t i=0; i<len; ++i)
data[i] = chars[i];
return JS_NewUCString(cx, data, len);
return wchars_to_jsstring(cx, chars, wcslen(chars));
}
JSString* StringConvert::wstring_to_jsstring(JSContext* cx, const std::wstring& str)
{
return wchars_to_jsstring(cx, str.data(), str.length());
}
void StringConvert::jsstring_to_wstring(JSString* str, std::wstring& result)
{
jschars_to_wstring(JS_GetStringChars(str), JS_GetStringLength(str), result);
@ -52,18 +51,19 @@ void StringConvert::jsstring_to_wstring(JSString* str, std::wstring& result)
void StringConvert::jschars_to_wstring(const jschar* chars, size_t len, std::wstring& result)
{
assert(result.empty());
result.reserve(len);
result.resize(len);
for (size_t i = 0; i < len; ++i)
result += chars[i];
result[i] = chars[i];
}
void StringConvert::ucs2le_to_wstring(const char* start, const char* end, std::wstring& result)
{
assert(result.empty());
result.reserve(end-start);
result.resize((end-start)/2);
size_t i = 0;
for (const char* pos = start; pos < end; pos += 2)
result += ucs2le_to_wchart(pos);
result[i++] = ucs2le_to_wchart(pos);
}

View File

@ -12,6 +12,7 @@ namespace StringConvert
JSString* wstring_to_jsstring(JSContext* cx, const std::wstring& str);
JSString* wchars_to_jsstring(JSContext* cx, const wchar_t* chars);
JSString* wchars_to_jsstring(JSContext* cx, const wchar_t* chars, size_t len);
void jsstring_to_wstring(JSString* str, std::wstring& result);
void jschars_to_wstring(const jschar* chars, size_t len, std::wstring& result);