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/. */
6 // IWYU pragma: private, include "nsString.h"
8 #ifndef nsTDependentSubstring_h
9 #define nsTDependentSubstring_h
11 #include "nsTSubstring.h"
12 #include "nsTLiteralString.h"
13 #include "mozilla/Span.h"
16 * nsTDependentSubstring_CharT
18 * A string class which wraps an external array of string characters. It
19 * is the client code's responsibility to ensure that the external buffer
20 * remains valid for a long as the string is alive.
23 * nsDependentSubstring for wide characters
24 * nsDependentCSubstring for narrow characters
27 class nsTDependentSubstring
: public nsTSubstring
<T
> {
29 typedef nsTDependentSubstring
<T
> self_type
;
30 typedef nsTSubstring
<T
> substring_type
;
31 typedef typename
substring_type::fallible_t fallible_t
;
33 typedef typename
substring_type::char_type char_type
;
34 typedef typename
substring_type::char_traits char_traits
;
36 typename
substring_type::incompatible_char_type incompatible_char_type
;
38 typedef typename
substring_type::substring_tuple_type substring_tuple_type
;
40 typedef typename
substring_type::const_iterator const_iterator
;
41 typedef typename
substring_type::iterator iterator
;
43 typedef typename
substring_type::comparator_type comparator_type
;
45 typedef typename
substring_type::const_char_iterator const_char_iterator
;
47 typedef typename
substring_type::string_view string_view
;
49 typedef typename
substring_type::index_type index_type
;
50 typedef typename
substring_type::size_type size_type
;
52 // These are only for internal use within the string classes:
53 typedef typename
substring_type::DataFlags DataFlags
;
54 typedef typename
substring_type::ClassFlags ClassFlags
;
57 void Rebind(const substring_type
&, size_type aStartPos
,
58 size_type aLength
= size_type(-1));
60 void Rebind(const char_type
* aData
, size_type aLength
);
62 void Rebind(const char_type
* aStart
, const char_type
* aEnd
);
64 nsTDependentSubstring(const substring_type
& aStr
, size_type aStartPos
,
65 size_type aLength
= size_type(-1))
67 Rebind(aStr
, aStartPos
, aLength
);
70 nsTDependentSubstring(const char_type
* aData
, size_type aLength
)
71 : substring_type(const_cast<char_type
*>(aData
), aLength
, DataFlags(0),
74 explicit nsTDependentSubstring(mozilla::Span
<const char_type
> aData
)
75 : nsTDependentSubstring(aData
.Elements(), aData
.Length()) {}
77 nsTDependentSubstring(const char_type
* aStart
, const char_type
* aEnd
);
79 #if defined(MOZ_USE_CHAR16_WRAPPER)
80 template <typename Q
= T
, typename EnableIfChar16
= mozilla::Char16OnlyT
<Q
>>
81 nsTDependentSubstring(char16ptr_t aData
, size_type aLength
)
82 : nsTDependentSubstring(static_cast<const char16_t
*>(aData
), aLength
) {}
84 template <typename Q
= T
, typename EnableIfChar16
= mozilla::Char16OnlyT
<Q
>>
85 nsTDependentSubstring(char16ptr_t aStart
, char16ptr_t aEnd
);
88 nsTDependentSubstring(const const_iterator
& aStart
,
89 const const_iterator
& aEnd
);
91 // Create a nsTDependentSubstring to be bound later
92 nsTDependentSubstring() : substring_type() {}
94 // auto-generated copy-constructor OK (XXX really?? what about base class
96 nsTDependentSubstring(const nsTDependentSubstring
&) = default;
100 void operator=(const self_type
&) =
101 delete; // we're immutable, you can't assign into a substring
104 extern template class nsTDependentSubstring
<char>;
105 extern template class nsTDependentSubstring
<char16_t
>;
107 template <typename T
>
108 inline const nsTDependentSubstring
<T
> Substring(const nsTSubstring
<T
>& aStr
,
110 size_t aLength
= size_t(-1)) {
111 return nsTDependentSubstring
<T
>(aStr
, aStartPos
, aLength
);
114 template <typename T
>
115 inline const nsTDependentSubstring
<T
> Substring(const nsTLiteralString
<T
>& aStr
,
117 size_t aLength
= size_t(-1)) {
118 return nsTDependentSubstring
<T
>(aStr
, aStartPos
, aLength
);
121 template <typename T
>
122 inline const nsTDependentSubstring
<T
> Substring(
123 const nsReadingIterator
<T
>& aStart
, const nsReadingIterator
<T
>& aEnd
) {
124 return nsTDependentSubstring
<T
>(aStart
.get(), aEnd
.get());
127 template <typename T
>
128 inline const nsTDependentSubstring
<T
> Substring(const T
* aData
,
130 return nsTDependentSubstring
<T
>(aData
, aLength
);
133 template <typename T
>
134 const nsTDependentSubstring
<T
> Substring(const T
* aStart
, const T
* aEnd
);
136 extern template const nsTDependentSubstring
<char> Substring(const char* aStart
,
139 extern template const nsTDependentSubstring
<char16_t
> Substring(
140 const char16_t
* aStart
, const char16_t
* aEnd
);
142 #if defined(MOZ_USE_CHAR16_WRAPPER)
143 inline const nsTDependentSubstring
<char16_t
> Substring(char16ptr_t aData
,
146 const nsTDependentSubstring
<char16_t
> Substring(char16ptr_t aStart
,
150 template <typename T
>
151 inline const nsTDependentSubstring
<T
> StringHead(const nsTSubstring
<T
>& aStr
,
153 return nsTDependentSubstring
<T
>(aStr
, 0, aCount
);
156 template <typename T
>
157 inline const nsTDependentSubstring
<T
> StringTail(const nsTSubstring
<T
>& aStr
,
159 return nsTDependentSubstring
<T
>(aStr
, aStr
.Length() - aCount
, aCount
);