mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-09 20:35:49 +01:00
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.
This commit is contained in:
parent
6858b5342f
commit
106fb627f1
12
ChangeLog
12
ChangeLog
@ -1,3 +1,15 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
2001-06-08 Havoc Pennington <hp@redhat.com>
|
2001-06-08 Havoc Pennington <hp@redhat.com>
|
||||||
|
|
||||||
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
2001-06-08 Havoc Pennington <hp@redhat.com>
|
2001-06-08 Havoc Pennington <hp@redhat.com>
|
||||||
|
|
||||||
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
2001-06-08 Havoc Pennington <hp@redhat.com>
|
2001-06-08 Havoc Pennington <hp@redhat.com>
|
||||||
|
|
||||||
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
2001-06-08 Havoc Pennington <hp@redhat.com>
|
2001-06-08 Havoc Pennington <hp@redhat.com>
|
||||||
|
|
||||||
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
2001-06-08 Havoc Pennington <hp@redhat.com>
|
2001-06-08 Havoc Pennington <hp@redhat.com>
|
||||||
|
|
||||||
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
2001-06-08 Havoc Pennington <hp@redhat.com>
|
2001-06-08 Havoc Pennington <hp@redhat.com>
|
||||||
|
|
||||||
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
2001-06-08 Havoc Pennington <hp@redhat.com>
|
2001-06-08 Havoc Pennington <hp@redhat.com>
|
||||||
|
|
||||||
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
2001-06-08 Havoc Pennington <hp@redhat.com>
|
2001-06-08 Havoc Pennington <hp@redhat.com>
|
||||||
|
|
||||||
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
* gspawn.c: support G_SPAWN_FILE_AND_ARGV_ZERO specifying that
|
||||||
|
146
glib/gstrfuncs.c
146
glib/gstrfuncs.c
@ -1497,3 +1497,149 @@ g_strjoin (const gchar *separator,
|
|||||||
|
|
||||||
return string;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,6 +60,15 @@ gsize g_strlcpy (gchar *dest,
|
|||||||
gsize g_strlcat (gchar *dest,
|
gsize g_strlcat (gchar *dest,
|
||||||
const gchar *src,
|
const gchar *src,
|
||||||
gsize dest_size);
|
gsize dest_size);
|
||||||
|
gchar * g_strstr_len (const gchar *haystack,
|
||||||
|
gint haystack_len,
|
||||||
|
const gchar *needle);
|
||||||
|
gchar * g_strrstr (const gchar *haystack,
|
||||||
|
const gchar *needle);
|
||||||
|
gchar * g_strrstr_len (const gchar *haystack,
|
||||||
|
gint haystack_len,
|
||||||
|
const gchar *needle);
|
||||||
|
|
||||||
/* removes leading spaces */
|
/* removes leading spaces */
|
||||||
gchar* g_strchug (gchar *string);
|
gchar* g_strchug (gchar *string);
|
||||||
/* removes trailing spaces */
|
/* removes trailing spaces */
|
||||||
|
@ -188,10 +188,11 @@ gchar* g_utf8_strncpy (gchar *dest,
|
|||||||
|
|
||||||
/* Find the UTF-8 character corresponding to ch, in string p. These
|
/* Find the UTF-8 character corresponding to ch, in string p. These
|
||||||
functions are equivalants to strchr and strrchr */
|
functions are equivalants to strchr and strrchr */
|
||||||
|
|
||||||
gchar* g_utf8_strchr (const gchar *p,
|
gchar* g_utf8_strchr (const gchar *p,
|
||||||
|
gint len,
|
||||||
gunichar c);
|
gunichar c);
|
||||||
gchar* g_utf8_strrchr (const gchar *p,
|
gchar* g_utf8_strrchr (const gchar *p,
|
||||||
|
gint len,
|
||||||
gunichar c);
|
gunichar c);
|
||||||
|
|
||||||
gunichar2 *g_utf8_to_utf16 (const gchar *str,
|
gunichar2 *g_utf8_to_utf16 (const gchar *str,
|
||||||
|
28
glib/gutf8.c
28
glib/gutf8.c
@ -494,51 +494,55 @@ g_unichar_to_utf8 (gunichar c, gchar *outbuf)
|
|||||||
/**
|
/**
|
||||||
* g_utf8_strchr:
|
* g_utf8_strchr:
|
||||||
* @p: a nul-terminated utf-8 string
|
* @p: a nul-terminated utf-8 string
|
||||||
|
* @p_len: the maximum length of p
|
||||||
* @c: a iso-10646 character
|
* @c: a iso-10646 character
|
||||||
*
|
*
|
||||||
* Find the leftmost occurence of the given iso-10646 character
|
* Find the leftmost occurence of the given iso-10646 character
|
||||||
* in a UTF-8 string.
|
* in a UTF-8 string, while limiting the search to p_len bytes.
|
||||||
|
* If len is -1, allow unbounded search.
|
||||||
*
|
*
|
||||||
* Return value: NULL if the string does not contain the character, otherwise, a
|
* Return value: NULL if the string does not contain the character, otherwise, a
|
||||||
* a pointer to the start of the leftmost of the character in the string.
|
* a pointer to the start of the leftmost of the character in the string.
|
||||||
**/
|
**/
|
||||||
gchar *
|
gchar *
|
||||||
g_utf8_strchr (const char *p, gunichar c)
|
g_utf8_strchr (const char *p,
|
||||||
|
gint p_len,
|
||||||
|
gunichar c)
|
||||||
{
|
{
|
||||||
gchar ch[10];
|
gchar ch[10];
|
||||||
|
|
||||||
gint len = g_unichar_to_utf8 (c, ch);
|
gint len = g_unichar_to_utf8 (c, ch);
|
||||||
ch[len] = '\0';
|
ch[len] = '\0';
|
||||||
|
|
||||||
return strstr(p, ch);
|
return g_strstr_len (p, p_len, ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
|
||||||
/**
|
/**
|
||||||
* g_utf8_strrchr:
|
* g_utf8_strrchr:
|
||||||
* @p: a nul-terminated utf-8 string
|
* @p: a nul-terminated utf-8 string
|
||||||
|
* @p_len: the maximum length of p
|
||||||
* @c: a iso-10646 character/
|
* @c: a iso-10646 character/
|
||||||
*
|
*
|
||||||
* Find the rightmost occurence of the given iso-10646 character
|
* Find the rightmost occurence of the given iso-10646 character
|
||||||
* in a UTF-8 string.
|
* in a UTF-8 string, while limiting the search to p_len bytes.
|
||||||
|
* If len is -1, allow unbounded search.
|
||||||
*
|
*
|
||||||
* Return value: NULL if the string does not contain the character, otherwise, a
|
* Return value: NULL if the string does not contain the character, otherwise, a
|
||||||
* a pointer to the start of the rightmost of the character in the string.
|
* a pointer to the start of the rightmost of the character in the string.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
/* This is ifdefed out atm as there is no strrstr function in libc.
|
|
||||||
*/
|
|
||||||
gchar *
|
gchar *
|
||||||
unicode_strrchr (const char *p, gunichar c)
|
g_utf8_strrchr (const char *p,
|
||||||
|
gint p_len,
|
||||||
|
gunichar c)
|
||||||
{
|
{
|
||||||
gchar ch[10];
|
gchar ch[10];
|
||||||
|
|
||||||
len = g_unichar_to_utf8 (c, ch);
|
gint len = g_unichar_to_utf8 (c, ch);
|
||||||
ch[len] = '\0';
|
ch[len] = '\0';
|
||||||
|
|
||||||
return strrstr(p, ch);
|
return g_strrstr_len (p, p_len, ch);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/* Like g_utf8_get_char, but take a maximum length
|
/* Like g_utf8_get_char, but take a maximum length
|
||||||
|
146
gstrfuncs.c
146
gstrfuncs.c
@ -1497,3 +1497,149 @@ g_strjoin (const gchar *separator,
|
|||||||
|
|
||||||
return string;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,6 +60,15 @@ gsize g_strlcpy (gchar *dest,
|
|||||||
gsize g_strlcat (gchar *dest,
|
gsize g_strlcat (gchar *dest,
|
||||||
const gchar *src,
|
const gchar *src,
|
||||||
gsize dest_size);
|
gsize dest_size);
|
||||||
|
gchar * g_strstr_len (const gchar *haystack,
|
||||||
|
gint haystack_len,
|
||||||
|
const gchar *needle);
|
||||||
|
gchar * g_strrstr (const gchar *haystack,
|
||||||
|
const gchar *needle);
|
||||||
|
gchar * g_strrstr_len (const gchar *haystack,
|
||||||
|
gint haystack_len,
|
||||||
|
const gchar *needle);
|
||||||
|
|
||||||
/* removes leading spaces */
|
/* removes leading spaces */
|
||||||
gchar* g_strchug (gchar *string);
|
gchar* g_strchug (gchar *string);
|
||||||
/* removes trailing spaces */
|
/* removes trailing spaces */
|
||||||
|
@ -188,10 +188,11 @@ gchar* g_utf8_strncpy (gchar *dest,
|
|||||||
|
|
||||||
/* Find the UTF-8 character corresponding to ch, in string p. These
|
/* Find the UTF-8 character corresponding to ch, in string p. These
|
||||||
functions are equivalants to strchr and strrchr */
|
functions are equivalants to strchr and strrchr */
|
||||||
|
|
||||||
gchar* g_utf8_strchr (const gchar *p,
|
gchar* g_utf8_strchr (const gchar *p,
|
||||||
|
gint len,
|
||||||
gunichar c);
|
gunichar c);
|
||||||
gchar* g_utf8_strrchr (const gchar *p,
|
gchar* g_utf8_strrchr (const gchar *p,
|
||||||
|
gint len,
|
||||||
gunichar c);
|
gunichar c);
|
||||||
|
|
||||||
gunichar2 *g_utf8_to_utf16 (const gchar *str,
|
gunichar2 *g_utf8_to_utf16 (const gchar *str,
|
||||||
|
28
gutf8.c
28
gutf8.c
@ -494,51 +494,55 @@ g_unichar_to_utf8 (gunichar c, gchar *outbuf)
|
|||||||
/**
|
/**
|
||||||
* g_utf8_strchr:
|
* g_utf8_strchr:
|
||||||
* @p: a nul-terminated utf-8 string
|
* @p: a nul-terminated utf-8 string
|
||||||
|
* @p_len: the maximum length of p
|
||||||
* @c: a iso-10646 character
|
* @c: a iso-10646 character
|
||||||
*
|
*
|
||||||
* Find the leftmost occurence of the given iso-10646 character
|
* Find the leftmost occurence of the given iso-10646 character
|
||||||
* in a UTF-8 string.
|
* in a UTF-8 string, while limiting the search to p_len bytes.
|
||||||
|
* If len is -1, allow unbounded search.
|
||||||
*
|
*
|
||||||
* Return value: NULL if the string does not contain the character, otherwise, a
|
* Return value: NULL if the string does not contain the character, otherwise, a
|
||||||
* a pointer to the start of the leftmost of the character in the string.
|
* a pointer to the start of the leftmost of the character in the string.
|
||||||
**/
|
**/
|
||||||
gchar *
|
gchar *
|
||||||
g_utf8_strchr (const char *p, gunichar c)
|
g_utf8_strchr (const char *p,
|
||||||
|
gint p_len,
|
||||||
|
gunichar c)
|
||||||
{
|
{
|
||||||
gchar ch[10];
|
gchar ch[10];
|
||||||
|
|
||||||
gint len = g_unichar_to_utf8 (c, ch);
|
gint len = g_unichar_to_utf8 (c, ch);
|
||||||
ch[len] = '\0';
|
ch[len] = '\0';
|
||||||
|
|
||||||
return strstr(p, ch);
|
return g_strstr_len (p, p_len, ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
|
||||||
/**
|
/**
|
||||||
* g_utf8_strrchr:
|
* g_utf8_strrchr:
|
||||||
* @p: a nul-terminated utf-8 string
|
* @p: a nul-terminated utf-8 string
|
||||||
|
* @p_len: the maximum length of p
|
||||||
* @c: a iso-10646 character/
|
* @c: a iso-10646 character/
|
||||||
*
|
*
|
||||||
* Find the rightmost occurence of the given iso-10646 character
|
* Find the rightmost occurence of the given iso-10646 character
|
||||||
* in a UTF-8 string.
|
* in a UTF-8 string, while limiting the search to p_len bytes.
|
||||||
|
* If len is -1, allow unbounded search.
|
||||||
*
|
*
|
||||||
* Return value: NULL if the string does not contain the character, otherwise, a
|
* Return value: NULL if the string does not contain the character, otherwise, a
|
||||||
* a pointer to the start of the rightmost of the character in the string.
|
* a pointer to the start of the rightmost of the character in the string.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
/* This is ifdefed out atm as there is no strrstr function in libc.
|
|
||||||
*/
|
|
||||||
gchar *
|
gchar *
|
||||||
unicode_strrchr (const char *p, gunichar c)
|
g_utf8_strrchr (const char *p,
|
||||||
|
gint p_len,
|
||||||
|
gunichar c)
|
||||||
{
|
{
|
||||||
gchar ch[10];
|
gchar ch[10];
|
||||||
|
|
||||||
len = g_unichar_to_utf8 (c, ch);
|
gint len = g_unichar_to_utf8 (c, ch);
|
||||||
ch[len] = '\0';
|
ch[len] = '\0';
|
||||||
|
|
||||||
return strrstr(p, ch);
|
return g_strrstr_len (p, p_len, ch);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/* Like g_utf8_get_char, but take a maximum length
|
/* Like g_utf8_get_char, but take a maximum length
|
||||||
|
@ -116,7 +116,13 @@ main (int argc,
|
|||||||
g_assert (strcmp (copy[1], "Bar") == 0);
|
g_assert (strcmp (copy[1], "Bar") == 0);
|
||||||
g_assert (copy[2] == NULL);
|
g_assert (copy[2] == NULL);
|
||||||
g_strfreev (copy);
|
g_strfreev (copy);
|
||||||
|
|
||||||
|
g_assert (strcmp (g_strstr_len ("FooBarFooBarFoo", 6, "Bar"),
|
||||||
|
"BarFooBarFoo") == 0);
|
||||||
|
g_assert (strcmp (g_strrstr ("FooBarFooBarFoo", "Bar"),
|
||||||
|
"BarFoo") == 0);
|
||||||
|
g_assert (strcmp (g_strrstr_len ("FooBarFooBarFoo", 14, "BarFoo"),
|
||||||
|
"BarFooBarFoo") == 0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user