Skip to content

Commit

Permalink
Cosmetic fixes in StringBuffer and StringBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Oct 17, 2023
1 parent 01a0e27 commit c082d25
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/internal_modules/roc_address/endpoint_uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ bool EndpointUri::set_encoded_path(const char* str, size_t str_len) {
return false;
}

if (!b.ok()) {
if (!b.is_ok()) {
set_invalid_(PartPath);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal_modules/roc_address/io_uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool IoUri::set_encoded_path(const char* str, size_t str_len) {
return false;
}

if (!b.ok()) {
if (!b.is_ok()) {
path_.clear();
return false;
}
Expand Down
12 changes: 11 additions & 1 deletion src/internal_modules/roc_core/string_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,34 @@ class StringBuffer : public NonCopyable<> {

//! Copy given string into buffer.
//! @p str should be zero-terminated.
//! @returns
//! false if allocation failed.
ROC_ATTR_NODISCARD bool assign(const char* str);

//! Copy given range into buffer.
//! Buffer will be automatically zero-terminated.
//! @returns
//! false if allocation failed.
ROC_ATTR_NODISCARD bool assign(const char* str_begin, const char* str_end);

//! Extend buffer by requested number of characters.
//! @remarks
//! Characters are appended to the buffer and filled with zeros.
//! It's the caller responsibility to fill them.
char* extend(size_t n_chars);
//! @returns
//! NULL if allocation failed.
ROC_ATTR_NODISCARD char* extend(size_t n_chars);

//! Grow capacity to be able to hold desired number of characters.
//! Capacity is increased linearly.
//! @returns
//! false if allocation failed.
ROC_ATTR_NODISCARD bool grow(size_t desired_len);

//! Grow capacity to be able to hold desired number of characters.
//! Capacity is increased exponentionally.
//! @returns
//! false if allocation failed.
ROC_ATTR_NODISCARD bool grow_exp(size_t desired_len);

private:
Expand Down
8 changes: 4 additions & 4 deletions src/internal_modules/roc_core/string_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ size_t StringBuilder::actual_size() const {
return n_written_ + 1;
}

bool StringBuilder::ok() const {
bool StringBuilder::is_ok() const {
return !truncation_error_ && !write_error_;
}

Expand Down Expand Up @@ -216,7 +216,7 @@ bool StringBuilder::append_(const char* str, size_t str_size, bool grow) {
if (grow) {
if (!writer_->grow_by(str_size)) {
write_error_ = true;
return ok();
return is_ok();
}
}

Expand All @@ -225,7 +225,7 @@ bool StringBuilder::append_(const char* str, size_t str_size, bool grow) {

if (write_size < 0) {
write_error_ = true;
return ok();
return is_ok();
}

if (write_size > 0) {
Expand All @@ -241,7 +241,7 @@ bool StringBuilder::append_(const char* str, size_t str_size, bool grow) {
}
}

return ok();
return is_ok();
}

} // namespace core
Expand Down
7 changes: 3 additions & 4 deletions src/internal_modules/roc_core/string_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ class StringBuilder : public NonCopyable<> {
size_t actual_size() const;

//! Check for errors.
//!
//! @remark
//! Error flag is raised if any of the methods fail, and is resetted
//! @remarks
//! Error flag is raised if any of the methods fail, and is cleared
//! if an assign* method succeeds.
bool ok() const;
bool is_ok() const;

//! Overwrite result with given string.
//! If there is not enough space, truncates the string and returns false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void BasicPort::update_descriptor() {

format_descriptor(b);

if (!b.ok() || b.actual_size() == 0) {
if (!b.is_ok() || b.actual_size() == 0) {
roc_panic("basic port: failed to format descriptor");
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/public_api/src/endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ int roc_endpoint_get_uri(const roc_endpoint* endpoint, char* buf, size_t* bufsz)
return -1;
}

if (!b.ok()) {
if (!b.is_ok()) {
roc_log(LogError,
"roc_endpoint_get_uri(): buffer too small: provided=%lu needed=%lu",
(unsigned long)*bufsz, (unsigned long)b.needed_size());
Expand Down Expand Up @@ -213,7 +213,7 @@ int roc_endpoint_get_host(const roc_endpoint* endpoint, char* buf, size_t* bufsz
return -1;
}

if (!b.ok()) {
if (!b.is_ok()) {
roc_log(LogError,
"roc_endpoint_get_host(): buffer too small: provided=%lu needed=%lu",
(unsigned long)*bufsz, (unsigned long)b.needed_size());
Expand Down Expand Up @@ -269,7 +269,7 @@ int roc_endpoint_get_resource(const roc_endpoint* endpoint, char* buf, size_t* b
return -1;
}

if (!b.ok()) {
if (!b.is_ok()) {
roc_log(LogError,
"roc_endpoint_get_resource(): buffer too small: provided=%lu needed=%lu",
(unsigned long)*bufsz, (unsigned long)b.needed_size());
Expand Down
4 changes: 2 additions & 2 deletions src/tests/roc_address/test_endpoint_uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,14 @@ TEST(endpoint_uri, small_buffer) {
core::StringBuilder b(buf, sizeof(buf));

CHECK(format_endpoint_uri(u, EndpointUri::Subset_Full, b));
CHECK(b.ok());
CHECK(b.is_ok());
}

for (size_t i = 0; i < sizeof(buf); i++) {
core::StringBuilder b(buf, i);

CHECK(format_endpoint_uri(u, EndpointUri::Subset_Full, b));
CHECK(!b.ok());
CHECK(!b.is_ok());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/tests/roc_address/test_io_uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ TEST(io_uri, small_buffer) {
core::StringBuilder b(buf, sizeof(buf));

CHECK(format_io_uri(u, b));
CHECK(b.ok());
CHECK(b.is_ok());
}

for (size_t i = 0; i < sizeof(buf); i++) {
core::StringBuilder b(buf, i);

CHECK(format_io_uri(u, b));
CHECK(!b.ok());
CHECK(!b.is_ok());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/tests/roc_address/test_pct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ssize_t encode(char* dst, size_t dst_sz, const char* src, size_t src_sz, PctMode
return -1;
}

if (!b.ok()) {
if (!b.is_ok()) {
return -1;
}

Expand All @@ -37,7 +37,7 @@ ssize_t decode(char* dst, size_t dst_sz, const char* src, size_t src_sz) {
return -1;
}

if (!b.ok()) {
if (!b.is_ok()) {
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/roc_core/test_hashmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ TEST_GROUP(hashmap) {
StringBuilder b(key, keysz);
CHECK(b.append_str("key"));
CHECK(b.append_uint((uint64_t)n, 10));
CHECK(b.ok());
CHECK(b.is_ok());
}

template <size_t Capacity> void test_embedded_capacity() {
Expand Down
Loading

0 comments on commit c082d25

Please sign in to comment.