95 lines
3.2 KiB
C
95 lines
3.2 KiB
C
// LCZ libraries v0.1b
|
|
|
|
#ifndef _LCZ_STRING_H_
|
|
#define _LCZ_STRING_H_
|
|
|
|
#ifndef _LCZ_TYPES_H_
|
|
#include "types.h"
|
|
#endif
|
|
|
|
|
|
/* String: constant length string.
|
|
* DString: dynamic length string.
|
|
* RString: raw c-style string. Not an actual type.
|
|
*
|
|
* USAGE INTERFACE:
|
|
* Length does not include '\0', but most strings should still be zero-terminated.
|
|
*
|
|
* DString is purposefully made to have the same memory layout as String,
|
|
* so you can use DString everywhere you can use String (but not vice-versa) by converting it with TO_STRING(dstr).
|
|
*
|
|
* Advanced: Actually String is not constant length. It's just unknown how much space has been allocated for it.
|
|
* Make sure you have enough space before modifying the lenght.
|
|
* DStrings change their size automatically if you use the functions in this library.
|
|
*
|
|
* IMPLEMENTER INTERFACE
|
|
* The "text" pointer should always be valid, unless you are building the String objects yourself.
|
|
* "capacity" and "length" do not include the zero terminator of the string. Make sure you malloc 1 more byte to fit it.
|
|
*
|
|
|
|
|
|
* String and DString are small containers, so they are not expensive to copy around.
|
|
* (It's PROBABLY more expensive to dereference a pointer than to copy a String container. Did not profile though.)
|
|
*/
|
|
|
|
/* @TODO: Add allocator function to parameters?
|
|
* @TODO: Use String type instead of passing text + length everywhere?
|
|
*
|
|
* @Note: I considered adding a SString (Static string, immutable), but it would be so inconvenient to use
|
|
* that I decided to leave it out (it's just like the "const" keyword)
|
|
*/
|
|
|
|
struct String
|
|
{
|
|
char *text;
|
|
u64 length;
|
|
};
|
|
|
|
struct DString
|
|
{
|
|
char *text;
|
|
u64 length;
|
|
u64 capacity;
|
|
};
|
|
|
|
typedef struct String String;
|
|
typedef struct DString DString;
|
|
|
|
|
|
#define TO_STRING(dstr) (*((String*)&dstr))
|
|
|
|
// String building / constructors
|
|
String string_take(char *src);
|
|
DString dstring_take(char *src);
|
|
String string_copy(const char *src, u64 length);
|
|
DString dstring_copy(const char *src, u64 length);
|
|
DString dstring_new(u64 capacity);
|
|
// n = number of strings, ... = , const char *src1, u64 length1, ...
|
|
String string_concat(u32 n, ...);
|
|
// n = number of strings, ... = , const char *src1, u64 length1, ...
|
|
DString dstring_concat(u32 n, ...);
|
|
|
|
// Compute string length, excluding zero-terminator
|
|
u64 rstring_length(const char *src);
|
|
|
|
// Comparison
|
|
int rstring_compare(const char *left, const char *right);
|
|
bool string_equal(String left, String right);
|
|
bool string_find(String str, u64 start_index, String to_find, u64 *found_index);
|
|
|
|
// String specific operations
|
|
// Warning: You have to make sure the space pointed by str is big enough to fit to_append.
|
|
void string_append(String *str, const char *to_append, u64 count);
|
|
String string_substring(String *str, u64 start, u64 end);
|
|
String string_trim(String *str);
|
|
|
|
// DString specific operations
|
|
void dstring_reserve(DString *dstr, u64 new_length);
|
|
void dstring_append(DString *dstr, const char *to_append, u64 count);
|
|
void dstring_insert(DString *dstr, u64 index, const char *to_insert, u64 count);
|
|
void dstring_erase(DString *dstr, u64 index, u64 count);
|
|
void dstring_replace(DString *dstr, u64 index, u64 rem_count, const char *to_insert, u64 ins_count);
|
|
|
|
|
|
#endif
|