[vlc-commits] input: use bsearch in input_item_slave_GetType
Thomas Guillem
git at videolan.org
Mon Dec 4 18:17:23 CET 2017
vlc | branch: master | Thomas Guillem <thomas at gllm.fr> | Mon Dec 4 17:49:18 2017 +0100| [c7a229ab34e705446f87c28c0afe8387676efca6] | committer: Thomas Guillem
input: use bsearch in input_item_slave_GetType
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=c7a229ab34e705446f87c28c0afe8387676efca6
---
include/vlc_input_item.h | 26 ++++++++++++++------------
src/input/item.c | 26 +++++++++++++++-----------
2 files changed, 29 insertions(+), 23 deletions(-)
diff --git a/include/vlc_input_item.h b/include/vlc_input_item.h
index 7d7928ede9..b9ee89eeac 100644
--- a/include/vlc_input_item.h
+++ b/include/vlc_input_item.h
@@ -141,20 +141,22 @@ enum slave_priority
SLAVE_PRIORITY_USER
};
+/* Extensions must be in alphabetical order */
#define SLAVE_SPU_EXTENSIONS \
- "idx", "sub", "srt", \
- "ssa", "ass", "smi", \
- "utf", "utf8", "utf-8", \
- "rt", "aqt", "txt", \
- "usf", "jss", "cdg", \
- "psb", "mpsub","mpl2", \
- "pjs", "dks", "stl", \
- "vtt", "sbv", "ttml",\
- "scc", "webvtt"
+ "aqt", "ass", "cdg", \
+ "dks", "idx", "jss", \
+ "mpl2", "mpsub", "pjs", \
+ "psb", "rt", "sbv", \
+ "scc", "smi", "srt", \
+ "ssa", "stl", "sub", \
+ "ttml", "txt", "usf", \
+ "utf", "utf-8", "utf8", \
+ "vtt", "webvtt"
+
#define SLAVE_AUDIO_EXTENSIONS \
- "ac3", "m4a", "aac", \
- "eac3", "dtshd", "flac", \
- "pcm", "dts", "mp3"
+ "aac", "ac3", "dts", \
+ "dtshd", "eac3", "flac", \
+ "m4a", "mp3", "pcm" \
struct input_item_slave
{
diff --git a/src/input/item.c b/src/input/item.c
index 95198d4e60..4ed02ab2da 100644
--- a/src/input/item.c
+++ b/src/input/item.c
@@ -617,18 +617,25 @@ void input_item_ApplyOptions(vlc_object_t *obj, input_item_t *item)
vlc_mutex_unlock(&item->lock);
}
+static int bsearch_strcmp_cb(const void *a, const void *b)
+{
+ const char *const *entry = b;
+ return strcasecmp(a, *entry);
+}
+
bool input_item_slave_GetType(const char *psz_filename,
enum slave_type *p_slave_type)
{
- static const char *const ppsz_sub_exts[] = { SLAVE_SPU_EXTENSIONS, NULL };
- static const char *const ppsz_audio_exts[] = { SLAVE_AUDIO_EXTENSIONS, NULL };
+ static const char *const ppsz_sub_exts[] = { SLAVE_SPU_EXTENSIONS };
+ static const char *const ppsz_audio_exts[] = { SLAVE_AUDIO_EXTENSIONS };
static struct {
enum slave_type i_type;
const char *const *ppsz_exts;
+ size_t nmemb;
} p_slave_list[] = {
- { SLAVE_TYPE_SPU, ppsz_sub_exts },
- { SLAVE_TYPE_AUDIO, ppsz_audio_exts },
+ { SLAVE_TYPE_SPU, ppsz_sub_exts, ARRAY_SIZE(ppsz_sub_exts) },
+ { SLAVE_TYPE_AUDIO, ppsz_audio_exts, ARRAY_SIZE(ppsz_audio_exts) },
};
const char *psz_ext = strrchr(psz_filename, '.');
@@ -637,14 +644,11 @@ bool input_item_slave_GetType(const char *psz_filename,
for (unsigned int i = 0; i < sizeof(p_slave_list) / sizeof(*p_slave_list); ++i)
{
- for (const char *const *ppsz_slave_ext = p_slave_list[i].ppsz_exts;
- *ppsz_slave_ext != NULL; ppsz_slave_ext++)
+ if (bsearch(psz_ext, p_slave_list[i].ppsz_exts, p_slave_list[i].nmemb,
+ sizeof(const char *), bsearch_strcmp_cb))
{
- if (!strcasecmp(psz_ext, *ppsz_slave_ext))
- {
- *p_slave_type = p_slave_list[i].i_type;
- return true;
- }
+ *p_slave_type = p_slave_list[i].i_type;
+ return true;
}
}
return false;
More information about the vlc-commits
mailing list