From 023844a2ef8e9cc9d68d685183805c9f137d7974 Mon Sep 17 00:00:00 2001 From: TimePath Date: Fri, 18 Dec 2015 09:15:28 +1100 Subject: [PATCH] i18n: CTX cache with map, not array indices. Fixes #1608 --- qcsrc/lib/i18n.qh | 14 +++++++------- qcsrc/lib/map.qc | 8 ++++++++ qcsrc/lib/unsafe.qh | 3 ++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/qcsrc/lib/i18n.qh b/qcsrc/lib/i18n.qh index fd8ec8306..e439b22aa 100644 --- a/qcsrc/lib/i18n.qh +++ b/qcsrc/lib/i18n.qh @@ -2,6 +2,7 @@ #define I18N_H #include "log.qh" +#include "map.qc" #include "unsafe.qh" // translation helpers @@ -29,29 +30,28 @@ string language_filename(string s) #endif #if CTX_CACHE - ArrayList CTX_cache; + HashMap CTX_cache; STATIC_INIT(CTX_cache) { - AL_NEW(CTX_cache, 0, string_null, s); + HM_NEW(CTX_cache); } SHUTDOWN(CTX_cache) { - AL_DELETE(CTX_cache); + HM_DELETE(CTX_cache); } #endif string CTX(string s) { #if CTX_CACHE - int i = strid(s); - string c = AL_gets(CTX_cache, i); - if (c) return c; + string c = HM_gets(CTX_cache, s); + if (c != "") return c; #endif int p = strstrofs(s, "^", 0); string ret = (p < 0) ? s : substring(s, p + 1, -1); #if CTX_CACHE LOG_DEBUGF("CTX(\"%s\")\n", s); - AL_sets(CTX_cache, i, ret); + HM_sets(CTX_cache, s, ret); #endif return ret; } diff --git a/qcsrc/lib/map.qc b/qcsrc/lib/map.qc index aee2a3637..fc7012cc0 100644 --- a/qcsrc/lib/map.qc +++ b/qcsrc/lib/map.qc @@ -1,6 +1,8 @@ #ifndef MAP_H #define MAP_H +#include "int.qh" + // Databases (hash tables) const int DB_BUCKETS = 8192; void db_save(int db, string filename) @@ -17,10 +19,13 @@ void db_save(int db, string filename) fclose(fh); } +typedef int HashMap; + int db_create() { return buf_create(); } +#define HM_NEW(this) (this = db_create()) void db_put(int db, string key, string value); @@ -75,12 +80,14 @@ void db_close(int db) { buf_del(db); } +#define HM_DELETE(this) db_close(this) string db_get(int db, string key) { int h = crc16(false, key) % DB_BUCKETS; return uri_unescape(infoget(bufstr_get(db, h), key)); } +#define HM_gets(this, k) db_get(this, k) #define db_remove(db, key) db_put(db, key, "") @@ -89,6 +96,7 @@ void db_put(int db, string key, string value) int h = crc16(false, key) % DB_BUCKETS; bufstr_set(db, h, infoadd(bufstr_get(db, h), key, uri_escape(value))); } +#define HM_sets(this, key, val) db_put(this, key, val) void db_test() { diff --git a/qcsrc/lib/unsafe.qh b/qcsrc/lib/unsafe.qh index b218b660a..6b4db2f1a 100644 --- a/qcsrc/lib/unsafe.qh +++ b/qcsrc/lib/unsafe.qh @@ -12,7 +12,8 @@ typedef float(...) rawfunc; X(rawfunc) #undef X -#define strid(s) ITOF(reinterpret_cast(int, s)) +#define _strid(s) ITOF(reinterpret_cast(int, s)) +#define strid(s) stof(sprintf("%i", s)) .int _unsafe_fld1, _unsafe_fld2; int INTEGER_ONE; -- 2.39.2