TString is a custom C++ string implementation designed for efficient memory management by utilizing a buffer size that is always the closest power of two to the actual string size. This approach optimizes memory usage and provides dynamic growth. The library also includes a TStringConst
class, which can be used for compile-time string operations, fully leveraging C++20's constexpr
capabilities.
- Dynamic Buffer Management: Allocates memory with buffer sizes that are powers of two, ensuring efficient memory management and reducing reallocations.
- String Operations: Provides common string operations, including concatenation, substring extraction, finding substrings, splitting, and appending.
- Move Semantics: Implements both copy and move constructors to efficiently manage resources during object transfers.
- Compile-time String Support:
TStringConst
allows compile-time operations for strings usingconstexpr
in C++20. - Utility Methods: Includes utility methods such as
clear()
,empty()
,split()
, and hash support. - User-defined Literals: Supports the
""_T
user-defined literal for easy creation ofTString
instances. - Custom Reserve: Allows pre-allocation of memory to improve efficiency for operations involving large or frequent modifications.
- Benchmarking Support: Includes a benchmark suite comparing
TString
tostd::string
in various scenarios.
- C++20 compatible compiler
- Microsoft Visual Studio, GCC, or Clang
- xmake build system
To use TString
, clone the repository and include TString.hpp
in your project:
# Clone the repository
git clone https://github.com/yourusername/TString.git
Include TString.hpp
in your code:
#include "include/TString.hpp"
You can also add TString
as a package through xmake by adding the TabNahida/repo-xmake
repository:
add_repositories("tab-repo https://github.com/TabNahida/repo-xmake.git")
add_requires("tstring")
Then, include it in your target:
target("example")
set_kind("binary")
add_files("src/*.cpp")
add_packages("tstring")
#include "TString.hpp"
#include <iostream>
int main() {
TString myStr = "Hello, ";
myStr.append("World!");
std::cout << myStr << std::endl; // Output: Hello, World!
TString subStr = myStr.substr(0, 5);
std::cout << subStr << std::endl; // Output: Hello
size_t found = myStr.find("World");
if (found != TString::npos) {
std::cout << "Found 'World' at position: " << found << std::endl;
}
TString combinedStr = myStr + " Again!";
std::cout << combinedStr << std::endl; // Output: Hello, World! Again!
return 0;
}
This project uses xmake to simplify the build process. To build and run tests, use the following commands:
- Make sure xmake is installed.
- Navigate to the project root directory.
- Run the following commands:
# Configure the project
xmake f -c
# Build the project
xmake
# Run the built target
xmake run Test
Here is the xmake.lua
configuration file used for the project:
add_languages("cxx20")
target("tstring")
set_kind("static")
add_files("src/TString.cpp")
add_headerfiles("include/TString.hpp")
add_includedirs("include")
target_end()
target("MainTest")
set_kind("binary")
add_files("src/main.cpp")
add_deps("tstring")
add_includedirs("include")
target_end()
target("BenchMark")
set_kind("binary")
add_files("src/benchmark.cpp")
add_deps("tstring")
add_includedirs("include")
target_end()
- Dynamic Buffer Growth: The buffer dynamically grows by powers of two, reducing the frequency of memory allocations during string operations.
- Move Semantics: The implementation includes move constructors and assignment operators, allowing efficient transfers of resources without unnecessary copies.
- Compile-time Strings:
TStringConst
is designed to provide compile-time constant string operations usingconstexpr
, enabling compile-time validation and manipulation. - Custom Reserve Functionality: The
reserve
function allows pre-allocating buffer space to prevent frequent reallocations when working with large strings or repeated appending operations. - Hash Support:
TString
can be used in hash containers likestd::unordered_set
andstd::unordered_map
by leveraging thestd::hash
specialization.
TString
has been benchmarked against std::string
across various common operations. The benchmark tests include construction, copy, append, substring, and find operations with both short and long strings. Below is a summary of some of the benchmark results:
Operation | TString (ms) | std::string (ms) |
---|---|---|
Construction (short string) | 91 | 245 |
Construction (long string) | 173 | 871 |
Copy (short string) | 66 | 156 |
Append (single char) | 14 | 19 |
Clear | 76 | 109 |
Find (short string) | 11 | 30 |
Find (long string) | 8 | 14 |
Substring (short string) | 71 | 67 |
Substring (long string) | 73 | 165 |
Note: The benchmarks were run with default settings on an Intel Core Ultra 5 125H processor with 32GB LPDDR5X 7467MHz memory. The results may vary depending on hardware and compiler optimizations. For some operations, such as finding a substring in very long strings, TString
and std::string
may show different performance characteristics due to the algorithms and optimizations used.
TString/
├── include/
│ └── TString.hpp
├── src/
│ └── main.cpp
├── xmake.lua
└── README.md
Contributions are welcome! If you have ideas for improving TString
, feel free to open an issue or submit a pull request.
This project is licensed under the MIT License.