]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
i18n: CTX cache with map, not array indices. Fixes #1608
authorTimePath <andrew.hardaker1995@gmail.com>
Thu, 17 Dec 2015 22:15:28 +0000 (09:15 +1100)
committerTimePath <andrew.hardaker1995@gmail.com>
Thu, 17 Dec 2015 22:16:49 +0000 (09:16 +1100)
qcsrc/lib/i18n.qh
qcsrc/lib/map.qc
qcsrc/lib/unsafe.qh

index fd8ec83066ec7438fc3c9f8302c8efbcc034d90b..e439b22aa56799006c3eea004fa17fd987e699ae 100644 (file)
@@ -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;
 }
index aee2a363765d50bb7f52629b6af65d161f4c10ac..fc7012cc0b43e16cfe0c76d29f78885167c83d07 100644 (file)
@@ -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()
 {
index b218b660a534f1468bc84f8e7a51c9f89aea7ed0..6b4db2f1ac2a27bbac257bcda3a47f9a921ff5ec 100644 (file)
@@ -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;