Drops custom utf16 string implementation (from cdd3317ded), uses C++11 one.

Patch By: sera
Differential Revision: https://code.wildfiregames.com/D4223
This was SVN commit r26023.
This commit is contained in:
vladislavbelov 2021-11-29 12:10:41 +00:00
parent e1374252b7
commit af567560b8
10 changed files with 23 additions and 138 deletions

View file

@ -241,6 +241,7 @@
{ "nick": "sbte", "name": "Sven Baars" },
{ "nick": "scroogie", "name": "André Gemünd" },
{ "nick": "scythetwirler", "name": "Casey X." },
{ "nick": "sera", "name": "Ralph Sennhauser" },
{ "nick": "serveurix" },
{ "nick": "Shane", "name": "Shane Grant" },
{ "nick": "shh" },

View file

@ -50,7 +50,6 @@ enum PS_TRIM_MODE
#define CSTR_H_A
#endif
#include "ps/utf16string.h"
#include "ps/CStrForward.h"
#include <string>
@ -81,11 +80,11 @@ public:
static CStr Repeat(const CStr& str, size_t reps);
/**
* Construction from utf16strings.
* Construction from u16strings.
*
* @param utf16string String utf16string to be used for initialization.
* @param u16string String u16string to be used for initialization.
**/
explicit CStr(const utf16string& str) : StrBase(str.begin(), str.end()) {}
explicit CStr(const std::u16string& str) : StrBase(str.begin(), str.end()) {}
// Conversion to/from UTF-8, encoded in a CStr8.
// Invalid bytes/characters (e.g. broken UTF-8, and Unicode characters
@ -324,8 +323,8 @@ public:
**/
CStr Pad(PS_TRIM_MODE mode, size_t len) const;
// Conversion to utf16string
utf16string utf16() const { return utf16string(begin(), end()); }
// Conversion to u16string
std::u16string utf16() const { return std::u16string(begin(), end()); }
// Calculates a hash of the string's contents
size_t GetHashCode() const;

View file

@ -1,114 +0,0 @@
/* Copyright (C) 2020 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
/*
A basic_string derivative that works with uint16_t as its underlying char
type.
*/
#ifndef INCLUDED_UTF16STRING
#define INCLUDED_UTF16STRING
// On Linux, wchar_t is 32-bit, so define a new version of it.
// We now use this code on Windows as well, because wchar_t is a
// native type and distinct from utf16_t.
#include <algorithm>
#include <cstring>
#include <memory>
#include <string>
typedef uint16_t utf16_t;
// jw: this was originally defined in the std namespace, which is at
// least frowned upon if not illegal. giving it a new name and passing it
// as a template parameter is the "correct" and safe way.
struct utf16_traits
{
typedef utf16_t char_type;
typedef int int_type;
typedef std::streampos pos_type;
typedef std::streamoff off_type;
typedef std::mbstate_t state_type;
static void assign(char_type& c1, const char_type& c2)
{ c1 = c2; }
static bool eq(const char_type& c1, const char_type& c2)
{ return c1 == c2; }
static bool lt(const char_type& c1, const char_type& c2)
{ return c1 < c2; }
static int compare(const char_type* s1, const char_type* s2, size_t n)
{
return memcmp(s1, s2, n*sizeof(char_type));
}
static size_t length(const char_type* s)
{
const char_type* end=s;
while (*end) end++;
return (size_t)(end-s);
}
static const char_type* find(const char_type* s, size_t n, const char_type& a)
{
const char_type *end = s+n;
const char_type *res = std::find(s, end, a);
return (res != end)?res:NULL;
}
static char_type* move(char_type* s1, const char_type* s2, size_t n)
{
return (char_type *)memmove(s1, s2, n*sizeof(char_type));
}
static char_type* copy(char_type* s1, const char_type* s2, size_t n)
{
return (char_type *)memcpy(s1, s2, n*sizeof(char_type));
}
static char_type* assign(char_type* s, size_t n, char_type a)
{
while (n--)
{
s[n]=a;
}
return s;
}
static char_type to_char_type(const int_type& c)
{ return (char_type)c; }
static int_type to_int_type(const char_type& c)
{ return (int_type)c; }
static bool eq_int_type(const int_type& c1, const int_type& c2)
{ return c1 == c2; }
static int_type eof()
{ return -1; }
static int_type not_eof(const int_type& c)
{ return (c == -1) ? 0 : c; }
};
typedef std::basic_string<utf16_t, utf16_traits> utf16string;
typedef std::basic_stringstream<utf16_t, utf16_traits> utf16stringstream;
#endif

View file

@ -21,12 +21,13 @@
#include "ps/CStr.h"
#include "ps/Filesystem.h"
#include "ps/utf16string.h"
#include "scriptinterface/FunctionWrapper.h"
#include "scriptinterface/ScriptExceptions.h"
#include "scriptinterface/ScriptRequest.h"
#include "scriptinterface/ScriptTypes.h"
#include <string>
// Ignore warnings in SM headers.
#if GCC_VERSION || CLANG_VERSION
# pragma GCC diagnostic push
@ -47,8 +48,8 @@
bool Script::ParseJSON(const ScriptRequest& rq, const std::string& string_utf8, JS::MutableHandleValue out)
{
std::wstring attrsW = wstring_from_utf8(string_utf8);
utf16string string(attrsW.begin(), attrsW.end());
if (JS_ParseJSON(rq.cx, reinterpret_cast<const char16_t*>(string.c_str()), (u32)string.size(), out))
std::u16string string(attrsW.begin(), attrsW.end());
if (JS_ParseJSON(rq.cx, string.c_str(), (u32)string.size(), out))
return true;
ScriptException::CatchPending(rq);
@ -85,7 +86,7 @@ struct Stringifier
{
static bool callback(const char16_t* buf, u32 len, void* data)
{
utf16string str(buf, buf+len);
std::u16string str(buf, buf+len);
std::wstring strw(str.begin(), str.end());
Status err; // ignore Unicode errors

View file

@ -24,10 +24,11 @@
#include "graphics/Entity.h"
#include "lib/file/vfs/vfs_path.h"
#include "maths/Vector2D.h"
#include "ps/utf16string.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
#include <string>
// Catch the raised exception right away to ensure the stack trace gets printed.
#define FAIL(msg) STMT(ScriptException::Raise(rq, msg); ScriptException::CatchPending(rq); return false)
@ -222,8 +223,8 @@ template<> void Script::ToJSVal<u32>(const ScriptRequest& UNUSED(rq), JS::Mutabl
template<> void Script::ToJSVal<std::wstring>(const ScriptRequest& rq, JS::MutableHandleValue ret, const std::wstring& val)
{
utf16string utf16(val.begin(), val.end());
JS::RootedString str(rq.cx, JS_NewUCStringCopyN(rq.cx, reinterpret_cast<const char16_t*> (utf16.c_str()), utf16.length()));
std::u16string utf16(val.begin(), val.end());
JS::RootedString str(rq.cx, JS_NewUCStringCopyN(rq.cx, utf16.c_str(), utf16.length()));
if (str)
ret.setString(str);
else

View file

@ -29,7 +29,6 @@
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "ps/Profile.h"
#include "ps/utf16string.h"
#include <map>
#include <string>

View file

@ -27,7 +27,6 @@
#include "maths/FixedVector3D.h"
#include "maths/Rect.h"
#include "ps/CLogger.h"
#include "ps/utf16string.h"
#include "simulation2/helpers/CinemaPath.h"
#include "simulation2/helpers/Grid.h"
#include "simulation2/system/IComponent.h"

View file

@ -214,14 +214,14 @@ JS::Value CStdDeserializer::ReadScriptVal(const char* UNUSED(name), JS::HandleOb
ReadStringLatin1("prop name", propname);
JS::RootedValue propval(rq.cx, ReadScriptVal("prop value", nullptr));
utf16string prp(propname.begin(), propname.end());;
std::u16string prp(propname.begin(), propname.end());;
// TODO: Should ask upstream about getting a variant of JS_SetProperty with a length param.
if (!JS_SetUCProperty(rq.cx, obj, (const char16_t*)prp.data(), prp.length(), propval))
throw PSERROR_Deserialize_ScriptError();
}
else
{
utf16string propname;
std::u16string propname;
ReadStringUTF16("prop name", propname);
JS::RootedValue propval(rq.cx, ReadScriptVal("prop value", nullptr));
@ -441,7 +441,7 @@ void CStdDeserializer::ReadStringLatin1(const char* name, std::vector<JS::Latin1
Get(name, (u8*)str.data(), len);
}
void CStdDeserializer::ReadStringUTF16(const char* name, utf16string& str)
void CStdDeserializer::ReadStringUTF16(const char* name, std::u16string& str)
{
uint32_t len;
NumberU32_Unbounded("string length", len);
@ -471,7 +471,7 @@ void CStdDeserializer::ScriptString(const char* name, JS::MutableHandleString ou
}
else
{
utf16string str;
std::u16string str;
ReadStringUTF16(name, str);
out.set(JS_NewUCStringCopyN(rq.cx, (const char16_t*)str.data(), str.length()));

View file

@ -20,8 +20,7 @@
#include "IDeserializer.h"
#include "ps/utf16string.h"
#include <string>
#include <vector>
class CStdDeserializer : public IDeserializer
@ -48,7 +47,7 @@ protected:
private:
JS::Value ReadScriptVal(const char* name, JS::HandleObject preexistingObject);
void ReadStringLatin1(const char* name, std::vector<JS::Latin1Char>& str);
void ReadStringUTF16(const char* name, utf16string& str);
void ReadStringUTF16(const char* name, std::u16string& str);
virtual void AddScriptBackref(JS::HandleObject obj);
virtual void GetScriptBackref(size_t tag, JS::MutableHandleObject ret);

View file

@ -411,8 +411,8 @@ void CParamNode::ConstructJSVal(const ScriptRequest& rq, JS::MutableHandleValue
// If the node has a string too, add that as an extra property
if (!m_Value.empty())
{
utf16string text(m_Value.begin(), m_Value.end());
JS::RootedString str(rq.cx, JS_AtomizeAndPinUCStringN(rq.cx, reinterpret_cast<const char16_t*>(text.data()), text.length()));
std::u16string text(m_Value.begin(), m_Value.end());
JS::RootedString str(rq.cx, JS_AtomizeAndPinUCStringN(rq.cx, text.c_str(), text.length()));
if (!str)
{
ret.setUndefined();