📄 su_strlst.c
字号:
str = ""; if (self && str && su_strlst_increase(self)) { int len = strlen(str); char *retval = su_alloc(self->sl_home, len + 1); if (retval) { memcpy(retval, str, len); retval[len] = 0; self->sl_list[self->sl_len++] = retval; self->sl_total += len; } return retval; } return NULL;}/**Append a string to list. * * The string is not copied, and it @b must not be modified while string * list exists. * * @param self pointer to a string list object * @param str string to be appended * * @return * Pointer to string, if successful, or NULL upon an error. */char const *su_strlst_append(su_strlst_t *self, char const *str){ if (str == NULL) str = ""; if (self && su_strlst_increase(self)) { self->sl_list[self->sl_len++] = str; self->sl_total += strlen(str); return str; } return NULL;}/**Append a formatted string to the list. * * Format a string according to a @a fmt like printf(). The resulting string * is copied to a memory area freshly allocated from a the memory home of * the list and appended to the string list. * * @param self pointer to a string list object * @param fmt format string * @param ... argument list (must match with the @a fmt format string) * * @return A pointer to a fresh copy of formatting result, or NULL upon an * error. */char const *su_slprintf(su_strlst_t *self, char const *fmt, ...){ char const *str; va_list ap; va_start(ap, fmt); str = su_slvprintf(self, fmt, ap); va_end(ap); return str;}/**Append a formatted string to the list. * * Format a string according to a @a fmt like vprintf(). The resulting * string is copied to a memory area freshly allocated from a the memory * home of the list and appended to the string list. * * @param self pointer to a string list object * @param fmt format string * @param ap stdarg argument list (must match with the @a fmt format string) * * @return A pointer to a fresh copy of formatting result, or NULL upon an * error. */char const *su_slvprintf(su_strlst_t *self, char const *fmt, va_list ap){ char *str = NULL; if (self && su_strlst_increase(self)) { str = su_vsprintf(self->sl_home, fmt, ap); if (str) { self->sl_list[self->sl_len++] = str; self->sl_total += strlen(str); } } return str;}/**Returns a numbered item from the list of strings. The numbering starts from * 0. * * @param self pointer to a string list object * @param i string index * * @return * Pointer to string, if item exists, or NULL if index is out of bounds or * list does not exist. */char const *su_strlst_item(su_strlst_t const *self, unsigned i){ if (self && i < self->sl_len) return self->sl_list[i]; else return NULL;}/**Sets a item to the list of strings. * * Note that the item numbering starts from 0. * * @param self pointer to a string list object * @param i string index * @param s string to be set as item @a i * * @return * Pointer to string, if item exists, or NULL if index is out of bounds or * list does not exist. */char const *su_strlst_set_item(su_strlst_t *self, unsigned i, char const *s){ char const *old = NULL; if (self == NULL) return NULL; else if (i == self->sl_len) return (void)su_strlst_append(self, s), NULL; else if (i > self->sl_len) return NULL; if (s == NULL) s = ""; old = self->sl_list[i]; self->sl_list[i] = s; return old;}/**Removes a numbered item from the list of strings. The numbering starts from * 0. The caller is responsible of reclaiming memory used by the removed string. * * @param self pointer to a string list object * @param i string index * * @return * Pointer to string, if item exists, or NULL if index is out of bounds or * list does not exist. */SU_DLL char const *su_strlst_remove(su_strlst_t *self, unsigned i){ if (self && i < self->sl_len) { char const *s = self->sl_list[i]; memmove(&self->sl_list[i], &self->sl_list[i + 1], &self->sl_list[self->sl_len] - &self->sl_list[i]); self->sl_len--; return s; } else return NULL;}/** Concatenate list of strings to one string. * * The function su_strlst_join() concatenates the list of strings. Between * each string in list it uses @a sep. The separator is not inserted after * the last string in list, but one can always append an empty string to the * list. * * The string is allocated from the memory @a home. If @a home is NULL, the * string is allocated using malloc(). * * @param self pointer to a string list object * @param home home pointer * @param sep separator (may be NULL) * * @return * * The function su_strlst_join() returns a concatenation of the strings in * list, or NULL upon an error. */char *su_strlst_join(su_strlst_t *self, su_home_t *home, char const *sep){ if (!sep) sep = ""; if (self && self->sl_len > 0) { unsigned seplen = strlen(sep); unsigned total = self->sl_total + seplen * (self->sl_len - 1); char *retval = su_alloc(home, total + 1); if (retval) { char *s = retval; unsigned i = 0, len; for (;;) { len = strlen(self->sl_list[i]); memcpy(s, self->sl_list[i], len), s += len; if (++i >= self->sl_len) break; memcpy(s, sep, seplen), s += seplen; } *s = '\0'; assert(s == retval + total); } return retval; } return su_strdup(home, "");}static inlinesu_strlst_t *su_strlst_split0(su_strlst_t *l, char *str, char const *sep){ int n = sep ? strlen(sep) : 0; char *s; if (n > 0) { while ((s = strstr(str, sep))) { *s = '\0'; if (!su_strlst_append(l, str)) return NULL; str = s + n; } } if (!su_strlst_append(l, str)) return NULL; return l;}/**Split a string. * * Splits a string to substrings. It returns a string list object. The * string to be split is not copied but instead modified in place. Use * su_strlst_dup_split() if you do not want to modify @a str. * * @param home home pointer * @param str string to be split * @param sep separator between substrings * * @return * Pointer to list of strings, if successful, or NULL upon an error. */su_strlst_t *su_strlst_split(su_home_t *home, char *str, char const *sep){ if (str) { su_strlst_t *l = su_strlst_create(home); if (!su_strlst_split0(l, str, sep)) su_strlst_destroy(l), l = NULL; return l; } return NULL;}/**Duplicate and split a string. * * Duplicates a string and splits the result to substrings. It returns a * string list object. The string to be splitted is not modified. * * @param home home pointer * @param cstr string to be split * @param sep separator between substrings * * @return * Pointer to list of strings, if successful, or NULL upon an error. */su_strlst_t *su_strlst_dup_split(su_home_t *home, char const *cstr, char const *sep){ if (cstr) { su_strlst_t *l = su_strlst_create(home); if (l) { char *s = su_strdup(su_strlst_home(l), cstr); if (s && !su_strlst_split0(l, s, sep)) su_strlst_destroy(l), l = NULL; } return l; } return NULL;}/** Get number of items in list. * * The function su_strlst_len() returns the number of items in the * string list. * */unsigned su_strlst_len(su_strlst_t const *l){ return l ? l->sl_len : 0;}/**Get a string array from list. * * The function su_strlst_get_array() returns an array of strings. The * length of the array is always one longer than the length of the string * list, and the last string in the returned array is always NULL. * * @param self pointer to a string list object * * @return * Pointer to array of strings, or NULL if error occurred. */char const **su_strlst_get_array(su_strlst_t *self){ if (self) { char const **retval = su_alloc(self->sl_home, sizeof(retval[0]) * (self->sl_len + 1)); if (retval) { retval[self->sl_len] = NULL; return memcpy(retval, self->sl_list, sizeof(retval[0]) * self->sl_len); } } return NULL;}/**Free a string array. * * The function su_strlst_free_array() discards a string array allocated * with su_strlst_get_array(). * * @param self pointer to a string list object * @param array string array to be freed * */void su_strlst_free_array(su_strlst_t *self, char const **array){ if (array) su_free(self->sl_home, (void *)array);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -