From 17e9fbaa18a08e3a84f67955da54527dbf2acee3 Mon Sep 17 00:00:00 2001 From: TimePath Date: Sat, 5 Dec 2015 13:28:43 +1100 Subject: [PATCH] i18n: cache CTX --- qcsrc/lib/_all.inc | 1 + qcsrc/lib/i18n.qh | 30 ++++++++++++++++++++++++++++-- qcsrc/lib/unsafe.qh | 15 +++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 qcsrc/lib/unsafe.qh diff --git a/qcsrc/lib/_all.inc b/qcsrc/lib/_all.inc index 9f7004c34..a63b43e22 100644 --- a/qcsrc/lib/_all.inc +++ b/qcsrc/lib/_all.inc @@ -67,5 +67,6 @@ #include "string.qh" #include "struct.qh" #include "test.qc" +#include "unsafe.qh" #include "urllib.qc" #include "vector.qh" diff --git a/qcsrc/lib/i18n.qh b/qcsrc/lib/i18n.qh index 3773e16d1..137e78204 100644 --- a/qcsrc/lib/i18n.qh +++ b/qcsrc/lib/i18n.qh @@ -2,6 +2,7 @@ #define I18N_H #include "log.qh" +#include "unsafe.qh" // translation helpers string prvm_language; @@ -20,11 +21,36 @@ string language_filename(string s) return s; } +#ifndef CTX_CACHE + #define CTX_CACHE 1 +#endif + +#if CTX_CACHE + AL_declare(CTX_cache); + STATIC_INIT(CTX_cache) + { + AL_init(CTX_cache, 0, string_null, s); + } + SHUTDOWN(CTX_cache) + { + AL_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; +#endif int p = strstrofs(s, "^", 0); - if (p < 0) return s; - return substring(s, p + 1, -1); + 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); +#endif + return ret; } #define ZCTX(s) strzone(CTX(s)) diff --git a/qcsrc/lib/unsafe.qh b/qcsrc/lib/unsafe.qh new file mode 100644 index 000000000..f8fa38280 --- /dev/null +++ b/qcsrc/lib/unsafe.qh @@ -0,0 +1,15 @@ +#ifndef UNSAFE_H +#define UNSAFE_H + +#define reinterpret_cast(T, it) _unsafe_cast_##T(0, it) +#define X(T) T _unsafe_cast_##T(int dummy, ...) { return ...(0, T); } +X(float) +X(entity) +X(string) +typedef float(...) rawfunc; +X(rawfunc) +#undef X + +#define strid(s) etof(reinterpret_cast(entity, s)) + +#endif -- 2.39.2