Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
util.hh File Reference
#include "types.hh"
#include "error.hh"
#include "logging.hh"
#include <functional>
#include <map>
#include <sstream>
#include <optional>
#include "strings.hh"

Go to the source code of this file.

Classes

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

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 >
 
template<class C>
Strings nix::quoteStrings (const C &c)
 
std::string nix::chomp (std::string_view s)
 
std::string nix::trim (std::string_view s, std::string_view whitespace)
 
std::string nix::replaceStrings (std::string res, std::string_view from, std::string_view to)
 
std::string nix::rewriteStrings (std::string s, const StringMap &rewrites)
 
template<class N>
std::optional< N > nix::string2Int (const std::string_view s)
 
template<class N>
nix::string2IntWithUnitPrefix (std::string_view s)
 
std::string nix::renderSize (uint64_t value, bool align)
 
template<class N>
std::optional< N > nix::string2Float (const std::string_view s)
 
template<typename T>
nix::readLittleEndian (unsigned char *p)
 
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::shellEscape (const std::string_view s)
 
void nix::ignoreExceptionInDestructor (Verbosity lvl)
 
void nix::ignoreExceptionExceptInterrupt (Verbosity lvl)
 
std::string nix::base64Encode (std::string_view s)
 
std::string nix::base64Decode (std::string_view s)
 
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::mapped_type * nix::get (const T &map, const typename T::key_type &key)
 
template<class T>
T::mapped_type * nix::get (T &map, const typename T::key_type &key)
 
template<class T>
const T::mapped_type & nix::getOr (T &map, const typename T::key_type &key, const typename T::mapped_type &defaultValue)
 
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<class C, typename T>
void nix::append (C &c, std::initializer_list< T > l)
 
template<typename T, typename TIter = decltype(std::begin(std::declval<T>())), typename = decltype(std::end(std::declval<T>()))>
constexpr auto nix::enumerate (T &&iterable)
 
template<class... Ts>
 nix::overloaded (Ts...) -> overloaded< Ts... >
 
std::string nix::showBytes (uint64_t bytes)
 
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 char nix::treeConn [] = "├───"
 
constexpr char nix::treeLast [] = "└───"
 
constexpr char nix::treeLine [] = "│ "
 
constexpr char nix::treeNull [] = " "
 

Function Documentation

◆ append()

template<class C, typename T>
void nix::append ( C & c,
std::initializer_list< T > l )

Append items to a container. TODO: remove this once we can use C++23's append_range().

◆ base64Decode()

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

Decode arbitrary bytes to Base64.

◆ base64Encode()

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

Encode arbitrary bytes as Base64.

◆ chomp()

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

Remove trailing whitespace from a string.

Todo
return std::string_view.

◆ enumerate()

template<typename T, typename TIter = decltype(std::begin(std::declval<T>())), typename = decltype(std::end(std::declval<T>()))>
auto nix::enumerate ( T && iterable)
constexpr

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

Borrowed from http://reedbeta.com/blog/python-like-enumerate-in-cpp17.

◆ get()

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

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

◆ 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()

template<class T>
const T::mapped_type & nix::getOr ( T & map,
const typename T::key_type & 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

◆ 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.

◆ quoteStrings()

template<class C>
Strings nix::quoteStrings ( const C & c)

Add quotes around a collection of strings.

◆ readLittleEndian()

template<typename T>
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 ( uint64_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.

◆ shellEscape()

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

Escape a string as a shell word.

◆ string2Float()

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

Parse a string into a float.

◆ string2Int()

template<class 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.

Variable Documentation

◆ treeConn

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

Tree formatting.