summaryrefslogtreecommitdiff
path: root/Source/Core/Common/IniFile.cpp
diff options
context:
space:
mode:
authorLioncash <mathew1800@gmail.com>2019-06-16 17:28:00 -0400
committerLioncash <mathew1800@gmail.com>2019-06-16 18:20:08 -0400
commit78d171625186eb1018f8827d48e3cc3a9185bca2 (patch)
tree0ddbd372b7af7ebca85b5eea0aef63c8c05474c3 /Source/Core/Common/IniFile.cpp
parentde7e9557dceb29de00b301a56df81a6ca419c772 (diff)
Common/IniFile: Make use of std::string_view where applicable
Now that the std::map less-than comparitor is capable of being used with heterogenous lookup, we're able to convert many of the querying functions that took std::string references over to std::string_view. Now these functions may be used without potentially allocating a std::string instance unnecessarily.
Diffstat (limited to 'Source/Core/Common/IniFile.cpp')
-rw-r--r--Source/Core/Common/IniFile.cpp61
1 files changed, 33 insertions, 28 deletions
diff --git a/Source/Core/Common/IniFile.cpp b/Source/Core/Common/IniFile.cpp
index 74f43dc165..c0b4f2be28 100644
--- a/Source/Core/Common/IniFile.cpp
+++ b/Source/Core/Common/IniFile.cpp
@@ -51,32 +51,34 @@ void IniFile::Section::Set(const std::string& key, std::string new_value)
keys_order.push_back(key);
}
-bool IniFile::Section::Get(const std::string& key, std::string* value,
- const std::string& defaultValue) const
+bool IniFile::Section::Get(std::string_view key, std::string* value,
+ const std::string& default_value) const
{
- auto it = values.find(key);
+ const auto it = values.find(key);
+
if (it != values.end())
{
*value = it->second;
return true;
}
- else if (&defaultValue != &NULL_STRING)
+
+ if (&default_value != &NULL_STRING)
{
- *value = defaultValue;
+ *value = default_value;
return true;
}
return false;
}
-bool IniFile::Section::Exists(const std::string& key) const
+bool IniFile::Section::Exists(std::string_view key) const
{
return values.find(key) != values.end();
}
-bool IniFile::Section::Delete(const std::string& key)
+bool IniFile::Section::Delete(std::string_view key)
{
- auto it = values.find(key);
+ const auto it = values.find(key);
if (it == values.end())
return false;
@@ -122,44 +124,45 @@ IniFile::IniFile() = default;
IniFile::~IniFile() = default;
-const IniFile::Section* IniFile::GetSection(const std::string& sectionName) const
+const IniFile::Section* IniFile::GetSection(std::string_view section_name) const
{
for (const Section& sect : sections)
{
- if (CaseInsensitiveStringCompare::IsEqual(sect.name, sectionName))
+ if (CaseInsensitiveStringCompare::IsEqual(sect.name, section_name))
return &sect;
}
return nullptr;
}
-IniFile::Section* IniFile::GetSection(const std::string& sectionName)
+IniFile::Section* IniFile::GetSection(std::string_view section_name)
{
for (Section& sect : sections)
{
- if (CaseInsensitiveStringCompare::IsEqual(sect.name, sectionName))
+ if (CaseInsensitiveStringCompare::IsEqual(sect.name, section_name))
return &sect;
}
return nullptr;
}
-IniFile::Section* IniFile::GetOrCreateSection(const std::string& sectionName)
+IniFile::Section* IniFile::GetOrCreateSection(std::string_view section_name)
{
- Section* section = GetSection(sectionName);
+ Section* section = GetSection(section_name);
if (!section)
{
- sections.emplace_back(sectionName);
+ sections.emplace_back(std::string(section_name));
section = &sections.back();
}
return section;
}
-bool IniFile::DeleteSection(const std::string& sectionName)
+bool IniFile::DeleteSection(std::string_view section_name)
{
- Section* s = GetSection(sectionName);
+ Section* s = GetSection(section_name);
if (!s)
return false;
+
for (auto iter = sections.begin(); iter != sections.end(); ++iter)
{
if (&(*iter) == s)
@@ -168,41 +171,43 @@ bool IniFile::DeleteSection(const std::string& sectionName)
return true;
}
}
+
return false;
}
-bool IniFile::Exists(const std::string& sectionName, const std::string& key) const
+bool IniFile::Exists(std::string_view section_name, std::string_view key) const
{
- const Section* section = GetSection(sectionName);
+ const Section* section = GetSection(section_name);
if (!section)
return false;
+
return section->Exists(key);
}
-void IniFile::SetLines(const std::string& sectionName, const std::vector<std::string>& lines)
+void IniFile::SetLines(std::string_view section_name, const std::vector<std::string>& lines)
{
- Section* section = GetOrCreateSection(sectionName);
+ Section* section = GetOrCreateSection(section_name);
section->SetLines(lines);
}
-void IniFile::SetLines(const std::string& section_name, std::vector<std::string>&& lines)
+void IniFile::SetLines(std::string_view section_name, std::vector<std::string>&& lines)
{
Section* section = GetOrCreateSection(section_name);
section->SetLines(std::move(lines));
}
-bool IniFile::DeleteKey(const std::string& sectionName, const std::string& key)
+bool IniFile::DeleteKey(std::string_view section_name, std::string_view key)
{
- Section* section = GetSection(sectionName);
+ Section* section = GetSection(section_name);
if (!section)
return false;
return section->Delete(key);
}
// Return a list of all keys in a section
-bool IniFile::GetKeys(const std::string& sectionName, std::vector<std::string>* keys) const
+bool IniFile::GetKeys(std::string_view section_name, std::vector<std::string>* keys) const
{
- const Section* section = GetSection(sectionName);
+ const Section* section = GetSection(section_name);
if (!section)
{
return false;
@@ -212,12 +217,12 @@ bool IniFile::GetKeys(const std::string& sectionName, std::vector<std::string>*
}
// Return a list of all lines in a section
-bool IniFile::GetLines(const std::string& sectionName, std::vector<std::string>* lines,
+bool IniFile::GetLines(std::string_view section_name, std::vector<std::string>* lines,
const bool remove_comments) const
{
lines->clear();
- const Section* section = GetSection(sectionName);
+ const Section* section = GetSection(section_name);
if (!section)
return false;