Repository: ashvardanian/StringZilla (language: rust)

Issue:
sz::string length();size() and rstrip()
```cpp
#include <iostream>
#include "stringzilla/stringzilla.hpp
namespace sz = ashvardanian::stringzilla;
int main(int argc, char* argv[])
{
        sz::string str = "aakjladskfj,,,;kdf";
        size_t index = str.find('h');

        std::cout << str << std::endl;
        index = str.find_last_of(",");
        str.erase(index);
        std::cout << str << std::endl;

        str = str.random(10);

        std::cout << str << std::endl;
        str = "abcdefhijklm  ";
        std::cout << str << "\tsize():" << str.size() << std::endl;//????
        std::cout << str << "\tlength()" << str.length() << std::endl;//????
        sz::string_view strv = " ";
        // str = str.lstrip(strv.as_set());
        std::cout << str << "Z" << std::endl;
        str = str.rstrip(strv.as_set());
        std::cout << str << "Z" << str.size() << std::endl;//????

        sz::string s = "abcdefhijklm";
        std::cout << s.size() << std::endl;
        return 0;
}
```


Discussion / hints:
Hi @ldyoungGod! Nice catch, on it 🤗 
这是来自QQ邮箱的假期自动回复邮件。
 
您好，我最近正在休假中，无法亲自回复您的邮件。我将在假期结束后，尽快给您回复。

Fix patch:
diff --git a/include/stringzilla/stringzilla.h b/include/stringzilla/stringzilla.h
index 225845df..6603b795 100644
--- a/include/stringzilla/stringzilla.h
+++ b/include/stringzilla/stringzilla.h
@@ -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.
diff --git a/include/stringzilla/stringzilla.hpp b/include/stringzilla/stringzilla.hpp
index 96e5cbae..3350a05b 100644
--- a/include/stringzilla/stringzilla.hpp
+++ b/include/stringzilla/stringzilla.hpp
@@ -1910,8 +1910,6 @@ class basic_string {
         return ashvardanian::stringzilla::_with_alloc<allocator_type_>(callback);
     }
 
-    bool is_internal() const noexcept { return sz_string_is_on_stack(&string_); }
-
     void init(std::size_t length, char_type value) noexcept(false) {
         sz_ptr_t start;
         if (!_with_alloc(
@@ -2460,6 +2458,7 @@ class basic_string {
     bool is_space() const noexcept { return !empty() && contains_only(whitespaces_set()); }
     bool is_upper() const noexcept { return !empty() && contains_only(ascii_uppercase_set()); }
     bool is_printable() const noexcept { return empty() || contains_only(ascii_printables_set()); }
+    bool is_internal() const noexcept { return sz_string_is_on_stack(&string_); }
 
 #pragma region Character Set Arguments
 
@@ -3343,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);
     }
@@ -3383,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;


Test patch: