Bug 1869043 assert that graph set access is main thread only r=padenot
[gecko.git] / xpcom / string / nsTDependentSubstring.cpp
blobba1620f98b135fec65e915f14ac697d02ce2bc1e
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"
11 template <typename T>
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.
15 this->Finalize();
17 size_type strLength = str.Length();
19 if (startPos > strLength) {
20 startPos = strLength;
23 char_type* newData =
24 const_cast<char_type*>(static_cast<const char_type*>(str.Data())) +
25 startPos;
26 size_type newLength = XPCOM_MIN(length, strLength - startPos);
27 DataFlags newDataFlags = DataFlags(0);
28 this->SetData(newData, newLength, newDataFlags);
31 template <typename T>
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.
36 this->Finalize();
38 char_type* newData =
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);
45 template <typename T>
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));
52 template <typename T>
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)
61 template <typename T>
62 template <typename Q, typename EnableIfChar16>
63 nsTDependentSubstring<T>::nsTDependentSubstring(char16ptr_t aStart,
64 char16ptr_t aEnd)
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),
69 "Overflow!");
71 #endif
73 template <typename T>
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!");
81 template <typename T>
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*,
88 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,
94 size_t aLength) {
95 return nsTDependentSubstring<char16_t>(aData, aLength);
98 const nsTDependentSubstring<char16_t> Substring(char16ptr_t aStart,
99 char16ptr_t aEnd) {
100 return Substring(static_cast<const char16_t*>(aStart),
101 static_cast<const char16_t*>(aEnd));
103 #endif
105 template class nsTDependentSubstring<char>;
106 template class nsTDependentSubstring<char16_t>;