Backed out 4 changesets (bug 1522790) - backout the remaining changesets. CLOSED...
[gecko.git] / xpcom / string / nsString.h
blobe86ea594ac3052a417b21b0d2e7674f9f9199fb5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
7 #ifndef nsString_h___
8 #define nsString_h___
10 #include <ostream>
12 #include "mozilla/Attributes.h"
14 #include "nsStringFwd.h"
16 #include "nsAString.h"
17 #include "nsDependentSubstring.h"
18 #include "nsReadableUtils.h"
20 #include "nsTString.h"
22 static_assert(sizeof(char16_t) == 2, "size of char16_t must be 2");
23 static_assert(sizeof(nsString::char_type) == 2,
24 "size of nsString::char_type must be 2");
25 static_assert(nsString::char_type(-1) > nsString::char_type(0),
26 "nsString::char_type must be unsigned");
27 static_assert(sizeof(nsCString::char_type) == 1,
28 "size of nsCString::char_type must be 1");
30 static_assert(sizeof(nsTLiteralString<char>) == sizeof(nsTString<char>),
31 "nsLiteralCString can masquerade as nsCString, "
32 "so they must have identical layout");
34 static_assert(sizeof(nsTLiteralString<char16_t>) == sizeof(nsTString<char16_t>),
35 "nsTLiteralString can masquerade as nsString, "
36 "so they must have identical layout");
38 /**
39 * A helper class that converts a UTF-16 string to ASCII in a lossy manner
41 class NS_LossyConvertUTF16toASCII : public nsAutoCString {
42 public:
43 explicit NS_LossyConvertUTF16toASCII(const char16ptr_t aString) {
44 LossyAppendUTF16toASCII(mozilla::MakeStringSpan(aString), *this);
47 NS_LossyConvertUTF16toASCII(const char16ptr_t aString, size_t aLength) {
48 LossyAppendUTF16toASCII(
49 Substring(static_cast<const char16_t*>(aString), aLength), *this);
52 explicit NS_LossyConvertUTF16toASCII(const nsAString& aString) {
53 LossyAppendUTF16toASCII(aString, *this);
56 private:
57 // NOT TO BE IMPLEMENTED
58 NS_LossyConvertUTF16toASCII(char) = delete;
61 class NS_ConvertASCIItoUTF16 : public nsAutoString {
62 public:
63 explicit NS_ConvertASCIItoUTF16(const char* aCString) {
64 AppendASCIItoUTF16(mozilla::MakeStringSpan(aCString), *this);
67 NS_ConvertASCIItoUTF16(const char* aCString, size_t aLength) {
68 AppendASCIItoUTF16(Substring(aCString, aLength), *this);
71 explicit NS_ConvertASCIItoUTF16(const nsACString& aCString) {
72 AppendASCIItoUTF16(aCString, *this);
75 explicit NS_ConvertASCIItoUTF16(mozilla::Span<const char> aCString) {
76 AppendASCIItoUTF16(aCString, *this);
79 private:
80 // NOT TO BE IMPLEMENTED
81 NS_ConvertASCIItoUTF16(char16_t) = delete;
84 /**
85 * A helper class that converts a UTF-16 string to UTF-8
87 class NS_ConvertUTF16toUTF8 : public nsAutoCString {
88 public:
89 explicit NS_ConvertUTF16toUTF8(const char16ptr_t aString) {
90 AppendUTF16toUTF8(mozilla::MakeStringSpan(aString), *this);
93 NS_ConvertUTF16toUTF8(const char16ptr_t aString, size_t aLength) {
94 AppendUTF16toUTF8(Substring(static_cast<const char16_t*>(aString), aLength),
95 *this);
98 explicit NS_ConvertUTF16toUTF8(const nsAString& aString) {
99 AppendUTF16toUTF8(aString, *this);
102 explicit NS_ConvertUTF16toUTF8(mozilla::Span<const char16_t> aString) {
103 AppendUTF16toUTF8(aString, *this);
106 private:
107 // NOT TO BE IMPLEMENTED
108 NS_ConvertUTF16toUTF8(char) = delete;
111 class NS_ConvertUTF8toUTF16 : public nsAutoString {
112 public:
113 explicit NS_ConvertUTF8toUTF16(const char* aCString) {
114 AppendUTF8toUTF16(mozilla::MakeStringSpan(aCString), *this);
117 NS_ConvertUTF8toUTF16(const char* aCString, size_t aLength) {
118 AppendUTF8toUTF16(Substring(aCString, aLength), *this);
121 explicit NS_ConvertUTF8toUTF16(const nsACString& aCString) {
122 AppendUTF8toUTF16(aCString, *this);
125 explicit NS_ConvertUTF8toUTF16(mozilla::Span<const char> aCString) {
126 AppendUTF8toUTF16(aCString, *this);
129 private:
130 // NOT TO BE IMPLEMENTED
131 NS_ConvertUTF8toUTF16(char16_t) = delete;
135 * Converts an integer (signed/unsigned, 32/64bit) to its decimal string
136 * representation and returns it as an nsAutoCString/nsAutoString.
138 template <typename T, typename U>
139 nsTAutoString<T> IntToTString(const U aInt, const int aRadix = 10) {
140 nsTAutoString<T> string;
141 string.AppendInt(aInt, aRadix);
142 return string;
145 template <typename U>
146 nsAutoCString IntToCString(const U aInt, const int aRadix = 10) {
147 return IntToTString<char>(aInt, aRadix);
150 template <typename U>
151 nsAutoString IntToString(const U aInt, const int aRadix = 10) {
152 return IntToTString<char16_t>(aInt, aRadix);
155 // MOZ_DBG support
157 inline std::ostream& operator<<(std::ostream& aOut, const nsACString& aString) {
158 aOut.write(aString.Data(), aString.Length());
159 return aOut;
162 inline std::ostream& operator<<(std::ostream& aOut, const nsAString& aString) {
163 return aOut << NS_ConvertUTF16toUTF8(aString);
166 // the following are included/declared for backwards compatibility
167 #include "nsDependentString.h"
168 #include "nsLiteralString.h"
169 #include "nsPromiseFlatString.h"
171 #endif // !defined(nsString_h___)