Skip to content

Latest commit

 

History

History
103 lines (79 loc) · 2.71 KB

File metadata and controls

103 lines (79 loc) · 2.71 KB

logo didn't load :(

safestring!

safestring! is a C++ library that provides safer alternatives to traditional string manipulation functions. It incorporates bounds checking and automatic memory management to prevent vulnerabilities such as buffer overflows.

Features

  • Concatenation: Combine strings safely without manual memory management.
  • Substring Extraction: Extract substrings with bounds checking.
  • Splitting: Split strings into components based on delimiters.
  • Replacing: Replace substrings with automatic error handling.
  • Formatting: Type-safe string formatting.
  • Utility Functions: Additional tools for trimming, padding, and case conversion.

Directory Structure

SafeString/
├── CMakeLists.txt         # Top-level CMake file
├── src/                   # Library source and headers
│   ├── SafeString.cpp     # Show case implementation
├── src/                   # Library source and headers
│   ├── SafeString.h       # Header file
├── tests/                 # Unit tests
│   ├── CMakeLists.txt     # CMake configuration for tests
│   └── TestSafeString.cpp # Test cases
└── build/                 # Build directory (generated by CMake)

Prerequisites

  • A C++17 compatible compiler (e.g., GCC, Clang, MSVC).
  • CMake version 3.15 or higher.

Building the Project

  1. Clone the repository:
git clone https://github.com/your-username/SafeString.git
cd SafeString
  1. Create a build directory and navigate to it:
mkdir build && cd build
  1. Run CMake to configure the project:
cmake ..
  1. Build the project:
cmake --build .

Running Tests

If you enabled BUILD_TESTS, you can run the unit tests:

  1. Use ctest in the build directory:
ctest
  1. Alternatively, run the test executable directly:
./tests/TestSafeString

Example Usage

Include the Header

#include "SafeString/SafeString.h"

Safe Concatenation

#include "SafeString/SafeString.h"
#include <iostream>

int main() {
    std::string result = SafeString::concatenate("Hello, ", "World!");
    std::cout << result << std::endl; // Outputs: Hello, World!
    return 0;
}

Safe Substring

std::string part = SafeString::safeSubstring("Hello, World!", 7, 5);
// Result: "World"

String Splitting

std::vector<std::string> tokens = SafeString::split("A,B,C", ',');
// Result: {"A", "B", "C"}