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 // FIXME: Due to an include cycle, we need to include `nsTSubstring` first.
8 #include "nsTSubstring.h"
9 #include "nsTDependentSubstring.h"
12 void nsTDependentSubstring
<T
>::Rebind(const substring_type
& str
,
13 size_type startPos
, size_type length
) {
14 // If we currently own a buffer, release it.
17 size_type strLength
= str
.Length();
19 if (startPos
> strLength
) {
24 const_cast<char_type
*>(static_cast<const char_type
*>(str
.Data())) +
26 size_type newLength
= XPCOM_MIN(length
, strLength
- startPos
);
27 DataFlags newDataFlags
= DataFlags(0);
28 this->SetData(newData
, newLength
, newDataFlags
);
32 void nsTDependentSubstring
<T
>::Rebind(const char_type
* data
, size_type length
) {
33 NS_ASSERTION(data
, "nsTDependentSubstring must wrap a non-NULL buffer");
35 // If we currently own a buffer, release it.
39 const_cast<char_type
*>(static_cast<const char_type
*>(data
));
40 size_type newLength
= length
;
41 DataFlags newDataFlags
= DataFlags(0);
42 this->SetData(newData
, newLength
, newDataFlags
);
46 void nsTDependentSubstring
<T
>::Rebind(const char_type
* aStart
,
47 const char_type
* aEnd
) {
48 MOZ_RELEASE_ASSERT(aStart
<= aEnd
, "Overflow!");
49 this->Rebind(aStart
, size_type(aEnd
- aStart
));
53 nsTDependentSubstring
<T
>::nsTDependentSubstring(const char_type
* aStart
,
54 const char_type
* aEnd
)
55 : substring_type(const_cast<char_type
*>(aStart
), aEnd
- aStart
,
56 DataFlags(0), ClassFlags(0)) {
57 MOZ_RELEASE_ASSERT(aStart
<= aEnd
, "Overflow!");
60 #if defined(MOZ_USE_CHAR16_WRAPPER)
62 template <typename Q
, typename EnableIfChar16
>
63 nsTDependentSubstring
<T
>::nsTDependentSubstring(char16ptr_t aStart
,
65 : substring_type(static_cast<const char16_t
*>(aStart
),
66 static_cast<const char16_t
*>(aEnd
)) {
67 MOZ_RELEASE_ASSERT(static_cast<const char16_t
*>(aStart
) <=
68 static_cast<const char16_t
*>(aEnd
),
74 nsTDependentSubstring
<T
>::nsTDependentSubstring(const const_iterator
& aStart
,
75 const const_iterator
& aEnd
)
76 : substring_type(const_cast<char_type
*>(aStart
.get()),
77 aEnd
.get() - aStart
.get(), DataFlags(0), ClassFlags(0)) {
78 MOZ_RELEASE_ASSERT(aStart
.get() <= aEnd
.get(), "Overflow!");
82 const nsTDependentSubstring
<T
> Substring(const T
* aStart
, const T
* aEnd
) {
83 MOZ_RELEASE_ASSERT(aStart
<= aEnd
, "Overflow!");
84 return nsTDependentSubstring
<T
>(aStart
, aEnd
);
87 template nsTDependentSubstring
<char> const Substring
<char>(char const*,
89 template nsTDependentSubstring
<char16_t
> const Substring
<char16_t
>(
90 char16_t
const*, char16_t
const*);
92 #if defined(MOZ_USE_CHAR16_WRAPPER)
93 const nsTDependentSubstring
<char16_t
> Substring(char16ptr_t aData
,
95 return nsTDependentSubstring
<char16_t
>(aData
, aLength
);
98 const nsTDependentSubstring
<char16_t
> Substring(char16ptr_t aStart
,
100 return Substring(static_cast<const char16_t
*>(aStart
),
101 static_cast<const char16_t
*>(aEnd
));
105 template class nsTDependentSubstring
<char>;
106 template class nsTDependentSubstring
<char16_t
>;