From 7a785a4224a2db65f9d066d895dce978412d9b3f Mon Sep 17 00:00:00 2001 From: Rubidium Date: Tue, 20 Jun 2023 19:16:38 +0200 Subject: [PATCH] Codechange: simplify StringParameters now type cannot be nullptr --- src/strings.cpp | 14 ++++++++------ src/strings_internal.h | 12 ++---------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/strings.cpp b/src/strings.cpp index 5c8ec2369e..f0d4d9102d 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -187,7 +187,7 @@ void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num) MemCpyT(dst, _global_string_params.GetPointerToOffset(0), num); for (int i = 0; i < num; i++) { - if (_global_string_params.HasTypeInformation() && _global_string_params.GetTypeAtOffset(i) == SCC_RAW_STRING_POINTER) { + if (_global_string_params.GetTypeAtOffset(i) == SCC_RAW_STRING_POINTER) { strings[i] = stredup((const char *)(size_t)_global_string_params.GetParam(i)); dst[i] = (size_t)strings[i]; } else { @@ -851,12 +851,14 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara { size_t orig_offset = args.GetOffset(); - if (!dry_run && args.HasTypeInformation()) { + if (!dry_run) { /* - * FormatString was called without `dry_run` set, however `args` has - * space allocated for type information and thus wants type checks on - * the parameters. So, we need to gather the type information via the - * dry run first, before we can continue formatting the string. + * This function is normally called with `dry_run` false, then we call this function again + * with `dry_run` being true. The dry run is required for the gender formatting. For the + * gender determination we need to format a sub string to get the gender, but for that we + * need to know as what string control code type the specific parameter is encoded. Since + * gendered words can be before the "parameter" words, this needs to be determined before + * the actual formatting. */ std::string buffer; StringBuilder dry_run_builder(buffer); diff --git a/src/strings_internal.h b/src/strings_internal.h index a78c72f3a8..8e32dccec9 100644 --- a/src/strings_internal.h +++ b/src/strings_internal.h @@ -17,7 +17,7 @@ class StringParameters { protected: StringParameters *parent = nullptr; ///< If not nullptr, this instance references data from this parent instance. uint64 *data; ///< Array with the actual data. - WChar *type; ///< Array with type information about the data. Can be nullptr when no type information is needed. See #StringControlCode. + WChar *type; ///< Array with type information about the data. See #StringControlCode. WChar next_type = 0; ///< The type of the next data that is retrieved. size_t offset = 0; ///< Current offset in the data/type arrays. @@ -114,8 +114,7 @@ public: */ StringParameters GetRemainingParameters(size_t offset) { - return StringParameters(&this->data[offset], GetDataLeft(), - this->type == nullptr ? nullptr : &this->type[offset]); + return StringParameters(&this->data[offset], GetDataLeft(), &this->type[offset]); } /** Return the amount of elements which can still be read. */ @@ -131,17 +130,10 @@ public: return &this->data[offset]; } - /** Does this instance store information about the type of the parameters. */ - bool HasTypeInformation() const - { - return this->type != nullptr; - } - /** Get the type of a specific element. */ WChar GetTypeAtOffset(size_t offset) const { assert(offset < this->num_param); - assert(this->HasTypeInformation()); return this->type[offset]; }