Skip to content

Concrete implementation of the string type and its functions in C

Notifications You must be signed in to change notification settings

fabricio-p/c-string

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

c-string

A library providing string functions and utilities in C. Before using it, do cd c-string/ && ./setup.sh to install libraries.

Example

#include <c-string/lib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

int main(void) {
  String str = String_from("Hello, world!");
  printf("%s\n", str); // Works, since it is NULL terminated.
  String_append(&str, "\nfoo bar");
  printf("%s\n", str);
  String str2 = String_clone(str);
  assert(String_equal(str, str2)); // Identical
  String_append(&str2, "\nbaz");
  assert(!String_equal(str, str2)); // Different, since one of them changed.
  String str3 = String_slice(str2, 0, 5);
  assert(!strcmp(str3, "Hello")); // You can use strcmp because it is NULL terminated.
  // Always be sure to cleanup(aka. free) the strings to prevent memory leaks.
  String_cleanup(str);
  String_cleanup(str2);
  String_cleanup(str3);
}

Types

StringBuffer

A typedef of char *. Used to represent a vector of characters. Is mainly used to represent the underlying buffer of String but can also be used like StringBuffer in Java, std::stringstream in C++ or StringSink in AssemblyScript. Made with c-vector.

String

A typedef of char const *. Used to represent a string. It is NULL terminated to stay compatitible with the C standrd library.

Vector_String

A vector of Strings, just CVECTOR(String), mafe with c-vector. Used by String_split and String_split_by_char.

FixedString

A struct describing an immutable fixed-size string.

Fields

  • int len: The length.
  • char const *str: The pointer the content.

Functions

Together with the functions declared below, there are other functions that operate on StringBuffer that are generated from the CVECTOR_WITH_NAME macro from c-vector.

StringBuffer_from_strlit

Creates a StringBuffer from a string literal without allocating on the heap, valid only inside the scope that it is created.

  • Arguments:
    • char const *: A C string literal.
  • Return(StringBuffer)

StringBuffer_push_push_bytes

Pushes a number of bytes one-by-one into the buffer.

  • Arguments:
    • StringBuffer *sb: The buffer where the bytes will be pushed.
    • char *bytes: The pointer to the bytes that will be pushed.
    • int n: Then number of bytes that will br pushed.
  • Return(CVECTOR_STATUS): Read silent-mode of c-vector.

StringBuffer_push_str

Pushes a NULL terminated string into the buffer.

  • Arguments:
    • StringBuffer *sb: The buffer where the string will be pushed.
    • char *str: The string that will be pushed.
  • Return(CVECTOR_STATUS): Read silent-mode of c-vector.

String_new

Creates a new empty String.

  • Arguments: None
  • Return(String): The newly created empty String.

String_from_strlit

Creates a String from a string literal without allocating on the heap, valid only inside the scope that it is created.

  • Arguments:
    • char const *: A C string literal.
  • Return(String)

String_from_bytes

Creates a new String from a chunk of bytes.

  • Arguments:
    • char *bytes: The pointer to the bytes that will be used as content.
    • int n: The number of bytes that wil be used.
  • Return(String): The new String with the chunk if bytes as content.

String_from

Creates a new String from a plain NULL terminated string.

  • Arguments:
    • char *str: The NULL terminated string.
  • Return(String): The new String with the NULL terminated string as content.

String_len

  • Arguments:
    • String str: The string.
  • Return(int): The length of the string.

String_clone

Clones a String. Works like how strdup duplicates a NULL terminated string.

  • Arguments:
    • String str: The string that will be cloned.
  • Return(String): A new identical String.

String_concat

Concatenates the contents of two Strings into one.

  • Arguments:
    • String str1: The first string.
    • String str2: The second string.
  • Return(String): A new String with the concatenated contents.

String_slice

Slices a segment of a String. Negative backward indexing is allowed(i.e. index -1 refers to the last item).

  • Arguments:
    • String str: The String from which the segment will be sliced.
    • int fi: The start index.
    • int li: The end index.
  • Return(String): The sliced segment. If fi is smaller than li or fi/li are smaller than 0 even after they are translated to psitive indices or if they are greater or equal than the length of the string, NULL will be returned.

String_hash

Hashes a String using the MurmurHash-2 non-cryptographic hashing function.

  • Arguments:
    • String str: The String that will be hashed.
  • Return(uint32_t): The hash of str.

String_append

Appends a plain NULL terminated string at the end.

  • Arguments:
    • String *str: The pointer to the String variable. It will probably reallocate the string to a bigger memory location, so it needs to reassign the variable too.
    • char *bstr: The NULL terminated string that will be appended.
  • Return(CVECTOR_STATUS): Read silent-mode of c-vector. On verbose-mode, it will return 1 if pushing any of the character fails, otherwise 1.

String_equal

Compares two Strings.

  • Arguments:
    • String str1,
    • String str2: The strings that will be compared.
  • Return(bool): true if both Strings are equal in length and content, otherwise false.

String_cleanup

Frees the String from memory.

  • Arguments:
    • String str: The string that will be freed.
  • Return(void)

String_to_fixed

Creates and returns the fixed version of a String

  • Arguments:
    • String str: The source.
  • Return(FixedString): The fixed string with the contents of the first argument.

String_split

Splits a String by a character sequence.

  • Arguments:
    • String str: The string that will be splitted.
    • char const *seq: The character sequence.
  • Return(Vector_String): The new strings that resulted from the split.

String_split_by_char

Like String_split, but splits by a character instead of a sequence.

  • Arguments:
    • String str: The string that will be splitted.
    • char c: The character.
  • Return(Vector_String): The new strings that resulted from the split.

String_trim_start

Removes the leading \t, , \n and \0 characters from the start of a String.

  • Arguments:
    • String str: The String that will be operated on.
  • Return(void)

String_trim_end

Works like String_trim_start but operates at the end of the String.

  • Arguments:
    • String str: The String that will be operated on.
  • Return(void)

String_trim

Applies String_trim_start String_trim_end on the String.

  • Arguments:
    • String str: The String that will be operated on.
  • Return(void)

StringBuffer_to_string

Creates and returns a new String with the current collected bytes of the StringBuffer as content.

StringBuffer_transform_to_string

Modifies the StringBuffer and transforms it into a String. Is useful when you are done writing to the StringBuffer and now you want a String with that content, but you want to prevent memory allocations.

  • Arguments:
    • StringBuffer sb: The StringBuffer that will be transformed.
  • Return(String): Essentially sb.

FixedString_from

Creates a FixedString from a plain static string literal.

FixedString_cleanup

free()s the str field if it isn't NULL and zeroes all the fields of the struct.

  • Arguments:
    • FixedString *fstr: The FixedString that will be operated on.
  • Return(void)

About

Concrete implementation of the string type and its functions in C

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published