Skip to content
Snippets Groups Projects
util.hpp 3.86 KiB
Newer Older
Franck Dary's avatar
Franck Dary committed
/*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>
Franck Dary's avatar
Franck Dary committed
#include <unordered_map>
Franck Dary's avatar
Franck Dary committed
#include <filesystem>
Franck Dary's avatar
Franck Dary committed
#include <experimental/source_location>
#include "fmt/core.h"
Franck Dary's avatar
Franck Dary committed
#include "utf8.hpp"
Franck Dary's avatar
Franck Dary committed
#include "utf8string.hpp"
Franck Dary's avatar
Franck Dary committed

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());

Franck Dary's avatar
Franck Dary committed
std::vector<std::filesystem::path> findFilesByExtension(std::filesystem::path directory, std::string extension);

Franck Dary's avatar
Franck Dary committed
std::string_view getFilenameFromPath(std::string_view s);

std::vector<std::string> split(std::string_view s, char delimiter);
Franck Dary's avatar
Franck Dary committed

utf8string splitAsUtf8(std::string_view s);

std::string int2HumanStr(int number);

std::string shrink(const std::string & s, int printedSize);

Franck Dary's avatar
Franck Dary committed
std::string strip(const std::string & s);

int printedLength(std::string_view s);

Franck Dary's avatar
Franck Dary committed
bool isSeparator(utf8char c);

bool isIllegal(utf8char c);

Franck Dary's avatar
Franck Dary committed
template <typename T>
bool isEmpty(const std::vector<T> & s)
{
  return s.empty();
}
Franck Dary's avatar
Franck Dary committed

Franck Dary's avatar
Franck Dary committed
template <typename T>
bool isEmpty(const std::basic_string<T> & s)
Franck Dary's avatar
Franck Dary committed
{
Franck Dary's avatar
Franck Dary committed
  return s.empty();
}

Franck Dary's avatar
Franck Dary committed
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());
}

Franck Dary's avatar
Franck Dary committed
template <typename T>
bool isEmpty(const boost::flyweight<T> & s)
{
  return isEmpty(s.get());
}
Franck Dary's avatar
Franck Dary committed

Franck Dary's avatar
Franck Dary committed
bool doIfNameMatch(const std::regex & reg, std::string_view name, const std::function<void(const std::smatch &)> & f);
Franck Dary's avatar
Franck Dary committed
};

Franck Dary's avatar
Franck Dary committed
struct fmt::formatter<std::experimental::source_location>
{
  constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }

  template <typename FormatContext>
Franck Dary's avatar
Franck Dary committed
  auto format(const std::experimental::source_location & d, FormatContext & ctx)
Franck Dary's avatar
Franck Dary committed
    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());
  }
};

Franck Dary's avatar
Franck Dary committed
#endif