Selaa lähdekoodia

Subdivide the `util.hpp` header

default_compile_flags
vector-of-bool 5 vuotta sitten
vanhempi
commit
9478c657ba
26 muutettua tiedostoa jossa 218 lisäystä ja 180 poistoa
  1. +3
    -0
      src/dds/build.cpp
  2. +1
    -1
      src/dds/build.hpp
  3. +1
    -0
      src/dds/compile.cpp
  4. +1
    -1
      src/dds/compile.hpp
  5. +1
    -1
      src/dds/ddslim.main.cpp
  6. +2
    -0
      src/dds/library.cpp
  7. +3
    -1
      src/dds/library_manifest.hpp
  8. +1
    -1
      src/dds/package_manifest.hpp
  9. +1
    -1
      src/dds/proc.common.cpp
  10. +1
    -0
      src/dds/project.cpp
  11. +1
    -1
      src/dds/project.hpp
  12. +2
    -0
      src/dds/sdist.cpp
  13. +1
    -1
      src/dds/sdist.hpp
  14. +3
    -0
      src/dds/source.cpp
  15. +2
    -1
      src/dds/source.hpp
  16. +1
    -1
      src/dds/temp.hpp
  17. +3
    -0
      src/dds/toolchain.cpp
  18. +1
    -1
      src/dds/toolchain.hpp
  19. +0
    -162
      src/dds/util.hpp
  20. +24
    -0
      src/dds/util/algo.hpp
  21. +1
    -5
      src/dds/util/fs.cpp
  22. +41
    -0
      src/dds/util/fs.hpp
  23. +78
    -0
      src/dds/util/string.hpp
  24. +2
    -1
      src/dds/util/string.test.cpp
  25. +41
    -0
      src/dds/util/tl.hpp
  26. +2
    -1
      src/libman/util.hpp

+ 3
- 0
src/dds/build.cpp Näytä tiedosto

@@ -6,6 +6,9 @@
#include <dds/project.hpp>
#include <dds/source.hpp>
#include <dds/toolchain.hpp>
#include <dds/util/algo.hpp>
#include <dds/util/string.hpp>
#include <dds/util/tl.hpp>
#include <libman/index.hpp>
#include <libman/parse.hpp>


+ 1
- 1
src/dds/build.hpp Näytä tiedosto

@@ -3,7 +3,7 @@

#include <dds/package_manifest.hpp>
#include <dds/toolchain.hpp>
#include <dds/util.hpp>
#include <dds/util/fs.hpp>

#include <optional>


+ 1
- 0
src/dds/compile.cpp Näytä tiedosto

@@ -1,6 +1,7 @@
#include "./compile.hpp"

#include <dds/proc.hpp>
#include <dds/util/algo.hpp>

#include <spdlog/spdlog.h>


+ 1
- 1
src/dds/compile.hpp Näytä tiedosto

@@ -2,7 +2,7 @@

#include <dds/source.hpp>
#include <dds/toolchain.hpp>
#include <dds/util.hpp>
#include <dds/util/fs.hpp>

#include <memory>
#include <optional>

+ 1
- 1
src/dds/ddslim.main.cpp Näytä tiedosto

@@ -1,7 +1,7 @@
#include <dds/build.hpp>
#include <dds/logging.hpp>
#include <dds/sdist.hpp>
#include <dds/util.hpp>
#include <dds/util/fs.hpp>

#include <libman/parse.hpp>


+ 2
- 0
src/dds/library.cpp Näytä tiedosto

@@ -1,5 +1,7 @@
#include <dds/library.hpp>

#include <dds/util/algo.hpp>

#include <spdlog/spdlog.h>

using namespace dds;

+ 3
- 1
src/dds/library_manifest.hpp Näytä tiedosto

@@ -1,6 +1,8 @@
#pragma once

#include <dds/util.hpp>
#include <dds/util/fs.hpp>

#include <vector>

namespace dds {


+ 1
- 1
src/dds/package_manifest.hpp Näytä tiedosto

@@ -1,6 +1,6 @@
#pragma once

#include <dds/util.hpp>
#include <dds/util/fs.hpp>

#include <string>


+ 1
- 1
src/dds/proc.common.cpp Näytä tiedosto

@@ -1,6 +1,6 @@
#include "./proc.hpp"

#include <dds/util.hpp>
#include <dds/util/string.hpp>

#include <algorithm>
#include <array>

+ 1
- 0
src/dds/project.cpp Näytä tiedosto

@@ -1,6 +1,7 @@
#include <dds/project.hpp>

#include <dds/source.hpp>
#include <dds/util/tl.hpp>

#include <range/v3/range/conversion.hpp>
#include <range/v3/view/filter.hpp>

+ 1
- 1
src/dds/project.hpp Näytä tiedosto

@@ -2,7 +2,7 @@

#include <dds/library.hpp>
#include <dds/package_manifest.hpp>
#include <dds/util.hpp>
#include <dds/util/fs.hpp>

#include <optional>
#include <vector>

+ 2
- 0
src/dds/sdist.cpp Näytä tiedosto

@@ -2,6 +2,8 @@

#include <dds/project.hpp>
#include <dds/temp.hpp>
#include <dds/util/fs.hpp>
#include <dds/util/tl.hpp>

#include <libman/parse.hpp>


+ 1
- 1
src/dds/sdist.hpp Näytä tiedosto

@@ -1,6 +1,6 @@
#pragma once

#include <dds/util.hpp>
#include <dds/util/fs.hpp>

namespace dds {


+ 3
- 0
src/dds/source.cpp Näytä tiedosto

@@ -1,5 +1,8 @@
#include "./source.hpp"

#include <dds/util/tl.hpp>
#include <dds/util/string.hpp>

#include <spdlog/spdlog.h>

#include <range/v3/range/conversion.hpp>

+ 2
- 1
src/dds/source.hpp Näytä tiedosto

@@ -1,6 +1,7 @@
#pragma once

#include <dds/util.hpp>
#include <dds/util/fs.hpp>

#include <optional>
#include <vector>


+ 1
- 1
src/dds/temp.hpp Näytä tiedosto

@@ -1,6 +1,6 @@
#pragma once

#include <dds/util.hpp>
#include <dds/util/fs.hpp>

#include <memory>


+ 3
- 0
src/dds/toolchain.cpp Näytä tiedosto

@@ -1,5 +1,8 @@
#include "./toolchain.hpp"

#include <dds/util/algo.hpp>
#include <dds/util/string.hpp>

#include <libman/parse.hpp>

#include <spdlog/fmt/fmt.h>

+ 1
- 1
src/dds/toolchain.hpp Näytä tiedosto

@@ -1,7 +1,7 @@
#ifndef DDS_TOOLCHAIN_HPP_INCLUDED
#define DDS_TOOLCHAIN_HPP_INCLUDED

#include <dds/util.hpp>
#include <dds/util/fs.hpp>

#include <optional>
#include <string>

+ 0
- 162
src/dds/util.hpp Näytä tiedosto

@@ -1,162 +0,0 @@
#ifndef DDS_UTIL_HPP_INCLUDED
#define DDS_UTIL_HPP_INCLUDED

#include <algorithm>
#include <cctype>
#include <filesystem>
#include <fstream>

namespace dds {

namespace detail {

template <std::size_t I,
typename A = std::nullptr_t,
typename B = std::nullptr_t,
typename C = std::nullptr_t,
typename D = std::nullptr_t>
decltype(auto) nth_arg(A&& a = nullptr, B&& b = nullptr, C&& c = nullptr, D&& d = nullptr) {
if constexpr (I == 0) {
return (A &&) a;
} else if constexpr (I == 1) {
return (B &&) b;
} else if constexpr (I == 2) {
return (C &&) c;
} else if constexpr (I == 3) {
return (D &&) d;
}
}

} // namespace detail

// Based on https://github.com/Quincunx271/TerseLambda
#define DDS_CTL(...) \
(auto&&... _args_)->auto { \
[[maybe_unused]] auto&& _1 = ::dds::detail::nth_arg<0>((decltype(_args_)&&)(_args_)...); \
[[maybe_unused]] auto&& _2 = ::dds::detail::nth_arg<1>((decltype(_args_)&&)(_args_)...); \
[[maybe_unused]] auto&& _3 = ::dds::detail::nth_arg<2>((decltype(_args_)&&)(_args_)...); \
[[maybe_unused]] auto&& _4 = ::dds::detail::nth_arg<3>((decltype(_args_)&&)(_args_)...); \
static_assert(sizeof...(_args_) <= 4); \
return (__VA_ARGS__); \
}

#define DDS_TL [&] DDS_CTL

inline namespace file_utils {

namespace fs = std::filesystem;

using path_ref = const fs::path&;

std::fstream open(const fs::path& filepath, std::ios::openmode mode, std::error_code& ec);
std::string slurp_file(const fs::path& path, std::error_code& ec);

inline std::fstream open(const fs::path& filepath, std::ios::openmode mode) {
std::error_code ec;
auto ret = dds::open(filepath, mode, ec);
if (ec) {
throw std::system_error{ec, "Error opening file: " + filepath.string()};
}
return ret;
}

inline std::string slurp_file(const fs::path& path) {
std::error_code ec;
auto contents = dds::slurp_file(path, ec);
if (ec) {
throw std::system_error{ec, "Reading file: " + path.string()};
}
return contents;
}

void safe_rename(path_ref source, path_ref dest);

} // namespace file_utils

template <typename Container, typename Predicate>
void erase_if(Container& c, Predicate&& p) {
auto erase_point = std::remove_if(c.begin(), c.end(), p);
c.erase(erase_point, c.end());
}

template <typename Container, typename Other>
void extend(Container& c, const Other& o) {
c.insert(c.end(), o.begin(), o.end());
}

template <typename Container, typename Item>
void extend(Container& c, std::initializer_list<Item> il) {
c.insert(c.end(), il.begin(), il.end());
}

inline namespace string_utils {

inline std::string_view sview(std::string_view::const_iterator beg,
std::string_view::const_iterator end) {
return std::string_view(&*beg, static_cast<std::size_t>(std::distance(beg, end)));
}

inline std::string_view trim(std::string_view s) {
auto iter = s.begin();
auto end = s.end();
while (iter != end && std::isspace(*iter)) {
++iter;
}
auto riter = s.rbegin();
auto rend = s.rend();
while (riter != rend && std::isspace(*riter)) {
++riter;
}
auto new_end = riter.base();
return sview(iter, new_end);
}

inline std::string trim(std::string&& s) { return std::string(trim(s)); }

inline bool ends_with(std::string_view s, std::string_view key) {
auto found = s.rfind(key);
return found != s.npos && found == s.size() - key.size();
}

inline bool starts_with(std::string_view s, std::string_view key) { return s.find(key) == 0; }

inline bool contains(std::string_view s, std::string_view key) { return s.find(key) != s.npos; }

inline std::vector<std::string> split(std::string_view str, std::string_view sep) {
std::vector<std::string> ret;
std::string_view::size_type prev_pos = 0;
auto pos = prev_pos;
while ((pos = str.find(sep, prev_pos)) != str.npos) {
ret.emplace_back(str.substr(prev_pos, pos - prev_pos));
prev_pos = pos + sep.length();
}
ret.emplace_back(str.substr(prev_pos));
return ret;
}

inline std::string replace(std::string_view str, std::string_view key, std::string_view repl) {
std::string ret;
std::string_view::size_type pos = 0;
std::string_view::size_type prev_pos = 0;
while (pos = str.find(key, pos), pos != key.npos) {
ret.append(str.begin() + prev_pos, str.begin() + pos);
ret.append(repl);
prev_pos = pos += key.size();
}
ret.append(str.begin() + prev_pos, str.end());
return ret;
}

inline std::vector<std::string>
replace(std::vector<std::string> strings, std::string_view key, std::string_view repl) {
for (auto& item : strings) {
item = replace(item, key, repl);
}
return strings;
}

} // namespace string_utils

} // namespace dds

#endif // DDS_UTIL_HPP_INCLUDED

+ 24
- 0
src/dds/util/algo.hpp Näytä tiedosto

@@ -0,0 +1,24 @@
#pragma once

#include <algorithm>
#include <initializer_list>

namespace dds {

template <typename Container, typename Predicate>
void erase_if(Container& c, Predicate&& p) {
auto erase_point = std::remove_if(c.begin(), c.end(), p);
c.erase(erase_point, c.end());
}

template <typename Container, typename Other>
void extend(Container& c, const Other& o) {
c.insert(c.end(), o.begin(), o.end());
}

template <typename Container, typename Item>
void extend(Container& c, std::initializer_list<Item> il) {
c.insert(c.end(), il.begin(), il.end());
}

} // namespace dds

src/dds/util.cpp → src/dds/util/fs.cpp Näytä tiedosto

@@ -1,11 +1,7 @@
#include "./util.hpp"
#include "./fs.hpp"

#include <spdlog/fmt/fmt.h>

#include <filesystem>
#include <fstream>
#include <sstream>

using namespace dds;

std::fstream dds::open(const fs::path& filepath, std::ios::openmode mode, std::error_code& ec) {

+ 41
- 0
src/dds/util/fs.hpp Näytä tiedosto

@@ -0,0 +1,41 @@
#pragma once

#include <filesystem>
#include <fstream>
#include <string>
#include <system_error>

namespace dds {

inline namespace file_utils {

namespace fs = std::filesystem;

using path_ref = const fs::path&;

std::fstream open(const fs::path& filepath, std::ios::openmode mode, std::error_code& ec);
std::string slurp_file(const fs::path& path, std::error_code& ec);

inline std::fstream open(const fs::path& filepath, std::ios::openmode mode) {
std::error_code ec;
auto ret = dds::open(filepath, mode, ec);
if (ec) {
throw std::system_error{ec, "Error opening file: " + filepath.string()};
}
return ret;
}

inline std::string slurp_file(const fs::path& path) {
std::error_code ec;
auto contents = dds::slurp_file(path, ec);
if (ec) {
throw std::system_error{ec, "Reading file: " + path.string()};
}
return contents;
}

void safe_rename(path_ref source, path_ref dest);

} // namespace file_utils

} // namespace dds

+ 78
- 0
src/dds/util/string.hpp Näytä tiedosto

@@ -0,0 +1,78 @@
#pragma once

#include <algorithm>
#include <string>
#include <string_view>
#include <vector>

namespace dds {

inline namespace string_utils {

inline std::string_view sview(std::string_view::const_iterator beg,
std::string_view::const_iterator end) {
return std::string_view(&*beg, static_cast<std::size_t>(std::distance(beg, end)));
}

inline std::string_view trim(std::string_view s) {
auto iter = s.begin();
auto end = s.end();
while (iter != end && std::isspace(*iter)) {
++iter;
}
auto riter = s.rbegin();
auto rend = s.rend();
while (riter != rend && std::isspace(*riter)) {
++riter;
}
auto new_end = riter.base();
return sview(iter, new_end);
}

inline std::string trim(std::string&& s) { return std::string(trim(s)); }

inline bool ends_with(std::string_view s, std::string_view key) {
auto found = s.rfind(key);
return found != s.npos && found == s.size() - key.size();
}

inline bool starts_with(std::string_view s, std::string_view key) { return s.find(key) == 0; }

inline bool contains(std::string_view s, std::string_view key) { return s.find(key) != s.npos; }

inline std::vector<std::string> split(std::string_view str, std::string_view sep) {
std::vector<std::string> ret;
std::string_view::size_type prev_pos = 0;
auto pos = prev_pos;
while ((pos = str.find(sep, prev_pos)) != str.npos) {
ret.emplace_back(str.substr(prev_pos, pos - prev_pos));
prev_pos = pos + sep.length();
}
ret.emplace_back(str.substr(prev_pos));
return ret;
}

inline std::string replace(std::string_view str, std::string_view key, std::string_view repl) {
std::string ret;
std::string_view::size_type pos = 0;
std::string_view::size_type prev_pos = 0;
while (pos = str.find(key, pos), pos != key.npos) {
ret.append(str.begin() + prev_pos, str.begin() + pos);
ret.append(repl);
prev_pos = pos += key.size();
}
ret.append(str.begin() + prev_pos, str.end());
return ret;
}

inline std::vector<std::string>
replace(std::vector<std::string> strings, std::string_view key, std::string_view repl) {
for (auto& item : strings) {
item = replace(item, key, repl);
}
return strings;
}

} // namespace string_utils

} // namespace dds

src/dds/util.test.cpp → src/dds/util/string.test.cpp Näytä tiedosto

@@ -1,4 +1,5 @@
#include <dds/util.hpp>
#include <dds/util/string.hpp>

#include <dds/util.test.hpp>

using namespace dds;

+ 41
- 0
src/dds/util/tl.hpp Näytä tiedosto

@@ -0,0 +1,41 @@
#pragma once

#include <cstddef>

namespace dds {

namespace detail {

template <std::size_t I,
typename A = std::nullptr_t,
typename B = std::nullptr_t,
typename C = std::nullptr_t,
typename D = std::nullptr_t>
decltype(auto) nth_arg(A&& a = nullptr, B&& b = nullptr, C&& c = nullptr, D&& d = nullptr) {
if constexpr (I == 0) {
return (A &&) a;
} else if constexpr (I == 1) {
return (B &&) b;
} else if constexpr (I == 2) {
return (C &&) c;
} else if constexpr (I == 3) {
return (D &&) d;
}
}

} // namespace detail

// Based on https://github.com/Quincunx271/TerseLambda
#define DDS_CTL(...) \
(auto&&... _args_)->auto { \
[[maybe_unused]] auto&& _1 = ::dds::detail::nth_arg<0>((decltype(_args_)&&)(_args_)...); \
[[maybe_unused]] auto&& _2 = ::dds::detail::nth_arg<1>((decltype(_args_)&&)(_args_)...); \
[[maybe_unused]] auto&& _3 = ::dds::detail::nth_arg<2>((decltype(_args_)&&)(_args_)...); \
[[maybe_unused]] auto&& _4 = ::dds::detail::nth_arg<3>((decltype(_args_)&&)(_args_)...); \
static_assert(sizeof...(_args_) <= 4); \
return (__VA_ARGS__); \
}

#define DDS_TL [&] DDS_CTL

} // namespace dds

+ 2
- 1
src/libman/util.hpp Näytä tiedosto

@@ -1,6 +1,7 @@
#pragma once

#include <dds/util.hpp>
#include <dds/util/fs.hpp>
#include <dds/util/string.hpp>

namespace lm {


Loading…
Peruuta
Tallenna