Merge pull request #19422 from ZehMatt/string-duplicate

Remove String::Duplicate
This commit is contained in:
Matthias Moninger 2023-03-06 23:29:32 +02:00 committed by GitHub
commit 30e6eb9f08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 73 deletions

View File

@ -85,9 +85,7 @@ void TextComposition::HandleMessage(const SDL_Event* e)
break; break;
} }
utf8* newText = String::Duplicate(e->text.text); Insert(e->text.text);
Insert(newText);
Memory::Free(newText);
console.RefreshCaret(_session.SelectionStart); console.RefreshCaret(_session.SelectionStart);
WindowUpdateTextbox(); WindowUpdateTextbox();

View File

@ -539,14 +539,14 @@ static void WindowScenarioselectPaint(WindowBase* w, DrawPixelInfo* dpi)
if (scenario->Highscore != nullptr) if (scenario->Highscore != nullptr)
{ {
// TODO: Should probably be translatable // TODO: Should probably be translatable
const utf8* completedByName = "???"; u8string completedByName = "???";
if (!String::IsNullOrEmpty(scenario->Highscore->name)) if (!scenario->Highscore->name.empty())
{ {
completedByName = scenario->Highscore->name; completedByName = scenario->Highscore->name;
} }
ft = Formatter(); ft = Formatter();
ft.Add<StringId>(STR_STRING); ft.Add<StringId>(STR_STRING);
ft.Add<const char*>(completedByName); ft.Add<const char*>(completedByName.c_str());
ft.Add<money64>(scenario->Highscore->company_value); ft.Add<money64>(scenario->Highscore->company_value);
screenPos.y += DrawTextWrapped(*dpi, screenPos, 170, STR_COMPLETED_BY_WITH_COMPANY_VALUE, ft); screenPos.y += DrawTextWrapped(*dpi, screenPos, 170, STR_COMPLETED_BY_WITH_COMPANY_VALUE, ft);
} }
@ -623,16 +623,15 @@ static void WindowScenarioselectScrollpaint(WindowBase* w, DrawPixelInfo* dpi, i
{ window_scenarioselect_widgets[WIDX_SCENARIOLIST].width() - 45, y + 1 }); { window_scenarioselect_widgets[WIDX_SCENARIOLIST].width() - 45, y + 1 });
// Draw completion score // Draw completion score
const utf8* completedByName = "???"; u8string completedByName = "???";
if (!String::IsNullOrEmpty(scenario->Highscore->name)) if (!scenario->Highscore->name.empty())
{ {
completedByName = scenario->Highscore->name; completedByName = scenario->Highscore->name;
} }
SafeStrCpy(buffer, completedByName, 64);
ft = Formatter(); ft = Formatter();
ft.Add<StringId>(STR_COMPLETED_BY); ft.Add<StringId>(STR_COMPLETED_BY);
ft.Add<StringId>(STR_STRING); ft.Add<StringId>(STR_STRING);
ft.Add<char*>(buffer); ft.Add<const char*>(completedByName.c_str());
DrawTextBasic( DrawTextBasic(
*dpi, { scrollCentre, y + scenarioTitleHeight + 1 }, format, ft, *dpi, { scrollCentre, y + scenarioTitleHeight + 1 }, format, ft,
{ FontStyle::Small, TextAlignment::CENTRE }); { FontStyle::Small, TextAlignment::CENTRE });

View File

@ -403,35 +403,6 @@ namespace String
return buffer; return buffer;
} }
utf8* Duplicate(const std::string& src)
{
return String::Duplicate(src.c_str());
}
utf8* Duplicate(const utf8* src)
{
utf8* result = nullptr;
if (src != nullptr)
{
size_t srcSize = SizeOf(src) + 1;
result = Memory::Allocate<utf8>(srcSize);
std::memcpy(result, src, srcSize);
}
return result;
}
utf8* DiscardUse(utf8** ptr, utf8* replacement)
{
Memory::Free(*ptr);
*ptr = replacement;
return replacement;
}
utf8* DiscardDuplicate(utf8** ptr, const utf8* replacement)
{
return DiscardUse(ptr, String::Duplicate(replacement));
}
std::vector<std::string> Split(std::string_view s, std::string_view delimiter) std::vector<std::string> Split(std::string_view s, std::string_view delimiter)
{ {
if (delimiter.empty()) if (delimiter.empty())

View File

@ -82,18 +82,6 @@ namespace String
u8string StdFormat(const utf8* format, ...); u8string StdFormat(const utf8* format, ...);
u8string Format_VA(const utf8* format, va_list args); u8string Format_VA(const utf8* format, va_list args);
utf8* AppendFormat(utf8* buffer, size_t bufferSize, const utf8* format, ...); utf8* AppendFormat(utf8* buffer, size_t bufferSize, const utf8* format, ...);
utf8* Duplicate(const std::string& src);
utf8* Duplicate(const utf8* src);
/**
* Helper method to free the string a string pointer points to and set it to a replacement string.
*/
utf8* DiscardUse(utf8** ptr, utf8* replacement);
/**
* Helper method to free the string a string pointer points to and set it to a copy of a replacement string.
*/
utf8* DiscardDuplicate(utf8** ptr, const utf8* replacement);
/** /**
* Splits the given string by a delimiter and returns the values as a new string array. * Splits the given string by a delimiter and returns the values as a new string array.

View File

@ -117,8 +117,6 @@ static int32_t ScenarioIndexEntryCompareByIndex(const ScenarioIndexEntry& entryA
static void ScenarioHighscoreFree(ScenarioHighscoreEntry* highscore) static void ScenarioHighscoreFree(ScenarioHighscoreEntry* highscore)
{ {
SafeFree(highscore->fileName);
SafeFree(highscore->name);
SafeDelete(highscore); SafeDelete(highscore);
} }
@ -465,7 +463,7 @@ public:
// Check if record company value has been broken or the highscore is the same but no name is registered // Check if record company value has been broken or the highscore is the same but no name is registered
ScenarioHighscoreEntry* highscore = scenario->Highscore; ScenarioHighscoreEntry* highscore = scenario->Highscore;
if (highscore == nullptr || companyValue > highscore->company_value if (highscore == nullptr || companyValue > highscore->company_value
|| (String::IsNullOrEmpty(highscore->name) && companyValue == highscore->company_value)) || (highscore->name.empty() && companyValue == highscore->company_value))
{ {
if (highscore == nullptr) if (highscore == nullptr)
{ {
@ -475,15 +473,13 @@ public:
} }
else else
{ {
if (!String::IsNullOrEmpty(highscore->name)) if (!highscore->name.empty())
{ {
highscore->timestamp = Platform::GetDatetimeNowUTC(); highscore->timestamp = Platform::GetDatetimeNowUTC();
} }
SafeFree(highscore->fileName);
SafeFree(highscore->name);
} }
highscore->fileName = String::Duplicate(Path::GetFileName(scenario->Path)); highscore->fileName = Path::GetFileName(scenario->Path);
highscore->name = String::Duplicate(name); highscore->name = name != nullptr ? name : "";
highscore->company_value = companyValue; highscore->company_value = companyValue;
SaveHighscores(); SaveHighscores();
return true; return true;
@ -493,10 +489,19 @@ public:
} }
private: private:
ScenarioIndexEntry* GetByFilename(const utf8* filename) ScenarioIndexEntry* GetByFilename(u8string_view filename)
{ {
const ScenarioRepository* repo = this; for (auto& scenario : _scenarios)
return const_cast<ScenarioIndexEntry*>(repo->GetByFilename(filename)); {
const auto scenarioFilename = Path::GetFileName(scenario.Path);
// Note: this is always case insensitive search for cross platform consistency
if (String::Equals(filename, scenarioFilename, true))
{
return &scenario;
}
}
return nullptr;
} }
ScenarioIndexEntry* GetByPath(const utf8* path) ScenarioIndexEntry* GetByPath(const utf8* path)
@ -623,8 +628,8 @@ private:
for (uint32_t i = 0; i < numHighscores; i++) for (uint32_t i = 0; i < numHighscores; i++)
{ {
ScenarioHighscoreEntry* highscore = InsertHighscore(); ScenarioHighscoreEntry* highscore = InsertHighscore();
highscore->fileName = fs.ReadString(); highscore->fileName = fs.ReadStdString();
highscore->name = fs.ReadString(); highscore->name = fs.ReadStdString();
highscore->company_value = fileVersion == 1 ? fs.ReadValue<money32>() : fs.ReadValue<money64>(); highscore->company_value = fileVersion == 1 ? fs.ReadValue<money32>() : fs.ReadValue<money64>();
highscore->timestamp = fs.ReadValue<datetime64>(); highscore->timestamp = fs.ReadValue<datetime64>();
} }
@ -684,9 +689,8 @@ private:
// Check if legacy highscore is better // Check if legacy highscore is better
if (scBasic.CompanyValue > highscore->company_value) if (scBasic.CompanyValue > highscore->company_value)
{ {
SafeFree(highscore->name);
std::string name = RCT2StringToUTF8(scBasic.CompletedBy, RCT2LanguageId::EnglishUK); std::string name = RCT2StringToUTF8(scBasic.CompletedBy, RCT2LanguageId::EnglishUK);
highscore->name = String::Duplicate(name.c_str()); highscore->name = name;
highscore->company_value = scBasic.CompanyValue; highscore->company_value = scBasic.CompanyValue;
highscore->timestamp = DATETIME64_MIN; highscore->timestamp = DATETIME64_MIN;
break; break;
@ -696,9 +700,9 @@ private:
if (notFound) if (notFound)
{ {
ScenarioHighscoreEntry* highscore = InsertHighscore(); ScenarioHighscoreEntry* highscore = InsertHighscore();
highscore->fileName = String::Duplicate(scBasic.Path); highscore->fileName = scBasic.Path;
std::string name = RCT2StringToUTF8(scBasic.CompletedBy, RCT2LanguageId::EnglishUK); std::string name = RCT2StringToUTF8(scBasic.CompletedBy, RCT2LanguageId::EnglishUK);
highscore->name = String::Duplicate(name.c_str()); highscore->name = name;
highscore->company_value = scBasic.CompanyValue; highscore->company_value = scBasic.CompanyValue;
highscore->timestamp = DATETIME64_MIN; highscore->timestamp = DATETIME64_MIN;
} }
@ -728,7 +732,6 @@ private:
ScenarioHighscoreEntry* InsertHighscore() ScenarioHighscoreEntry* InsertHighscore()
{ {
auto highscore = new ScenarioHighscoreEntry(); auto highscore = new ScenarioHighscoreEntry();
std::memset(highscore, 0, sizeof(ScenarioHighscoreEntry));
_highscores.push_back(highscore); _highscores.push_back(highscore);
return highscore; return highscore;
} }

View File

@ -11,6 +11,7 @@
#pragma once #pragma once
#include "../common.h" #include "../common.h"
#include "../core/String.hpp"
#include "../scenario/Scenario.h" #include "../scenario/Scenario.h"
#include <memory> #include <memory>
@ -19,10 +20,10 @@ struct RCTObjectEntry;
struct ScenarioHighscoreEntry struct ScenarioHighscoreEntry
{ {
utf8* fileName; u8string fileName;
utf8* name; u8string name;
money64 company_value; money64 company_value{};
datetime64 timestamp; datetime64 timestamp{};
}; };
enum class ScenarioSource : uint8_t enum class ScenarioSource : uint8_t