-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(stdlib) Support dynamically resizing
StringBuilder
buffer
It would previously use a fixed buffer of 1024 bytes; trying to use more than this would cause exceptions to be thrown. Growing the buffer using `realloc()` would have been possible, but the problem is that this would have required the memory to have been allocated with `malloc()` in the first place (i.e. not `new[]`). Because of this, and because of suggestions seen on Stack Overflow, I rewrote the code to use `std::vector` instead, which have a built-in `resize()` operation. Also added a C++-based unit test to validate the new functionality. https://gitlab.perlang.org/perlang/perlang/-/merge_requests/550
- Loading branch information
Showing
5 changed files
with
36 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// string_builder.cc - tests for the perlang::text::StringBuilder class | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include "perlang_stdlib.h" | ||
|
||
TEST_CASE( "perlang::text::StringBuilder::append, resizing the string beyond its initial capacity" ) | ||
{ | ||
// Arrange & Act | ||
perlang::text::StringBuilder sb; | ||
|
||
for (int i = 0; i < 100; i++) { | ||
sb.append(*perlang::ASCIIString::from_static_string("this is an ASCII string")); | ||
} | ||
|
||
uint expected_length = 100 * strlen("this is an ASCII string"); | ||
|
||
// Assert | ||
REQUIRE(sb.length() == expected_length); | ||
} |