From c082d2506929f36af593e8d89b11b232c4da23a9 Mon Sep 17 00:00:00 2001 From: Victor Gaydov Date: Tue, 17 Oct 2023 16:02:00 +0400 Subject: [PATCH] Cosmetic fixes in StringBuffer and StringBuilder --- .../roc_address/endpoint_uri.cpp | 2 +- src/internal_modules/roc_address/io_uri.cpp | 2 +- src/internal_modules/roc_core/string_buffer.h | 12 ++- .../roc_core/string_builder.cpp | 8 +- .../roc_core/string_builder.h | 7 +- .../target_libuv/roc_netio/basic_port.cpp | 2 +- src/public_api/src/endpoint.cpp | 6 +- src/tests/roc_address/test_endpoint_uri.cpp | 4 +- src/tests/roc_address/test_io_uri.cpp | 4 +- src/tests/roc_address/test_pct.cpp | 4 +- src/tests/roc_core/test_hashmap.cpp | 2 +- src/tests/roc_core/test_string_builder.cpp | 78 +++++++++---------- 12 files changed, 70 insertions(+), 61 deletions(-) diff --git a/src/internal_modules/roc_address/endpoint_uri.cpp b/src/internal_modules/roc_address/endpoint_uri.cpp index 6368019d7..b440e498b 100644 --- a/src/internal_modules/roc_address/endpoint_uri.cpp +++ b/src/internal_modules/roc_address/endpoint_uri.cpp @@ -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; } diff --git a/src/internal_modules/roc_address/io_uri.cpp b/src/internal_modules/roc_address/io_uri.cpp index b87e6ca8f..45eddbf5c 100644 --- a/src/internal_modules/roc_address/io_uri.cpp +++ b/src/internal_modules/roc_address/io_uri.cpp @@ -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; } diff --git a/src/internal_modules/roc_core/string_buffer.h b/src/internal_modules/roc_core/string_buffer.h index cf94c3e4b..c1851ec54 100644 --- a/src/internal_modules/roc_core/string_buffer.h +++ b/src/internal_modules/roc_core/string_buffer.h @@ -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: diff --git a/src/internal_modules/roc_core/string_builder.cpp b/src/internal_modules/roc_core/string_builder.cpp index e265297ba..4841b1338 100644 --- a/src/internal_modules/roc_core/string_builder.cpp +++ b/src/internal_modules/roc_core/string_builder.cpp @@ -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_; } @@ -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(); } } @@ -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) { @@ -241,7 +241,7 @@ bool StringBuilder::append_(const char* str, size_t str_size, bool grow) { } } - return ok(); + return is_ok(); } } // namespace core diff --git a/src/internal_modules/roc_core/string_builder.h b/src/internal_modules/roc_core/string_builder.h index 01d249c82..8f2a17974 100644 --- a/src/internal_modules/roc_core/string_builder.h +++ b/src/internal_modules/roc_core/string_builder.h @@ -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. diff --git a/src/internal_modules/roc_netio/target_libuv/roc_netio/basic_port.cpp b/src/internal_modules/roc_netio/target_libuv/roc_netio/basic_port.cpp index 38f8fc979..e7b49de37 100644 --- a/src/internal_modules/roc_netio/target_libuv/roc_netio/basic_port.cpp +++ b/src/internal_modules/roc_netio/target_libuv/roc_netio/basic_port.cpp @@ -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"); } } diff --git a/src/public_api/src/endpoint.cpp b/src/public_api/src/endpoint.cpp index a7c0f955d..bdadfbaaa 100644 --- a/src/public_api/src/endpoint.cpp +++ b/src/public_api/src/endpoint.cpp @@ -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()); @@ -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()); @@ -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()); diff --git a/src/tests/roc_address/test_endpoint_uri.cpp b/src/tests/roc_address/test_endpoint_uri.cpp index bb72adaf4..3f6a88331 100644 --- a/src/tests/roc_address/test_endpoint_uri.cpp +++ b/src/tests/roc_address/test_endpoint_uri.cpp @@ -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()); } } diff --git a/src/tests/roc_address/test_io_uri.cpp b/src/tests/roc_address/test_io_uri.cpp index 4fef261df..709179bb1 100644 --- a/src/tests/roc_address/test_io_uri.cpp +++ b/src/tests/roc_address/test_io_uri.cpp @@ -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()); } } diff --git a/src/tests/roc_address/test_pct.cpp b/src/tests/roc_address/test_pct.cpp index 7c64ae454..1ebce495a 100644 --- a/src/tests/roc_address/test_pct.cpp +++ b/src/tests/roc_address/test_pct.cpp @@ -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; } @@ -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; } diff --git a/src/tests/roc_core/test_hashmap.cpp b/src/tests/roc_core/test_hashmap.cpp index f4768a276..d3988044c 100644 --- a/src/tests/roc_core/test_hashmap.cpp +++ b/src/tests/roc_core/test_hashmap.cpp @@ -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 void test_embedded_capacity() { diff --git a/src/tests/roc_core/test_string_builder.cpp b/src/tests/roc_core/test_string_builder.cpp index 9c15c317a..3dcffa8bd 100644 --- a/src/tests/roc_core/test_string_builder.cpp +++ b/src/tests/roc_core/test_string_builder.cpp @@ -24,14 +24,14 @@ TEST(string_builder, init) { { // null zero-size buffer StringBuilder b(NULL, 0); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(1, b.needed_size()); UNSIGNED_LONGS_EQUAL(0, b.actual_size()); } { // null one-byte buffer StringBuilder b(NULL, 1); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(1, b.needed_size()); UNSIGNED_LONGS_EQUAL(0, b.actual_size()); } @@ -39,7 +39,7 @@ TEST(string_builder, init) { char buf[1] = { 'x' }; StringBuilder b(buf, 0); - CHECK(!b.ok()); + CHECK(!b.is_ok()); UNSIGNED_LONGS_EQUAL(1, b.needed_size()); UNSIGNED_LONGS_EQUAL(0, b.actual_size()); @@ -49,7 +49,7 @@ TEST(string_builder, init) { char buf[1] = { 'x' }; StringBuilder b(buf, 1); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(1, b.needed_size()); UNSIGNED_LONGS_EQUAL(1, b.actual_size()); @@ -59,7 +59,7 @@ TEST(string_builder, init) { StringBuffer buf(arena); StringBuilder b(buf); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(1, b.needed_size()); UNSIGNED_LONGS_EQUAL(1, b.actual_size()); UNSIGNED_LONGS_EQUAL(0, buf.len()); @@ -72,7 +72,7 @@ TEST(string_builder, init) { StringBuilder b(buf); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(1, b.needed_size()); UNSIGNED_LONGS_EQUAL(1, b.actual_size()); UNSIGNED_LONGS_EQUAL(0, buf.len()); @@ -92,7 +92,7 @@ TEST(string_builder, assign) { StringBuilder b(dst, Size); CHECK(b.assign_str(src, src + strlen(src))); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(Size, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -109,7 +109,7 @@ TEST(string_builder, assign) { StringBuilder b(dst, Size); CHECK(b.assign_str(src, src + 4)); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(5, b.needed_size()); UNSIGNED_LONGS_EQUAL(5, b.actual_size()); @@ -126,7 +126,7 @@ TEST(string_builder, assign) { StringBuilder b(dst, Size); CHECK(!b.assign_str(src, src + strlen(src))); - CHECK(!b.ok()); + CHECK(!b.is_ok()); UNSIGNED_LONGS_EQUAL(strlen(src) + 1, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -143,7 +143,7 @@ TEST(string_builder, assign) { StringBuilder b(dst, Size); CHECK(!b.assign_str(src, src + strlen(src))); - CHECK(!b.ok()); + CHECK(!b.is_ok()); UNSIGNED_LONGS_EQUAL(strlen(src) + 1, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -160,7 +160,7 @@ TEST(string_builder, assign) { StringBuilder b(dst, Size); CHECK(!b.assign_str(src, src + strlen(src))); - CHECK(!b.ok()); + CHECK(!b.is_ok()); UNSIGNED_LONGS_EQUAL(strlen(src) + 1, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -174,12 +174,12 @@ TEST(string_builder, assign) { CHECK(b.append_str("123")); CHECK(b.append_str("456")); - CHECK(b.ok()); + CHECK(b.is_ok()); STRCMP_EQUAL("123456", buf); CHECK(b.assign_str("abc")); - CHECK(b.ok()); + CHECK(b.is_ok()); STRCMP_EQUAL("abc", buf); } @@ -189,12 +189,12 @@ TEST(string_builder, assign) { StringBuilder b(buf, sizeof(buf)); CHECK(!b.assign_str("1235678")); - CHECK(!b.ok()); + CHECK(!b.is_ok()); STRCMP_EQUAL("123", buf); CHECK(b.assign_str("abc")); - CHECK(b.ok()); + CHECK(b.is_ok()); STRCMP_EQUAL("abc", buf); } @@ -215,7 +215,7 @@ TEST(string_builder, append_str) { StringBuilder b(dst, Size); CHECK(b.append_str(src1, src1 + strlen(src1))); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(strlen(src1) + 1, b.needed_size()); UNSIGNED_LONGS_EQUAL(strlen(src1) + 1, b.actual_size()); @@ -223,7 +223,7 @@ TEST(string_builder, append_str) { CHECK(memcmp(dst, res1, sizeof(res1)) == 0); CHECK(b.append_str(src2, src2 + strlen(src2))); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(Size, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -244,7 +244,7 @@ TEST(string_builder, append_str) { StringBuilder b(dst, Size); CHECK(b.append_str(src1, src1 + strlen(src1))); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(strlen(src1) + 1, b.needed_size()); UNSIGNED_LONGS_EQUAL(strlen(src1) + 1, b.actual_size()); @@ -252,7 +252,7 @@ TEST(string_builder, append_str) { CHECK(memcmp(dst, res1, sizeof(res1)) == 0); CHECK(b.append_str(src2, src2 + strlen(src2))); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(strlen(src1) + strlen(src2) + 1, b.needed_size()); UNSIGNED_LONGS_EQUAL(strlen(src1) + strlen(src2) + 1, b.actual_size()); @@ -273,7 +273,7 @@ TEST(string_builder, append_str) { StringBuilder b(dst, Size); CHECK(b.append_str(src1, src1 + strlen(src1))); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(strlen(src1) + 1, b.needed_size()); UNSIGNED_LONGS_EQUAL(strlen(src1) + 1, b.actual_size()); @@ -281,7 +281,7 @@ TEST(string_builder, append_str) { CHECK(memcmp(dst, res1, sizeof(res1)) == 0); CHECK(!b.append_str(src2, src2 + strlen(src2))); - CHECK(!b.ok()); + CHECK(!b.is_ok()); UNSIGNED_LONGS_EQUAL(strlen(src1) + strlen(src2) + 1, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -302,7 +302,7 @@ TEST(string_builder, append_str) { StringBuilder b(dst, Size); CHECK(b.append_str(src1, src1 + strlen(src1))); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(Size, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -310,7 +310,7 @@ TEST(string_builder, append_str) { CHECK(memcmp(dst, res1, sizeof(res1)) == 0); CHECK(!b.append_str(src2, src2 + strlen(src2))); - CHECK(!b.ok()); + CHECK(!b.is_ok()); UNSIGNED_LONGS_EQUAL(strlen(src1) + strlen(src2) + 1, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -329,10 +329,10 @@ TEST(string_builder, append_uint) { StringBuilder b(dst, Size); CHECK(b.append_str("----")); - CHECK(b.ok()); + CHECK(b.is_ok()); CHECK(b.append_uint(1234, 10)); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(Size, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -348,10 +348,10 @@ TEST(string_builder, append_uint) { StringBuilder b(dst, Size); CHECK(b.append_str("----")); - CHECK(b.ok()); + CHECK(b.is_ok()); CHECK(b.append_uint(0xdead, 16)); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(Size, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -367,10 +367,10 @@ TEST(string_builder, append_uint) { StringBuilder b(dst, Size); CHECK(b.append_str("----")); - CHECK(b.ok()); + CHECK(b.is_ok()); CHECK(b.append_uint(12, 10)); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(7, b.needed_size()); UNSIGNED_LONGS_EQUAL(7, b.actual_size()); @@ -386,10 +386,10 @@ TEST(string_builder, append_uint) { StringBuilder b(dst, Size); CHECK(b.append_str("----")); - CHECK(b.ok()); + CHECK(b.is_ok()); CHECK(b.append_uint(0, 10)); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(6, b.needed_size()); UNSIGNED_LONGS_EQUAL(6, b.actual_size()); @@ -405,10 +405,10 @@ TEST(string_builder, append_uint) { StringBuilder b(dst, Size); CHECK(b.append_str("----")); - CHECK(b.ok()); + CHECK(b.is_ok()); CHECK(!b.append_uint(12345678, 10)); - CHECK(!b.ok()); + CHECK(!b.is_ok()); UNSIGNED_LONGS_EQUAL(Size + 4, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -424,10 +424,10 @@ TEST(string_builder, append_uint) { StringBuilder b(dst, Size); CHECK(b.append_str("--------")); - CHECK(b.ok()); + CHECK(b.is_ok()); CHECK(!b.append_uint(1234, 10)); - CHECK(!b.ok()); + CHECK(!b.is_ok()); UNSIGNED_LONGS_EQUAL(Size + 4, b.needed_size()); UNSIGNED_LONGS_EQUAL(Size, b.actual_size()); @@ -448,7 +448,7 @@ TEST(string_builder, resizing) { STRCMP_EQUAL("", buf.c_str()); CHECK(b.assign_str("1234")); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(5, b.needed_size()); UNSIGNED_LONGS_EQUAL(5, b.actual_size()); @@ -457,7 +457,7 @@ TEST(string_builder, resizing) { STRCMP_EQUAL("1234", buf.c_str()); CHECK(b.assign_str("1234abcd")); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(9, b.needed_size()); UNSIGNED_LONGS_EQUAL(9, b.actual_size()); @@ -476,7 +476,7 @@ TEST(string_builder, resizing) { STRCMP_EQUAL("", buf.c_str()); CHECK(b.append_str("1234")); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(5, b.needed_size()); UNSIGNED_LONGS_EQUAL(5, b.actual_size()); @@ -485,7 +485,7 @@ TEST(string_builder, resizing) { STRCMP_EQUAL("1234", buf.c_str()); CHECK(b.append_str("abcd")); - CHECK(b.ok()); + CHECK(b.is_ok()); UNSIGNED_LONGS_EQUAL(9, b.needed_size()); UNSIGNED_LONGS_EQUAL(9, b.actual_size());