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 #include "nsTSubstringTuple.h"
8 #include "mozilla/CheckedInt.h"
11 * computes the aggregate string length
15 typename nsTSubstringTuple
<T
>::size_type nsTSubstringTuple
<T
>::Length() const {
16 mozilla::CheckedInt
<size_type
> len
;
18 len
= mHead
->Length();
20 len
= mFragA
->Length();
23 len
+= mFragB
->Length();
24 MOZ_RELEASE_ASSERT(len
.isValid(), "Substring tuple length is invalid");
29 * writes the aggregate string to the given buffer. aBufLen is assumed
30 * to be equal to or greater than the value returned by the Length()
31 * method. the string written to |aBuf| is not null-terminated.
35 void nsTSubstringTuple
<T
>::WriteTo(char_type
* aBuf
, size_type aBufLen
) const {
36 MOZ_RELEASE_ASSERT(aBufLen
>= mFragB
->Length(), "buffer too small");
37 size_type headLen
= aBufLen
- mFragB
->Length();
39 mHead
->WriteTo(aBuf
, headLen
);
41 MOZ_RELEASE_ASSERT(mFragA
->Length() == headLen
, "buffer incorrectly sized");
42 char_traits::copy(aBuf
, mFragA
->Data(), mFragA
->Length());
45 char_traits::copy(aBuf
+ headLen
, mFragB
->Data(), mFragB
->Length());
49 * returns true if this tuple is dependent on (i.e., overlapping with)
50 * the given char sequence.
54 bool nsTSubstringTuple
<T
>::IsDependentOn(const char_type
* aStart
,
55 const char_type
* aEnd
) const {
56 // we start with the right-most fragment since it is faster to check.
58 if (mFragB
->IsDependentOn(aStart
, aEnd
)) {
63 return mHead
->IsDependentOn(aStart
, aEnd
);
66 return mFragA
->IsDependentOn(aStart
, aEnd
);
70 auto nsTSubstringTuple
<T
>::IsDependentOnWithLength(const char_type
* aStart
,
71 const char_type
* aEnd
) const
72 -> std::pair
<bool, size_type
> {
73 // we start with the right-most fragment since it is faster to check for
75 const bool rightDependentOn
= mFragB
->IsDependentOn(aStart
, aEnd
);
77 if (rightDependentOn
) {
78 return {true, Length()};
81 const auto [leftDependentOn
, leftLen
] =
82 mHead
? mHead
->IsDependentOnWithLength(aStart
, aEnd
)
83 : std::pair
{mFragA
->IsDependentOn(aStart
, aEnd
), mFragA
->Length()};
85 const auto checkedLen
=
86 mozilla::CheckedInt
<size_type
>{leftLen
} + mFragB
->Length();
87 MOZ_RELEASE_ASSERT(checkedLen
.isValid(), "Substring tuple length is invalid");
88 return {leftDependentOn
, checkedLen
.value()};
91 template class nsTSubstringTuple
<char>;
92 template class nsTSubstringTuple
<char16_t
>;