|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef nsString_h___ |
|
8 #define nsString_h___ |
|
9 |
|
10 #include "mozilla/Attributes.h" |
|
11 |
|
12 #include "nsSubstring.h" |
|
13 #include "nsDependentSubstring.h" |
|
14 #include "nsReadableUtils.h" |
|
15 |
|
16 #include <new> |
|
17 |
|
18 // enable support for the obsolete string API if not explicitly disabled |
|
19 #ifndef MOZ_STRING_WITH_OBSOLETE_API |
|
20 #define MOZ_STRING_WITH_OBSOLETE_API 1 |
|
21 #endif |
|
22 |
|
23 #if MOZ_STRING_WITH_OBSOLETE_API |
|
24 // radix values for ToInteger/AppendInt |
|
25 #define kRadix10 (10) |
|
26 #define kRadix16 (16) |
|
27 #define kAutoDetect (100) |
|
28 #define kRadixUnknown (kAutoDetect+1) |
|
29 #define IGNORE_CASE (true) |
|
30 #endif |
|
31 |
|
32 |
|
33 // declare nsString, et. al. |
|
34 #include "string-template-def-unichar.h" |
|
35 #include "nsTString.h" |
|
36 #include "string-template-undef.h" |
|
37 |
|
38 // declare nsCString, et. al. |
|
39 #include "string-template-def-char.h" |
|
40 #include "nsTString.h" |
|
41 #include "string-template-undef.h" |
|
42 |
|
43 static_assert(sizeof(char16_t) == 2, "size of char16_t must be 2"); |
|
44 static_assert(sizeof(nsString::char_type) == 2, |
|
45 "size of nsString::char_type must be 2"); |
|
46 static_assert(nsString::char_type(-1) > nsString::char_type(0), |
|
47 "nsString::char_type must be unsigned"); |
|
48 static_assert(sizeof(nsCString::char_type) == 1, |
|
49 "size of nsCString::char_type must be 1"); |
|
50 |
|
51 |
|
52 /** |
|
53 * A helper class that converts a UTF-16 string to ASCII in a lossy manner |
|
54 */ |
|
55 class NS_LossyConvertUTF16toASCII : public nsAutoCString |
|
56 { |
|
57 public: |
|
58 explicit |
|
59 NS_LossyConvertUTF16toASCII( const char16_t* aString ) |
|
60 { |
|
61 LossyAppendUTF16toASCII(aString, *this); |
|
62 } |
|
63 |
|
64 NS_LossyConvertUTF16toASCII( const char16_t* aString, uint32_t aLength ) |
|
65 { |
|
66 LossyAppendUTF16toASCII(Substring(aString, aLength), *this); |
|
67 } |
|
68 |
|
69 #ifdef MOZ_USE_CHAR16_WRAPPER |
|
70 explicit |
|
71 NS_LossyConvertUTF16toASCII( char16ptr_t aString ) |
|
72 : NS_LossyConvertUTF16toASCII(static_cast<const char16_t*>(aString)) {} |
|
73 |
|
74 NS_LossyConvertUTF16toASCII( char16ptr_t aString, uint32_t aLength ) |
|
75 : NS_LossyConvertUTF16toASCII(static_cast<const char16_t*>(aString), aLength) {} |
|
76 #endif |
|
77 |
|
78 explicit |
|
79 NS_LossyConvertUTF16toASCII( const nsAString& aString ) |
|
80 { |
|
81 LossyAppendUTF16toASCII(aString, *this); |
|
82 } |
|
83 |
|
84 private: |
|
85 // NOT TO BE IMPLEMENTED |
|
86 NS_LossyConvertUTF16toASCII( char ); |
|
87 }; |
|
88 |
|
89 |
|
90 class NS_ConvertASCIItoUTF16 : public nsAutoString |
|
91 { |
|
92 public: |
|
93 explicit |
|
94 NS_ConvertASCIItoUTF16( const char* aCString ) |
|
95 { |
|
96 AppendASCIItoUTF16(aCString, *this); |
|
97 } |
|
98 |
|
99 NS_ConvertASCIItoUTF16( const char* aCString, uint32_t aLength ) |
|
100 { |
|
101 AppendASCIItoUTF16(Substring(aCString, aLength), *this); |
|
102 } |
|
103 |
|
104 explicit |
|
105 NS_ConvertASCIItoUTF16( const nsACString& aCString ) |
|
106 { |
|
107 AppendASCIItoUTF16(aCString, *this); |
|
108 } |
|
109 |
|
110 private: |
|
111 // NOT TO BE IMPLEMENTED |
|
112 NS_ConvertASCIItoUTF16( char16_t ); |
|
113 }; |
|
114 |
|
115 |
|
116 /** |
|
117 * A helper class that converts a UTF-16 string to UTF-8 |
|
118 */ |
|
119 class NS_ConvertUTF16toUTF8 : public nsAutoCString |
|
120 { |
|
121 public: |
|
122 explicit |
|
123 NS_ConvertUTF16toUTF8( const char16_t* aString ) |
|
124 { |
|
125 AppendUTF16toUTF8(aString, *this); |
|
126 } |
|
127 |
|
128 NS_ConvertUTF16toUTF8( const char16_t* aString, uint32_t aLength ) |
|
129 { |
|
130 AppendUTF16toUTF8(Substring(aString, aLength), *this); |
|
131 } |
|
132 |
|
133 #ifdef MOZ_USE_CHAR16_WRAPPER |
|
134 NS_ConvertUTF16toUTF8( char16ptr_t aString ) : NS_ConvertUTF16toUTF8(static_cast<const char16_t*>(aString)) {} |
|
135 |
|
136 NS_ConvertUTF16toUTF8( char16ptr_t aString, uint32_t aLength ) |
|
137 : NS_ConvertUTF16toUTF8(static_cast<const char16_t*>(aString), aLength) {} |
|
138 #endif |
|
139 |
|
140 explicit |
|
141 NS_ConvertUTF16toUTF8( const nsAString& aString ) |
|
142 { |
|
143 AppendUTF16toUTF8(aString, *this); |
|
144 } |
|
145 |
|
146 private: |
|
147 // NOT TO BE IMPLEMENTED |
|
148 NS_ConvertUTF16toUTF8( char ); |
|
149 }; |
|
150 |
|
151 |
|
152 class NS_ConvertUTF8toUTF16 : public nsAutoString |
|
153 { |
|
154 public: |
|
155 explicit |
|
156 NS_ConvertUTF8toUTF16( const char* aCString ) |
|
157 { |
|
158 AppendUTF8toUTF16(aCString, *this); |
|
159 } |
|
160 |
|
161 NS_ConvertUTF8toUTF16( const char* aCString, uint32_t aLength ) |
|
162 { |
|
163 AppendUTF8toUTF16(Substring(aCString, aLength), *this); |
|
164 } |
|
165 |
|
166 explicit |
|
167 NS_ConvertUTF8toUTF16( const nsACString& aCString ) |
|
168 { |
|
169 AppendUTF8toUTF16(aCString, *this); |
|
170 } |
|
171 |
|
172 private: |
|
173 // NOT TO BE IMPLEMENTED |
|
174 NS_ConvertUTF8toUTF16( char16_t ); |
|
175 }; |
|
176 |
|
177 |
|
178 #ifdef MOZ_USE_CHAR16_WRAPPER |
|
179 |
|
180 inline char16_t* |
|
181 wwc(wchar_t *str) |
|
182 { |
|
183 return reinterpret_cast<char16_t*>(str); |
|
184 } |
|
185 |
|
186 inline wchar_t* |
|
187 wwc(char16_t *str) |
|
188 { |
|
189 return reinterpret_cast<wchar_t*>(str); |
|
190 } |
|
191 |
|
192 #else |
|
193 |
|
194 inline char16_t* |
|
195 wwc(char16_t *str) |
|
196 { |
|
197 return str; |
|
198 } |
|
199 |
|
200 #endif |
|
201 |
|
202 // the following are included/declared for backwards compatibility |
|
203 typedef nsAutoString nsVoidableString; |
|
204 |
|
205 #include "nsDependentString.h" |
|
206 #include "nsLiteralString.h" |
|
207 #include "nsPromiseFlatString.h" |
|
208 |
|
209 // need to include these for backwards compatibility |
|
210 #include "nsMemory.h" |
|
211 #include <string.h> |
|
212 #include <stdio.h> |
|
213 #include "plhash.h" |
|
214 |
|
215 #endif // !defined(nsString_h___) |