Skip to content

Commit

Permalink
Fix: Boundary copy-assign & expand
Browse files Browse the repository at this point in the history
Closes #140

Co-authored-by: ldyoungGod <ldyoungGod@users.noreply.github.com>
  • Loading branch information
ashvardanian and ldyoungGod committed Apr 17, 2024
1 parent b87c9bc commit c5ad68b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/stringzilla/stringzilla.h
Original file line number Diff line number Diff line change
Expand Up @@ -3287,7 +3287,7 @@ SZ_PUBLIC sz_ptr_t sz_string_expand(sz_string_t *string, sz_size_t offset, sz_si
offset = sz_min_of_two(offset, string_length);

// If we are lucky, no memory allocations will be needed.
if (offset + string_length + added_length < string_space) {
if (string_length + added_length < string_space) {
sz_move(string_start + offset + added_length, string_start + offset, string_length - offset);
string_start[string_length + added_length] = 0;
// Even if the string is on the stack, the `+=` won't affect the tail of the string.
Expand Down
7 changes: 4 additions & 3 deletions include/stringzilla/stringzilla.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3342,8 +3342,9 @@ bool basic_string<char_type_, allocator_>::try_resize(size_type count, value_typ

// Allocate more space if needed.
if (count >= string_space) {
if (!_with_alloc(
[&](sz_alloc_type &alloc) { return sz_string_expand(&string_, SZ_SIZE_MAX, count, &alloc) != NULL; }))
if (!_with_alloc([&](sz_alloc_type &alloc) {
return sz_string_expand(&string_, SZ_SIZE_MAX, count - string_length, &alloc) != NULL;
}))
return false;
sz_string_unpack(&string_, &string_start, &string_length, &string_space, &string_is_external);
}
Expand Down Expand Up @@ -3382,7 +3383,7 @@ bool basic_string<char_type_, allocator_>::try_assign(string_view other) noexcep
// In the common case, however, we need to allocate.
else {
if (!_with_alloc([&](sz_alloc_type &alloc) {
string_start = sz_string_expand(&string_, SZ_SIZE_MAX, other.length(), &alloc);
string_start = sz_string_expand(&string_, SZ_SIZE_MAX, other.length() - string_length, &alloc);
if (!string_start) return false;
other.copy(string_start, other.length());
return true;
Expand Down

0 comments on commit c5ad68b

Please sign in to comment.