Optimizations to gettext (thanks V10lator)

This commit is contained in:
Xpl0itU 2022-08-23 20:42:34 +02:00
parent b28fd6cef5
commit 9e506320f4
7 changed files with 87 additions and 107 deletions

View File

@ -1,5 +1,4 @@
#ifndef CFILE_HPP_
#define CFILE_HPP_
#pragma once
#include <stdio.h>
#include <string>
@ -57,5 +56,3 @@ protected:
uint64_t filesize;
uint64_t pos;
};
#endif

View File

@ -26,5 +26,5 @@ typedef enum
Swkbd_LanguageType__Invalid = 12
} Swkbd_LanguageType;
void loadLanguage(Swkbd_LanguageType language);
Swkbd_LanguageType getSystemLanguage();
void loadLanguage(Swkbd_LanguageType language) __attribute__((cold));
Swkbd_LanguageType getSystemLanguage() __attribute__((cold));

View File

@ -14,8 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#ifndef _GETTEXT_H_
#define _GETTEXT_H_
#pragma once
#include <wut_types.h>
@ -33,12 +32,10 @@ void gettextCleanUp(void);
* input msg = a text in ASCII
* output = the translated msg in utf-8
*/
const char *gettext(const char *msg);
const char *gettext(const char *msg) __attribute__((hot));
#define tr(s) gettext(s)
#define trNOOP(s) s
#ifdef __cplusplus
}
#endif
#endif /* _GETTEXT_H_ */
#endif

View File

@ -23,8 +23,7 @@
*
* for WiiXplorer 2010
***************************************************************************/
#ifndef __STRING_TOOLS_H
#define __STRING_TOOLS_H
#pragma once
#include <vector>
#include <string>
@ -78,5 +77,3 @@ class StringTools{
static std::vector<std::string> stringSplit(const std::string & value, const std::string & splitter);
};
#endif /* __STRING_TOOLS_H */

View File

@ -1,16 +1,19 @@
msgid "\\ue000 Confirm - \\ue001 Cancel"
msgid "Press \\ue044 to exit."
msgstr ""
msgid "\\ue000 Yes - \\ue001 No"
msgid "\\ue000: Copy \\ue001: Back"
msgstr ""
msgid "\\ue000: Select Task \\ue001: Back"
msgid "\\ue000: Export \\ue001: Back"
msgstr ""
msgid "\\ue000: Select Game \\ue001: Back"
msgid "\\ue000: Import \\ue001: Back"
msgstr ""
msgid "\\ue000: Backup \\ue001: Back"
msgid "\\ue000: Wipe \\ue001: Back"
msgstr ""
msgid "\\ue000: Restore \\ue001: Back"
msgstr ""
msgid ""
@ -23,22 +26,19 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
msgid "\\ue000: Restore \\ue001: Back"
msgid "\\ue000: Backup \\ue001: Back"
msgstr ""
msgid "\\ue000: Wipe \\ue001: Back"
msgid "\\ue000: Select Game \\ue001: Back"
msgstr ""
msgid "\\ue000: Import \\ue001: Back"
msgid "\\ue000: Select Task \\ue001: Back"
msgstr ""
msgid "\\ue000: Export \\ue001: Back"
msgid "\\ue000 Yes - \\ue001 No"
msgstr ""
msgid "\\ue000: Copy \\ue001: Back"
msgstr ""
msgid "Press \\ue044 to exit."
msgid "\\ue000 Confirm - \\ue001 Cancel"
msgstr ""
#: src/main.cpp:63

View File

@ -1,18 +1,21 @@
msgid "\\ue000 Confirm - \\ue001 Cancel"
msgstr "\\ue000 Confirmar - \\ue001 Cancelar"
msgid "Press \\ue044 to exit."
msgstr "Presiona \\ue044 para salir."
msgid "\\ue000 Yes - \\ue001 No"
msgstr "\\ue000 Sí - \\ue001 No"
msgid "\\ue000: Select Task \\ue001: Back"
msgstr "\\ue000: Seleccionar Tarea \\ue001: Atrás"
msgid "\\ue000: Select Game \\ue001: Back"
msgstr "\\ue000: Seleccionar Juego \\ue001: Atrás"
msgid "\\ue000: Backup \\ue001: Back"
msgid "\\ue000: Copy \\ue001: Back"
msgstr "\\ue000: Copiar \\ue001: Atrás"
msgid "\\ue000: Export \\ue001: Back"
msgstr "\\ue000: Exportar \\ue001: Atrás"
msgid "\\ue000: Import \\ue001: Back"
msgstr "\\ue000: Importar \\ue001: Atrás"
msgid "\\ue000: Wipe \\ue001: Back"
msgstr "\\ue000: Borrar \\ue001: Atrás"
msgid "\\ue000: Restore \\ue001: Back"
msgstr "\\ue000: Restaurar \\ue001: Atrás"
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: \n"
@ -23,23 +26,20 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
msgid "\\ue000: Restore \\ue001: Back"
msgstr "\\ue000: Restaurar \\ue001: Atrás"
msgid "\\ue000: Wipe \\ue001: Back"
msgstr "\\ue000: Borrar \\ue001: Atrás"
msgid "\\ue000: Import \\ue001: Back"
msgstr "\\ue000: Importar \\ue001: Atrás"
msgid "\\ue000: Export \\ue001: Back"
msgstr "\\ue000: Exportar \\ue001: Atrás"
msgid "\\ue000: Copy \\ue001: Back"
msgid "\\ue000: Backup \\ue001: Back"
msgstr "\\ue000: Copiar \\ue001: Atrás"
msgid "Press \\ue044 to exit."
msgstr "Presiona \\ue044 para salir."
msgid "\\ue000: Select Game \\ue001: Back"
msgstr "\\ue000: Seleccionar Juego \\ue001: Atrás"
msgid "\\ue000: Select Task \\ue001: Back"
msgstr "\\ue000: Seleccionar Tarea \\ue001: Atrás"
msgid "\\ue000 Yes - \\ue001 No"
msgstr "\\ue000 Sí - \\ue001 No"
msgid "\\ue000 Confirm - \\ue001 Cancel"
msgstr "\\ue000 Confirmar - \\ue001 Cancelar"
#: src/main.cpp:63
msgid "Disclaimer:"

View File

@ -27,14 +27,12 @@
char* strdup (const char* s)
{
size_t slen = strlen(s);
char* result = (char *) malloc(slen + 1);
size_t slen = strlen(s) + 1;
char* result = (char *) malloc(slen);
if(result == NULL)
{
return NULL;
}
memcpy(result, s, slen+1);
memcpy(result, s, slen);
return result;
}
@ -43,29 +41,18 @@ typedef struct _MSG {
char* msgstr;
struct _MSG *next;
} MSG;
static MSG *baseMSG = 0;
static MSG *baseMSG = NULL;
#define HASHWORDBITS 32
#define HASHMULTIPLIER 31 // or 37
/* Defines the so called `hashpjw' function by P.J. Weinberger
[see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
1986, 1987 Bell Telephone Laboratories, Inc.] */
// Hashing function from https://stackoverflow.com/a/2351171
static inline uint32_t hash_string(const char *str_param) {
uint32_t hval, g;
const char *str = str_param;
uint32_t hash = 0;
unsigned char *p;
/* Compute the hash value for the given string. */
hval = 0;
while (*str != '\0') {
hval <<= 4;
hval += (uint8_t) *str++;
g = hval & ((uint32_t) 0xf << (HASHWORDBITS - 4));
if (g != 0) {
hval ^= g >> (HASHWORDBITS - 8);
hval ^= g;
}
}
return hval;
while(*str_param != '\0')
hash = HASHMULTIPLIER * hash + *p++;
return hash;
}
/* Expand some escape sequences found in the argument string. */
@ -78,9 +65,10 @@ expand_escape(const char *str) {
if (retval == NULL) return NULL;
rp = retval;
while (cp[0] != '\0' && cp[0] != '\\')
while (*cp != '\0' && *cp != '\\')
*rp++ = *cp++;
if (cp[0] == '\0') goto terminate;
if (*cp == '\0') goto terminate;
do {
/* Here cp[0] == '\\'. */
@ -117,10 +105,6 @@ expand_escape(const char *str) {
*rp++ = '\v';
++cp;
break;
case '\\':
*rp = '\\';
++cp;
break;
case '0':
case '1':
case '2':
@ -143,14 +127,17 @@ expand_escape(const char *str) {
*rp = ch;
}
break;
case '\\':
++cp;
default:
*rp = '\\';
break;
}
while (cp[0] != '\0' && cp[0] != '\\')
while (*cp != '\0' && *cp != '\\')
*rp++ = *cp++;
} while (cp[0] != '\0');
} while (*cp != '\0');
/* Terminate string. */
terminate:
@ -158,33 +145,35 @@ terminate:
return retval;
}
static MSG *findMSG(uint32_t id) {
MSG *msg;
for (msg = baseMSG; msg; msg = msg->next) {
if (msg->id == id) return msg;
}
static inline MSG *findMSG(uint32_t id) {
for (MSG *msg = baseMSG; msg; msg = msg->next)
if (msg->id == id)
return msg;
return NULL;
}
static MSG *setMSG(const char *msgid, const char *msgstr) {
if(!msgstr)
return NULL;
uint32_t id = hash_string(msgid);
MSG *msg = findMSG(id);
if (!msg) {
msg = (MSG *) malloc(sizeof(MSG));
msg->id = id;
msg->msgstr = NULL;
msg->msgstr = expand_escape(msgstr);
msg->next = baseMSG;
baseMSG = msg;
return NULL;
}
if (msg) {
if (msgstr) {
if (msg->msgstr) free(msg->msgstr);
//msg->msgstr = strdup(msgstr);
msg->msgstr = expand_escape(msgstr);
}
return msg;
if (msgstr) {
if (msg->msgstr) free(msg->msgstr);
//msg->msgstr = strdup(msgstr);
msg->msgstr = expand_escape(msgstr);
}
return NULL;
return msg;
}
extern "C" void gettextCleanUp(void) {
@ -262,9 +251,9 @@ extern "C" BOOL gettextLoadLanguage(const char* langFile) {
}
extern "C" const char *gettext(const char *msgid) {
if(!msgid) return NULL;
MSG *msg = findMSG(hash_string(msgid));
if (msg && msg->msgstr) return msg->msgstr;
return msgid;
}
if(!msgid)
return NULL;
MSG *msg = findMSG(hash_string(msgid));
return msg ? msg->msgstr : msgid;
}