Skip to content
Snippets Groups Projects
util.cpp 5.3 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.*/

#include "util.hpp"
#include "utf8.hpp"
#include <ctime>
#include <algorithm>

Franck Dary's avatar
Franck Dary committed
int util::printedLength(std::string_view s)
{
  return splitAsUtf8(s).size();
}

Franck Dary's avatar
Franck Dary committed
std::string_view util::getFilenameFromPath(std::string_view s)
Franck Dary's avatar
Franck Dary committed
{
  int indexOfSlash = s.find_last_of('/');
  return {s.data()+indexOfSlash+1, s.size()-1-indexOfSlash};
}

Franck Dary's avatar
Franck Dary committed
bool util::isSeparator(utf8char c)
Franck Dary's avatar
Franck Dary committed
{
  return c == ' ' || isIllegal(c);
}

Franck Dary's avatar
Franck Dary committed
bool util::isIllegal(utf8char c)
Franck Dary's avatar
Franck Dary committed
{
  return c == '\n' || c == '\t';
}

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

  for (auto firstDelimiterIndex = remaining.find_first_of(delimiter); firstDelimiterIndex != std::string_view::npos; firstDelimiterIndex = remaining.find_first_of(delimiter))
  {
    if (remaining[0] != delimiter)
      result.emplace_back(remaining.data(), firstDelimiterIndex);
    remaining = std::string_view(remaining.data()+firstDelimiterIndex+1, remaining.size()-1-firstDelimiterIndex);
  }

  if (remaining.size() > 0)
    result.emplace_back(remaining);

  return result;
}

Franck Dary's avatar
Franck Dary committed
util::utf8string util::splitAsUtf8(std::string_view s)
{
  utf8string result;
  const char * beginPtr = s.data();
  const char * currentPtr = beginPtr;
Franck Dary's avatar
Franck Dary committed
  const char * endPtr = s.data()+s.size();
Franck Dary's avatar
Franck Dary committed
    try {utf8::next(currentPtr, endPtr);}
    catch (std::exception &)
    {
      break;
    }
    if (currentPtr - beginPtr > 4 || currentPtr - beginPtr == 0)
      myThrow(fmt::format("Invalid utf8 character at index {}", beginPtr-s.data()));
Franck Dary's avatar
Franck Dary committed
    utf8char c;
    for (int i = 0; i < currentPtr - beginPtr; i++)
      ((char*)&c)[i] = beginPtr[i];
    beginPtr = currentPtr;
    result.push_back(c);
  }

  return result;
}

std::string util::shrink(const std::string & s, int printedSize)
{
  static const std::string filler = "…";

  if (printedLength(s) <= printedSize)
    return s;

  auto splited = splitAsUtf8(s);

  std::string result;
  std::string begin, end;
  int nbLoop = 0;

  while (printedLength(begin)+printedLength(end)+printedLength(filler) <= printedSize)
  {
    result = begin + filler + end;

    if (nbLoop % 2)
      end = fmt::format("{}{}", splited[splited.size()-1-(nbLoop/2)], end);
    else
      begin = fmt::format("{}{}", begin, splited[nbLoop/2]);

    ++nbLoop;
  }

  return result;
}

Franck Dary's avatar
Franck Dary committed
void util::warning(std::string_view message, const std::experimental::source_location & location)
Franck Dary's avatar
Franck Dary committed
{
  fmt::print(stderr, "WARNING ({}) : {}\n", location, message);
}

Franck Dary's avatar
Franck Dary committed
void util::error(std::string_view message, const std::experimental::source_location & location)
Franck Dary's avatar
Franck Dary committed
{
  fmt::print(stderr, "ERROR ({}) : {}\n", location, message);
  exit(1);
}

Franck Dary's avatar
Franck Dary committed
void util::error(const std::exception & e, const std::experimental::source_location & location)
Franck Dary's avatar
Franck Dary committed
{
  error(e.what(), location);
}

Franck Dary's avatar
Franck Dary committed
void util::myThrow(std::string_view message, const std::experimental::source_location & location)
Franck Dary's avatar
Franck Dary committed
{
  throw std::invalid_argument(fmt::format("from ({}) {}", location, message));
}

Franck Dary's avatar
Franck Dary committed
std::string util::int2HumanStr(int number)
{
  std::string nb = std::to_string(number);
  std::string result;

  for (unsigned int i = 0; i < nb.size(); i++)
  {
    result.push_back(nb[i]);
    if (((nb.size()-i-1) % 3 == 0) && i < nb.size()-1)
      result.push_back(' ');
  }

  return result;
}

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

Franck Dary's avatar
Franck Dary committed

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

  if (s.empty())
    return striped;

  std::size_t first = 0;
  while (first < s.size() and (s[first] == ' ' or s[first] == '\t'))
    ++first;

  std::size_t last = s.size()-1;
  while (last > first and (s[last] == ' ' or s[last] == '\t'))
    --last;

  return std::string(s.begin()+first, s.begin()+last+1);
}

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

  for (auto entry : std::filesystem::directory_iterator(directory))
    if (entry.is_regular_file())
    {
      auto path = entry.path();
      if (path.extension() == extension)
        files.push_back(path);
    }

  return files;
}