Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
util.hh File Reference
#include "nix/util/types.hh"
#include "nix/util/error.hh"
#include "nix/util/strings.hh"
#include <set>
#include <bit>
#include <optional>
#include <ranges>
#include <concepts>

Go to the source code of this file.

Classes

struct  nix::MaintainCount< T >
struct  nix::overloaded< Ts >

Macros

#define NIX_DELETE_STRING2INT_OVERLOAD(T)
#define NIX_UTIL_SIZE_UNITS
#define NIX_UTIL_DEFINE_SIZE_UNIT(name, suffix)
#define NIX_UTIL_DEFINE_SIZE_UNIT(name, suffix)
#define NIX_INCOMPLETE_SWITCH_BEGIN   _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wswitch-enum\"")
#define NIX_INCOMPLETE_SWITCH_END   _Pragma("GCC diagnostic pop")

Enumerations

enum class  SizeUnit { NIX_UTIL_SIZE_UNITS }

Functions

void nix::initLibUtil ()
std::vector< char * > nix::stringsToCharPtrs (const Strings &ss)
 nix::MakeError (FormatError, Error)
template<class... Parts>
auto nix::concatStrings (Parts &&... parts) -> std::enable_if_t<(... &&std::is_convertible_v< Parts, std::string_view >), std::string >
std::string nix::quoteString (std::string_view s, char quote='\'')
template<class C>
Strings nix::quoteStrings (const C &c, char quote='\'')
std::string nix::chomp (std::string_view s)
std::string nix::trim (std::string_view s, std::string_view whitespace=" \n\r\t")
std::string nix::replaceStrings (std::string s, std::string_view from, std::string_view to)
std::string nix::rewriteStrings (std::string s, const StringMap &rewrites, std::set< uint64_t > *matches=nullptr, uint64_t offsetShift=0)
template<std::integral N>
std::optional< N > nix::string2Int (const std::string_view s)
template<class N>
N nix::string2IntWithUnitPrefix (std::string_view s)
SizeUnit nix::getSizeUnit (int64_t value)
std::optional< SizeUnit > nix::getCommonSizeUnit (std::initializer_list< int64_t > values)
std::string nix::renderSizeWithoutUnit (int64_t value, SizeUnit unit, bool align=false)
char nix::getSizeUnitSuffix (SizeUnit unit)
std::string nix::renderSize (int64_t value, bool align=false)
template<class N>
std::optional< N > nix::string2Float (const std::string_view s)
template<typename T>
requires std::is_unsigned_v<T> && (sizeof(T) > 1)
T nix::readLittleEndian (unsigned char *p)
template<typename T>
requires std::is_unsigned_v<T> && (sizeof(T) > 1)
void nix::writeBigEndian (unsigned char *p, T x)
bool nix::hasPrefix (std::string_view s, std::string_view prefix)
bool nix::hasSuffix (std::string_view s, std::string_view suffix)
std::string nix::toLower (std::string s)
std::string nix::escapeShellArgAlways (const std::string_view s)
void nix::ignoreExceptionInDestructor (Verbosity lvl=lvlError)
void nix::ignoreExceptionExceptInterrupt (Verbosity lvl=lvlError)
std::string nix::stripIndentation (std::string_view s)
std::pair< std::string_view, std::string_view > nix::getLine (std::string_view s)
template<class T>
const T * nix::get (const std::optional< T > &opt)
template<class T>
T * nix::get (std::optional< T > &opt)
template<class T, typename K>
const T::mapped_type * nix::get (const T &map, const K &key)
template<class T, typename K>
T::mapped_type * nix::get (T &map, const K &key)
template<class T, typename K>
T::mapped_type * nix::get (T &&map, const K &key)=delete
template<class T>
std::optional< typename T::mapped_type > nix::getConcurrent (const T &map, const typename T::key_type &key)
template<class T, typename K>
const T::mapped_type & nix::getOr (T &map, const K &key, const typename T::mapped_type &defaultValue)
template<class T, typename K>
const T::mapped_type & nix::getOr (T &&map, const K &key, const typename T::mapped_type &defaultValue)=delete
template<class T>
std::optional< typename T::value_type > nix::remove_begin (T &c)
template<class T>
std::optional< typename T::value_type > nix::pop (T &c)
template<std::ranges::viewable_range R>
constexpr auto nix::enumerate (R &&range)
template<std::ranges::viewable_range R>
requires std::ranges::sized_range<R>
constexpr auto nix::markLast (R &&range)
template<class... Ts>
 nix::overloaded (Ts...) -> overloaded< Ts... >
std::string nix::operator+ (const std::string &s1, std::string_view s2)
std::string nix::operator+ (std::string &&s, std::string_view s2)
std::string nix::operator+ (std::string_view s1, const char *s2)

Variables

constexpr auto nix::sizeUnits
constexpr char nix::treeConn [] = "├───"
constexpr char nix::treeLast [] = "└───"
constexpr char nix::treeLine [] = "│ "
constexpr char nix::treeNull [] = " "

Macro Definition Documentation

◆ NIX_DELETE_STRING2INT_OVERLOAD

#define NIX_DELETE_STRING2INT_OVERLOAD ( T)
Value:
template<> \
std::optional<T> string2Int(const std::string_view s) = delete;

◆ NIX_INCOMPLETE_SWITCH_BEGIN

#define NIX_INCOMPLETE_SWITCH_BEGIN   _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wswitch-enum\"")

Allow selecting a subset of enum values.

◆ NIX_UTIL_DEFINE_SIZE_UNIT [1/2]

#define NIX_UTIL_DEFINE_SIZE_UNIT ( name,
suffix )
Value:
SizeUnit::name,

◆ NIX_UTIL_DEFINE_SIZE_UNIT [2/2]

#define NIX_UTIL_DEFINE_SIZE_UNIT ( name,
suffix )
Value:
name,

◆ NIX_UTIL_SIZE_UNITS

#define NIX_UTIL_SIZE_UNITS
Value:
NIX_UTIL_DEFINE_SIZE_UNIT(Base, 'K') \
NIX_UTIL_DEFINE_SIZE_UNIT(Kilo, 'K') \
NIX_UTIL_DEFINE_SIZE_UNIT(Mega, 'M') \
NIX_UTIL_DEFINE_SIZE_UNIT(Giga, 'G') \
NIX_UTIL_DEFINE_SIZE_UNIT(Tera, 'T') \
NIX_UTIL_DEFINE_SIZE_UNIT(Peta, 'P') \
NIX_UTIL_DEFINE_SIZE_UNIT(Exa, 'E') \
NIX_UTIL_DEFINE_SIZE_UNIT(Zetta, 'Z') \
NIX_UTIL_DEFINE_SIZE_UNIT(Yotta, 'Y')

Function Documentation

◆ chomp()

std::string nix::chomp ( std::string_view s)

Remove trailing whitespace from a string.

Todo
return std::string_view.

◆ enumerate()

template<std::ranges::viewable_range R>
auto nix::enumerate ( R && range)
constexpr

A Rust/Python-like enumerate() iterator adapter.

◆ escapeShellArgAlways()

std::string nix::escapeShellArgAlways ( const std::string_view s)

Escape a string as a shell word.

This always adds single quotes, even if escaping is not strictly necessary. So both

  • "hello world" -> "'hello world'", which needs escaping because of the space
  • "echo" -> "'echo'", which doesn't need escaping

◆ get() [1/4]

template<class T>
const T * nix::get ( const std::optional< T > & opt)

Get a pointer to the contents of a std::optional if it is set, or a null pointer otherwise.

Const version.

◆ get() [2/4]

template<class T, typename K>
const T::mapped_type * nix::get ( const T & map,
const K & key )

Get a value for the specified key from an associate container.

◆ get() [3/4]

template<class T>
T * nix::get ( std::optional< T > & opt)

Non-const counterpart of const T * get(const std::optional<T>). Takes a mutable reference, but returns a mutable pointer.

◆ get() [4/4]

template<class T, typename K>
T::mapped_type * nix::get ( T && map,
const K & key )
delete

Deleted because this is use-after-free liability. Just don't pass temporaries to this overload set.

◆ getCommonSizeUnit()

std::optional< SizeUnit > nix::getCommonSizeUnit ( std::initializer_list< int64_t > values)

Returns the unit if all values would be rendered using the same unit otherwise returns std::nullopt.

◆ getConcurrent()

template<class T>
std::optional< typename T::mapped_type > nix::getConcurrent ( const T & map,
const typename T::key_type & key )

Look up a value in a boost::concurrent_flat_map.

◆ getLine()

std::pair< std::string_view, std::string_view > nix::getLine ( std::string_view s)

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.

◆ getOr() [1/2]

template<class T, typename K>
const T::mapped_type & nix::getOr ( T && map,
const K & key,
const typename T::mapped_type & defaultValue )
delete

Deleted because this is use-after-free liability. Just don't pass temporaries to this overload set.

◆ getOr() [2/2]

template<class T, typename K>
const T::mapped_type & nix::getOr ( T & map,
const K & key,
const typename T::mapped_type & defaultValue )

Get a value for the specified key from an associate container, or a default value if the key isn't present.

◆ hasPrefix()

bool nix::hasPrefix ( std::string_view s,
std::string_view prefix )
Returns
true iff s starts with prefix.

◆ hasSuffix()

bool nix::hasSuffix ( std::string_view s,
std::string_view suffix )
Returns
true iff s ends in suffix.

◆ ignoreExceptionExceptInterrupt()

void nix::ignoreExceptionExceptInterrupt ( Verbosity lvl = lvlError)

Not destructor-safe. Print an error message, then ignore the exception. If the exception is an Interrupted exception, rethrow it.

This may be used in a few places where Interrupt can't happen, but that's ok.

◆ ignoreExceptionInDestructor()

void nix::ignoreExceptionInDestructor ( Verbosity lvl = lvlError)

Exception handling in destructors: print an error message, then ignore the exception.

If you're not in a destructor, you usually want to use ignoreExceptionExceptInterrupt().

This function might also be used in callbacks whose caller may not handle exceptions, but ideally we propagate the exception using an exception_ptr in such cases. See e.g. PackBuilderContext

◆ markLast()

template<std::ranges::viewable_range R>
requires std::ranges::sized_range<R>
auto nix::markLast ( R && range)
constexpr

An iterator adapter that enumerates the elements of a range, pairing each element with a boolean indicating whether it is the last element.

◆ operator+()

std::string nix::operator+ ( const std::string & s1,
std::string_view s2 )
inline

Provide an addition operator between strings and string_views inexplicably omitted from the standard library.

◆ pop()

template<class T>
std::optional< typename T::value_type > nix::pop ( T & c)

Remove and return the first item from a container.

◆ quoteString()

std::string nix::quoteString ( std::string_view s,
char quote = '\'' )
inline

Add quotes around a string.

◆ quoteStrings()

template<class C>
Strings nix::quoteStrings ( const C & c,
char quote = '\'' )

Add quotes around a collection of strings.

◆ readLittleEndian()

template<typename T>
requires std::is_unsigned_v<T> && (sizeof(T) > 1)
T nix::readLittleEndian ( unsigned char * p)

Convert a little-endian integer to host order.

◆ remove_begin()

template<class T>
std::optional< typename T::value_type > nix::remove_begin ( T & c)

Remove and return the first item from a container.

◆ renderSize()

std::string nix::renderSize ( int64_t value,
bool align = false )

Pretty-print a byte value, e.g. 12433615056 is rendered as 11.6 GiB. If align is set, the number will be right-justified by padding with spaces on the left.

◆ replaceStrings()

std::string nix::replaceStrings ( std::string s,
std::string_view from,
std::string_view to )

Replace all occurrences of a string inside another string.

◆ rewriteStrings()

std::string nix::rewriteStrings ( std::string s,
const StringMap & rewrites,
std::set< uint64_t > * matches = nullptr,
uint64_t offsetShift = 0 )

Replace all occurrences of the keys in rewrites with their corresponding values. Optionally returns the positions of the matches in matches.

◆ string2Float()

template<class N>
std::optional< N > nix::string2Float ( const std::string_view s)

Parse a string into a float.

◆ string2Int()

template<std::integral N>
std::optional< N > nix::string2Int ( const std::string_view s)

Parse a string into an integer.

◆ string2IntWithUnitPrefix()

template<class N>
N nix::string2IntWithUnitPrefix ( std::string_view s)

Like string2Int(), but support an optional suffix 'K', 'M', 'G' or 'T' denoting a binary unit prefix.

◆ stringsToCharPtrs()

std::vector< char * > nix::stringsToCharPtrs ( const Strings & ss)

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.

◆ stripIndentation()

std::string nix::stripIndentation ( std::string_view s)

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.

◆ toLower()

std::string nix::toLower ( std::string s)

Convert a string to lower case.

◆ trim()

std::string nix::trim ( std::string_view s,
std::string_view whitespace = " \n\r\t" )

Remove whitespace from the start and end of a string.

◆ writeBigEndian()

template<typename T>
requires std::is_unsigned_v<T> && (sizeof(T) > 1)
void nix::writeBigEndian ( unsigned char * p,
T x )

Write the integer in big endian order to the output.

Variable Documentation

◆ sizeUnits

auto nix::sizeUnits
inlineconstexpr
Initial value:
= std::to_array<SizeUnit>({
#define NIX_UTIL_DEFINE_SIZE_UNIT(name, suffix)
NIX_UTIL_SIZE_UNITS
})

◆ treeConn

char nix::treeConn[] = "├───"
constexpr

Tree formatting.