diff --git a/src/strings.cpp b/src/strings.cpp index e02ae292a4..7ee8497cd5 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -1802,55 +1802,49 @@ static const char _initial_name_letters[] = { 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', }; -static void GenAndCoName(StringBuilder &builder, uint32_t arg) +static std::span GetSurnameOptions() { - const char * const *base; - uint num; + if (_settings_game.game_creation.landscape == LT_TOYLAND) return _silly_surname_list; + return _surname_list; +} - if (_settings_game.game_creation.landscape == LT_TOYLAND) { - base = _silly_surname_list; - num = lengthof(_silly_surname_list); - } else { - base = _surname_list; - num = lengthof(_surname_list); - } +/** + * Get the surname of the president with the given seed. + * @param seed The seed the surname was generated from. + * @return The surname. + */ +static const char *GetSurname(uint32_t seed) +{ + auto surname_options = GetSurnameOptions(); + return surname_options[surname_options.size() * GB(seed, 16, 8) >> 8]; +} - builder += base[num * GB(arg, 16, 8) >> 8]; +static void GenAndCoName(StringBuilder &builder, uint32_t seed) +{ + builder += GetSurname(seed); builder += " & Co."; } -static void GenPresidentName(StringBuilder &builder, uint32_t x) +static void GenPresidentName(StringBuilder &builder, uint32_t seed) { - char initial[] = "?. "; - const char * const *base; - uint num; - uint i; + builder += _initial_name_letters[std::size(_initial_name_letters) * GB(seed, 0, 8) >> 8]; + builder += ". "; - initial[0] = _initial_name_letters[sizeof(_initial_name_letters) * GB(x, 0, 8) >> 8]; - builder += initial; - - i = (sizeof(_initial_name_letters) + 35) * GB(x, 8, 8) >> 8; - if (i < sizeof(_initial_name_letters)) { - initial[0] = _initial_name_letters[i]; - builder += initial; + /* The second initial is optional. */ + size_t index = (std::size(_initial_name_letters) + 35) * GB(seed, 8, 8) >> 8; + if (index < std::size(_initial_name_letters)) { + builder += _initial_name_letters[index]; + builder += ". "; } - if (_settings_game.game_creation.landscape == LT_TOYLAND) { - base = _silly_surname_list; - num = lengthof(_silly_surname_list); - } else { - base = _surname_list; - num = lengthof(_surname_list); - } - - builder += base[num * GB(x, 16, 8) >> 8]; + builder += GetSurname(seed); } static void GetSpecialNameString(StringBuilder &builder, int ind, StringParameters &args) { switch (ind) { case 1: // not used - builder += _silly_company_names[std::min(args.GetNextParameter(), lengthof(_silly_company_names) - 1)]; + builder += _silly_company_names[std::min(args.GetNextParameter(), std::size(_silly_company_names) - 1)]; return; case 2: // used for Foobar & Co company names