2004-07-10 20:47:19 +02:00
|
|
|
/*
|
|
|
|
A basic_string derivative that works with uint16_t as its underlying char
|
|
|
|
type.
|
|
|
|
*/
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_UTF16STRING
|
|
|
|
#define INCLUDED_UTF16STRING
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2006-02-03 21:36:15 +01:00
|
|
|
// 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.
|
2004-07-10 20:47:19 +02:00
|
|
|
#include <string>
|
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
#include "lib/sysdep/cpu.h" // cpu_memcpy
|
2007-04-25 20:19:35 +02:00
|
|
|
|
2004-07-11 13:51:27 +02:00
|
|
|
typedef uint16_t utf16_t;
|
2004-07-10 22:44:28 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
// 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.
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
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;
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static void assign(char_type& c1, const char_type& c2)
|
|
|
|
{ c1 = c2; }
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static bool eq(const char_type& c1, const char_type& c2)
|
|
|
|
{ return c1 == c2; }
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static bool lt(const char_type& c1, const char_type& c2)
|
|
|
|
{ return c1 < c2; }
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static int compare(const char_type* s1, const char_type* s2, size_t n)
|
|
|
|
{
|
|
|
|
return memcmp(s1, s2, n*sizeof(char_type));
|
|
|
|
}
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static size_t length(const char_type* s)
|
|
|
|
{
|
|
|
|
const char_type* end=s;
|
|
|
|
while (*end) end++;
|
|
|
|
return end-s;
|
|
|
|
}
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
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;
|
|
|
|
}
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static char_type* move(char_type* s1, const char_type* s2, size_t n)
|
|
|
|
{
|
|
|
|
return (char_type *)memmove(s1, s2, n*sizeof(char_type));
|
|
|
|
}
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static char_type* copy(char_type* s1, const char_type* s2, size_t n)
|
|
|
|
{
|
|
|
|
return (char_type *)cpu_memcpy(s1, s2, n*sizeof(char_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
static char_type* assign(char_type* s, size_t n, char_type a)
|
|
|
|
{
|
|
|
|
while (n--)
|
2004-07-10 20:47:19 +02:00
|
|
|
{
|
2007-12-20 21:21:45 +01:00
|
|
|
s[n]=a;
|
2004-07-10 20:47:19 +02:00
|
|
|
}
|
2007-12-20 21:21:45 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char_type to_char_type(const int_type& c)
|
|
|
|
{ return (char_type)c; }
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static int_type to_int_type(const char_type& c)
|
|
|
|
{ return (int_type)c; }
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static bool eq_int_type(const int_type& c1, const int_type& c2)
|
|
|
|
{ return c1 == c2; }
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static int_type eof()
|
|
|
|
{ return -1; }
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
static int_type not_eof(const int_type& c)
|
|
|
|
{ return (c == -1) ? 0 : c; }
|
|
|
|
};
|
2004-07-10 20:47:19 +02:00
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
typedef std::basic_string<utf16_t, utf16_traits> utf16string;
|
|
|
|
typedef std::basic_stringstream<utf16_t, utf16_traits> utf16stringstream;
|
2004-07-10 20:47:19 +02:00
|
|
|
|
|
|
|
#endif
|