Commit: 7395a4294f48b07939c7d7febe190c7b0973f26e
Parent: 177155ed10760d1ce5811629bec51d39fe805cee
Author: opask
Date: Wed, 1 Aug 2018 20:49:42 -0600
minor simplification and style tweaks in language.c and charsets.c
Diffstat:
M | charsets.c | | | 65 | +++++++++++++++++++++++++++++++++++++++++------------------------ |
M | language.c | | | 61 | +++++++++++++++++++++++++------------------------------------ |
M | links.h | | | 2 | +- |
3 files changed, 67 insertions(+), 61 deletions(-)
diff --git a/charsets.c b/charsets.c
@@ -304,8 +304,10 @@ static int table_init = 1;
void free_conv_table(void)
{
- if (!utf_table_init) free_utf_table();
- if (!table_init) new_translation_table(table);
+ if (!utf_table_init)
+ free_utf_table();
+ if (!table_init)
+ new_translation_table(table);
}
struct conv_table *get_translation_table(int from, int to)
@@ -313,38 +315,52 @@ struct conv_table *get_translation_table(int from, int to)
int i;
static int lfr = -1;
static int lto = -1;
- if (/*from == to ||*/ from == -1 || to == -1) return NULL;
- if (to == utf8_table) return get_translation_table_to_utf_8(from);
+ if (from == -1 || to == -1)
+ return NULL;
+ if (to == utf8_table)
+ return get_translation_table_to_utf_8(from);
if (table_init) {
memset(table, 0, sizeof(struct conv_table) * 256);
table_init = 0;
}
- if (from == lfr && to == lto) return table;
- lfr = from; lto = to;
+ if (from == lfr && to == lto)
+ return table;
+ lfr = from;
+ lto = to;
new_translation_table(table);
if (from == utf8_table) {
int j;
- for (j = 0; codepages[to].table[j].c; j++) add_utf_8(table, codepages[to].table[j].u, is_nbsp(codepages[to].table[j].u) ? cast_uchar strings[1] : codepages[to].table[j].u == 0xad ? cast_uchar strings[0] : cast_uchar strings[codepages[to].table[j].c]);
- for (i = 0; unicode_7b[i].x != -1; i++) if (unicode_7b[i].x >= 0x80) add_utf_8(table, unicode_7b[i].x, cast_uchar unicode_7b[i].s);
- } else for (i = 128; i < 256; i++) {
- int j;
- unsigned char *u;
- for (j = 0; codepages[from].table[j].c; j++) {
- if (codepages[from].table[j].c == i) goto f;
+ for (j = 0; codepages[to].table[j].c; j++)
+ add_utf_8(table, codepages[to].table[j].u,
+ is_nbsp(codepages[to].table[j].u) ?
+ cast_uchar strings[1] : codepages[to].table[j].u == 0xad ?
+ cast_uchar strings[0] : cast_uchar strings[codepages[to].table[j].c]);
+ for (i = 0; unicode_7b[i].x != -1; i++)
+ if (unicode_7b[i].x >= 0x80)
+ add_utf_8(table, unicode_7b[i].x,
+ cast_uchar unicode_7b[i].s);
+ } else
+ for (i = 128; i < 256; i++) {
+ int j;
+ unsigned char *u;
+ for (j = 0; codepages[from].table[j].c; j++)
+ if (codepages[from].table[j].c == i)
+ goto f;
+ continue;
+f:
+ if ((u = u2cp(codepages[from].table[j].u, to, 1)))
+ table[i].u.str = u;
}
- continue;
- f:
- u = u2cp(codepages[from].table[j].u, to, 1);
- if (u) table[i].u.str = u;
- }
return table;
}
static inline int xxstrcmp(unsigned char *s1, unsigned char *s2, int l2)
{
while (l2) {
- if (*s1 > *s2) return 1;
- if (!*s1 || *s1 < *s2) return -1;
+ if (*s1 > *s2)
+ return 1;
+ if (!*s1 || *s1 < *s2)
+ return -1;
s1++;
s2++;
l2--;
@@ -400,7 +416,7 @@ unsigned char *get_entity_string(unsigned char *st, int l, int encoding)
else s = m + 1;
}
return NULL;
- f:;
+f:;
}
return u2cp(n, encoding, 1);
@@ -494,7 +510,7 @@ need_table:
return convert_string(ct, c, (int)strlen(cast_const_char c), dopt);
}
-int get_cp_index(unsigned char *n)
+int get_cp_index(const unsigned char *n)
{
int i, a, p, q, sl, ii = -1, ll = 0;
for (i = 0; codepages[i].name; i++) {
@@ -510,7 +526,7 @@ int get_cp_index(unsigned char *n)
ii = i;
}
}
- fail:;
+fail:;
}
}
}
@@ -665,6 +681,7 @@ unsigned char *cp_strchr(int charset, unsigned char *str, unsigned chr)
void init_charset(void)
{
utf8_table = get_cp_index(cast_uchar "UTF-8");
- if (utf8_table == -1) internal("no UTF-8 charset");
+ if (utf8_table == -1)
+ internal("no UTF-8 charset");
bookmarks_codepage = utf8_table;
}
diff --git a/language.c b/language.c
@@ -33,15 +33,16 @@ void init_trans(void)
void shutdown_trans(void)
{
int j, k;
- for (j = 0; j < N_CODEPAGES; j++) if (translation_array[j]) {
- for (k = 0; k < T__N_TEXTS; k++) {
- unsigned char *txt = translation_array[j][k];
- if (txt &&
- txt != cast_uchar translation_english[k].name)
- free(txt);
+ for (j = 0; j < N_CODEPAGES; j++)
+ if (translation_array[j]) {
+ for (k = 0; k < T__N_TEXTS; k++) {
+ unsigned char *txt = translation_array[j][k];
+ if (txt
+ && txt != cast_uchar translation_english[k].name)
+ free(txt);
+ }
+ free(translation_array[j]);
}
- free(translation_array[j]);
- }
}
int get_language_from_lang(unsigned char *lang)
@@ -50,7 +51,7 @@ int get_language_from_lang(unsigned char *lang)
lang = stracpy(lang);
lang[strcspn(cast_const_char lang, ".@")] = 0;
if (!casestrcmp(lang, cast_uchar "nn_NO"))
- strcpy(cast_char lang, "no");
+ strcpy((char *)lang, "no");
for (p = lang; *p; p++) {
if (*p >= 'A' && *p <= 'Z')
*p += 'a' - 'A';
@@ -68,7 +69,7 @@ search_again:
}
free(p);
- if ((p = cast_uchar strchr(cast_const_char lang, '-'))) {
+ if ((p = cast_uchar strchr((const char *)lang, '-'))) {
*p = 0;
goto search_again;
}
@@ -79,18 +80,19 @@ search_again:
int get_default_charset(void)
{
unsigned char *lang, *p;
- int i;
+ const int i = get_cp_index(p);
lang = cast_uchar getenv("LC_CTYPE");
if (!lang)
lang = cast_uchar getenv("LANG");
- if (!lang) {
+ if (!lang)
return 0;
- }
- if ((p = cast_uchar strchr(cast_const_char lang, '.'))) {
+ if ((p = cast_uchar strchr(cast_const_char lang, '.')))
p++;
- } else {
- if (strlen(cast_const_char lang) > 5 && !casestrcmp(cast_uchar (strchr(cast_const_char lang, 0) - 5), cast_uchar "@euro")) {
+ else {
+ if (strlen((const char *)lang) > 5
+ && !casestrcmp(cast_uchar (strchr((const char *)lang, 0) - 5),
+ cast_uchar "@euro")) {
p = cast_uchar "ISO-8859-15";
} else {
p = cast_uchar translations[0].t[T__DEFAULT_CHAR_SET].name;
@@ -98,8 +100,6 @@ int get_default_charset(void)
p = cast_uchar "";
}
}
- i = get_cp_index(p);
-
if (i < 0)
return 0;
@@ -113,9 +113,7 @@ int get_commandline_charset(void)
static inline int is_direct_text(unsigned char *text)
{
-/* Do not compare to dummyarray directly - thwart some misoptimizations */
- unsigned char * volatile dm = dummyarray;
- return !((my_uintptr_t)text >= (my_uintptr_t)dm && (my_uintptr_t)text < (my_uintptr_t)(dm + T__N_TEXTS));
+ return !(text >= dummyarray && text < (dummyarray + T__N_TEXTS));
}
unsigned char *get_text_translation(unsigned char *text, struct terminal *term)
@@ -135,7 +133,7 @@ unsigned char *get_text_translation(unsigned char *text, struct terminal *term)
unsigned char *tt;
if ((trn = current_tra[text - dummyarray]))
return trn;
- tr:
+tr:
if (!(tt = cast_uchar translations[0].t[text - dummyarray].name))
trn = cast_uchar translation_english[text - dummyarray].name;
else {
@@ -172,19 +170,10 @@ unsigned char *get_english_translation(unsigned char *text)
void set_language(void)
{
- int i;
- unsigned char *cp;
- for (i = 0; i < T__N_TEXTS; i++)
- if (translations[0].t[i].code != i) {
- internal("Bad table for language %s. Run script synclang.",
- translations[0].t[T__LANGUAGE].name);
- return;
- }
- cp = cast_uchar translations[0].t[T__CHAR_SET].name;
- i = get_cp_index(cp);
- if (i == -1) {
- internal("Unknown charset for language %s.", translations[0].t[T__LANGUAGE].name);
- i = 0;
+ const unsigned char *cp = (unsigned char *)translations[0].t[T__CHAR_SET].name;
+ if ((current_lang_charset = get_cp_index(cp)) == -1) {
+ internal("Unknown charset for language %s.",
+ translations[0].t[T__LANGUAGE].name);
+ current_lang_charset = 0;
}
- current_lang_charset = i;
}
diff --git a/links.h b/links.h
@@ -2869,7 +2869,7 @@ int get_entity_number(unsigned char *st, int l);
unsigned char *get_entity_string(unsigned char *, int, int);
unsigned char *convert_string(struct conv_table *, unsigned char *, int, struct document_options *);
unsigned char *convert(int from, int to, unsigned char *c, struct document_options *dopt);
-int get_cp_index(unsigned char *);
+int get_cp_index(const unsigned char *);
unsigned char *get_cp_name(int);
unsigned char *get_cp_mime_name(int);
void free_conv_table(void);