Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
string_buffer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Roc Streaming authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_core/string_buffer.h
10//! @brief String buffer.
11
12#ifndef ROC_CORE_STRING_BUFFER_H_
13#define ROC_CORE_STRING_BUFFER_H_
14
15#include "roc_core/array.h"
16#include "roc_core/attributes.h"
17#include "roc_core/iarena.h"
19#include "roc_core/panic.h"
20
21namespace roc {
22namespace core {
23
24//! String buffer.
25//!
26//! Dynamic array storing zero-terminated string. Works on top of Array,
27//! but guarantees that the string is always zero-terminated.
28//!
29//! @tparam EmbeddedCapacity is the same as for Array.
30class StringBuffer : public NonCopyable<> {
31public:
32 //! Initialize empty buffer.
33 explicit StringBuffer(IArena& arena);
34
35 //! Check if buffer is empty.
36 bool is_empty() const;
37
38 //! Get string length, excluding terminating zero.
39 size_t len() const;
40
41 //! Get zero-terminated string.
42 const char* c_str() const;
43
44 //! Set buffer to empty string.
45 void clear();
46
47 //! Copy given string into buffer.
48 //! @p str should be zero-terminated.
49 //! @returns
50 //! false if allocation failed.
51 ROC_ATTR_NODISCARD bool assign(const char* str);
52
53 //! Copy given range into buffer.
54 //! Buffer will be automatically zero-terminated.
55 //! @returns
56 //! false if allocation failed.
57 ROC_ATTR_NODISCARD bool assign(const char* str_begin, const char* str_end);
58
59 //! Extend buffer by requested number of characters.
60 //! @remarks
61 //! Characters are appended to the buffer and filled with zeros.
62 //! It's the caller responsibility to fill them.
63 //! @returns
64 //! NULL if allocation failed.
66
67 //! Grow capacity to be able to hold desired number of characters.
68 //! Capacity is increased linearly.
69 //! @returns
70 //! false if allocation failed.
72
73 //! Grow capacity to be able to hold desired number of characters.
74 //! Capacity is increased exponentially.
75 //! @returns
76 //! false if allocation failed.
78
79private:
80 Array<char, 32> data_;
81};
82
83} // namespace core
84} // namespace roc
85
86#endif // ROC_CORE_STRING_BUFFER_H_
Dynamic array.
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition attributes.h:31
Memory arena interface.
Definition iarena.h:23
Base class for non-copyable objects.
Definition noncopyable.h:23
Shared ownership intrusive pointer.
Definition shared_ptr.h:32
size_t len() const
Get string length, excluding terminating zero.
bool assign(const char *str)
Copy given string into buffer. str should be zero-terminated.
char * extend(size_t n_chars)
Extend buffer by requested number of characters.
bool grow(size_t desired_len)
Grow capacity to be able to hold desired number of characters. Capacity is increased linearly.
void clear()
Set buffer to empty string.
bool is_empty() const
Check if buffer is empty.
bool assign(const char *str_begin, const char *str_end)
Copy given range into buffer. Buffer will be automatically zero-terminated.
bool grow_exp(size_t desired_len)
Grow capacity to be able to hold desired number of characters. Capacity is increased exponentially.
StringBuffer(IArena &arena)
Initialize empty buffer.
const char * c_str() const
Get zero-terminated string.
Memory arena interface.
Root namespace.
Non-copyable object.
Panic.