📄 gstrfuncs.c
字号:
g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (valid_chars != NULL, NULL); for (c = string; *c; c++) { if (!strchr (valid_chars, *c)) *c = substitutor; } return string;}gchar*g_strcompress (const gchar *source){ const gchar *p = source, *octal; gchar *dest = g_malloc (strlen (source) + 1); gchar *q = dest; while (*p) { if (*p == '\\') { p++; switch (*p) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': *q = 0; octal = p; while ((p < octal + 3) && (*p >= '0') && (*p <= '7')) { *q = (*q * 8) + (*p - '0'); p++; } q++; p--; break; case 'b': *q++ = '\b'; break; case 'f': *q++ = '\f'; break; case 'n': *q++ = '\n'; break; case 'r': *q++ = '\r'; break; case 't': *q++ = '\t'; break; default: /* Also handles \" and \\ */ *q++ = *p; break; } } else *q++ = *p; p++; } *q = 0; return dest;}gchar *g_strescape (const gchar *source, const gchar *exceptions){ const guchar *p; gchar *dest; gchar *q; guchar excmap[256]; g_return_val_if_fail (source != NULL, NULL); p = (guchar *) source; /* Each source byte needs maximally four destination chars (\777) */ q = dest = g_malloc (strlen (source) * 4 + 1); memset (excmap, 0, 256); if (exceptions) { guchar *e = (guchar *) exceptions; while (*e) { excmap[*e] = 1; e++; } } while (*p) { if (excmap[*p]) *q++ = *p; else { switch (*p) { case '\b': *q++ = '\\'; *q++ = 'b'; break; case '\f': *q++ = '\\'; *q++ = 'f'; break; case '\n': *q++ = '\\'; *q++ = 'n'; break; case '\r': *q++ = '\\'; *q++ = 'r'; break; case '\t': *q++ = '\\'; *q++ = 't'; break; case '\\': *q++ = '\\'; *q++ = '\\'; break; case '"': *q++ = '\\'; *q++ = '"'; break; default: if ((*p < ' ') || (*p >= 0177)) { *q++ = '\\'; *q++ = '0' + (((*p) >> 6) & 07); *q++ = '0' + (((*p) >> 3) & 07); *q++ = '0' + ((*p) & 07); } else *q++ = *p; break; } } p++; } *q = 0; return dest;}gchar*g_strchug (gchar *string){ guchar *start; g_return_val_if_fail (string != NULL, NULL); for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++) ; g_memmove (string, start, strlen ((gchar *) start) + 1); return string;}gchar*g_strchomp (gchar *string){ gchar *s; g_return_val_if_fail (string != NULL, NULL); if (!*string) return string; for (s = string + strlen (string) - 1; s >= string && g_ascii_isspace ((guchar)*s); s--) *s = '\0'; return string;}/** * g_strsplit: * @string: a string to split. * @delimiter: a string which specifies the places at which to split the string. * The delimiter is not included in any of the resulting strings, unless * @max_tokens is reached. * @max_tokens: the maximum number of pieces to split @string into. If this is * less than 1, the string is split completely. * * Splits a string into a maximum of @max_tokens pieces, using the given * @delimiter. If @max_tokens is reached, the remainder of @string is appended * to the last token. * * As a special case, the result of splitting the empty string "" is an empty * vector, not a vector containing a single string. The reason for this * special case is that being able to represent a empty vector is typically * more useful than consistent handling of empty elements. If you do need * to represent empty elements, you'll need to check for the empty string * before calling g_strsplit(). * * Return value: a newly-allocated %NULL-terminated array of strings. Use * g_strfreev() to free it. **/gchar**g_strsplit (const gchar *string, const gchar *delimiter, gint max_tokens){ GSList *string_list = NULL, *slist; gchar **str_array, *s; guint n = 0; const gchar *remainder; g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (delimiter != NULL, NULL); g_return_val_if_fail (delimiter[0] != '\0', NULL); if (max_tokens < 1) max_tokens = G_MAXINT; remainder = string; s = strstr (remainder, delimiter); if (s) { gsize delimiter_len = strlen (delimiter); while (--max_tokens && s) { gsize len; gchar *new_string; len = s - remainder; new_string = g_new (gchar, len + 1); strncpy (new_string, remainder, len); new_string[len] = 0; string_list = g_slist_prepend (string_list, new_string); n++; remainder = s + delimiter_len; s = strstr (remainder, delimiter); } } if (*string) { n++; string_list = g_slist_prepend (string_list, g_strdup (remainder)); } str_array = g_new (gchar*, n + 1); str_array[n--] = NULL; for (slist = string_list; slist; slist = slist->next) str_array[n--] = slist->data; g_slist_free (string_list); return str_array;}voidg_strfreev (gchar **str_array){ if (str_array) { int i; for(i = 0; str_array[i] != NULL; i++) g_free(str_array[i]); g_free (str_array); }}/** * g_strdupv: * @str_array: %NULL-terminated array of strings. * * Copies %NULL-terminated array of strings. The copy is a deep copy; * the new array should be freed by first freeing each string, then * the array itself. g_strfreev() does this for you. If called * on a %NULL value, g_strdupv() simply returns %NULL. * * Return value: a new %NULL-terminated array of strings. **/gchar**g_strdupv (gchar **str_array){ if (str_array) { gint i; gchar **retval; i = 0; while (str_array[i]) ++i; retval = g_new (gchar*, i + 1); i = 0; while (str_array[i]) { retval[i] = g_strdup (str_array[i]); ++i; } retval[i] = NULL; return retval; } else return NULL;}gchar*g_strjoinv (const gchar *separator, gchar **str_array){ gchar *string; gchar *ptr; g_return_val_if_fail (str_array != NULL, NULL); if (separator == NULL) separator = ""; if (*str_array) { gint i; gsize len; gsize separator_len; separator_len = strlen (separator); /* First part, getting length */ len = 1 + strlen (str_array[0]); for (i = 1; str_array[i] != NULL; i++) len += strlen (str_array[i]); len += separator_len * (i - 1); /* Second part, building string */ string = g_new (gchar, len); ptr = g_stpcpy (string, *str_array); for (i = 1; str_array[i] != NULL; i++) { ptr = g_stpcpy (ptr, separator); ptr = g_stpcpy (ptr, str_array[i]); } } else string = g_strdup (""); return string;}gchar*g_strjoin (const gchar *separator, ...){ gchar *string, *s; va_list args; gsize len; gsize separator_len; gchar *ptr; if (separator == NULL) separator = ""; separator_len = strlen (separator); va_start (args, separator); s = va_arg (args, gchar*); if (s) { /* First part, getting length */ len = 1 + strlen (s); s = va_arg (args, gchar*); while (s) { len += separator_len + strlen (s); s = va_arg (args, gchar*); } va_end (args); /* Second part, building string */ string = g_new (gchar, len); va_start (args, separator); s = va_arg (args, gchar*); ptr = g_stpcpy (string, s); s = va_arg (args, gchar*); while (s) { ptr = g_stpcpy (ptr, separator); ptr = g_stpcpy (ptr, s); s = va_arg (args, gchar*); } } else string = g_strdup (""); va_end (args); return string;}/** * g_strstr_len: * @haystack: a string. * @haystack_len: the maximum length of @haystack. * @needle: the string to search for. * * Searches the string @haystack for the first occurrence * of the string @needle, limiting the length of the search * to @haystack_len. * * Return value: a pointer to the found occurrence, or * %NULL if not found. **/gchar *g_strstr_len (const gchar *haystack, gssize haystack_len, const gchar *needle){ g_return_val_if_fail (haystack != NULL, NULL); g_return_val_if_fail (needle != NULL, NULL); if (haystack_len < 0) return strstr (haystack, needle); else { const gchar *p = haystack; gsize needle_len = strlen (needle); const gchar *end; gsize i; if (needle_len == 0) return (gchar *)haystack; if (haystack_len < needle_len) return NULL; end = haystack + haystack_len - needle_len; while (*p && p <= end) { for (i = 0; i < needle_len; i++) if (p[i] != needle[i]) goto next; return (gchar *)p; next: p++; } return NULL; }}/** * g_strrstr: * @haystack: a nul-terminated string. * @needle: the nul-terminated string to search for. * * Searches the string @haystack for the last occurrence * of the string @needle. * * Return value: a pointer to the found occurrence, or * %NULL if not found. **/gchar *g_strrstr (const gchar *haystack, const gchar *needle){ gsize i; gsize needle_len; gsize haystack_len; const gchar *p; g_return_val_if_fail (haystack != NULL, NULL); g_return_val_if_fail (needle != NULL, NULL); needle_len = strlen (needle); haystack_len = strlen (haystack); if (needle_len == 0) return (gchar *)haystack; if (haystack_len < needle_len) return NULL; p = haystack + haystack_len - needle_len; while (p >= haystack) { for (i = 0; i < needle_len; i++) if (p[i] != needle[i]) goto next; return (gchar *)p; next: p--; } return NULL;}/** * g_strrstr_len: * @haystack: a nul-terminated string. * @haystack_len: the maximum length of @haystack. * @needle: the nul-terminated string to search for. * * Searches the string @haystack for the last occurrence * of the string @needle, limiting the length of the search * to @haystack_len. * * Return value: a pointer to the found occurrence, or * %NULL if not found. **/gchar *g_strrstr_len (const gchar *haystack, gssize haystack_len, const gchar *needle){ g_return_val_if_fail (haystack != NULL, NULL); g_return_val_if_fail (needle != NULL, NULL); if (haystack_len < 0) return g_strrstr (haystack, needle); else { gsize needle_len = strlen (needle); const gchar *haystack_max = haystack + haystack_len; const gchar *p = haystack; gsize i; while (p < haystack_max && *p) p++; if (p < haystack + needle_len) return NULL; p -= needle_len; while (p >= haystack) { for (i = 0; i < needle_len; i++) if (p[i] != needle[i]) goto next; return (gchar *)p; next: p--; } return NULL; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -