Skip to content

Repository files navigation

TString

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.

Features

  • 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 using constexpr 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 of TString 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 to std::string in various scenarios.

Getting Started

Prerequisites

  • C++20 compatible compiler
  • Microsoft Visual Studio, GCC, or Clang
  • xmake build system

Installation

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")

Example Usage

#include"TString.hpp"
#include<iostream>intmain() {
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: Hellosize_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!return0;
}

Build with xmake

This project uses xmake to simplify the build process. To build and run tests, use the following commands:

  1. Make sure xmake is installed.
  2. Navigate to the project root directory.
  3. 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()

Features in Detail

  • 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 using constexpr, 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 like std::unordered_set and std::unordered_map by leveraging the std::hash specialization.

Benchmark Results

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:

OperationTString (ms)std::string (ms)
Construction (short string)91245
Construction (long string)173871
Copy (short string)66156
Append (single char)1419
Clear76109
Find (short string)1130
Find (long string)814
Substring (short string)7167
Substring (long string)73165

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.

Directory Structure

TString/
├── include/
│ └── TString.hpp
├── src/
│ └── main.cpp
├── xmake.lua
└── README.md

Contributing

Contributions are welcome! If you have ideas for improving TString, feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License.

About

Better string implement for C++.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages