diff options
author | Max Kellermann <max@duempel.org> | 2010-12-23 16:16:01 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2010-12-23 16:23:20 +0100 |
commit | 5462f34ed037111d2d57638352b2f1a65a322579 (patch) | |
tree | d1acf3493d2d8edbbda9ae77a327d26ae249f3f9 /src | |
parent | 0958ed584431ac09e676dd51925e56142416e7c2 (diff) | |
download | mpd-5462f34ed037111d2d57638352b2f1a65a322579.tar.gz mpd-5462f34ed037111d2d57638352b2f1a65a322579.tar.xz mpd-5462f34ed037111d2d57638352b2f1a65a322579.zip |
string_util: add function strchug_fast()
Replace g_strchug() calls with a cheaper implementation.
Diffstat (limited to '')
-rw-r--r-- | src/conf.c | 8 | ||||
-rw-r--r-- | src/playlist/extm3u_playlist_plugin.c | 3 | ||||
-rw-r--r-- | src/playlist_database.c | 3 | ||||
-rw-r--r-- | src/song_save.c | 3 | ||||
-rw-r--r-- | src/string_util.c | 9 | ||||
-rw-r--r-- | src/string_util.h | 41 | ||||
-rw-r--r-- | src/tokenizer.c | 7 |
7 files changed, 64 insertions, 10 deletions
diff --git a/src/conf.c b/src/conf.c index 3310da17e..c759a1940 100644 --- a/src/conf.c +++ b/src/conf.c @@ -270,7 +270,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r) } (*count)++; - line = g_strchug(line); + line = strchug_fast(line); if (*line == 0 || *line == CONF_COMMENT) 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 (and from this "while" loop) */ - line = g_strchug(line + 1); + line = strchug_fast(line + 1); if (*line != 0 && *line != CONF_COMMENT) { config_param_free(ret); g_set_error(error_r, config_quark(), 0, @@ -356,7 +356,7 @@ config_read_file(const char *file, GError **error_r) count++; - line = g_strchug(string); + line = strchug_fast(string); if (*line == 0 || *line == CONF_COMMENT) continue; @@ -402,7 +402,7 @@ config_read_file(const char *file, GError **error_r) return false; } - line = g_strchug(line + 1); + line = strchug_fast(line + 1); if (*line != 0 && *line != CONF_COMMENT) { g_set_error(error_r, config_quark(), 0, "line %i: Unknown tokens after '{'", diff --git a/src/playlist/extm3u_playlist_plugin.c b/src/playlist/extm3u_playlist_plugin.c index 9a04aa066..91f6eed08 100644 --- a/src/playlist/extm3u_playlist_plugin.c +++ b/src/playlist/extm3u_playlist_plugin.c @@ -24,6 +24,7 @@ #include "uri.h" #include "song.h" #include "tag.h" +#include "string_util.h" #include <glib.h> @@ -89,7 +90,7 @@ extm3u_parse_tag(const char *line) /* 0 means unknown duration */ duration = 0; - name = g_strchug(endptr + 1); + name = strchug_fast_c(endptr + 1); if (*name == 0 && duration == 0) /* no information available; don't allocate a tag object */ diff --git a/src/playlist_database.c b/src/playlist_database.c index 0a8a6f139..97da1c98e 100644 --- a/src/playlist_database.c +++ b/src/playlist_database.c @@ -21,6 +21,7 @@ #include "playlist_database.h" #include "playlist_vector.h" #include "text_file.h" +#include "string_util.h" #include <string.h> #include <stdlib.h> @@ -62,7 +63,7 @@ playlist_metadata_load(FILE *fp, struct playlist_vector *pv, const char *name, } *colon++ = 0; - value = g_strchug(colon); + value = strchug_fast_c(colon); if (strcmp(line, "mtime") == 0) pm.mtime = strtol(value, NULL, 10); diff --git a/src/song_save.c b/src/song_save.c index a1a573298..d33e99c27 100644 --- a/src/song_save.c +++ b/src/song_save.c @@ -24,6 +24,7 @@ #include "directory.h" #include "tag.h" #include "text_file.h" +#include "string_util.h" #include <glib.h> @@ -96,7 +97,7 @@ song_load(FILE *fp, struct directory *parent, const char *uri, } *colon++ = 0; - value = g_strchug(colon); + value = strchug_fast_c(colon); if ((type = tag_name_parse(line)) != TAG_NUM_OF_ITEM_TYPES) { if (!song->tag) { diff --git a/src/string_util.c b/src/string_util.c index b93292854..3ae759917 100644 --- a/src/string_util.c +++ b/src/string_util.c @@ -24,6 +24,15 @@ #include <assert.h> +const char * +strchug_fast_c(const char *p) +{ + while (*p != 0 && g_ascii_isspace(*p)) + ++p; + + return p; +} + bool string_array_contains(const char *const* haystack, const char *needle) { diff --git a/src/string_util.h b/src/string_util.h index df8ceeef3..372354722 100644 --- a/src/string_util.h +++ b/src/string_util.h @@ -20,9 +20,50 @@ #ifndef MPD_STRING_UTIL_H #define MPD_STRING_UTIL_H +#include <glib.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. * * @param haystack a NULL terminated list of strings diff --git a/src/tokenizer.c b/src/tokenizer.c index 2b9e05070..358d804dc 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -19,6 +19,7 @@ #include "config.h" #include "tokenizer.h" +#include "string_util.h" #include <stdbool.h> #include <assert.h> @@ -72,7 +73,7 @@ tokenizer_next_word(char **input_p, GError **error_r) /* a whitespace: the word ends here */ *input = 0; /* skip all following spaces, too */ - input = g_strchug(input + 1); + input = strchug_fast(input + 1); break; } @@ -126,7 +127,7 @@ tokenizer_next_unquoted(char **input_p, GError **error_r) /* a whitespace: the word ends here */ *input = 0; /* skip all following spaces, too */ - input = g_strchug(input + 1); + input = strchug_fast(input + 1); break; } @@ -205,7 +206,7 @@ tokenizer_next_string(char **input_p, GError **error_r) /* finish the string and return it */ *dest = 0; - *input_p = g_strchug(input); + *input_p = strchug_fast(input); return word; } |