mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-11 11:56:16 +01:00
string: Optimize g_string_append(_len)
Add static inline versions of these functions that boil down to just an memcpy. ag_string_append_len is used quite a bit in GMarkup and GTK's css parser.
This commit is contained in:
parent
0cc74d5a37
commit
9810803358
@ -539,8 +539,8 @@ g_string_append_uri_escaped (GString *string,
|
|||||||
* Returns: (transfer none): @string
|
* Returns: (transfer none): @string
|
||||||
*/
|
*/
|
||||||
GString *
|
GString *
|
||||||
g_string_append (GString *string,
|
(g_string_append) (GString *string,
|
||||||
const gchar *val)
|
const gchar *val)
|
||||||
{
|
{
|
||||||
return g_string_insert_len (string, -1, val, -1);
|
return g_string_insert_len (string, -1, val, -1);
|
||||||
}
|
}
|
||||||
@ -564,9 +564,9 @@ g_string_append (GString *string,
|
|||||||
* Returns: (transfer none): @string
|
* Returns: (transfer none): @string
|
||||||
*/
|
*/
|
||||||
GString *
|
GString *
|
||||||
g_string_append_len (GString *string,
|
(g_string_append_len) (GString *string,
|
||||||
const gchar *val,
|
const gchar *val,
|
||||||
gssize len)
|
gssize len)
|
||||||
{
|
{
|
||||||
return g_string_insert_len (string, -1, val, len);
|
return g_string_insert_len (string, -1, val, len);
|
||||||
}
|
}
|
||||||
@ -581,10 +581,9 @@ g_string_append_len (GString *string,
|
|||||||
*
|
*
|
||||||
* Returns: (transfer none): @string
|
* Returns: (transfer none): @string
|
||||||
*/
|
*/
|
||||||
#undef g_string_append_c
|
|
||||||
GString *
|
GString *
|
||||||
g_string_append_c (GString *string,
|
(g_string_append_c) (GString *string,
|
||||||
gchar c)
|
gchar c)
|
||||||
{
|
{
|
||||||
g_return_val_if_fail (string != NULL, NULL);
|
g_return_val_if_fail (string != NULL, NULL);
|
||||||
|
|
||||||
|
@ -35,6 +35,7 @@
|
|||||||
#include <glib/gunicode.h>
|
#include <glib/gunicode.h>
|
||||||
#include <glib/gbytes.h>
|
#include <glib/gbytes.h>
|
||||||
#include <glib/gutils.h> /* for G_CAN_INLINE */
|
#include <glib/gutils.h> /* for G_CAN_INLINE */
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
@ -178,6 +179,36 @@ g_string_append_c_inline (GString *gstring,
|
|||||||
return gstring;
|
return gstring;
|
||||||
}
|
}
|
||||||
#define g_string_append_c(gstr,c) g_string_append_c_inline (gstr, c)
|
#define g_string_append_c(gstr,c) g_string_append_c_inline (gstr, c)
|
||||||
|
|
||||||
|
static inline GString *
|
||||||
|
g_string_append_len_inline (GString *gstring,
|
||||||
|
const char *val,
|
||||||
|
gssize len)
|
||||||
|
{
|
||||||
|
if (len < 0)
|
||||||
|
len = strlen (val);
|
||||||
|
|
||||||
|
if (G_LIKELY (gstring->len + len < gstring->allocated_len))
|
||||||
|
{
|
||||||
|
char *end = gstring->str + gstring->len;
|
||||||
|
if (G_LIKELY (val + len <= end || val > end + len))
|
||||||
|
memcpy (end, val, len);
|
||||||
|
else
|
||||||
|
memmove (end, val, len);
|
||||||
|
gstring->len += len;
|
||||||
|
gstring->str[gstring->len] = 0;
|
||||||
|
return gstring;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return g_string_insert_len (gstring, -1, val, len);
|
||||||
|
}
|
||||||
|
#define g_string_append_len(gstr,val,len) g_string_append_len_inline (gstr, val, len)
|
||||||
|
|
||||||
|
#if G_GNUC_CHECK_VERSION (2, 0)
|
||||||
|
|
||||||
|
#define g_string_append(gstr,val) g_string_append_len (gstr, val, __builtin_constant_p (val) ? (gssize) strlen (val) : (gssize) -1)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* G_CAN_INLINE */
|
#endif /* G_CAN_INLINE */
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user