Codechange: Use string_view in IniItem/IniGroup/IniFile. (#12535)

This avoids making extra copies of strings.
This commit is contained in:
Peter Nelson 2024-04-19 13:54:22 +01:00 committed by GitHub
parent 3b80a8255f
commit 6ee31a2a22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 27 deletions

View File

@ -20,7 +20,7 @@
* @param parent the group we belong to * @param parent the group we belong to
* @param name the name of the item * @param name the name of the item
*/ */
IniItem::IniItem(const std::string &name) IniItem::IniItem(std::string_view name)
{ {
this->name = StrMakeValid(name); this->name = StrMakeValid(name);
} }
@ -29,7 +29,7 @@ IniItem::IniItem(const std::string &name)
* Replace the current value with another value. * Replace the current value with another value.
* @param value the value to replace with. * @param value the value to replace with.
*/ */
void IniItem::SetValue(const std::string_view value) void IniItem::SetValue(std::string_view value)
{ {
this->value.emplace(value); this->value.emplace(value);
} }
@ -39,7 +39,7 @@ void IniItem::SetValue(const std::string_view value)
* @param parent the file we belong to * @param parent the file we belong to
* @param name the name of the group * @param name the name of the group
*/ */
IniGroup::IniGroup(const std::string &name, IniGroupType type) : type(type), comment("\n") IniGroup::IniGroup(std::string_view name, IniGroupType type) : type(type), comment("\n")
{ {
this->name = StrMakeValid(name); this->name = StrMakeValid(name);
} }
@ -49,7 +49,7 @@ IniGroup::IniGroup(const std::string &name, IniGroupType type) : type(type), com
* @param name name of the item to find. * @param name name of the item to find.
* @return the requested item or nullptr if not found. * @return the requested item or nullptr if not found.
*/ */
const IniItem *IniGroup::GetItem(const std::string &name) const const IniItem *IniGroup::GetItem(std::string_view name) const
{ {
for (const IniItem &item : this->items) { for (const IniItem &item : this->items) {
if (item.name == name) return &item; if (item.name == name) return &item;
@ -63,7 +63,7 @@ const IniItem *IniGroup::GetItem(const std::string &name) const
* @param name name of the item to find. * @param name name of the item to find.
* @return the requested item. * @return the requested item.
*/ */
IniItem &IniGroup::GetOrCreateItem(const std::string &name) IniItem &IniGroup::GetOrCreateItem(std::string_view name)
{ {
for (IniItem &item : this->items) { for (IniItem &item : this->items) {
if (item.name == name) return item; if (item.name == name) return item;
@ -78,7 +78,7 @@ IniItem &IniGroup::GetOrCreateItem(const std::string &name)
* @param name name of the item to create. * @param name name of the item to create.
* @return the created item. * @return the created item.
*/ */
IniItem &IniGroup::CreateItem(const std::string &name) IniItem &IniGroup::CreateItem(std::string_view name)
{ {
return this->items.emplace_back(name); return this->items.emplace_back(name);
} }
@ -87,7 +87,7 @@ IniItem &IniGroup::CreateItem(const std::string &name)
* Remove the item with the given name. * Remove the item with the given name.
* @param name Name of the item to remove. * @param name Name of the item to remove.
*/ */
void IniGroup::RemoveItem(const std::string &name) void IniGroup::RemoveItem(std::string_view name)
{ {
this->items.remove_if([&name](const IniItem &item) { return item.name == name; }); this->items.remove_if([&name](const IniItem &item) { return item.name == name; });
} }
@ -116,7 +116,7 @@ IniLoadFile::IniLoadFile(const IniGroupNameList &list_group_names, const IniGrou
* @param name name of the group to find. * @param name name of the group to find.
* @return The requested group or \c nullptr if not found. * @return The requested group or \c nullptr if not found.
*/ */
const IniGroup *IniLoadFile::GetGroup(const std::string &name) const const IniGroup *IniLoadFile::GetGroup(std::string_view name) const
{ {
for (const IniGroup &group : this->groups) { for (const IniGroup &group : this->groups) {
if (group.name == name) return &group; if (group.name == name) return &group;
@ -130,7 +130,7 @@ const IniGroup *IniLoadFile::GetGroup(const std::string &name) const
* @param name name of the group to find. * @param name name of the group to find.
* @return The requested group or \c nullptr if not found. * @return The requested group or \c nullptr if not found.
*/ */
IniGroup *IniLoadFile::GetGroup(const std::string &name) IniGroup *IniLoadFile::GetGroup(std::string_view name)
{ {
for (IniGroup &group : this->groups) { for (IniGroup &group : this->groups) {
if (group.name == name) return &group; if (group.name == name) return &group;
@ -144,7 +144,7 @@ IniGroup *IniLoadFile::GetGroup(const std::string &name)
* @param name name of the group to find. * @param name name of the group to find.
* @return the requested group. * @return the requested group.
*/ */
IniGroup &IniLoadFile::GetOrCreateGroup(const std::string &name) IniGroup &IniLoadFile::GetOrCreateGroup(std::string_view name)
{ {
for (IniGroup &group : this->groups) { for (IniGroup &group : this->groups) {
if (group.name == name) return group; if (group.name == name) return group;
@ -159,7 +159,7 @@ IniGroup &IniLoadFile::GetOrCreateGroup(const std::string &name)
* @param name name of the group to create. * @param name name of the group to create.
* @return the created group. * @return the created group.
*/ */
IniGroup &IniLoadFile::CreateGroup(const std::string &name) IniGroup &IniLoadFile::CreateGroup(std::string_view name)
{ {
IniGroupType type = IGT_VARIABLES; IniGroupType type = IGT_VARIABLES;
if (std::find(this->list_group_names.begin(), this->list_group_names.end(), name) != this->list_group_names.end()) type = IGT_LIST; if (std::find(this->list_group_names.begin(), this->list_group_names.end(), name) != this->list_group_names.end()) type = IGT_LIST;
@ -172,7 +172,7 @@ IniGroup &IniLoadFile::CreateGroup(const std::string &name)
* Remove the group with the given name. * Remove the group with the given name.
* @param name name of the group to remove. * @param name name of the group to remove.
*/ */
void IniLoadFile::RemoveGroup(const std::string &name) void IniLoadFile::RemoveGroup(std::string_view name)
{ {
size_t len = name.length(); size_t len = name.length();
this->groups.remove_if([&name, &len](const IniGroup &group) { return group.name.compare(0, len, name) == 0; }); this->groups.remove_if([&name, &len](const IniGroup &group) { return group.name.compare(0, len, name) == 0; });
@ -237,7 +237,7 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
e--; e--;
} }
s++; // skip [ s++; // skip [
group = &this->CreateGroup(std::string(s, e - s)); group = &this->CreateGroup(std::string_view(s, e - s));
if (comment_size != 0) { if (comment_size != 0) {
group->comment.assign(comment, comment_size); group->comment.assign(comment, comment_size);
comment_size = 0; comment_size = 0;
@ -245,7 +245,7 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
} else if (group != nullptr) { } else if (group != nullptr) {
if (group->type == IGT_SEQUENCE) { if (group->type == IGT_SEQUENCE) {
/* A sequence group, use the line as item name without further interpretation. */ /* A sequence group, use the line as item name without further interpretation. */
IniItem &item = group->CreateItem(std::string(buffer, e - buffer)); IniItem &item = group->CreateItem(std::string_view(buffer, e - buffer));
if (comment_size) { if (comment_size) {
item.comment.assign(comment, comment_size); item.comment.assign(comment, comment_size);
comment_size = 0; comment_size = 0;
@ -263,7 +263,7 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
} }
/* it's an item in an existing group */ /* it's an item in an existing group */
IniItem &item = group->CreateItem(std::string(s, t - s)); IniItem &item = group->CreateItem(std::string_view(s, t - s));
if (comment_size != 0) { if (comment_size != 0) {
item.comment.assign(comment, comment_size); item.comment.assign(comment, comment_size);
comment_size = 0; comment_size = 0;

View File

@ -25,9 +25,9 @@ struct IniItem {
std::optional<std::string> value; ///< The value of this item std::optional<std::string> value; ///< The value of this item
std::string comment; ///< The comment associated with this item std::string comment; ///< The comment associated with this item
IniItem(const std::string &name); IniItem(std::string_view name);
void SetValue(const std::string_view value); void SetValue(std::string_view value);
}; };
/** A group within an ini file. */ /** A group within an ini file. */
@ -37,12 +37,12 @@ struct IniGroup {
std::string name; ///< name of group std::string name; ///< name of group
std::string comment; ///< comment for group std::string comment; ///< comment for group
IniGroup(const std::string &name, IniGroupType type); IniGroup(std::string_view name, IniGroupType type);
const IniItem *GetItem(const std::string &name) const; const IniItem *GetItem(std::string_view name) const;
IniItem &GetOrCreateItem(const std::string &name); IniItem &GetOrCreateItem(std::string_view name);
IniItem &CreateItem(const std::string &name); IniItem &CreateItem(std::string_view name);
void RemoveItem(const std::string &name); void RemoveItem(std::string_view name);
void Clear(); void Clear();
}; };
@ -58,11 +58,11 @@ struct IniLoadFile {
IniLoadFile(const IniGroupNameList &list_group_names = {}, const IniGroupNameList &seq_group_names = {}); IniLoadFile(const IniGroupNameList &list_group_names = {}, const IniGroupNameList &seq_group_names = {});
virtual ~IniLoadFile() { } virtual ~IniLoadFile() { }
const IniGroup *GetGroup(const std::string &name) const; const IniGroup *GetGroup(std::string_view name) const;
IniGroup *GetGroup(const std::string &name); IniGroup *GetGroup(std::string_view name);
IniGroup &GetOrCreateGroup(const std::string &name); IniGroup &GetOrCreateGroup(std::string_view name);
IniGroup &CreateGroup(const std::string &name); IniGroup &CreateGroup(std::string_view name);
void RemoveGroup(const std::string &name); void RemoveGroup(std::string_view name);
void LoadFromDisk(const std::string &filename, Subdirectory subdir); void LoadFromDisk(const std::string &filename, Subdirectory subdir);