2012-07-18 21:59:03 +03:00
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
///@file
|
2003-04-04 19:14:56 +03:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
#include "types.hh"
|
2020-04-24 23:57:51 +03:00
|
|
|
#include "error.hh"
|
2016-04-25 16:26:07 +03:00
|
|
|
#include "logging.hh"
|
2016-09-20 16:39:08 +03:00
|
|
|
|
2021-12-31 04:16:59 +02:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
2014-07-10 17:50:51 +03:00
|
|
|
#include <functional>
|
2016-09-20 16:39:08 +03:00
|
|
|
#include <map>
|
2017-03-08 23:24:10 +02:00
|
|
|
#include <sstream>
|
2019-02-12 14:43:32 +02:00
|
|
|
#include <optional>
|
2009-11-24 14:56:26 +02:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
namespace nix {
|
2003-10-07 17:37:41 +03:00
|
|
|
|
2023-02-01 14:34:32 +02:00
|
|
|
void initLibUtil();
|
2003-10-07 17:37:41 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Convert a list of strings to a null-terminated vector of `char
|
|
|
|
* *`s. The result must not be accessed beyond the lifetime of the
|
|
|
|
* list of strings.
|
|
|
|
*/
|
2015-06-09 11:50:55 +03:00
|
|
|
std::vector<char *> stringsToCharPtrs(const Strings & ss);
|
2014-12-12 16:01:16 +02:00
|
|
|
|
2004-01-15 22:23:55 +02:00
|
|
|
|
2019-11-10 18:14:26 +02:00
|
|
|
MakeError(FormatError, Error);
|
2016-07-19 01:50:27 +03:00
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* String tokenizer.
|
|
|
|
*/
|
2022-01-12 17:02:29 +02:00
|
|
|
template<class C> C tokenizeString(std::string_view s, std::string_view separators = " \t\n\r");
|
2005-09-22 18:43:22 +03:00
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Concatenate the given strings with a separator between the
|
|
|
|
* elements.
|
|
|
|
*/
|
2019-05-02 22:09:52 +03:00
|
|
|
template<class C>
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string concatStringsSep(const std::string_view sep, const C & ss)
|
2019-05-02 22:09:52 +03:00
|
|
|
{
|
2022-01-12 17:02:29 +02:00
|
|
|
size_t size = 0;
|
|
|
|
// need a cast to string_view since this is also called with Symbols
|
|
|
|
for (const auto & s : ss) size += sep.size() + std::string_view(s).size();
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string s;
|
2022-01-12 17:02:29 +02:00
|
|
|
s.reserve(size);
|
2019-05-02 22:09:52 +03:00
|
|
|
for (auto & i : ss) {
|
|
|
|
if (s.size() != 0) s += sep;
|
|
|
|
s += i;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:02:29 +02:00
|
|
|
template<class ... Parts>
|
|
|
|
auto concatStrings(Parts && ... parts)
|
2022-02-25 17:00:00 +02:00
|
|
|
-> std::enable_if_t<(... && std::is_convertible_v<Parts, std::string_view>), std::string>
|
2022-01-12 17:02:29 +02:00
|
|
|
{
|
|
|
|
std::string_view views[sizeof...(parts)] = { parts... };
|
|
|
|
return concatStringsSep({}, views);
|
|
|
|
}
|
|
|
|
|
2019-05-02 22:09:52 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Add quotes around a collection of strings.
|
|
|
|
*/
|
2019-05-02 22:09:52 +03:00
|
|
|
template<class C> Strings quoteStrings(const C & c)
|
|
|
|
{
|
|
|
|
Strings res;
|
|
|
|
for (auto & s : c)
|
|
|
|
res.push_back("'" + s + "'");
|
|
|
|
return res;
|
|
|
|
}
|
2010-08-27 16:18:13 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Remove trailing whitespace from a string.
|
|
|
|
*
|
|
|
|
* \todo return std::string_view.
|
|
|
|
*/
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string chomp(std::string_view s);
|
2012-08-01 18:19:24 +03:00
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Remove whitespace from the start and end of a string.
|
|
|
|
*/
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string trim(std::string_view s, std::string_view whitespace = " \n\r\t");
|
2015-04-09 12:42:04 +03:00
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Replace all occurrences of a string inside another string.
|
|
|
|
*/
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string replaceStrings(
|
|
|
|
std::string s,
|
|
|
|
std::string_view from,
|
|
|
|
std::string_view to);
|
2015-06-17 17:20:11 +03:00
|
|
|
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string rewriteStrings(std::string s, const StringMap & rewrites);
|
2018-03-30 01:56:13 +03:00
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Parse a string into an integer.
|
|
|
|
*/
|
2021-01-08 13:22:21 +02:00
|
|
|
template<class N>
|
2021-12-31 04:16:59 +02:00
|
|
|
std::optional<N> string2Int(const std::string_view s)
|
2009-11-24 14:26:25 +02:00
|
|
|
{
|
2021-01-08 13:22:21 +02:00
|
|
|
if (s.substr(0, 1) == "-" && !std::numeric_limits<N>::is_signed)
|
2021-01-08 13:51:19 +02:00
|
|
|
return std::nullopt;
|
2021-12-31 04:16:59 +02:00
|
|
|
try {
|
|
|
|
return boost::lexical_cast<N>(s.data(), s.size());
|
|
|
|
} catch (const boost::bad_lexical_cast &) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-01-08 13:51:19 +02:00
|
|
|
}
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Like string2Int(), but support an optional suffix 'K', 'M', 'G' or
|
|
|
|
* 'T' denoting a binary unit prefix.
|
|
|
|
*/
|
2021-01-08 13:51:19 +02:00
|
|
|
template<class N>
|
2021-12-31 04:16:59 +02:00
|
|
|
N string2IntWithUnitPrefix(std::string_view s)
|
2021-01-08 13:51:19 +02:00
|
|
|
{
|
|
|
|
N multiplier = 1;
|
|
|
|
if (!s.empty()) {
|
|
|
|
char u = std::toupper(*s.rbegin());
|
|
|
|
if (std::isalpha(u)) {
|
|
|
|
if (u == 'K') multiplier = 1ULL << 10;
|
|
|
|
else if (u == 'M') multiplier = 1ULL << 20;
|
|
|
|
else if (u == 'G') multiplier = 1ULL << 30;
|
|
|
|
else if (u == 'T') multiplier = 1ULL << 40;
|
|
|
|
else throw UsageError("invalid unit specifier '%1%'", u);
|
2021-12-31 04:16:59 +02:00
|
|
|
s.remove_suffix(1);
|
2021-01-08 13:51:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (auto n = string2Int<N>(s))
|
|
|
|
return *n * multiplier;
|
|
|
|
throw UsageError("'%s' is not an integer", s);
|
2009-11-24 14:26:25 +02:00
|
|
|
}
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Parse a string into a float.
|
|
|
|
*/
|
2021-01-08 13:22:21 +02:00
|
|
|
template<class N>
|
2021-12-31 04:16:59 +02:00
|
|
|
std::optional<N> string2Float(const std::string_view s)
|
2016-01-05 01:40:40 +02:00
|
|
|
{
|
2021-12-31 04:16:59 +02:00
|
|
|
try {
|
|
|
|
return boost::lexical_cast<N>(s.data(), s.size());
|
|
|
|
} catch (const boost::bad_lexical_cast &) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2016-01-05 01:40:40 +02:00
|
|
|
}
|
|
|
|
|
2004-09-10 16:32:08 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Convert a little-endian integer to host order.
|
|
|
|
*/
|
2022-12-07 13:58:58 +02:00
|
|
|
template<typename T>
|
|
|
|
T readLittleEndian(unsigned char * p)
|
|
|
|
{
|
|
|
|
T x = 0;
|
2022-12-12 13:40:51 +02:00
|
|
|
for (size_t i = 0; i < sizeof(x); ++i, ++p) {
|
|
|
|
x |= ((T) *p) << (i * 8);
|
|
|
|
}
|
2022-12-07 13:58:58 +02:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* @return true iff `s` starts with `prefix`.
|
|
|
|
*/
|
2020-06-13 00:12:36 +03:00
|
|
|
bool hasPrefix(std::string_view s, std::string_view prefix);
|
2016-04-29 22:04:40 +03:00
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* @return true iff `s` ends in `suffix`.
|
|
|
|
*/
|
2019-12-05 20:11:09 +02:00
|
|
|
bool hasSuffix(std::string_view s, std::string_view suffix);
|
2008-08-25 16:31:57 +03:00
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Convert a string to lower case.
|
|
|
|
*/
|
2016-09-14 15:42:15 +03:00
|
|
|
std::string toLower(const std::string & s);
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Escape a string as a shell word.
|
|
|
|
*/
|
2022-01-21 18:55:51 +02:00
|
|
|
std::string shellEscape(const std::string_view s);
|
2017-08-25 16:57:49 +03:00
|
|
|
|
|
|
|
|
2021-08-28 23:26:53 +03:00
|
|
|
/* Exception handling in destructors: print an error message, then
|
|
|
|
ignore the exception. */
|
2022-12-02 16:03:40 +02:00
|
|
|
void ignoreException(Verbosity lvl = lvlError);
|
2008-09-17 13:02:55 +03:00
|
|
|
|
|
|
|
|
2014-08-20 17:01:16 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Tree formatting.
|
|
|
|
*/
|
2020-03-24 15:17:10 +02:00
|
|
|
constexpr char treeConn[] = "├───";
|
|
|
|
constexpr char treeLast[] = "└───";
|
|
|
|
constexpr char treeLine[] = "│ ";
|
|
|
|
constexpr char treeNull[] = " ";
|
|
|
|
|
2014-08-20 17:01:16 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Base64 encoding/decoding.
|
|
|
|
*/
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string base64Encode(std::string_view s);
|
|
|
|
std::string base64Decode(std::string_view s);
|
2015-02-09 16:09:39 +02:00
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Remove common leading whitespace from the lines in the string
|
|
|
|
* 's'. For example, if every line is indented by at least 3 spaces,
|
|
|
|
* then we remove 3 spaces from the start of every line.
|
|
|
|
*/
|
2020-08-20 13:21:46 +03:00
|
|
|
std::string stripIndentation(std::string_view s);
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Get the prefix of 's' up to and excluding the next line break (LF
|
|
|
|
* optionally preceded by CR), and the remainder following the line
|
|
|
|
* break.
|
|
|
|
*/
|
2022-12-07 13:58:58 +02:00
|
|
|
std::pair<std::string_view, std::string_view> getLine(std::string_view s);
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Get a value for the specified key from an associate container.
|
|
|
|
*/
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
template <class T>
|
2022-05-04 08:44:32 +03:00
|
|
|
const typename T::mapped_type * get(const T & map, const typename T::key_type & key)
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
{
|
|
|
|
auto i = map.find(key);
|
2022-05-04 08:44:32 +03:00
|
|
|
if (i == map.end()) return nullptr;
|
|
|
|
return &i->second;
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-04 08:44:32 +03:00
|
|
|
template <class T>
|
|
|
|
typename T::mapped_type * get(T & map, const typename T::key_type & key)
|
|
|
|
{
|
|
|
|
auto i = map.find(key);
|
|
|
|
if (i == map.end()) return nullptr;
|
|
|
|
return &i->second;
|
|
|
|
}
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Get a value for the specified key from an associate container, or a default value if the key isn't present.
|
|
|
|
*/
|
2022-05-04 08:44:32 +03:00
|
|
|
template <class T>
|
|
|
|
const typename T::mapped_type & getOr(T & map,
|
|
|
|
const typename T::key_type & key,
|
|
|
|
const typename T::mapped_type & defaultValue)
|
|
|
|
{
|
|
|
|
auto i = map.find(key);
|
|
|
|
if (i == map.end()) return defaultValue;
|
|
|
|
return i->second;
|
|
|
|
}
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Remove and return the first item from a container.
|
|
|
|
*/
|
2021-08-20 12:18:35 +03:00
|
|
|
template <class T>
|
|
|
|
std::optional<typename T::value_type> remove_begin(T & c)
|
|
|
|
{
|
|
|
|
auto i = c.begin();
|
|
|
|
if (i == c.end()) return {};
|
|
|
|
auto v = std::move(*i);
|
|
|
|
c.erase(i);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Remove and return the first item from a container.
|
|
|
|
*/
|
2021-10-14 13:31:21 +03:00
|
|
|
template <class T>
|
|
|
|
std::optional<typename T::value_type> pop(T & c)
|
|
|
|
{
|
|
|
|
if (c.empty()) return {};
|
|
|
|
auto v = std::move(c.front());
|
|
|
|
c.pop();
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-27 23:16:01 +03:00
|
|
|
template<typename T>
|
2020-09-21 19:40:11 +03:00
|
|
|
class Callback;
|
2016-09-16 19:54:14 +03:00
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* A RAII helper that increments a counter on construction and
|
|
|
|
* decrements it on destruction.
|
|
|
|
*/
|
2017-08-14 23:42:17 +03:00
|
|
|
template<typename T>
|
|
|
|
struct MaintainCount
|
|
|
|
{
|
|
|
|
T & counter;
|
|
|
|
long delta;
|
|
|
|
MaintainCount(T & counter, long delta = 1) : counter(counter), delta(delta) { counter += delta; }
|
|
|
|
~MaintainCount() { counter -= delta; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* A Rust/Python-like enumerate() iterator adapter.
|
|
|
|
*
|
|
|
|
* Borrowed from http://reedbeta.com/blog/python-like-enumerate-in-cpp17.
|
|
|
|
*/
|
2020-03-24 15:17:10 +02:00
|
|
|
template <typename T,
|
|
|
|
typename TIter = decltype(std::begin(std::declval<T>())),
|
|
|
|
typename = decltype(std::end(std::declval<T>()))>
|
|
|
|
constexpr auto enumerate(T && iterable)
|
|
|
|
{
|
|
|
|
struct iterator
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
TIter iter;
|
2023-04-03 01:11:16 +03:00
|
|
|
constexpr bool operator != (const iterator & other) const { return iter != other.iter; }
|
|
|
|
constexpr void operator ++ () { ++i; ++iter; }
|
|
|
|
constexpr auto operator * () const { return std::tie(i, *iter); }
|
2020-03-24 15:17:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct iterable_wrapper
|
|
|
|
{
|
|
|
|
T iterable;
|
2023-04-03 01:11:16 +03:00
|
|
|
constexpr auto begin() { return iterator{ 0, std::begin(iterable) }; }
|
|
|
|
constexpr auto end() { return iterator{ 0, std::end(iterable) }; }
|
2020-03-24 15:17:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return iterable_wrapper{ std::forward<T>(iterable) };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* C++17 std::visit boilerplate
|
|
|
|
*/
|
2020-07-13 01:15:14 +03:00
|
|
|
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
|
|
|
|
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
|
|
|
|
|
|
|
|
2020-10-06 11:40:49 +03:00
|
|
|
std::string showBytes(uint64_t bytes);
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Provide an addition operator between strings and string_views
|
|
|
|
* inexplicably omitted from the standard library.
|
|
|
|
*/
|
2022-05-09 15:28:27 +03:00
|
|
|
inline std::string operator + (const std::string & s1, std::string_view s2)
|
|
|
|
{
|
|
|
|
auto s = s1;
|
|
|
|
s.append(s2);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string operator + (std::string && s, std::string_view s2)
|
|
|
|
{
|
|
|
|
s.append(s2);
|
2022-06-02 17:48:53 +03:00
|
|
|
return std::move(s);
|
2022-05-09 15:28:27 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 13:58:58 +02:00
|
|
|
inline std::string operator + (std::string_view s1, const char * s2)
|
|
|
|
{
|
2022-12-12 13:36:19 +02:00
|
|
|
std::string s;
|
|
|
|
s.reserve(s1.size() + strlen(s2));
|
|
|
|
s.append(s1);
|
2022-12-07 13:58:58 +02:00
|
|
|
s.append(s2);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|