xpcom/string/public/nsString.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial