Skip to content
Permalink
Browse files

Common/IniFile: Make CaseInsensitiveStringCompare usable with heterog…

…enous lookup

Previously, when performing find() operations or indexing operations on
the section map, it would need to operate on a std::string key.

This means cases like:

map.find(some_string_view)

aren't usable, which kind of sucks, especially given for most cases, we
use regular string literals to perform operations in calling code.
However, since C++14, it's possible to use heterogenous lookup to avoid
needing to construct exact key types. In otherwords, we can perform the
above or use string literals without constructing a std::string instance
around them implicitly.

We simply need to specify a member type within our comparison struct
named is_transparent, to allow std::map to perform automatic type
deduction.

We also slightly alter the algorithm to an equivalent compatible with
std::string_view (which need not be null-terminated), as strcasecmp
requires null-terminated strings.

While we're at it, we can also provide a helper function to the struct
for comparing string equality rather than only less than. This allows
removing other usages of strcasecmp in other functions, allowing for the
transition of them to std::string_view.
  • Loading branch information...
lioncash committed Jun 16, 2019
1 parent b3525ad commit de7e9557dceb29de00b301a56df81a6ca419c772
Showing with 31 additions and 11 deletions.
  1. +10 −7 Source/Core/Common/IniFile.cpp
  2. +21 −4 Source/Core/Common/IniFile.h
@@ -5,16 +5,13 @@
#include "Common/IniFile.h"

#include <algorithm>
#include <cinttypes>
#include <cstddef>
#include <cstring>
#include <fstream>
#include <map>
#include <string>
#include <utility>
#include <vector>

#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/StringUtil.h"

@@ -128,16 +125,22 @@ IniFile::~IniFile() = default;
const IniFile::Section* IniFile::GetSection(const std::string& sectionName) const
{
for (const Section& sect : sections)
if (!strcasecmp(sect.name.c_str(), sectionName.c_str()))
return (&(sect));
{
if (CaseInsensitiveStringCompare::IsEqual(sect.name, sectionName))
return &sect;
}

return nullptr;
}

IniFile::Section* IniFile::GetSection(const std::string& sectionName)
{
for (Section& sect : sections)
if (!strcasecmp(sect.name.c_str(), sectionName.c_str()))
return (&(sect));
{
if (CaseInsensitiveStringCompare::IsEqual(sect.name, sectionName))
return &sect;
}

return nullptr;
}

@@ -4,21 +4,38 @@

#pragma once

#include <cstring>
#include <algorithm>
#include <cctype>
#include <list>
#include <map>
#include <string>
#include <string_view>
#include <vector>

#include "Common/CommonFuncs.h"
#include "Common/CommonTypes.h"
#include "Common/StringUtil.h"

struct CaseInsensitiveStringCompare
{
bool operator()(const std::string& a, const std::string& b) const
// Allow heterogenous lookup.
using is_transparent = void;

bool operator()(std::string_view a, std::string_view b) const
{
return std::lexicographical_compare(
a.begin(), a.end(), b.begin(), b.end(), [](char lhs, char rhs) {
return std::tolower(static_cast<u8>(lhs)) < std::tolower(static_cast<u8>(rhs));
});
}

static bool IsEqual(std::string_view a, std::string_view b)
{
return strcasecmp(a.c_str(), b.c_str()) < 0;
if (a.size() != b.size())
return false;

return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char lhs, char rhs) {
return std::tolower(static_cast<u8>(lhs)) == std::tolower(static_cast<u8>(rhs));
});
}
};

0 comments on commit de7e955

Please sign in to comment.
You can’t perform that action at this time.