Commit af4133e3 authored by Max Kellermann's avatar Max Kellermann

Util/StringUtil: add StringStartsWith()

Replaces GLib's g_str_has_prefix().
parent a788b7e7
......@@ -27,6 +27,7 @@
#include "tag/Tag.hxx"
#include "tag/TagSettings.h"
#include "fs/Charset.hxx"
#include "util/StringUtil.hxx"
#include "util/Error.hxx"
#include "Log.hxx"
......@@ -81,16 +82,16 @@ db_load_internal(TextFile &file, Directory &music_root, Error &error)
while ((line = file.ReadLine()) != nullptr &&
strcmp(line, DIRECTORY_INFO_END) != 0) {
if (g_str_has_prefix(line, DB_FORMAT_PREFIX)) {
if (StringStartsWith(line, DB_FORMAT_PREFIX)) {
format = atoi(line + sizeof(DB_FORMAT_PREFIX) - 1);
} else if (g_str_has_prefix(line, DIRECTORY_MPD_VERSION)) {
} else if (StringStartsWith(line, DIRECTORY_MPD_VERSION)) {
if (found_version) {
error.Set(db_domain, "Duplicate version line");
return false;
}
found_version = true;
} else if (g_str_has_prefix(line, DIRECTORY_FS_CHARSET)) {
} else if (StringStartsWith(line, DIRECTORY_FS_CHARSET)) {
const char *new_charset;
if (found_charset) {
......@@ -111,7 +112,7 @@ db_load_internal(TextFile &file, Directory &music_root, Error &error)
new_charset, old_charset);
return false;
}
} else if (g_str_has_prefix(line, DB_TAG_PREFIX)) {
} else if (StringStartsWith(line, DB_TAG_PREFIX)) {
const char *name = line + sizeof(DB_TAG_PREFIX) - 1;
TagType tag = tag_name_parse(name);
if (tag == TAG_NUM_OF_ITEM_TYPES) {
......
......@@ -24,6 +24,7 @@
#include "SongSave.hxx"
#include "PlaylistDatabase.hxx"
#include "TextFile.hxx"
#include "util/StringUtil.hxx"
#include "util/NumberParser.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
......@@ -88,7 +89,7 @@ directory_load_subdir(TextFile &file, Directory &parent, const char *name,
return nullptr;
}
if (g_str_has_prefix(line, DIRECTORY_MTIME)) {
if (StringStartsWith(line, DIRECTORY_MTIME)) {
directory->mtime =
ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1);
......@@ -100,7 +101,7 @@ directory_load_subdir(TextFile &file, Directory &parent, const char *name,
}
}
if (!g_str_has_prefix(line, DIRECTORY_BEGIN)) {
if (!StringStartsWith(line, DIRECTORY_BEGIN)) {
error.Format(directory_domain, "Malformed line: %s", line);
directory->Delete();
return nullptr;
......@@ -121,15 +122,15 @@ directory_load(TextFile &file, Directory &directory, Error &error)
const char *line;
while ((line = file.ReadLine()) != nullptr &&
!g_str_has_prefix(line, DIRECTORY_END)) {
if (g_str_has_prefix(line, DIRECTORY_DIR)) {
!StringStartsWith(line, DIRECTORY_END)) {
if (StringStartsWith(line, DIRECTORY_DIR)) {
Directory *subdir =
directory_load_subdir(file, directory,
line + sizeof(DIRECTORY_DIR) - 1,
error);
if (subdir == nullptr)
return false;
} else if (g_str_has_prefix(line, SONG_BEGIN)) {
} else if (StringStartsWith(line, SONG_BEGIN)) {
const char *name = line + sizeof(SONG_BEGIN) - 1;
Song *song;
......@@ -144,7 +145,7 @@ directory_load(TextFile &file, Directory &directory, Error &error)
return false;
directory.AddSong(song);
} else if (g_str_has_prefix(line, PLAYLIST_META_BEGIN)) {
} else if (StringStartsWith(line, PLAYLIST_META_BEGIN)) {
/* duplicate the name, because
playlist_metadata_load() will overwrite the
buffer */
......
......@@ -28,8 +28,7 @@
#include "OutputInternal.hxx"
#include "OutputError.hxx"
#include "Log.hxx"
#include <glib.h>
#include "util/StringUtil.hxx"
#include <assert.h>
#include <stdlib.h>
......@@ -61,7 +60,7 @@ audio_output_state_read(const char *line)
const char *name;
struct audio_output *ao;
if (!g_str_has_prefix(line, AUDIO_DEVICE_STATE))
if (!StringStartsWith(line, AUDIO_DEVICE_STATE))
return false;
line += sizeof(AUDIO_DEVICE_STATE) - 1;
......
......@@ -33,10 +33,9 @@
#include "ConfigOption.hxx"
#include "fs/Limits.hxx"
#include "util/CharUtil.hxx"
#include "util/StringUtil.hxx"
#include "Log.hxx"
#include <glib.h>
#include <string.h>
#include <stdlib.h>
......@@ -112,7 +111,7 @@ playlist_state_load(TextFile &file, struct playlist &playlist)
return;
}
while (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
while (!StringStartsWith(line, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
queue_load_song(file, line, playlist.queue);
line = file.ReadLine();
......@@ -135,7 +134,7 @@ playlist_state_restore(const char *line, TextFile &file,
int seek_time = 0;
bool random_mode = false;
if (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_STATE))
if (!StringStartsWith(line, PLAYLIST_STATE_FILE_STATE))
return false;
line += sizeof(PLAYLIST_STATE_FILE_STATE) - 1;
......@@ -149,40 +148,40 @@ playlist_state_restore(const char *line, TextFile &file,
state = PlayerState::STOP;
while ((line = file.ReadLine()) != nullptr) {
if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_TIME)) {
if (StringStartsWith(line, PLAYLIST_STATE_FILE_TIME)) {
seek_time =
atoi(&(line[strlen(PLAYLIST_STATE_FILE_TIME)]));
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_REPEAT)) {
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_REPEAT)) {
playlist.SetRepeat(pc,
strcmp(&(line[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
"1") == 0);
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_SINGLE)) {
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_SINGLE)) {
playlist.SetSingle(pc,
strcmp(&(line[strlen(PLAYLIST_STATE_FILE_SINGLE)]),
"1") == 0);
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CONSUME)) {
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CONSUME)) {
playlist.SetConsume(strcmp(&(line[strlen(PLAYLIST_STATE_FILE_CONSUME)]),
"1") == 0);
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CROSSFADE)) {
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CROSSFADE)) {
pc.SetCrossFade(atoi(line + strlen(PLAYLIST_STATE_FILE_CROSSFADE)));
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_MIXRAMPDB)) {
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_MIXRAMPDB)) {
pc.SetMixRampDb(atof(line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB)));
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) {
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) {
const char *p = line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDELAY);
/* this check discards "nan" which was used
prior to MPD 0.18 */
if (IsDigitASCII(*p))
pc.SetMixRampDelay(atof(p));
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_RANDOM)) {
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_RANDOM)) {
random_mode =
strcmp(line + strlen(PLAYLIST_STATE_FILE_RANDOM),
"1") == 0;
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CURRENT)) {
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CURRENT)) {
current = atoi(&(line
[strlen
(PLAYLIST_STATE_FILE_CURRENT)]));
} else if (g_str_has_prefix(line,
} else if (StringStartsWith(line,
PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
playlist_state_load(file, playlist);
}
......
......@@ -26,13 +26,12 @@
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
#include "TextFile.hxx"
#include "util/StringUtil.hxx"
#include "util/UriUtil.hxx"
#include "util/Error.hxx"
#include "fs/Traits.hxx"
#include "Log.hxx"
#include <glib.h>
#include <stdlib.h>
#define PRIO_LABEL "Prio: "
......@@ -78,7 +77,7 @@ queue_load_song(TextFile &file, const char *line, queue &queue)
return;
uint8_t priority = 0;
if (g_str_has_prefix(line, PRIO_LABEL)) {
if (StringStartsWith(line, PRIO_LABEL)) {
priority = strtoul(line + sizeof(PRIO_LABEL) - 1, nullptr, 10);
line = file.ReadLine();
......@@ -89,7 +88,7 @@ queue_load_song(TextFile &file, const char *line, queue &queue)
const Database *db = nullptr;
Song *song;
if (g_str_has_prefix(line, SONG_BEGIN)) {
if (StringStartsWith(line, SONG_BEGIN)) {
const char *uri = line + sizeof(SONG_BEGIN) - 1;
if (!uri_has_scheme(uri) && !PathTraits::IsAbsoluteUTF8(uri))
return;
......
......@@ -22,12 +22,11 @@
#include "MixerAll.hxx"
#include "Idle.hxx"
#include "GlobalEvents.hxx"
#include "util/StringUtil.hxx"
#include "util/Domain.hxx"
#include "system/PeriodClock.hxx"
#include "Log.hxx"
#include <glib.h>
#include <assert.h>
#include <stdlib.h>
......@@ -106,7 +105,7 @@ read_sw_volume_state(const char *line)
char *end = nullptr;
long int sv;
if (!g_str_has_prefix(line, SW_VOLUME_STATE))
if (!StringStartsWith(line, SW_VOLUME_STATE))
return false;
line += sizeof(SW_VOLUME_STATE) - 1;
......
......@@ -26,6 +26,7 @@
#include "tag/TagRva2.hxx"
#include "tag/TagHandler.hxx"
#include "CheckAudioFormat.hxx"
#include "util/StringUtil.hxx"
#include "util/ASCII.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
......@@ -689,7 +690,7 @@ parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen)
/* This is technically incorrect, since the encoder might not be lame.
* But there's no other way to determine if this is a lame tag, and we
* wouldn't want to go reading a tag that's not there. */
if (!g_str_has_prefix(lame->encoder, "LAME"))
if (!StringStartsWith(lame->encoder, "LAME"))
return false;
if (sscanf(lame->encoder+4, "%u.%u",
......
......@@ -25,6 +25,7 @@
#include "CdioParanoiaInputPlugin.hxx"
#include "InputStream.hxx"
#include "InputPlugin.hxx"
#include "util/StringUtil.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "system/ByteOrder.hxx"
......@@ -117,7 +118,7 @@ struct cdio_uri {
static bool
parse_cdio_uri(struct cdio_uri *dest, const char *src, Error &error)
{
if (!g_str_has_prefix(src, "cdda://"))
if (!StringStartsWith(src, "cdda://"))
return false;
src += 7;
......
......@@ -23,14 +23,13 @@
#include "InputStream.hxx"
#include "InputPlugin.hxx"
#include "tag/Tag.hxx"
#include "util/StringUtil.hxx"
#include "Log.hxx"
extern "C" {
#include <despotify.h>
}
#include <glib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
......@@ -130,7 +129,7 @@ input_despotify_open(const char *url,
struct ds_link *ds_link;
struct ds_track *track;
if (!g_str_has_prefix(url, "spt://"))
if (!StringStartsWith(url, "spt://"))
return nullptr;
session = mpd_despotify_get_session();
......
......@@ -24,6 +24,7 @@
#include "FfmpegInputPlugin.hxx"
#include "InputStream.hxx"
#include "InputPlugin.hxx"
#include "util/StringUtil.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
......@@ -32,8 +33,6 @@ extern "C" {
#include <libavformat/avformat.h>
}
#include <glib.h>
struct FfmpegInputStream {
InputStream base;
......@@ -90,12 +89,12 @@ input_ffmpeg_open(const char *uri,
Mutex &mutex, Cond &cond,
Error &error)
{
if (!g_str_has_prefix(uri, "gopher://") &&
!g_str_has_prefix(uri, "rtp://") &&
!g_str_has_prefix(uri, "rtsp://") &&
!g_str_has_prefix(uri, "rtmp://") &&
!g_str_has_prefix(uri, "rtmpt://") &&
!g_str_has_prefix(uri, "rtmps://"))
if (!StringStartsWith(uri, "gopher://") &&
!StringStartsWith(uri, "rtp://") &&
!StringStartsWith(uri, "rtsp://") &&
!StringStartsWith(uri, "rtmp://") &&
!StringStartsWith(uri, "rtmpt://") &&
!StringStartsWith(uri, "rtmps://"))
return nullptr;
AVIOContext *h;
......
......@@ -21,10 +21,10 @@
#include "MmsInputPlugin.hxx"
#include "InputStream.hxx"
#include "InputPlugin.hxx"
#include "util/StringUtil.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include <glib.h>
#include <libmms/mmsx.h>
struct MmsInputStream {
......@@ -58,10 +58,10 @@ input_mms_open(const char *url,
Mutex &mutex, Cond &cond,
Error &error)
{
if (!g_str_has_prefix(url, "mms://") &&
!g_str_has_prefix(url, "mmsh://") &&
!g_str_has_prefix(url, "mmst://") &&
!g_str_has_prefix(url, "mmsu://"))
if (!StringStartsWith(url, "mms://") &&
!StringStartsWith(url, "mmsh://") &&
!StringStartsWith(url, "mmst://") &&
!StringStartsWith(url, "mmsu://"))
return nullptr;
const auto mms = mmsx_connect(nullptr, nullptr, url, 128 * 1024);
......
......@@ -19,11 +19,10 @@
#include "config.h"
#include "ls.hxx"
#include "util/StringUtil.hxx"
#include "util/UriUtil.hxx"
#include "Client.hxx"
#include <glib.h>
#include <assert.h>
/**
......@@ -90,7 +89,7 @@ bool uri_supported_scheme(const char *uri)
assert(uri_has_scheme(uri));
while (*urlPrefixes) {
if (g_str_has_prefix(uri, *urlPrefixes))
if (StringStartsWith(uri, *urlPrefixes))
return true;
urlPrefixes++;
}
......
......@@ -26,8 +26,6 @@
#include "util/StringUtil.hxx"
#include "TextInputStream.hxx"
#include <glib.h>
#include <string.h>
#include <stdlib.h>
......@@ -119,7 +117,7 @@ ExtM3uPlaylist::NextSong()
line_s = line.c_str();
if (g_str_has_prefix(line_s, "#EXTINF:")) {
if (StringStartsWith(line_s, "#EXTINF:")) {
delete tag;
tag = extm3u_parse_tag(line_s + 8);
continue;
......
......@@ -25,6 +25,7 @@
#include "InputStream.hxx"
#include "Song.hxx"
#include "tag/Tag.hxx"
#include "util/StringUtil.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
......@@ -65,9 +66,9 @@ static char *
soundcloud_resolve(const char* uri) {
char *u, *ru;
if (g_str_has_prefix(uri, "http://")) {
if (StringStartsWith(uri, "http://")) {
u = g_strdup(uri);
} else if (g_str_has_prefix(uri, "soundcloud.com")) {
} else if (StringStartsWith(uri, "soundcloud.com")) {
u = g_strconcat("http://", uri, nullptr);
} else {
/* assume it's just a path on soundcloud.com */
......
......@@ -22,6 +22,7 @@
#include "ASCII.hxx"
#include <assert.h>
#include <string.h>
const char *
strchug_fast(const char *p)
......@@ -33,6 +34,13 @@ strchug_fast(const char *p)
}
bool
StringStartsWith(const char *haystack, const char *needle)
{
const size_t length = strlen(needle);
return memcmp(haystack, needle, length) == 0;
}
bool
string_array_contains(const char *const* haystack, const char *needle)
{
assert(haystack != nullptr);
......
......@@ -40,6 +40,10 @@ strchug_fast(char *p)
return const_cast<char *>(strchug_fast((const char *)p));
}
gcc_pure
bool
StringStartsWith(const char *haystack, const char *needle);
/**
* Checks whether a string array contains the specified string.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment