Newer
Older
/*Copyright (c) 2019 Alexis Nasr && Franck Dary
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:i
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
/// \file util.hpp
/// \brief Contains a variety of useful functions that doesn't operate on custom classes.
/// \author Franck Dary
/// @version 2.0
/// @date 2019-12-10
#ifndef UTIL__H
#define UTIL__H
#include <string>
#include <vector>
#include <array>
Franck Dary
committed
#include <regex>
#include <boost/flyweight.hpp>
namespace util
{
void warning(std::string_view message, const std::experimental::source_location & location = std::experimental::source_location::current());
void error(std::string_view message, const std::experimental::source_location & location = std::experimental::source_location::current());
void error(const std::exception & e, const std::experimental::source_location & location = std::experimental::source_location::current());
void myThrow(std::string_view message, const std::experimental::source_location & location = std::experimental::source_location::current());
std::vector<std::filesystem::path> findFilesByExtension(std::filesystem::path directory, std::string extension);
std::string_view getFilenameFromPath(std::string_view s);
std::vector<std::string> split(std::string_view s, char delimiter);
utf8string splitAsUtf8(std::string_view s);
std::string int2HumanStr(int number);
std::string shrink(const std::string & s, int printedSize);
int printedLength(std::string_view s);
bool isSeparator(utf8char c);
bool isIllegal(utf8char c);
template <typename T>
bool isEmpty(const std::vector<T> & s)
{
return s.empty();
}
template <typename T>
bool isEmpty(const std::basic_string<T> & s)
template <typename T>
std::size_t getSize(const std::vector<T> & s)
{
return s.size();
}
template <typename T>
std::size_t getSize(const boost::flyweight<T> & s)
{
return getSize(s.get());
}
template <typename T>
bool isEmpty(const boost::flyweight<T> & s)
{
return isEmpty(s.get());
}
bool doIfNameMatch(const std::regex & reg, std::string_view name, const std::function<void(const std::smatch &)> & f);
Franck Dary
committed
template <>
struct fmt::formatter<std::experimental::source_location>
{
constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const std::experimental::source_location & d, FormatContext & ctx)
return format_to(ctx.out(), "{},l.{},'{}'", util::getFilenameFromPath(d.file_name()), d.line(), d.function_name());
}
};
template <typename T>
struct fmt::formatter<boost::flyweight<T>>
{
constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const boost::flyweight<T> & s, FormatContext & ctx)
{
return format_to(ctx.out(), "{}", s.get());
}
};