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

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

std::vector<std::string_view> splitAsUtf8(std::string_view s)
{
  std::vector<std::string_view> result;
  const char * beginPtr = s.data();
  const char * currentPtr = beginPtr;
  const char * endPtr = s.data()+s.size()-1;

  while (true)
    try
    {
      utf8::next(currentPtr, endPtr);
      result.emplace_back(beginPtr, currentPtr-beginPtr);
      beginPtr = currentPtr;
    } catch (std::exception &) {break;}

  return result;
}

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

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_view operator+(std::string_view a, std::string_view b)
{
  if (b.data() <= a.data())
    util::myThrow(fmt::format("std::string_view a+b with b <= a"));

  return std::string_view(a.data(), b.data()-a.data()+b.size());
}

void operator+=(std::string_view & a, std::string_view b)
{
  a = a + b;
}