diff options
author | Alex Larsson <alexl@redhat.com> | 2001-06-08 23:14:03 +0000 |
---|---|---|
committer | Alexander Larsson <alexl@src.gnome.org> | 2001-06-08 23:14:03 +0000 |
commit | 106fb627f1dc5c51a8fb759702344ae6f08d60c7 (patch) | |
tree | 91646d99c9408ab0658cd7ec21973301c5713e4b /gstrfuncs.c | |
parent | 6858b5342f8811923d67f4c54213a5fd827aae3d (diff) |
Added new functions g_strstr_len, g_strrstr and g_strrstr_len
2001-06-08 Alex Larsson <alexl@redhat.com>
* gstrfuncs.[ch]:
Added new functions g_strstr_len, g_strrstr and g_strrstr_len
* tests/strfunc-test.c:
Add some tests for the new functions.
* gunicode.h:
* gutf8.c:
Add length argument to g_utf8_strchr and g_utf8_strrchr.
Diffstat (limited to 'gstrfuncs.c')
-rw-r--r-- | gstrfuncs.c | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/gstrfuncs.c b/gstrfuncs.c index 734118a7e..dd7dc4520 100644 --- a/gstrfuncs.c +++ b/gstrfuncs.c @@ -1497,3 +1497,149 @@ g_strjoin (const gchar *separator, 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, + gint haystack_len, + const gchar *needle) +{ + int i; + + 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 char *p = haystack; + int needle_len = strlen (needle); + const char *end = haystack + haystack_len - needle_len; + + if (needle_len == 0) + return (char *)haystack; + + while (*p && p <= end) + { + for (i = 0; i < needle_len; i++) + if (p[i] != needle[i]) + goto next; + + return (char *)p; + + next: + p++; + } + } + + return NULL; +} + +/** + * g_strrstr_len: + * @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) +{ + int i; + int needle_len = strlen (needle); + int haystack_len = strlen (haystack); + const char *p = haystack + haystack_len - needle_len; + + g_return_val_if_fail (haystack != NULL, NULL); + g_return_val_if_fail (needle != NULL, NULL); + + if (needle_len == 0) + return (char *)p; + + while (p >= haystack) + { + for (i = 0; i < needle_len; i++) + if (p[i] != needle[i]) + goto next; + + return (char *)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, + gint haystack_len, + const gchar *needle) +{ + int i; + + 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 + { + int needle_len = strlen (needle); + const char *haystack_max = haystack + haystack_len; + const char *p = haystack; + + while (p < haystack_max && *p) + p++; + + p -= needle_len; + + while (p >= haystack) + { + for (i = 0; i < needle_len; i++) + if (p[i] != needle[i]) + goto next; + + return (char *)p; + + next: + p--; + } + } + + return NULL; +} + + |