[utils] more/better char_array methods

This commit is contained in:
Al
2015-04-05 22:01:46 -04:00
parent 79fd7a8ded
commit 198e51b8a3
2 changed files with 62 additions and 22 deletions

View File

@@ -160,46 +160,80 @@ char *stripped = string_strip_whitespace(str);
free(str); free(str);
*/ */
char *string_strip_whitespace(char *str) { size_t string_rstrip(char *str) {
char *end; size_t spaces = 0;
size_t initial_spaces = 0; char *end = str + strlen(str) - 1;
while (end > str && isspace(*end)) {
char *ptr = str; spaces++;
end--;
while (isspace(*ptr++)) {
initial_spaces++;
} }
if (*ptr == '\0')
return str;
end = str + strlen(str) - 1;
while (end > str && isspace(*end)) end--;
*(end+1) = '\0'; *(end+1) = '\0';
return ptr; return spaces;
}
size_t string_lstrip(char *str) {
char *end;
size_t spaces = 0;
size_t len = strlen(str) - 1;
char *ptr = str;
while (isspace(*ptr++)) {
spaces++;
}
if (spaces > 0) {
memmove(str, str + spaces, len + 1 - spaces);
}
return spaces;
}
size_t string_strip(char *str) {
size_t spaces = string_lstrip(str);
spaces += string_rstrip(str);
return spaces;
} }
char_array *char_array_from_string(char *str) { char_array *char_array_from_string(char *str) {
char_array *array = char_array_new(); char_array *array = char_array_new();
array->a = str; array->a = str;
array->m = array->n = strlen(str); array->m = array->n = strlen(str);
return array;
} }
char *char_array_to_string(char_array *array, bool free_array) { inline char *char_array_get_string(char_array *array) {
if (free_array) free(array);
return array->a; return array->a;
} }
inline char *char_array_to_string(char_array *array) {
if (array->n == 0 || array->a[array->n - 1] != '\0') {
char_array_terminate(array);
}
char *a = array->a;
free(array);
return a;
}
static inline void char_array_strip_nul_byte(char_array *array) { static inline void char_array_strip_nul_byte(char_array *array) {
if (array->n > 0 && array->a[array->n - 1] == '\0') { if (array->n > 0 && array->a[array->n - 1] == '\0') {
array->n--; array->n--;
} }
} }
size_t char_array_len(char_array *array) {
if (array->n > 0 && array->a[array->n - 1] == '\0') {
return array->n - 1;
} else {
return array->n;
}
}
void char_array_append(char_array *array, char *str) { void char_array_append(char_array *array, char *str) {
while(*str) { while(*str) {
char_array_push(array, *str++); char_array_push(array, *str++);
@@ -417,4 +451,3 @@ cstring_array *cstring_array_split(char *str, const char *separator, size_t sepa
return cstring_array_from_char_array(array); return cstring_array_from_char_array(array);
} }

View File

@@ -10,7 +10,6 @@ extern "C" {
#include <stdbool.h> #include <stdbool.h>
#include <stdarg.h> #include <stdarg.h>
#include "collections.h" #include "collections.h"
#include "sds/sds.h"
#include "utf8proc/utf8proc.h" #include "utf8proc/utf8proc.h"
#include "vector.h" #include "vector.h"
@@ -31,7 +30,9 @@ char *utf8_reversed_string(const char *s); // returns a copy, caller frees
ssize_t utf8proc_iterate_reversed(const uint8_t *str, const uint8_t *start, int32_t *dst); ssize_t utf8proc_iterate_reversed(const uint8_t *str, const uint8_t *start, int32_t *dst);
bool utf8_is_letter(int32_t ch); bool utf8_is_letter(int32_t ch);
char *string_strip_whitespace(char *str); size_t string_lstrip(char *str);
size_t string_rstrip(char *str);
size_t string_strip(char *str);
/* Caller has to free the original string, /* Caller has to free the original string,
also keep in mind that after operating on a char array, also keep in mind that after operating on a char array,
@@ -40,7 +41,12 @@ char *string_strip_whitespace(char *str);
Consider a macro which does this consistently Consider a macro which does this consistently
*/ */
char_array *char_array_from_string(char *str); char_array *char_array_from_string(char *str);
char *char_array_to_string(char_array *array, bool free_array); char *char_array_get_string(char_array *array);
// Frees the char_array and returns a standard NUL-terminated string
char *char_array_to_string(char_array *array);
size_t char_array_len(char_array *array);
void char_array_append(char_array *array, char *str); void char_array_append(char_array *array, char *str);
void char_array_append_len(char_array *array, char *str, size_t len); void char_array_append_len(char_array *array, char *str, size_t len);
@@ -95,6 +101,7 @@ char *cstring_array_get_token(cstring_array *self, uint32_t i);
void cstring_array_destroy(cstring_array *self); void cstring_array_destroy(cstring_array *self);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif