📄 gstrfuncs.c
字号:
#ifdef SIGTSTP case SIGTSTP: return "Stopped";#endif#ifdef SIGTTIN case SIGTTIN: return "Stopped (tty input)";#endif#ifdef SIGTTOU case SIGTTOU: return "Stopped (tty output)";#endif#ifdef SIGURG case SIGURG: return "Urgent condition";#endif#ifdef SIGXCPU case SIGXCPU: return "CPU time limit exceeded";#endif#ifdef SIGXFSZ case SIGXFSZ: return "File size limit exceeded";#endif#ifdef SIGVTALRM case SIGVTALRM: return "Virtual time alarm";#endif#ifdef SIGPROF case SIGPROF: return "Profile signal";#endif#ifdef SIGWINCH case SIGWINCH: return "Window size changed";#endif#ifdef SIGIO case SIGIO: return "Possible I/O";#endif#ifdef SIGPWR case SIGPWR: return "Power failure";#endif#ifdef SIGUNUSED case SIGUNUSED: return "Unused signal";#endif }#else /* NO_SYS_SIGLIST */#ifdef NO_SYS_SIGLIST_DECL extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/#endif return (char*) /* this function should return const --josh */ sys_siglist [signum];#endif /* NO_SYS_SIGLIST */ msg = g_static_private_get (&msg_private); if (!msg) { msg = g_new (gchar, 64); g_static_private_set (&msg_private, msg, g_free); } sprintf (msg, "unknown signal (%d)", signum); return msg;}/* Functions g_strlcpy and g_strlcat were originally developed by * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code. * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3 * for more information. */#ifdef HAVE_STRLCPY/* Use the native ones, if available; they might be implemented in assembly */gsizeg_strlcpy (gchar *dest, const gchar *src, gsize dest_size){ g_return_val_if_fail (dest != NULL, 0); g_return_val_if_fail (src != NULL, 0); return strlcpy (dest, src, dest_size);}gsizeg_strlcat (gchar *dest, const gchar *src, gsize dest_size){ g_return_val_if_fail (dest != NULL, 0); g_return_val_if_fail (src != NULL, 0); return strlcat (dest, src, dest_size);}#else /* ! HAVE_STRLCPY *//* g_strlcpy * * Copy string src to buffer dest (of buffer size dest_size). At most * dest_size-1 characters will be copied. Always NUL terminates * (unless dest_size == 0). This function does NOT allocate memory. * Unlike strncpy, this function doesn't pad dest (so it's often faster). * Returns size of attempted result, strlen(src), * so if retval >= dest_size, truncation occurred. */gsizeg_strlcpy (gchar *dest, const gchar *src, gsize dest_size){ register gchar *d = dest; register const gchar *s = src; register gsize n = dest_size; g_return_val_if_fail (dest != NULL, 0); g_return_val_if_fail (src != NULL, 0); /* Copy as many bytes as will fit */ if (n != 0 && --n != 0) do { register gchar c = *s++; *d++ = c; if (c == 0) break; } while (--n != 0); /* If not enough room in dest, add NUL and traverse rest of src */ if (n == 0) { if (dest_size != 0) *d = 0; while (*s++) ; } return s - src - 1; /* count does not include NUL */}/* g_strlcat * * Appends string src to buffer dest (of buffer size dest_size). * At most dest_size-1 characters will be copied. * Unlike strncat, dest_size is the full size of dest, not the space left over. * This function does NOT allocate memory. * This always NUL terminates (unless siz == 0 or there were no NUL characters * in the dest_size characters of dest to start with). * Returns size of attempted result, which is * MIN (dest_size, strlen (original dest)) + strlen (src), * so if retval >= dest_size, truncation occurred. */gsizeg_strlcat (gchar *dest, const gchar *src, gsize dest_size){ register gchar *d = dest; register const gchar *s = src; register gsize bytes_left = dest_size; gsize dlength; /* Logically, MIN (strlen (d), dest_size) */ g_return_val_if_fail (dest != NULL, 0); g_return_val_if_fail (src != NULL, 0); /* Find the end of dst and adjust bytes left but don't go past end */ while (*d != 0 && bytes_left-- != 0) d++; dlength = d - dest; bytes_left = dest_size - dlength; if (bytes_left == 0) return dlength + strlen (s); while (*s != 0) { if (bytes_left != 1) { *d++ = *s; bytes_left--; } s++; } *d = 0; return dlength + (s - src); /* count does not include NUL */}#endif /* ! HAVE_STRLCPY *//** * g_ascii_strdown: * @str: a string. * @len: length of @str in bytes, or -1 if @str is nul-terminated. * * Converts all upper case ASCII letters to lower case ASCII letters. * * Return value: a newly-allocated string, with all the upper case * characters in @str converted to lower case, with * semantics that exactly match g_ascii_tolower(). (Note * that this is unlike the old g_strdown(), which modified * the string in place.) **/gchar*g_ascii_strdown (const gchar *str, gssize len){ gchar *result, *s; g_return_val_if_fail (str != NULL, NULL); if (len < 0) len = strlen (str); result = g_strndup (str, len); for (s = result; *s; s++) *s = g_ascii_tolower (*s); return result;}/** * g_ascii_strup: * @str: a string. * @len: length of @str in bytes, or -1 if @str is nul-terminated. * * Converts all lower case ASCII letters to upper case ASCII letters. * * Return value: a newly allocated string, with all the lower case * characters in @str converted to upper case, with * semantics that exactly match g_ascii_toupper(). (Note * that this is unlike the old g_strup(), which modified * the string in place.) **/gchar*g_ascii_strup (const gchar *str, gssize len){ gchar *result, *s; g_return_val_if_fail (str != NULL, NULL); if (len < 0) len = strlen (str); result = g_strndup (str, len); for (s = result; *s; s++) *s = g_ascii_toupper (*s); return result;}gchar*g_strdown (gchar *string){ register guchar *s; g_return_val_if_fail (string != NULL, NULL); s = (guchar *) string; while (*s) { if (isupper (*s)) *s = tolower (*s); s++; } return (gchar *) string;}gchar*g_strup (gchar *string){ register guchar *s; g_return_val_if_fail (string != NULL, NULL); s = (guchar *) string; while (*s) { if (islower (*s)) *s = toupper (*s); s++; } return (gchar *) string;}gchar*g_strreverse (gchar *string){ g_return_val_if_fail (string != NULL, NULL); if (*string) { register gchar *h, *t; h = string; t = string + strlen (string) - 1; while (h < t) { register gchar c; c = *h; *h = *t; h++; *t = c; t--; } } return string;}/** * g_ascii_tolower: * @c: any character. * * Convert a character to ASCII lower case. * * Unlike the standard C library tolower() function, this only * recognizes standard ASCII letters and ignores the locale, returning * all non-ASCII characters unchanged, even if they are lower case * letters in a particular character set. Also unlike the standard * library function, this takes and returns a char, not an int, so * don't call it on %EOF but no need to worry about casting to #guchar * before passing a possibly non-ASCII character in. * * Return value: the result of converting @c to lower case. * If @c is not an ASCII upper case letter, * @c is returned unchanged. **/gcharg_ascii_tolower (gchar c){ return g_ascii_isupper (c) ? c - 'A' + 'a' : c;}/** * g_ascii_toupper: * @c: any character. * * Convert a character to ASCII upper case. * * Unlike the standard C library toupper() function, this only * recognizes standard ASCII letters and ignores the locale, returning * all non-ASCII characters unchanged, even if they are upper case * letters in a particular character set. Also unlike the standard * library function, this takes and returns a char, not an int, so * don't call it on %EOF but no need to worry about casting to #guchar * before passing a possibly non-ASCII character in. * * Return value: the result of converting @c to upper case. * If @c is not an ASCII lower case letter, * @c is returned unchanged. **/gcharg_ascii_toupper (gchar c){ return g_ascii_islower (c) ? c - 'a' + 'A' : c;}/** * g_ascii_digit_value: * @c: an ASCII character. * * Determines the numeric value of a character as a decimal * digit. Differs from g_unichar_digit_value() because it takes * a char, so there's no worry about sign extension if characters * are signed. * * Return value: If @c is a decimal digit (according to * g_ascii_isdigit()), its numeric value. Otherwise, -1. **/intg_ascii_digit_value (gchar c){ if (g_ascii_isdigit (c)) return c - '0'; return -1;}/** * g_ascii_xdigit_value: * @c: an ASCII character. * * Determines the numeric value of a character as a hexidecimal * digit. Differs from g_unichar_xdigit_value() because it takes * a char, so there's no worry about sign extension if characters * are signed. * * Return value: If @c is a hex digit (according to * g_ascii_isxdigit()), its numeric value. Otherwise, -1. **/intg_ascii_xdigit_value (gchar c){ if (c >= 'A' && c <= 'F') return c - 'A' + 10; if (c >= 'a' && c <= 'f') return c - 'a' + 10; return g_ascii_digit_value (c);}/** * g_ascii_strcasecmp: * @s1: string to compare with @s2. * @s2: string to compare with @s1. * * Compare two strings, ignoring the case of ASCII characters. * * Unlike the BSD strcasecmp() function, this only recognizes standard * ASCII letters and ignores the locale, treating all non-ASCII * characters as if they are not letters. * * Return value: an integer less than, equal to, or greater than * zero if @s1 is found, respectively, to be less than, * to match, or to be greater than @s2. **/gintg_ascii_strcasecmp (const gchar *s1, const gchar *s2){ gint c1, c2; g_return_val_if_fail (s1 != NULL, 0); g_return_val_if_fail (s2 != NULL, 0); while (*s1 && *s2) { c1 = (gint)(guchar) g_ascii_tolower (*s1); c2 = (gint)(guchar) g_ascii_tolower (*s2); if (c1 != c2) return (c1 - c2); s1++; s2++; } return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));}/** * g_ascii_strncasecmp: * @s1: string to compare with @s2. * @s2: string to compare with @s1. * @n: number of characters to compare. * * Compare @s1 and @s2, ignoring the case of ASCII characters and any * characters after the first @n in each string. * * Unlike the BSD strcasecmp() function, this only recognizes standard * ASCII letters and ignores the locale, treating all non-ASCII * characters as if they are not letters. * * Return value: an integer less than, equal to, or greater than zero * if the first @n bytes of @s1 is found, respectively, * to be less than, to match, or to be greater than the * first @n bytes of @s2. **/gintg_ascii_strncasecmp (const gchar *s1, const gchar *s2, gsize n){ gint c1, c2; g_return_val_if_fail (s1 != NULL, 0); g_return_val_if_fail (s2 != NULL, 0); while (n && *s1 && *s2) { n -= 1; c1 = (gint)(guchar) g_ascii_tolower (*s1); c2 = (gint)(guchar) g_ascii_tolower (*s2); if (c1 != c2) return (c1 - c2); s1++; s2++; } if (n) return (((gint) (guchar) *s1) - ((gint) (guchar) *s2)); else return 0;}gintg_strcasecmp (const gchar *s1, const gchar *s2){#ifdef HAVE_STRCASECMP g_return_val_if_fail (s1 != NULL, 0); g_return_val_if_fail (s2 != NULL, 0); return strcasecmp (s1, s2);#else gint c1, c2; g_return_val_if_fail (s1 != NULL, 0); g_return_val_if_fail (s2 != NULL, 0); while (*s1 && *s2) { /* According to A. Cox, some platforms have islower's that * don't work right on non-uppercase */ c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1; c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2; if (c1 != c2) return (c1 - c2); s1++; s2++; } return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));#endif}gintg_strncasecmp (const gchar *s1, const gchar *s2, gsize n) {#ifdef HAVE_STRNCASECMP return strncasecmp (s1, s2, n);#else gint c1, c2; g_return_val_if_fail (s1 != NULL, 0); g_return_val_if_fail (s2 != NULL, 0); while (n && *s1 && *s2) { n -= 1; /* According to A. Cox, some platforms have islower's that * don't work right on non-uppercase */ c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1; c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2; if (c1 != c2) return (c1 - c2); s1++; s2++; } if (n) return (((gint) (guchar) *s1) - ((gint) (guchar) *s2)); else return 0;#endif}gchar*g_strdelimit (gchar *string, const gchar *delimiters, gchar new_delim){ register gchar *c; g_return_val_if_fail (string != NULL, NULL); if (!delimiters) delimiters = G_STR_DELIMITERS; for (c = string; *c; c++) { if (strchr (delimiters, *c)) *c = new_delim; } return string;}gchar*g_strcanon (gchar *string, const gchar *valid_chars, gchar substitutor){ register gchar *c;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -