Skip to content

Commit

Permalink
textlang: get rid of LANG_STARTS_WITH(…) macro hack (#589)
Browse files Browse the repository at this point in the history
Simplify the code and make cppcheck happy.
  • Loading branch information
benoit-pierre authored Jul 28, 2024
1 parent e0172c1 commit 4e7bb07
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions crengine/src/textlang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ static bool langStartsWith(const lString32 lang_tag, const char * prefix) {
return false;
}

// Some macros to expand: LANG_STARTS_WITH(("fr") ("es")) (no comma!)
// to langStartsWith(lang_tag, "fr") || langStartsWith(lang_tag, "es") || false
// (from https://stackoverflow.com/questions/19680962/translate-sequence-in-macro-parameters-to-separate-macros )
#define PRIMITIVE_SEQ_ITERATE(...) __VA_ARGS__ ## _END
#define SEQ_ITERATE(...) PRIMITIVE_SEQ_ITERATE(__VA_ARGS__)
#define LANG_STARTS_WITH(seq) SEQ_ITERATE(LANG_STARTS_WITH_EACH_1 seq)
#define LANG_STARTS_WITH_EACH_1(...) langStartsWith(lang_tag, __VA_ARGS__) || LANG_STARTS_WITH_EACH_2
#define LANG_STARTS_WITH_EACH_2(...) langStartsWith(lang_tag, __VA_ARGS__) || LANG_STARTS_WITH_EACH_1
#define LANG_STARTS_WITH_EACH_1_END false
#define LANG_STARTS_WITH_EACH_2_END false
// Support `langStartsWith(lang_tag, "fr", "es")` as a shortcut for:
// `langStartsWith(lang_tag, "fr") || langStartsWith(lang_tag, "es")`.
template<typename ...Args>
static inline bool langStartsWith(const lString32 lang_tag, const char *prefix, const Args &... prefix_list) {
return langStartsWith(lang_tag, prefix) || langStartsWith(lang_tag, prefix_list...);
}

// (hyph_filename_prefix added because CoolReader may still have both
// current "Italian.pattern" and old "Italian_hyphen_(Alan).pdb".)
Expand Down Expand Up @@ -996,7 +992,7 @@ TextLangCfg::TextLangCfg( lString32 lang_tag ) {
// so let it deal the the provided one as-is.
lString32 hb_lang_tag = lang_tag;
// Lowercase it for our tests
lang_tag.lowercase(); // (used by LANG_STARTS_WITH() macros)
lang_tag.lowercase(); // (used by langStartsWith(…) macros)

// Get hyph method/dictionary from _hyph_dict_table
_hyph_method = TextLangMan::getHyphMethodForLang(lang_tag);
Expand Down Expand Up @@ -1040,11 +1036,11 @@ TextLangCfg::TextLangCfg( lString32 lang_tag ) {
_is_ja = false;
_is_zh_SC = false;
_is_zh_TC = false;
if ( LANG_STARTS_WITH(("ja")) ) {
if ( langStartsWith(lang_tag, "ja") ) {
_is_ja_zh = true;
_is_ja = true;
}
else if ( LANG_STARTS_WITH(("zh")) ) {
else if ( langStartsWith(lang_tag, "zh") ) {
_is_ja_zh = true;
if ( lang_tag.pos("-hant") > 0 ) {
_is_zh_TC = true;
Expand All @@ -1062,7 +1058,7 @@ TextLangCfg::TextLangCfg( lString32 lang_tag ) {
_is_zh_SC = true;
}
}
else if ( LANG_STARTS_WITH(("lzh")) ) { // Literary/Classical Chinese
else if ( langStartsWith(lang_tag, "lzh") ) { // Literary/Classical Chinese
_is_ja_zh = true;
_is_zh_TC = true;
}
Expand Down Expand Up @@ -1116,12 +1112,12 @@ TextLangCfg::TextLangCfg( lString32 lang_tag ) {
bool has_em_dash_alphabetic = false; // U+2014 —, U+2E3A ⸺, U+2E3B ⸻

// Note: these macros use 'lang_tag'.
if ( LANG_STARTS_WITH(("en")) ) { // English
if ( langStartsWith(lang_tag, "en") ) { // English
has_left_single_quotation_mark_opening = true; // no right..closing in linebreakdef.c
has_left_double_quotation_mark_opening = true;
has_right_double_quotation_mark_closing = true;
}
else if ( LANG_STARTS_WITH(("fr") ("es")) ) { // French, Spanish
else if ( langStartsWith(lang_tag, "fr", "es") ) { // French, Spanish
has_left_single_quotation_mark_opening = true; // no right..closing in linebreakdef.c
has_left_double_quotation_mark_opening = true;
has_right_double_quotation_mark_closing = true;
Expand All @@ -1131,7 +1127,7 @@ TextLangCfg::TextLangCfg( lString32 lang_tag ) {
has_right_double_angle_quotation_mark_closing = true;
has_em_dash_alphabetic = true;
}
else if ( LANG_STARTS_WITH(("de")) ) { // German
else if ( langStartsWith(lang_tag, "de") ) { // German
has_left_single_quotation_mark_closing = true;
has_right_single_quotation_mark_glue = true;
has_left_double_quotation_mark_closing = true;
Expand All @@ -1148,14 +1144,14 @@ TextLangCfg::TextLangCfg( lString32 lang_tag ) {
has_right_double_angle_quotation_mark_opening = true;
*/
}
else if ( LANG_STARTS_WITH(("ru")) ) { // Russian
else if ( langStartsWith(lang_tag, "ru") ) { // Russian
// The following rule is disabled because Russian texts often
// use quotation marks from Word (“”)
// has_left_double_quotation_mark_closing = true;
has_left_double_angle_quotation_mark_opening = true;
has_right_double_angle_quotation_mark_closing = true;
}
else if ( LANG_STARTS_WITH(("zh")) ) { // Chinese
else if ( langStartsWith(lang_tag, "zh") ) { // Chinese
has_left_single_quotation_mark_opening = true;
has_right_single_quotation_mark_closing = true;
has_left_double_quotation_mark_opening = true;
Expand Down Expand Up @@ -1198,20 +1194,20 @@ TextLangCfg::TextLangCfg( lString32 lang_tag ) {

// Other line breaking and text layout tweaks
_lb_char_sub_func = NULL;
if ( LANG_STARTS_WITH(("en")) ) { // English
if ( langStartsWith(lang_tag, "en") ) { // English
_lb_char_sub_func = &lb_char_sub_func_english;
}
else if ( LANG_STARTS_WITH(("pl")) ) { // Polish
else if ( langStartsWith(lang_tag, "pl") ) { // Polish
_lb_char_sub_func = &lb_char_sub_func_polish;
_duplicate_real_hyphen_on_next_line = true;
}
else if ( LANG_STARTS_WITH(("cs") ("sk")) ) { // Czech, Slovak
else if ( langStartsWith(lang_tag, "cs", "sk") ) { // Czech, Slovak
_lb_char_sub_func = &lb_char_sub_func_czech_slovak;
}
else if ( LANG_STARTS_WITH(("pt") ("sr")) ) { // Portuguese, Serbian
else if ( langStartsWith(lang_tag, "pt", "sr") ) { // Portuguese, Serbian
_duplicate_real_hyphen_on_next_line = true;
}
else if ( LANG_STARTS_WITH(("ru")) ) { // Russian
else if ( langStartsWith(lang_tag, "ru") ) { // Russian
_lb_char_sub_func = &lb_char_sub_func_russian;
}
#endif
Expand Down Expand Up @@ -1306,7 +1302,7 @@ int TextLangCfg::getHangingPercent( bool right_hanging, bool rtl_line, bool & ch
// ratio if the next/prev char is a space char.
// This might not happen in other languages, so let's do that
// prevention generically. If needed, make that dependant on
// a boolean member, set to true if LANG_STARTS_WITH(("fr")).
// a boolean member, set to true if langStartsWith(…, "fr").
bool space_alongside = false;
if ( right_hanging ) {
if ( pos > 0 ) {
Expand Down

0 comments on commit 4e7bb07

Please sign in to comment.