iadityanath8 / sstr

This is a minimilistic c string library and still it is in heavy development
MIT License
4 stars 0 forks source link

SSTR

Introduction

The Custom String Library with Fat Pointer is a powerful C-based string manipulation tool designed to provide efficient and flexible string handling capabilities. It leverages the concept of fat pointers for enhanced memory management and offers a comprehensive set of functionalities for creating, manipulating, and managing strings.

Key Features

Fat Pointer Implementation

The library uses the concept of fat pointers to efficiently manage string data. Fat pointers store both the pointer to the string data and additional metadata such as length and capacity, allowing for streamlined memory operations.

sstr is nothing it is just a typedef to char* this is due to the fat pointer implementation of the library

String Creation and Manipulation

Users can easily create new strings from C-style strings and perform various string manipulation operations such as appending characters, strings, or other strings. The library provides functions for concatenating strings, finding characters, checking equality, accessing characters at specific indices, and splitting strings based on delimiters.

Memory Management

Efficient memory management is a core aspect of the library. With the fat pointer implementation, memory allocation and deallocation are optimized for performance and reliability. Users can free memory allocated for strings when no longer needed, ensuring proper resource utilization.

Usage

The library offers a user-friendly interface with intuitive function names and straightforward usage patterns. Developers can seamlessly integrate the Custom String Library with Fat Pointer into their C projects to handle string-related tasks efficiently and effectively.

Benefits

sstr API

This is a simple C string API that includes the following functions:

Here is an example of how to use the SString API:

#include <stdio.h>
#include "sstr.h"

int main() {
  // create a new SString object and initialize it with a C-string
  sstr str = sstr_new("Hello, world!");

  // You can easily print this thing like a normal string
  printf("%s\n",str);

  // print the length and capacity of the SString object
  printf("Length: %zu, Capacity: %zu\n", sstr_len(str), sstr_cap(str));

  // append a character to the SString object
  sstr_append_char(str, '!');

  // print the updated length and capacity of the SString object
  printf("Length: %zu, Capacity: %zu\n", sstr_len(str), sstr_cap(str));

  // split the SString object into an array of SString objects
  size_t num_parts = 0;
  sstr* parts = sstr_split(str, ' ', &num_parts);

  // print the array of SString objects
  for (size_t i = 0; i < num_parts; i++) {
    printf("Part %zu: %s\n", i, parts[i]);
    sstr_free(parts[i]);
  }
  sstr_freeList(parts);

  // free the memory used by the sstr object
  sstr_free(str);

  return 0;
}

Conclusion

The Custom String Library with Fat Pointer empowers C developers with a powerful tool for string manipulation. By leveraging the benefits of fat pointers, the library offers efficiency, flexibility, and reliability in handling string data. Whether for small-scale projects or large-scale applications, the library provides a solid foundation for efficient string management in C programming.