Pyrogenesis  trunk
utf16string.h
Go to the documentation of this file.
1 /* Copyright (C) 2009 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  A basic_string derivative that works with uint16_t as its underlying char
20  type.
21 */
22 #ifndef INCLUDED_UTF16STRING
23 #define INCLUDED_UTF16STRING
24 
25 // On Linux, wchar_t is 32-bit, so define a new version of it.
26 // We now use this code on Windows as well, because wchar_t is a
27 // native type and distinct from utf16_t.
28 #include <string>
29 #include <memory.h>
30 #include <algorithm>
31 
32 typedef uint16_t utf16_t;
33 
34 // jw: this was originally defined in the std namespace, which is at
35 // least frowned upon if not illegal. giving it a new name and passing it
36 // as a template parameter is the "correct" and safe way.
37 
39 {
40  typedef utf16_t char_type;
41  typedef int int_type;
42  typedef std::streampos pos_type;
43  typedef std::streamoff off_type;
44  typedef std::mbstate_t state_type;
45 
46  static void assign(char_type& c1, const char_type& c2)
47  { c1 = c2; }
48 
49  static bool eq(const char_type& c1, const char_type& c2)
50  { return c1 == c2; }
51 
52  static bool lt(const char_type& c1, const char_type& c2)
53  { return c1 < c2; }
54 
55  static int compare(const char_type* s1, const char_type* s2, size_t n)
56  {
57  return memcmp(s1, s2, n*sizeof(char_type));
58  }
59 
60  static size_t length(const char_type* s)
61  {
62  const char_type* end=s;
63  while (*end) end++;
64  return (size_t)(end-s);
65  }
66 
67  static const char_type* find(const char_type* s, size_t n, const char_type& a)
68  {
69  const char_type *end = s+n;
70  const char_type *res = std::find(s, end, a);
71  return (res != end)?res:NULL;
72  }
73 
74  static char_type* move(char_type* s1, const char_type* s2, size_t n)
75  {
76  return (char_type *)memmove(s1, s2, n*sizeof(char_type));
77  }
78 
79  static char_type* copy(char_type* s1, const char_type* s2, size_t n)
80  {
81  return (char_type *)memcpy(s1, s2, n*sizeof(char_type));
82  }
83 
84  static char_type* assign(char_type* s, size_t n, char_type a)
85  {
86  while (n--)
87  {
88  s[n]=a;
89  }
90  return s;
91  }
92 
93  static char_type to_char_type(const int_type& c)
94  { return (char_type)c; }
95 
96  static int_type to_int_type(const char_type& c)
97  { return (int_type)c; }
98 
99  static bool eq_int_type(const int_type& c1, const int_type& c2)
100  { return c1 == c2; }
101 
102  static int_type eof()
103  { return -1; }
104 
105  static int_type not_eof(const int_type& c)
106  { return (c == -1) ? 0 : c; }
107 };
108 
109 typedef std::basic_string<utf16_t, utf16_traits> utf16string;
110 typedef std::basic_stringstream<utf16_t, utf16_traits> utf16stringstream;
111 
112 #endif
static int_type eof()
Definition: utf16string.h:102
uint16_t utf16_t
Definition: utf16string.h:32
static bool eq(const char_type &c1, const char_type &c2)
Definition: utf16string.h:49
int int_type
Definition: utf16string.h:41
static bool lt(const char_type &c1, const char_type &c2)
Definition: utf16string.h:52
static int compare(const char_type *s1, const char_type *s2, size_t n)
Definition: utf16string.h:55
static char_type * copy(char_type *s1, const char_type *s2, size_t n)
Definition: utf16string.h:79
std::streamoff off_type
Definition: utf16string.h:43
Definition: utf16string.h:38
static const char_type * find(const char_type *s, size_t n, const char_type &a)
Definition: utf16string.h:67
static bool eq_int_type(const int_type &c1, const int_type &c2)
Definition: utf16string.h:99
static int_type not_eof(const int_type &c)
Definition: utf16string.h:105
static char_type * move(char_type *s1, const char_type *s2, size_t n)
Definition: utf16string.h:74
static size_t length(const char_type *s)
Definition: utf16string.h:60
utf16_t char_type
Definition: utf16string.h:40
std::mbstate_t state_type
Definition: utf16string.h:44
std::basic_stringstream< utf16_t, utf16_traits > utf16stringstream
Definition: utf16string.h:110
static char_type * assign(char_type *s, size_t n, char_type a)
Definition: utf16string.h:84
std::basic_string< utf16_t, utf16_traits > utf16string
Definition: utf16string.h:109
static void assign(char_type &c1, const char_type &c2)
Definition: utf16string.h:46
static int_type to_int_type(const char_type &c)
Definition: utf16string.h:96
unsigned short uint16_t
Definition: wposix_types.h:52
std::streampos pos_type
Definition: utf16string.h:42
static char_type to_char_type(const int_type &c)
Definition: utf16string.h:93