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/iallocator.h"
18#include "roc_core/panic.h"
19
20namespace roc {
21namespace core {
22
23//! String buffer.
24//!
25//! Dynamic array storing zero-terminated string. Works on top of Array,
26//! but guarantees that the string is always zero-terminated.
27//!
28//! @tparam EmbeddedCapacity is the same as for Array.
29template <size_t EmbeddedCapacity = 0> class StringBuffer : public NonCopyable<> {
30public:
31 //! Initialize empty buffer.
32 explicit StringBuffer(IAllocator& allocator)
33 : data_(allocator) {
34 clear();
35 }
36
37 //! Check if buffer is empty.
38 bool is_empty() const {
39 return len() == 0;
40 }
41
42 //! Get string length, excluding terminating zero.
43 size_t len() const {
44 return data_.size() - 1;
45 }
46
47 //! Get zero-terminated string.
48 const char* c_str() const {
49 return data_.data();
50 }
51
52 //! Set buffer to empty string.
53 void clear() {
54 data_.resize(1);
55 data_[0] = '\0';
56 }
57
58 //! Copy given string into buffer.
59 //! @p str should be zero-terminated.
60 bool assign(const char* str) {
62
63 return assign(str, str + strlen(str));
64 }
65
66 //! Copy given range into buffer.
67 //! Buffer will be automatically zero-terminated.
68 bool assign(const char* str_begin, const char* str_end) {
69 roc_panic_if_not(str_begin);
70 roc_panic_if_not(str_begin <= str_end);
71
72 const size_t str_sz = size_t(str_end - str_begin);
73
74 if (!data_.resize(str_sz + 1)) {
75 clear();
76 return false;
77 }
78
79 if (str_sz != 0) {
80 memcpy(data_.data(), str_begin, str_sz);
81 }
82 data_[str_sz] = '\0';
83
84 return true;
85 }
86
87 //! Extend buffer by requested number of characters.
88 //! @remarks
89 //! Characters are appended to the buffer and filled with zeros.
90 //! It's the caller responsibility to fill them.
91 char* extend(size_t n_chars) {
92 roc_panic_if_not(n_chars > 0);
93
94 const size_t orig_sz = data_.size();
95
96 if (!data_.resize(orig_sz + n_chars)) {
97 clear();
98 return NULL;
99 }
100
101 return data_.data() + orig_sz - 1;
102 }
103
104 //! Grow capacity to be able to hold desired number of characters.
105 //! Capacity is increased linearly.
106 bool grow(size_t desired_len) {
107 return data_.grow(desired_len + 1);
108 }
109
110 //! Grow capacity to be able to hold desired number of characters.
111 //! Capacity is increased exponentionally.
112 bool grow_exp(size_t desired_len) {
113 return data_.grow_exp(desired_len + 1);
114 }
115
116private:
118};
119
120} // namespace core
121} // namespace roc
122
123#endif // ROC_CORE_STRING_BUFFER_H_
Dynamic array.
Dynamic array.
Definition array.h:38
bool grow_exp(size_t min_size)
Increase array capacity exponentially.
Definition array.h:204
T * data()
Get pointer to first element.
Definition array.h:80
bool grow(size_t max_sz)
Increase array capacity.
Definition array.h:161
bool resize(size_t sz)
Set array size.
Definition array.h:134
size_t size() const
Get number of elements.
Definition array.h:73
Memory allocator interface.
Definition iallocator.h:23
Base class for non-copyable objects.
Definition noncopyable.h:23
void clear()
Set buffer to empty string.
bool grow_exp(size_t desired_len)
Grow capacity to be able to hold desired number of characters. Capacity is increased exponentionally.
StringBuffer(IAllocator &allocator)
Initialize empty buffer.
bool grow(size_t desired_len)
Grow capacity to be able to hold desired number of characters. Capacity is increased linearly.
bool assign(const char *str_begin, const char *str_end)
Copy given range into buffer. Buffer will be automatically zero-terminated.
const char * c_str() const
Get zero-terminated string.
char * extend(size_t n_chars)
Extend buffer by requested number of characters.
size_t len() const
Get string length, excluding terminating zero.
bool is_empty() const
Check if buffer is empty.
bool assign(const char *str)
Copy given string into buffer. str should be zero-terminated.
Memory allocator interface.
Root namespace.
Non-copyable object.
Panic.
#define roc_panic_if_not(x)
Panic if condition is false.
Definition panic.h:37