Add DStr docs.
Small test changes
This commit is contained in:
+64
-4
@@ -7,13 +7,17 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** A dynamic string.
|
/** A dynamically managed C-string.
|
||||||
* The string might not always be nul terminated,
|
*
|
||||||
* Normally, use uc_dstr_str() to manipulate the managed string
|
* The .str member might not always be nul terminated,
|
||||||
|
* Normally, use uc_dstr_str() to retreive the managed string
|
||||||
* instead of accessing ->str directly.
|
* instead of accessing ->str directly.
|
||||||
*/
|
*/
|
||||||
struct DStr {
|
struct DStr {
|
||||||
|
/** Length of the managed string.*/
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
|
//internal fields:
|
||||||
size_t allocated;
|
size_t allocated;
|
||||||
char *str;
|
char *str;
|
||||||
};
|
};
|
||||||
@@ -37,34 +41,90 @@ void uc_dstr_destroy(struct DStr *str);
|
|||||||
size_t uc_dstr_available(const struct DStr *str);
|
size_t uc_dstr_available(const struct DStr *str);
|
||||||
|
|
||||||
/** Ensure the DStr is of at least 'len' bytes,
|
/** Ensure the DStr is of at least 'len' bytes,
|
||||||
* allocating room if needed.
|
* allocating room if needed.
|
||||||
|
*
|
||||||
|
* @return 0 on success (!= 0 if an allocation fail)
|
||||||
*/
|
*/
|
||||||
int uc_dstr_ensure(struct DStr *str, size_t len);
|
int uc_dstr_ensure(struct DStr *str, size_t len);
|
||||||
|
|
||||||
|
/** Reset the DStr, setting its length to 0
|
||||||
|
*/
|
||||||
void uc_dstr_reset(struct DStr *str);
|
void uc_dstr_reset(struct DStr *str);
|
||||||
|
|
||||||
|
/** nul terminate the DStr */
|
||||||
void uc_dstr_terminate(struct DStr *str);
|
void uc_dstr_terminate(struct DStr *str);
|
||||||
|
|
||||||
|
/** Retreive the managed string, the returned string
|
||||||
|
* is always nul terminated. The DStr will still be managing
|
||||||
|
* the returned char *.
|
||||||
|
*/
|
||||||
char *uc_dstr_str(struct DStr *str);
|
char *uc_dstr_str(struct DStr *str);
|
||||||
|
|
||||||
|
/** reserve len bytes for appending to the dstr
|
||||||
|
* you must fill in a string in the 0-len range of
|
||||||
|
* the returned pointer.
|
||||||
|
*
|
||||||
|
* @param len bytes to reseve
|
||||||
|
* @return poiinter to the start of the data to append
|
||||||
|
*/
|
||||||
char *uc_dstr_put(struct DStr *str, size_t len);
|
char *uc_dstr_put(struct DStr *str, size_t len);
|
||||||
|
|
||||||
|
/** Append a string, filtering out characters.
|
||||||
|
* e.g. pass in the ctype.h isdigit as the is_x function
|
||||||
|
* to append only the digits in the supplied string.
|
||||||
|
*
|
||||||
|
* @param s string to append
|
||||||
|
* @param is_x ctype.h compatible function to use as a filter.
|
||||||
|
* @return number of chars appended
|
||||||
|
*/
|
||||||
size_t uc_dstr_put_str_filter(struct DStr *str, const char *s, int (*is_x)(int c));
|
size_t uc_dstr_put_str_filter(struct DStr *str, const char *s, int (*is_x)(int c));
|
||||||
|
|
||||||
|
/* Append a string of the give size.
|
||||||
|
* (The nul terminator is not appended)
|
||||||
|
*
|
||||||
|
* @param s string to append
|
||||||
|
* @param len bytes to append (not including the nul terminator)
|
||||||
|
* @return 0 on success (!= 0 if an allocation fail)
|
||||||
|
*/
|
||||||
int uc_dstr_put_str_sz(struct DStr *str, const char *s, size_t len);
|
int uc_dstr_put_str_sz(struct DStr *str, const char *s, size_t len);
|
||||||
|
|
||||||
|
/* Append a string.
|
||||||
|
* (The nul terminator is not appended)
|
||||||
|
* @return 0 on success (!= 0 if an allocation fail)
|
||||||
|
*/
|
||||||
int uc_dstr_put_str(struct DStr *str, const char *s);
|
int uc_dstr_put_str(struct DStr *str, const char *s);
|
||||||
|
|
||||||
|
/* Append a single char.
|
||||||
|
* @return 0 on success (!= 0 if an allocation fail)
|
||||||
|
*/
|
||||||
int uc_dstr_put_char(struct DStr *str, char c);
|
int uc_dstr_put_char(struct DStr *str, char c);
|
||||||
|
|
||||||
|
/* Make the DStr take ownership of the give dynamically allocated string.
|
||||||
|
* Any existing string in the DStr is freed.
|
||||||
|
*/
|
||||||
void uc_dstr_own(struct DStr *str, char *s);
|
void uc_dstr_own(struct DStr *str, char *s);
|
||||||
|
|
||||||
|
/** Release the managed string.
|
||||||
|
* The caller is now responsible for managing/free'ing the returned string.
|
||||||
|
* The DStr is considered empty afterwards.
|
||||||
|
*
|
||||||
|
* @return The dynamically allocated string that was managed.
|
||||||
|
*/
|
||||||
char *uc_dstr_steal(struct DStr *str);
|
char *uc_dstr_steal(struct DStr *str);
|
||||||
|
|
||||||
|
/** Swap the A and B dstr */
|
||||||
void uc_str_swap(struct DStr *a, struct DStr *b);
|
void uc_str_swap(struct DStr *a, struct DStr *b);
|
||||||
|
|
||||||
|
/** Replace all occurences in the DStr
|
||||||
|
*
|
||||||
|
* @param f char to find
|
||||||
|
* @param r char to replace with
|
||||||
|
*/
|
||||||
size_t uc_dstr_replace(struct DStr *str, char f, char r);
|
size_t uc_dstr_replace(struct DStr *str, char f, char r);
|
||||||
|
|
||||||
|
/** Remove leading and trailing spaces in the DStr
|
||||||
|
* Spaces are the ascii ones: " \t\r\v\t\n"
|
||||||
|
*/
|
||||||
void uc_dstr_trim(struct DStr *str);
|
void uc_dstr_trim(struct DStr *str);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|||||||
+1
-2
@@ -65,9 +65,8 @@ START_TEST (test_dstr_own_steal)
|
|||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
START_TEST (test_dstr_trim)
|
START_TEST (test_dstr_trim)
|
||||||
struct DStr str;
|
struct DStr str = UC_DSTR_STATIC_INIT;
|
||||||
|
|
||||||
uc_dstr_init(&str);
|
|
||||||
uc_dstr_put_str(&str, " \t \nHello !\r\n");
|
uc_dstr_put_str(&str, " \t \nHello !\r\n");
|
||||||
uc_dstr_trim(&str);
|
uc_dstr_trim(&str);
|
||||||
ck_assert_str_eq(uc_dstr_str(&str), "Hello !");
|
ck_assert_str_eq(uc_dstr_str(&str), "Hello !");
|
||||||
|
|||||||
Reference in New Issue
Block a user