string_util: add function strchug_fast()

Replace g_strchug() calls with a cheaper implementation.
This commit is contained in:
Max Kellermann 2010-12-23 16:16:01 +01:00
parent 0958ed5844
commit 5462f34ed0
7 changed files with 64 additions and 10 deletions

View File

@ -270,7 +270,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
} }
(*count)++; (*count)++;
line = g_strchug(line); line = strchug_fast(line);
if (*line == 0 || *line == CONF_COMMENT) if (*line == 0 || *line == CONF_COMMENT)
continue; continue;
@ -278,7 +278,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
/* end of this block; return from the function /* end of this block; return from the function
(and from this "while" loop) */ (and from this "while" loop) */
line = g_strchug(line + 1); line = strchug_fast(line + 1);
if (*line != 0 && *line != CONF_COMMENT) { if (*line != 0 && *line != CONF_COMMENT) {
config_param_free(ret); config_param_free(ret);
g_set_error(error_r, config_quark(), 0, g_set_error(error_r, config_quark(), 0,
@ -356,7 +356,7 @@ config_read_file(const char *file, GError **error_r)
count++; count++;
line = g_strchug(string); line = strchug_fast(string);
if (*line == 0 || *line == CONF_COMMENT) if (*line == 0 || *line == CONF_COMMENT)
continue; continue;
@ -402,7 +402,7 @@ config_read_file(const char *file, GError **error_r)
return false; return false;
} }
line = g_strchug(line + 1); line = strchug_fast(line + 1);
if (*line != 0 && *line != CONF_COMMENT) { if (*line != 0 && *line != CONF_COMMENT) {
g_set_error(error_r, config_quark(), 0, g_set_error(error_r, config_quark(), 0,
"line %i: Unknown tokens after '{'", "line %i: Unknown tokens after '{'",

View File

@ -24,6 +24,7 @@
#include "uri.h" #include "uri.h"
#include "song.h" #include "song.h"
#include "tag.h" #include "tag.h"
#include "string_util.h"
#include <glib.h> #include <glib.h>
@ -89,7 +90,7 @@ extm3u_parse_tag(const char *line)
/* 0 means unknown duration */ /* 0 means unknown duration */
duration = 0; duration = 0;
name = g_strchug(endptr + 1); name = strchug_fast_c(endptr + 1);
if (*name == 0 && duration == 0) if (*name == 0 && duration == 0)
/* no information available; don't allocate a tag /* no information available; don't allocate a tag
object */ object */

View File

@ -21,6 +21,7 @@
#include "playlist_database.h" #include "playlist_database.h"
#include "playlist_vector.h" #include "playlist_vector.h"
#include "text_file.h" #include "text_file.h"
#include "string_util.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -62,7 +63,7 @@ playlist_metadata_load(FILE *fp, struct playlist_vector *pv, const char *name,
} }
*colon++ = 0; *colon++ = 0;
value = g_strchug(colon); value = strchug_fast_c(colon);
if (strcmp(line, "mtime") == 0) if (strcmp(line, "mtime") == 0)
pm.mtime = strtol(value, NULL, 10); pm.mtime = strtol(value, NULL, 10);

View File

@ -24,6 +24,7 @@
#include "directory.h" #include "directory.h"
#include "tag.h" #include "tag.h"
#include "text_file.h" #include "text_file.h"
#include "string_util.h"
#include <glib.h> #include <glib.h>
@ -96,7 +97,7 @@ song_load(FILE *fp, struct directory *parent, const char *uri,
} }
*colon++ = 0; *colon++ = 0;
value = g_strchug(colon); value = strchug_fast_c(colon);
if ((type = tag_name_parse(line)) != TAG_NUM_OF_ITEM_TYPES) { if ((type = tag_name_parse(line)) != TAG_NUM_OF_ITEM_TYPES) {
if (!song->tag) { if (!song->tag) {

View File

@ -24,6 +24,15 @@
#include <assert.h> #include <assert.h>
const char *
strchug_fast_c(const char *p)
{
while (*p != 0 && g_ascii_isspace(*p))
++p;
return p;
}
bool bool
string_array_contains(const char *const* haystack, const char *needle) string_array_contains(const char *const* haystack, const char *needle)
{ {

View File

@ -20,8 +20,49 @@
#ifndef MPD_STRING_UTIL_H #ifndef MPD_STRING_UTIL_H
#define MPD_STRING_UTIL_H #define MPD_STRING_UTIL_H
#include <glib.h>
#include <stdbool.h> #include <stdbool.h>
/**
* Remove the "const" attribute from a string pointer. This is a
* dirty hack, don't use it unless you know what you're doing!
*/
G_GNUC_CONST
static inline char *
deconst_string(const char *p)
{
union {
const char *in;
char *out;
} u = {
.in = p,
};
return u.out;
}
/**
* Returns a pointer to the first non-whitespace character in the
* string, or to the end of the string.
*
* This is a faster version of g_strchug(), because it does not move
* data.
*/
G_GNUC_PURE
const char *
strchug_fast_c(const char *p);
/**
* Same as strchug_fast_c(), but works with a writable pointer.
*/
G_GNUC_PURE
static inline char *
strchug_fast(char *p)
{
return deconst_string(strchug_fast_c(p));
}
/** /**
* Checks whether a string array contains the specified string. * Checks whether a string array contains the specified string.
* *

View File

@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "tokenizer.h" #include "tokenizer.h"
#include "string_util.h"
#include <stdbool.h> #include <stdbool.h>
#include <assert.h> #include <assert.h>
@ -72,7 +73,7 @@ tokenizer_next_word(char **input_p, GError **error_r)
/* a whitespace: the word ends here */ /* a whitespace: the word ends here */
*input = 0; *input = 0;
/* skip all following spaces, too */ /* skip all following spaces, too */
input = g_strchug(input + 1); input = strchug_fast(input + 1);
break; break;
} }
@ -126,7 +127,7 @@ tokenizer_next_unquoted(char **input_p, GError **error_r)
/* a whitespace: the word ends here */ /* a whitespace: the word ends here */
*input = 0; *input = 0;
/* skip all following spaces, too */ /* skip all following spaces, too */
input = g_strchug(input + 1); input = strchug_fast(input + 1);
break; break;
} }
@ -205,7 +206,7 @@ tokenizer_next_string(char **input_p, GError **error_r)
/* finish the string and return it */ /* finish the string and return it */
*dest = 0; *dest = 0;
*input_p = g_strchug(input); *input_p = strchug_fast(input);
return word; return word;
} }