Merge pull request #1739 from leiradel/master

swap series of strcmp with hash-based switch
This commit is contained in:
Twinaphex 2015-06-03 07:44:25 +02:00
commit 2a6ffa2315

View File

@ -29,6 +29,21 @@
#include "config.h"
#endif
#include <stdint.h>
static inline uint32_t djb2(const char *str)
{
const unsigned char *aux = (const unsigned char*)str;
uint32_t hash = 5381;
while ( *aux )
{
hash = ( hash << 5 ) + hash + *aux++;
}
return hash;
}
int database_info_build_query(
char *query, size_t len, const char *label, const char *path)
{
@ -36,57 +51,65 @@ int database_info_build_query(
strlcpy(query, "{'", len);
if (!strcmp(label, "displaylist_parse_database_entry"))
strlcat(query, "name", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_publisher"))
strlcat(query, "publisher", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_developer"))
strlcat(query, "developer", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_origin"))
strlcat(query, "origin", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_franchise"))
strlcat(query, "franchise", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_esrb_rating"))
strlcat(query, "esrb_rating", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_bbfc_rating"))
strlcat(query, "bbfc_rating", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_elspa_rating"))
strlcat(query, "elspa_rating", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_pegi_rating"))
strlcat(query, "pegi_rating", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_enhancement_hw"))
strlcat(query, "enhancement_hw", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_cero_rating"))
strlcat(query, "cero_rating", len);
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_edge_magazine_rating"))
switch (djb2(label))
{
case 0x1c310956U: /* displaylist_parse_database_entry */
strlcat(query, "name", len);
break;
case 0x125e594dU: /* deferred_cursor_manager_list_rdb_entry_publisher */
strlcat(query, "publisher", len);
break;
case 0xcbd89be5U: /* deferred_cursor_manager_list_rdb_entry_developer */
strlcat(query, "developer", len);
break;
case 0x4ebaa767U: /* deferred_cursor_manager_list_rdb_entry_origin */
strlcat(query, "origin", len);
break;
case 0x77f9eff2U: /* deferred_cursor_manager_list_rdb_entry_franchise */
strlcat(query, "franchise", len);
break;
case 0x68eba20fU: /* deferred_cursor_manager_list_rdb_entry_esrb_rating */
strlcat(query, "esrb_rating", len);
break;
case 0x0a8e67f0U: /* deferred_cursor_manager_list_rdb_entry_bbfc_rating */
strlcat(query, "bbfc_rating", len);
break;
case 0x8bf6ab18U: /* deferred_cursor_manager_list_rdb_entry_elspa_rating */
strlcat(query, "elspa_rating", len);
break;
case 0x5fc77328U: /* deferred_cursor_manager_list_rdb_entry_pegi_rating */
strlcat(query, "pegi_rating", len);
break;
case 0x9866bda3U: /* deferred_cursor_manager_list_rdb_entry_enhancement_hw */
strlcat(query, "enhancement_hw", len);
break;
case 0x24f6172cU: /* deferred_cursor_manager_list_rdb_entry_cero_rating */
strlcat(query, "cero_rating", len);
break;
case 0x1c7f8a43U: /* deferred_cursor_manager_list_rdb_entry_edge_magazine_rating */
strlcat(query, "edge_rating", len);
add_quotes = false;
}
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_edge_magazine_issue"))
{
break;
case 0xaaeebde7U: /* deferred_cursor_manager_list_rdb_entry_edge_magazine_issue */
strlcat(query, "edge_issue", len);
add_quotes = false;
}
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_famitsu_magazine_rating"))
{
break;
case 0xbf7ff5e7U: /* deferred_cursor_manager_list_rdb_entry_famitsu_magazine_rating */
strlcat(query, "famitsu_rating", len);
add_quotes = false;
}
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_releasemonth"))
{
break;
case 0x2b36ce66U: /* deferred_cursor_manager_list_rdb_entry_releasemonth */
strlcat(query, "releasemonth", len);
add_quotes = false;
}
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_releaseyear"))
{
break;
case 0x9c7c6e91U: /* deferred_cursor_manager_list_rdb_entry_releaseyear */
strlcat(query, "releaseyear", len);
add_quotes = false;
}
else if (!strcmp(label, "deferred_cursor_manager_list_rdb_entry_max_users"))
{
break;
case 0xbfcba816U: /* deferred_cursor_manager_list_rdb_entry_max_users */
strlcat(query, "users", len);
add_quotes = false;
break;
}
strlcat(query, "':", len);
@ -139,75 +162,81 @@ static int database_cursor_iterate(libretrodb_cursor_t *cur,
if (!key || !val)
continue;
if (!strcmp(key->string.buff, "name"))
switch (djb2(key->string.buff))
{
case 0x7c9b0c46U: /* name */
db_info->name = strdup(val->string.buff);
if (!strcmp(key->string.buff, "description"))
break;
case 0x91b0c789U: /* description */
db_info->description = strdup(val->string.buff);
if (!strcmp(key->string.buff, "publisher"))
break;
case 0x5e099013U: /* publisher */
db_info->publisher = strdup(val->string.buff);
if (!strcmp(key->string.buff, "developer"))
break;
case 0x1783d2abU: /* developer */
db_info->developer = strdup(val->string.buff);
if (!strcmp(key->string.buff, "origin"))
break;
case 0x1315e3edU: /* origin */
db_info->origin = strdup(val->string.buff);
if (!strcmp(key->string.buff, "franchise"))
break;
case 0xc3a526b8U: /* franchise */
db_info->franchise = strdup(val->string.buff);
if (!strcmp(key->string.buff, "bbfc_rating"))
break;
case 0xede26836U: /* bbfc_rating */
db_info->bbfc_rating = strdup(val->string.buff);
if (!strcmp(key->string.buff, "esrb_rating"))
break;
case 0x4c3fa255U: /* esrb_rating */
db_info->esrb_rating = strdup(val->string.buff);
if (!strcmp(key->string.buff, "elspa_rating"))
break;
case 0xd9cab41eU: /* elspa_rating */
db_info->elspa_rating = strdup(val->string.buff);
if (!strcmp(key->string.buff, "cero_rating"))
break;
case 0x084a1772U: /* cero_rating */
db_info->cero_rating = strdup(val->string.buff);
if (!strcmp(key->string.buff, "pegi_rating"))
break;
case 0x431b736eU: /* pegi_rating */
db_info->pegi_rating = strdup(val->string.buff);
if (!strcmp(key->string.buff, "enhancement_hw"))
break;
case 0xab612029U: /* enhancement_hw */
db_info->enhancement_hw = strdup(val->string.buff);
if (!strcmp(key->string.buff, "edge_review"))
break;
case 0xd3573eabU: /* edge_magazine_review */
db_info->edge_magazine_review = strdup(val->string.buff);
if (!strcmp(key->string.buff, "edge_rating"))
break;
case 0xd30dc4feU: /* edge_magazine_rating */
db_info->edge_magazine_rating = val->uint_;
if (!strcmp(key->string.buff, "edge_issue"))
break;
case 0xa0f30d42U: /* edge_magazine_issue */
db_info->edge_magazine_issue = val->uint_;
if (!strcmp(key->string.buff, "famitsu_rating"))
break;
case 0x0a50ca62U: /* famitsu_magazine_rating */
db_info->famitsu_magazine_rating = val->uint_;
if (!strcmp(key->string.buff, "users"))
break;
case 0x1084ff77U: /* max_users */
db_info->max_users = val->uint_;
if (!strcmp(key->string.buff, "releasemonth"))
break;
case 0x790ad76cU: /* releasemonth */
db_info->releasemonth = val->uint_;
if (!strcmp(key->string.buff, "releaseyear"))
break;
case 0x7fd06ed7U: /* releaseyear */
db_info->releaseyear = val->uint_;
if (!strcmp(key->string.buff, "rumble"))
break;
case 0x1a4dc3ecU: /* rumble_supported */
db_info->rumble_supported = val->uint_;
if (!strcmp(key->string.buff, "analog"))
break;
case 0xf220fc17U: /* analog_supported */
db_info->analog_supported = val->uint_;
if (!strcmp(key->string.buff, "crc"))
break;
case 0x0b88671dU: /* crc32 */
db_info->crc32 = bin_to_hex_alloc((uint8_t*)val->binary.buff, val->binary.len);
if (!strcmp(key->string.buff, "sha1"))
break;
case 0x7c9de632U: /* sha1 */
db_info->sha1 = bin_to_hex_alloc((uint8_t*)val->binary.buff, val->binary.len);
if (!strcmp(key->string.buff, "md5"))
break;
case 0x0b888fabU: /* md5 */
db_info->md5 = bin_to_hex_alloc((uint8_t*)val->binary.buff, val->binary.len);
break;
}
}
rmsgpack_dom_value_free(&item);