13 #include <string_view> 18 #include <hilti/rt/autogen/config.h> 19 #include <hilti/rt/exception.h> 20 #include <hilti/rt/filesystem.h> 21 #include <hilti/rt/result.h> 22 #include <hilti/rt/types/set_fwd.h> 23 #include <hilti/rt/types/time.h> 24 #include <hilti/rt/types/vector_fwd.h> 28 void internalError(
const std::string& msg) __attribute__((noreturn));
32 #undef TINYFORMAT_ERROR 33 #define TINYFORMAT_ERROR(reason) throw ::hilti::rt::FormattingError(reason) 34 #include <hilti/rt/3rdparty/tinyformat/tinyformat.h> 35 #include <hilti/rt/extension-points.h> 36 #include <hilti/rt/fmt.h> 38 extern const char* __hlto_scope;
65 uint64_t cached_fibers;
72 extern std::optional<std::string>
getenv(
const std::string& name);
83 hilti::rt::filesystem::path
normalizePath(
const hilti::rt::filesystem::path& p);
90 inline std::string_view
rtrim(std::string_view s,
const std::string& chars) noexcept {
91 s.remove_suffix(s.size() -
92 [](
size_t pos) {
return pos != std::string_view::npos ? pos + 1 : 0; }(s.find_last_not_of(chars)));
101 inline std::string_view
ltrim(std::string_view s,
const std::string& chars) noexcept {
102 s.remove_prefix(std::min(s.find_first_not_of(chars), s.size()));
112 inline std::string_view
trim(std::string_view s,
const std::string& chars) noexcept {
117 constexpr
char whitespace_chars[] =
" \t\f\v\n\r";
125 inline std::string_view
rtrim(std::string_view s) noexcept {
return rtrim(s, detail::whitespace_chars); }
132 inline std::string_view
ltrim(std::string_view s) noexcept {
return ltrim(s, detail::whitespace_chars); }
139 inline std::string_view
trim(std::string_view s) noexcept {
return trim(s, detail::whitespace_chars); }
147 std::vector<std::string_view>
split(std::string_view s, std::string_view delim);
154 std::vector<std::string_view>
split(std::string_view s);
162 extern std::pair<std::string, std::string>
split1(std::string s);
170 extern std::pair<std::string, std::string>
rsplit1(std::string s);
178 extern std::pair<std::string, std::string>
split1(std::string s,
const std::string& delim);
186 extern std::pair<std::string, std::string>
rsplit1(std::string s,
const std::string& delim);
193 std::string
replace(std::string s, std::string_view o, std::string_view n);
200 inline bool startsWith(
const std::string& s,
const std::string& prefix) {
return s.find(prefix) == 0; }
206 template<
typename T,
typename TIter = decltype(std::begin(std::declval<T>())),
207 typename = decltype(std::end(std::declval<T>()))>
212 bool operator!=(
const iterator& other)
const {
return iter != other.iter; }
217 auto operator*()
const {
return std::tie(i, *iter); }
219 struct iterable_wrapper {
221 auto begin() {
return iterator{0, std::begin(iterable)}; }
222 auto end() {
return iterator{0, std::end(iterable)}; }
224 return iterable_wrapper{std::forward<T>(iterable)};
246 std::string expandEscapes(std::string s);
259 std::string escapeBytes(std::string_view s,
bool escape_quotes =
false,
bool use_octal =
false);
273 std::string escapeUTF8(std::string_view s,
bool escape_quotes =
false,
bool escape_control =
true,
274 bool keep_hex =
false);
281 std::string
join(
const T& l,
const std::string& delim =
"") {
285 for (
const auto& i : l ) {
288 result += std::string(i);
298 template<
typename X,
typename F>
300 using Y =
typename std::result_of<F(X&)>::type;
303 for (
const auto& i : x )
304 y.emplace_back(f(i));
311 template<
typename X,
typename F>
313 using Y =
typename std::result_of<F(X&)>::type;
316 for (
const auto& i : x )
317 y.emplace_back(f(i));
324 template<
typename X,
typename F>
326 using Y =
typename std::result_of<F(X&)>::type;
328 for (
const auto& i : x )
334 template<
typename X,
typename F>
336 using Y =
typename std::result_of<F(X&)>::type;
338 for (
const auto& i : x )
344 template<
typename X,
typename Allocator,
typename F>
346 using Y =
typename std::result_of<F(X&)>::type;
348 std::transform(x.begin(), x.end(), std::back_inserter(y), [&](
const auto& value) {
return f(value); });
376 template<
class Iter,
typename Result>
378 if ( base < 2 || base > 36 )
379 throw OutOfRange(
"base for numerical conversion must be between 2 and 36");
382 throw InvalidArgument(
"cannot decode from empty range");
384 std::optional<Result> n = std::nullopt;
392 else if ( *it ==
'+' ) {
397 for ( ; it != e; ++it ) {
401 if ( c >=
'0' && c <
'0' + base )
403 else if ( c >=
'a' && c <
'a' - 10 + base )
405 else if ( c >=
'A' && c <
'A' - 10 + base )
410 n = n.value_or(
Result()) * base + d;
429 template<
typename I1,
typename I2>
430 inline I1
pow(I1 base, I2 exp) {
447 template<
typename T,
typename F, std::size_t... Is>
448 constexpr
auto map_tuple(T&& tup, F& f, std::index_sequence<Is...> ) {
449 return std::make_tuple(f(std::get<Is>(std::forward<T>(tup)))...);
452 template<
typename T, std::size_t... Is>
453 auto join_tuple(T&& tup, std::index_sequence<Is...> ) {
454 std::vector<std::string> x = {
rt::to_string(std::get<Is>(std::forward<T>(tup)))...};
455 return join(x,
", ");
458 template<
typename T, std::size_t... Is>
460 std::vector<std::string> x = {rt::to_string_for_print(std::get<Is>(std::forward<T>(tup)))...};
461 return join(x,
", ");
466 template<
typename F, std::size_t I = 0,
typename... Ts>
468 if constexpr ( I ==
sizeof...(Ts) )
471 func(std::get<I>(tup));
472 tuple_for_each<F, I + 1>(tup, func);
480 template<
typename T,
typename F, std::
size_t TupSize = std::tuple_size_v<std::decay_t<T>>>
482 return detail::map_tuple(std::forward<T>(tup), f, std::make_index_sequence<TupSize>{});
491 template<
typename T, std::
size_t TupSize = std::tuple_size_v<std::decay_t<T>>>
493 return detail::join_tuple(std::forward<T>(tup), std::make_index_sequence<TupSize>{});
502 template<
typename T, std::
size_t TupSize = std::tuple_size_v<std::decay_t<T>>>
504 return detail::join_tuple_for_print(std::forward<T>(tup), std::make_index_sequence<TupSize>{});
511 template<
typename... T>
523 namespace detail::adl {
539 std::string
strftime(
const std::string& format,
const Time& time);
553 Time strptime(
const std::string& buf,
const std::string& format);
559 const auto tmpdir = hilti::rt::filesystem::temp_directory_path();
560 auto template_ = (tmpdir /
"hilti-rt-test-XXXXXX").native();
561 auto path = ::mkdtemp(template_.data());
563 throw RuntimeError(
"cannot create temporary directory");
572 if ( ! hilti::rt::filesystem::exists(_path) )
576 hilti::rt::filesystem::permissions(_path, hilti::rt::filesystem::perms::all);
577 for (
const auto& entry : hilti::rt::filesystem::recursive_directory_iterator(_path) )
578 hilti::rt::filesystem::permissions(entry, hilti::rt::filesystem::perms::all);
581 hilti::rt::filesystem::remove_all(_path, ec);
584 const auto& path()
const {
return _path; }
588 _path = std::move(other._path);
593 hilti::rt::filesystem::path _path;
ByteOrder
Definition: util.h:515
std::string to_string(T &&x)
Definition: extension-points.h:26
bool isDebugVersion()
Definition: util.cc:34
std::string_view rtrim(std::string_view s, const std::string &chars) noexcept
Definition: util.h:90
std::string_view ltrim(std::string_view s, const std::string &chars) noexcept
Definition: util.h:101
void internalError(const std::string &msg) __attribute__((noreturn))
Definition: logging.cc:18
Definition: optional.h:79
std::string replace(std::string s, std::string_view o, std::string_view n)
Definition: util.cc:379
auto transform(const std::vector< X > &x, F f)
Definition: util.h:299
hilti::rt::filesystem::path normalizePath(const hilti::rt::filesystem::path &p)
Definition: util.cc:102
Iter atoi_n(Iter s, Iter e, int base, Result *result)
Definition: util.h:377
std::pair< std::string, std::string > split1(std::string s)
Definition: util.cc:156
bool startsWith(const std::string &s, const std::string &prefix)
Definition: util.h:200
hilti::rt::Result< hilti::rt::filesystem::path > createTemporaryFile(const std::string &prefix="")
Definition: util.cc:84
void cannot_be_reached() __attribute__((noreturn))
Definition: util.cc:52
std::string strftime(const std::string &format, const Time &time)
Definition: util.cc:414
void tuple_for_each(const std::tuple< Ts... > &tup, F func)
Definition: util.h:467
constexpr auto map_tuple(T &&tup, F f)
Definition: util.h:481
std::vector< std::string_view > split(std::string_view s, std::string_view delim)
Definition: util.cc:112
Time strptime(const std::string &buf, const std::string &format)
Definition: util.cc:440
auto join_tuple_for_print(T &&tup)
Definition: util.h:503
std::optional< std::string > getenv(const std::string &name)
Definition: util.cc:77
constexpr auto enumerate(T &&iterable)
Definition: util.h:208
std::pair< std::string, std::string > rsplit1(std::string s)
Definition: util.cc:163
std::string join(const T &l, const std::string &delim="")
Definition: util.h:281
std::string version()
Definition: util.cc:22
ByteOrder systemByteOrder()
Definition: util.cc:392
auto join_tuple(T &&tup)
Definition: util.h:492
void abort_with_backtrace() __attribute__((noreturn))
Definition: util.cc:44
ResourceUsage resource_usage()
Definition: util.cc:54
std::string_view trim(std::string_view s, const std::string &chars) noexcept
Definition: util.h:112
I1 pow(I1 base, I2 exp)
Definition: util.h:430
std::string linker_scope()
Definition: util.h:52