Commit 6b39a562 authored by Max Kellermann's avatar Max Kellermann

locate: add "casefold" flag to parser

Fold the case during construction, without having to create another copy.
parent 0d46e118
...@@ -60,7 +60,7 @@ enum command_return ...@@ -60,7 +60,7 @@ enum command_return
handle_find(struct client *client, int argc, char *argv[]) handle_find(struct client *client, int argc, char *argv[])
{ {
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1, false);
if (list == NULL || list->length == 0) { if (list == NULL || list->length == 0) {
if (list != NULL) if (list != NULL)
...@@ -84,7 +84,7 @@ enum command_return ...@@ -84,7 +84,7 @@ enum command_return
handle_findadd(struct client *client, int argc, char *argv[]) handle_findadd(struct client *client, int argc, char *argv[])
{ {
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1, false);
if (list == NULL || list->length == 0) { if (list == NULL || list->length == 0) {
if (list != NULL) if (list != NULL)
locate_item_list_free(list); locate_item_list_free(list);
...@@ -108,7 +108,7 @@ enum command_return ...@@ -108,7 +108,7 @@ enum command_return
handle_search(struct client *client, int argc, char *argv[]) handle_search(struct client *client, int argc, char *argv[])
{ {
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1, true);
if (list == NULL || list->length == 0) { if (list == NULL || list->length == 0) {
if (list != NULL) if (list != NULL)
...@@ -132,7 +132,7 @@ enum command_return ...@@ -132,7 +132,7 @@ enum command_return
handle_searchadd(struct client *client, int argc, char *argv[]) handle_searchadd(struct client *client, int argc, char *argv[])
{ {
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1, true);
if (list == NULL || list->length == 0) { if (list == NULL || list->length == 0) {
if (list != NULL) if (list != NULL)
...@@ -159,7 +159,7 @@ handle_searchaddpl(struct client *client, int argc, char *argv[]) ...@@ -159,7 +159,7 @@ handle_searchaddpl(struct client *client, int argc, char *argv[])
const char *playlist = argv[1]; const char *playlist = argv[1];
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 2, argc - 2); locate_item_list_parse(argv + 2, argc - 2, true);
if (list == NULL || list->length == 0) { if (list == NULL || list->length == 0) {
if (list != NULL) if (list != NULL)
...@@ -184,7 +184,7 @@ enum command_return ...@@ -184,7 +184,7 @@ enum command_return
handle_count(struct client *client, int argc, char *argv[]) handle_count(struct client *client, int argc, char *argv[])
{ {
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1, false);
if (list == NULL || list->length == 0) { if (list == NULL || list->length == 0) {
if (list != NULL) if (list != NULL)
...@@ -245,14 +245,14 @@ handle_list(struct client *client, int argc, char *argv[]) ...@@ -245,14 +245,14 @@ handle_list(struct client *client, int argc, char *argv[])
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
} }
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1, false);
conditionals = locate_item_list_new(1); conditionals = locate_item_list_new(1);
conditionals->items[0].tag = TAG_ARTIST; conditionals->items[0].tag = TAG_ARTIST;
conditionals->items[0].needle = g_strdup(argv[2]); conditionals->items[0].needle = g_strdup(argv[2]);
} else { } else {
conditionals = conditionals =
locate_item_list_parse(argv + 2, argc - 2); locate_item_list_parse(argv + 2, argc - 2, false);
if (conditionals == NULL) { if (conditionals == NULL) {
command_error(client, ACK_ERROR_ARG, command_error(client, ACK_ERROR_ARG,
"not able to parse args"); "not able to parse args");
......
...@@ -66,15 +66,8 @@ search_add_to_playlist(const char *uri, const char *playlist_path_utf8, ...@@ -66,15 +66,8 @@ search_add_to_playlist(const char *uri, const char *playlist_path_utf8,
{ {
const DatabaseSelection selection(uri, true); const DatabaseSelection selection(uri, true);
struct locate_item_list *new_list
= locate_item_list_casefold(criteria);
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(SearchAddSong, playlist_path_utf8, const auto f = std::bind(SearchAddSong, playlist_path_utf8,
new_list, _1, _2); criteria, _1, _2);
bool success = GetDatabase()->Visit(selection, f, error_r); return GetDatabase()->Visit(selection, f, error_r);
locate_item_list_free(new_list);
return success;
} }
...@@ -155,16 +155,9 @@ searchForSongsIn(struct client *client, const char *uri, ...@@ -155,16 +155,9 @@ searchForSongsIn(struct client *client, const char *uri,
{ {
const DatabaseSelection selection(uri, true); const DatabaseSelection selection(uri, true);
struct locate_item_list *new_list
= locate_item_list_casefold(criteria);
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(SearchPrintSong, client, new_list, _1); const auto f = std::bind(SearchPrintSong, client, criteria, _1);
bool success = GetDatabase()->Visit(selection, f, error_r); return GetDatabase()->Visit(selection, f, error_r);
locate_item_list_free(new_list);
return success;
} }
static bool static bool
......
...@@ -92,14 +92,7 @@ search_add_songs(struct player_control *pc, const char *uri, ...@@ -92,14 +92,7 @@ search_add_songs(struct player_control *pc, const char *uri,
{ {
const DatabaseSelection selection(uri, true); const DatabaseSelection selection(uri, true);
struct locate_item_list *new_list =
locate_item_list_casefold(criteria);
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(SearchAddSong, pc, new_list, _1, _2); const auto f = std::bind(SearchAddSong, pc, criteria, _1, _2);
bool success = GetDatabase()->Visit(selection, f, error_r); return GetDatabase()->Visit(selection, f, error_r);
locate_item_list_free(new_list);
return success;
} }
...@@ -677,7 +677,7 @@ static enum command_return ...@@ -677,7 +677,7 @@ static enum command_return
handle_playlistfind(struct client *client, int argc, char *argv[]) handle_playlistfind(struct client *client, int argc, char *argv[])
{ {
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1, false);
if (list == NULL || list->length == 0) { if (list == NULL || list->length == 0) {
if (list != NULL) if (list != NULL)
...@@ -698,7 +698,7 @@ static enum command_return ...@@ -698,7 +698,7 @@ static enum command_return
handle_playlistsearch(struct client *client, int argc, char *argv[]) handle_playlistsearch(struct client *client, int argc, char *argv[])
{ {
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1, true);
if (list == NULL || list->length == 0) { if (list == NULL || list->length == 0) {
if (list != NULL) if (list != NULL)
......
...@@ -50,14 +50,17 @@ locate_parse_type(const char *str) ...@@ -50,14 +50,17 @@ locate_parse_type(const char *str)
static bool static bool
locate_item_init(struct locate_item *item, locate_item_init(struct locate_item *item,
const char *type_string, const char *needle) const char *type_string, const char *needle,
bool fold_case)
{ {
item->tag = locate_parse_type(type_string); item->tag = locate_parse_type(type_string);
if (item->tag < 0) if (item->tag < 0)
return false; return false;
item->needle = g_strdup(needle); item->needle = fold_case
? g_utf8_casefold(needle, -1)
: g_strdup(needle);
return true; return true;
} }
...@@ -83,7 +86,7 @@ locate_item_list_new(unsigned length) ...@@ -83,7 +86,7 @@ locate_item_list_new(unsigned length)
} }
struct locate_item_list * struct locate_item_list *
locate_item_list_parse(char *argv[], int argc) locate_item_list_parse(char *argv[], int argc, bool fold_case)
{ {
if (argc % 2 != 0) if (argc % 2 != 0)
return NULL; return NULL;
...@@ -92,7 +95,7 @@ locate_item_list_parse(char *argv[], int argc) ...@@ -92,7 +95,7 @@ locate_item_list_parse(char *argv[], int argc)
for (unsigned i = 0; i < list->length; ++i) { for (unsigned i = 0; i < list->length; ++i) {
if (!locate_item_init(&list->items[i], argv[i * 2], if (!locate_item_init(&list->items[i], argv[i * 2],
argv[i * 2 + 1])) { argv[i * 2 + 1], fold_case)) {
locate_item_list_free(list); locate_item_list_free(list);
return NULL; return NULL;
} }
...@@ -101,20 +104,6 @@ locate_item_list_parse(char *argv[], int argc) ...@@ -101,20 +104,6 @@ locate_item_list_parse(char *argv[], int argc)
return list; return list;
} }
struct locate_item_list *
locate_item_list_casefold(const struct locate_item_list *list)
{
struct locate_item_list *new_list = locate_item_list_new(list->length);
for (unsigned i = 0; i < list->length; i++){
new_list->items[i].needle =
g_utf8_casefold(list->items[i].needle, -1);
new_list->items[i].tag = list->items[i].tag;
}
return new_list;
}
static bool static bool
locate_tag_search(const struct song *song, enum tag_type type, const char *str) locate_tag_search(const struct song *song, enum tag_type type, const char *str)
{ {
......
...@@ -61,15 +61,7 @@ locate_item_list_new(unsigned length); ...@@ -61,15 +61,7 @@ locate_item_list_new(unsigned length);
/* return number of items or -1 on error */ /* return number of items or -1 on error */
gcc_nonnull(1) gcc_nonnull(1)
struct locate_item_list * struct locate_item_list *
locate_item_list_parse(char *argv[], int argc); locate_item_list_parse(char *argv[], int argc, bool fold_case);
/**
* Duplicate the struct locate_item_list object and convert all
* needles with g_utf8_casefold().
*/
gcc_nonnull(1)
struct locate_item_list *
locate_item_list_casefold(const struct locate_item_list *list);
gcc_nonnull(1) gcc_nonnull(1)
void void
......
...@@ -95,18 +95,12 @@ void ...@@ -95,18 +95,12 @@ void
queue_search(struct client *client, const struct queue *queue, queue_search(struct client *client, const struct queue *queue,
const struct locate_item_list *criteria) const struct locate_item_list *criteria)
{ {
unsigned i; for (unsigned i = 0; i < queue_length(queue); i++) {
struct locate_item_list *new_list =
locate_item_list_casefold(criteria);
for (i = 0; i < queue_length(queue); i++) {
const struct song *song = queue_get(queue, i); const struct song *song = queue_get(queue, i);
if (locate_song_search(song, new_list)) if (locate_song_search(song, criteria))
queue_print_song_info(client, queue, i); queue_print_song_info(client, queue, i);
} }
locate_item_list_free(new_list);
} }
void void
......
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