diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /intl/components/src/FormatBuffer.h | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'intl/components/src/FormatBuffer.h')
-rw-r--r-- | intl/components/src/FormatBuffer.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/intl/components/src/FormatBuffer.h b/intl/components/src/FormatBuffer.h new file mode 100644 index 0000000000..774e74d2ba --- /dev/null +++ b/intl/components/src/FormatBuffer.h @@ -0,0 +1,77 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef intl_components_FormatBuffer_h +#define intl_components_FormatBuffer_h + +/** + * This file contains public adaptors for the mozilla::intl Buffer template + * argument. Adaptors that can automatically be deduced are kept as private + * in ICU4CGlue.h. There is also the SpiderMonkey specific adaptor + * js::intl::FormatBuffer in js/src/builtin/intl/FormatBuffer.h. + */ + +#include "nsTString.h" + +namespace mozilla::intl { + +/** + * mozilla::intl APIs require sizeable buffers. This class abstracts over + * the nsTSubstring. + */ +template <typename T> +class nsTStringToBufferAdapter { + public: + using CharType = T; + + // Do not allow copy or move. Move could be added in the future if needed. + nsTStringToBufferAdapter(const nsTStringToBufferAdapter&) = delete; + nsTStringToBufferAdapter& operator=(const nsTStringToBufferAdapter&) = delete; + + explicit nsTStringToBufferAdapter(nsTSubstring<CharType>& aString) + : mString(aString) {} + + /** + * Ensures the buffer has enough space to accommodate |size| elements. + */ + [[nodiscard]] bool reserve(size_t size) { + return mString.SetLength(size, fallible); + } + + /** + * Returns the raw data inside the buffer. + */ + CharType* data() { return mString.BeginWriting(); } + + /** + * Returns the count of elements written into the buffer. + */ + size_t length() const { return mString.Length(); } + + /** + * Returns the buffer's overall capacity. + */ + size_t capacity() const { + // nsString's Capacity() method is protected, so just return the length. + return mString.Length(); + } + + /** + * Resizes the buffer to the given amount of written elements. + */ + void written(size_t amount) { + MOZ_ASSERT(amount <= mString.Length()); + // This sets |mString|'s internal size so that it matches how much was + // written. This is necessary because the write happens across FFI + // boundaries. + mString.SetLength(amount); + } + + private: + nsTSubstring<CharType>& mString; +}; + +} // namespace mozilla::intl + +#endif /* intl_components_FormatBuffer_h */ |