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

namespace util
{

int printedLength(std::string_view s)
{
  return splitAsUtf8(s).size();
}

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

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

bool isIllegal(utf8char c)
{
  return c == '\n' || c == '\t';
}

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

  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;
}

utf8string 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;
}

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

void error(std::string_view message, const std::experimental::source_location & location)
{
  fmt::print(stderr, "ERROR ({}) : {}\n", location, message);
  exit(1);
}

void error(const std::exception & e, const std::experimental::source_location & location)
{
  error(e.what(), location);
}

void myThrow(std::string_view message, const std::experimental::source_location & location)
{
  throw std::invalid_argument(fmt::format("from ({}) {}", location, message));
}

std::string 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;
}

bool doIfNameMatch(const std::regex & reg, const std::string & name, const std::function<void(const std::smatch &)> & f)
{
  std::smatch sm;
  std::regex_match(name, sm, reg);
  if (sm.empty())
    return false;

  f(sm);

  return true;
};

Franck Dary's avatar
Franck Dary committed
};