2011-06-19 16:14:03 +00:00
|
|
|
#ifndef MISC_STRINGOPS_H
|
|
|
|
#define MISC_STRINGOPS_H
|
|
|
|
|
2020-01-09 16:42:06 +00:00
|
|
|
#include <cctype>
|
2012-12-23 19:23:24 +00:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
2021-09-04 16:07:23 +00:00
|
|
|
#include <string_view>
|
|
|
|
#include <iterator>
|
refactors a case insensitive map (#3184)
This PR aims to spark the retirement of a questionable pattern I have found all over our code base. I will illustrate how this pattern encourages code duplication, lacks type safety, requires documentation and can be prone to bugs.
```
std::map<std::string, Object> mMap; // Stored in all lowercase for a case-insensitive lookup
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.emplace(lowerKey, object);
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.find(lowerKey);
mMap.find(key); // Not found. Oops!
```
An alternative approach produces no such issues.
```
std::unordered_map<std::string, Object, Misc::StringUtils::CiHash, Misc::StringUtils::CiEqual> mMap;
mMap.emplace(key, object);
mMap.find(key);
```
Of course, such an alternative will work for a `map` as well, but an `unordered_map` is generally preferable over a `map` with these changes because we have moved `lowerCase` into the comparison operator.
In this PR I have refactored `Animation::mNodeMap` accordingly. I have reviewed and adapted all direct and indirect usage of `Animation::mNodeMap` to ensure we do not change behaviour with this PR.
2021-10-25 07:18:26 +00:00
|
|
|
#include <functional>
|
2012-12-23 19:23:24 +00:00
|
|
|
|
2011-06-19 16:14:03 +00:00
|
|
|
namespace Misc
|
|
|
|
{
|
2012-12-23 19:23:24 +00:00
|
|
|
class StringUtils
|
|
|
|
{
|
|
|
|
struct ci
|
|
|
|
{
|
2014-07-04 21:19:40 +00:00
|
|
|
bool operator()(char x, char y) const {
|
2015-12-07 20:58:30 +00:00
|
|
|
return toLower(x) < toLower(y);
|
2012-12-23 19:23:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-19 08:33:57 +00:00
|
|
|
// Allow to convert complex arguments to C-style strings for format() function
|
|
|
|
template <typename T>
|
|
|
|
static T argument(T value) noexcept
|
|
|
|
{
|
2021-10-06 18:59:48 +00:00
|
|
|
static_assert(!std::is_same_v<T, std::string_view>, "std::string_view is not supported");
|
2019-05-19 08:33:57 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static T const * argument(std::basic_string<T> const & value) noexcept
|
|
|
|
{
|
|
|
|
return value.c_str();
|
|
|
|
}
|
|
|
|
|
2012-12-23 19:23:24 +00:00
|
|
|
public:
|
2015-12-07 20:58:30 +00:00
|
|
|
|
|
|
|
/// Plain and simple locale-unaware toLower. Anything from A to Z is lower-cased, multibyte characters are unchanged.
|
|
|
|
/// Don't use std::tolower(char, locale&) because that is abysmally slow.
|
|
|
|
/// Don't use tolower(int) because that depends on global locale.
|
|
|
|
static char toLower(char c)
|
|
|
|
{
|
2019-02-27 21:24:43 +00:00
|
|
|
return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c;
|
2015-12-07 20:58:30 +00:00
|
|
|
}
|
|
|
|
|
2012-12-23 19:23:24 +00:00
|
|
|
static bool ciLess(const std::string &x, const std::string &y) {
|
|
|
|
return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), ci());
|
|
|
|
}
|
|
|
|
|
2021-09-04 16:07:23 +00:00
|
|
|
template <class X, class Y>
|
|
|
|
static bool ciEqual(const X& x, const Y& y)
|
|
|
|
{
|
|
|
|
if (std::size(x) != std::size(y))
|
2012-12-23 19:23:24 +00:00
|
|
|
return false;
|
2021-09-04 16:07:23 +00:00
|
|
|
return std::equal(std::begin(x), std::end(x), std::begin(y),
|
|
|
|
[] (char l, char r) { return toLower(l) == toLower(r); });
|
|
|
|
}
|
|
|
|
|
|
|
|
template <std::size_t n>
|
|
|
|
static auto ciEqual(const char(& x)[n], const char(& y)[n])
|
|
|
|
{
|
|
|
|
static_assert(n > 0);
|
|
|
|
return ciEqual(std::string_view(x, n - 1), std::string_view(y, n - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <std::size_t n, class T>
|
|
|
|
static auto ciEqual(const char(& x)[n], const T& y)
|
|
|
|
{
|
|
|
|
static_assert(n > 0);
|
|
|
|
return ciEqual(std::string_view(x, n - 1), y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <std::size_t n, class T>
|
|
|
|
static auto ciEqual(const T& x, const char(& y)[n])
|
|
|
|
{
|
|
|
|
static_assert(n > 0);
|
|
|
|
return ciEqual(x, std::string_view(y, n - 1));
|
2012-12-23 19:23:24 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 07:35:19 +00:00
|
|
|
static int ciCompareLen(const std::string &x, const std::string &y, size_t len)
|
|
|
|
{
|
|
|
|
std::string::const_iterator xit = x.begin();
|
|
|
|
std::string::const_iterator yit = y.begin();
|
|
|
|
for(;xit != x.end() && yit != y.end() && len > 0;++xit,++yit,--len)
|
|
|
|
{
|
2018-08-05 08:22:45 +00:00
|
|
|
char left = *xit;
|
|
|
|
char right = *yit;
|
|
|
|
if (left == right)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
left = toLower(left);
|
|
|
|
right = toLower(right);
|
|
|
|
int res = left - right;
|
|
|
|
if(res != 0)
|
2013-08-11 07:35:19 +00:00
|
|
|
return (res > 0) ? 1 : -1;
|
|
|
|
}
|
|
|
|
if(len > 0)
|
|
|
|
{
|
|
|
|
if(xit != x.end())
|
|
|
|
return 1;
|
|
|
|
if(yit != y.end())
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-23 19:23:24 +00:00
|
|
|
/// Transforms input string to lower case w/o copy
|
2015-12-07 21:49:15 +00:00
|
|
|
static void lowerCaseInPlace(std::string &inout) {
|
2014-07-04 21:19:40 +00:00
|
|
|
for (unsigned int i=0; i<inout.size(); ++i)
|
2015-12-07 20:58:30 +00:00
|
|
|
inout[i] = toLower(inout[i]);
|
2012-12-23 19:23:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns lower case copy of input string
|
2021-09-04 16:07:23 +00:00
|
|
|
static std::string lowerCase(std::string_view in)
|
2012-12-23 19:23:24 +00:00
|
|
|
{
|
2021-09-04 16:07:23 +00:00
|
|
|
std::string out(in);
|
2015-12-07 21:49:15 +00:00
|
|
|
lowerCaseInPlace(out);
|
2015-12-07 21:41:55 +00:00
|
|
|
return out;
|
2012-12-23 19:23:24 +00:00
|
|
|
}
|
2017-02-23 16:49:37 +00:00
|
|
|
|
refactors a case insensitive map (#3184)
This PR aims to spark the retirement of a questionable pattern I have found all over our code base. I will illustrate how this pattern encourages code duplication, lacks type safety, requires documentation and can be prone to bugs.
```
std::map<std::string, Object> mMap; // Stored in all lowercase for a case-insensitive lookup
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.emplace(lowerKey, object);
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.find(lowerKey);
mMap.find(key); // Not found. Oops!
```
An alternative approach produces no such issues.
```
std::unordered_map<std::string, Object, Misc::StringUtils::CiHash, Misc::StringUtils::CiEqual> mMap;
mMap.emplace(key, object);
mMap.find(key);
```
Of course, such an alternative will work for a `map` as well, but an `unordered_map` is generally preferable over a `map` with these changes because we have moved `lowerCase` into the comparison operator.
In this PR I have refactored `Animation::mNodeMap` accordingly. I have reviewed and adapted all direct and indirect usage of `Animation::mNodeMap` to ensure we do not change behaviour with this PR.
2021-10-25 07:18:26 +00:00
|
|
|
struct CiEqual
|
|
|
|
{
|
|
|
|
bool operator()(const std::string& left, const std::string& right) const
|
|
|
|
{
|
|
|
|
return ciEqual(left, right);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct CiHash
|
|
|
|
{
|
|
|
|
std::size_t operator()(std::string str) const
|
|
|
|
{
|
|
|
|
lowerCaseInPlace(str);
|
|
|
|
return std::hash<std::string>{}(str);
|
|
|
|
}
|
|
|
|
};
|
2017-02-23 16:49:37 +00:00
|
|
|
struct CiComp
|
|
|
|
{
|
|
|
|
bool operator()(const std::string& left, const std::string& right) const
|
|
|
|
{
|
|
|
|
return ciLess(left, right);
|
|
|
|
}
|
|
|
|
};
|
2017-05-07 21:28:56 +00:00
|
|
|
|
2017-11-07 21:16:59 +00:00
|
|
|
/** @brief Replaces all occurrences of a string in another string.
|
|
|
|
*
|
|
|
|
* @param str The string to operate on.
|
|
|
|
* @param what The string to replace.
|
|
|
|
* @param with The replacement string.
|
|
|
|
* @return A reference to the string passed in @p str.
|
|
|
|
*/
|
2021-11-05 09:53:52 +00:00
|
|
|
static std::string &replaceAll(std::string &str, std::string_view what, std::string_view with)
|
2017-11-07 21:16:59 +00:00
|
|
|
{
|
|
|
|
std::size_t found;
|
|
|
|
std::size_t offset = 0;
|
2021-11-05 09:53:52 +00:00
|
|
|
while((found = str.find(what, offset)) != std::string::npos)
|
2017-11-07 21:16:59 +00:00
|
|
|
{
|
2021-11-05 09:53:52 +00:00
|
|
|
str.replace(found, what.size(), with);
|
|
|
|
offset = found + with.size();
|
2017-11-07 21:16:59 +00:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
2019-02-22 21:14:07 +00:00
|
|
|
|
2019-05-19 08:33:57 +00:00
|
|
|
// Requires some C++11 features:
|
|
|
|
// 1. std::string needs to be contiguous
|
|
|
|
// 2. std::snprintf with zero size (second argument) returns an output string size
|
|
|
|
// 3. variadic templates support
|
|
|
|
template <typename ... Args>
|
|
|
|
static std::string format(const char* fmt, Args const & ... args)
|
2019-02-22 21:14:07 +00:00
|
|
|
{
|
2019-05-19 08:33:57 +00:00
|
|
|
auto size = std::snprintf(nullptr, 0, fmt, argument(args) ...);
|
|
|
|
// Note: sprintf also writes a trailing null character. We should remove it.
|
|
|
|
std::string ret(size+1, '\0');
|
|
|
|
std::sprintf(&ret[0], fmt, argument(args) ...);
|
|
|
|
ret.erase(size);
|
2019-02-22 21:14:07 +00:00
|
|
|
|
2019-05-19 08:33:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2019-02-22 21:14:07 +00:00
|
|
|
|
2019-05-19 08:33:57 +00:00
|
|
|
template <typename ... Args>
|
|
|
|
static std::string format(const std::string& fmt, Args const & ... args)
|
|
|
|
{
|
|
|
|
return format(fmt.c_str(), args ...);
|
2019-02-22 21:14:07 +00:00
|
|
|
}
|
2020-01-09 11:57:05 +00:00
|
|
|
|
|
|
|
static inline void trim(std::string &s)
|
|
|
|
{
|
2021-11-10 21:53:25 +00:00
|
|
|
const auto notSpace = [](char ch)
|
2020-01-09 11:57:05 +00:00
|
|
|
{
|
2021-11-10 20:25:16 +00:00
|
|
|
// TODO Do we care about multibyte whitespace?
|
2021-11-11 17:14:10 +00:00
|
|
|
return !std::isspace(static_cast<unsigned char>(ch));
|
2021-11-10 20:25:16 +00:00
|
|
|
};
|
|
|
|
// left trim
|
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), notSpace));
|
2020-01-09 11:57:05 +00:00
|
|
|
|
|
|
|
// right trim
|
2021-11-10 20:25:16 +00:00
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), notSpace).base(), s.end());
|
2020-01-09 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Container>
|
|
|
|
static inline void split(const std::string& str, Container& cont, const std::string& delims = " ")
|
|
|
|
{
|
|
|
|
std::size_t current, previous = 0;
|
|
|
|
current = str.find_first_of(delims);
|
|
|
|
while (current != std::string::npos)
|
|
|
|
{
|
|
|
|
cont.push_back(str.substr(previous, current - previous));
|
|
|
|
previous = current + 1;
|
|
|
|
current = str.find_first_of(delims, previous);
|
|
|
|
}
|
|
|
|
cont.push_back(str.substr(previous, current - previous));
|
|
|
|
}
|
|
|
|
|
2021-10-06 18:59:48 +00:00
|
|
|
static inline void replaceLast(std::string& str, const std::string& substr, const std::string& with)
|
|
|
|
{
|
|
|
|
size_t pos = str.rfind(substr);
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
return;
|
|
|
|
str.replace(pos, substr.size(), with);
|
|
|
|
}
|
2020-01-09 11:57:05 +00:00
|
|
|
|
2021-10-06 18:59:48 +00:00
|
|
|
static inline bool ciEndsWith(std::string_view s, std::string_view suffix)
|
|
|
|
{
|
|
|
|
return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin(),
|
|
|
|
[](char l, char r) { return toLower(l) == toLower(r); });
|
|
|
|
};
|
2012-12-23 19:23:24 +00:00
|
|
|
};
|
|
|
|
|
2011-06-19 16:14:03 +00:00
|
|
|
}
|
|
|
|
|
2010-01-04 13:48:18 +00:00
|
|
|
#endif
|