Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / xpcom / string / nsTSubstringTuple.cpp
blob3219cb19faeeca57fe730aa1abcf27a0d0c02aa2
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"
10 /**
11 * computes the aggregate string length
14 template <typename T>
15 typename nsTSubstringTuple<T>::size_type nsTSubstringTuple<T>::Length() const {
16 mozilla::CheckedInt<size_type> len;
17 if (mHead) {
18 len = mHead->Length();
19 } else {
20 len = mFragA->Length();
23 len += mFragB->Length();
24 MOZ_RELEASE_ASSERT(len.isValid(), "Substring tuple length is invalid");
25 return len.value();
28 /**
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.
34 template <typename T>
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();
38 if (mHead) {
39 mHead->WriteTo(aBuf, headLen);
40 } else {
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());
48 /**
49 * returns true if this tuple is dependent on (i.e., overlapping with)
50 * the given char sequence.
53 template <typename T>
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)) {
59 return true;
62 if (mHead) {
63 return mHead->IsDependentOn(aStart, aEnd);
66 return mFragA->IsDependentOn(aStart, aEnd);
69 template <typename T>
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
74 // dependency.
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>;