Spicy
Classes | Functions
hilti::util Namespace Reference

Classes

class  Cache
 
class  SourceCodePreprocessor
 
class  Uniquer
 

Functions

void cannot_be_reached () __attribute__((noreturn))
 
template<typename T >
std::string typename_ ()
 
template<typename... Args>
std::string fmt (const char *fmt, const Args &... args)
 
template<typename X , typename F >
auto transform (const std::vector< X > &x, F f)
 
template<typename X , typename F >
auto transform (const std::set< X > &x, F f)
 
template<typename X , typename F >
auto transform (const std::initializer_list< X > &x, F f)
 
template<typename X , typename F >
auto transform_to_vector (const std::set< X > &x, F f)
 
template<typename X , typename F >
auto filter (const std::vector< X > &x, F f)
 
template<typename X , typename F >
auto filter (const std::set< X > &x, F f)
 
template<typename T , typename TIter = decltype(std::begin(std::declval<T>())), typename = decltype(std::end(std::declval<T>()))>
constexpr auto enumerate (T &&iterable)
 
std::vector< std::string > split (std::string s, const std::string &delim=" ")
 
std::pair< std::string, std::string > split1 (std::string s, const std::string &delim=" ")
 
std::pair< std::string, std::string > rsplit1 (std::string s, const std::string &delim=" ")
 
template<typename T >
std::vector< T > slice (const std::vector< T > &v, int begin, int end=-1)
 
template<typename T >
std::string join (const T &l, const std::string &delim="")
 
template<typename T >
std::string join (const std::initializer_list< T > &l, const std::string &delim="")
 
template<typename iterator >
std::string join (const iterator &begin, const iterator &end, const std::string &delim="")
 
std::string prefixParts (const std::string &in, const std::string &prefix, const std::string &include_tag="")
 
std::vector< std::string > flattenParts (const std::vector< std::string > &in)
 
std::string replace (const std::string &s, const std::string &o, const std::string &n)
 
std::string tolower (const std::string &s)
 
std::string toupper (const std::string &s)
 
std::string trim (const std::string &s)
 
std::string rtrim (const std::string &s)
 
std::string ltrim (const std::string &s)
 
bool startsWith (const std::string &s, const std::string &prefix)
 
bool endsWith (const std::string &s, const std::string &suffix)
 
uint64_t hash (const std::string &str)
 
uint64_t hash (const char *data, size_t len)
 
constexpr std::pair< intmax_t, intmax_t > signed_integer_range (int width)
 
constexpr std::pair< uintmax_t, uintmax_t > unsigned_integer_range (int width)
 
template<typename Error >
uint64_t chars_to_uint64 (const char *dgts, unsigned int base, Error handler)
 
template<typename Error >
double chars_to_double (const char *dgts, Error handler)
 
std::string uitoa_n (uint64_t value, unsigned int base, int n=-1)
 
std::string escapeBytesForCxx (std::string_view s)
 
std::string toIdentifier (const std::string &s, bool ensure_non_keyword=false)
 
double currentTime ()
 
hilti::Result< hilti::rt::filesystem::path > findInPaths (const hilti::rt::filesystem::path &file, const std::vector< hilti::rt::filesystem::path > &paths)
 
hilti::rt::filesystem::path currentExecutable ()
 
void abort_with_backtrace ()
 
template<class Iter , typename Result >
auto atoi_n (Iter s, Iter e, int base, Result *result)
 
template<typename A , typename B >
std::list< std::pair< A, B > > zip2 (const std::list< A > &lhs, const std::list< B > &rhs)
 
template<typename A , typename B >
std::vector< std::pair< A, B > > zip2 (const std::vector< A > &lhs, const std::vector< B > &rhs)
 
template<typename A , typename B >
std::set< A > map_keys (const std::map< A, B > &m)
 
template<typename A , typename B >
std::set< B > map_values (const std::map< A, B > &m)
 
template<typename A , typename B >
std::set< A > map_keys (const std::unordered_map< A, B > &m)
 
template<typename A , typename B >
std::set< B > map_values (const std::unordered_map< A, B > &m)
 
template<typename A , typename Compare = std::less<A>>
std::set< A, Compare > set_difference (const std::set< A, Compare > &a, const std::set< A, Compare > &b)
 
template<typename A , typename Compare = std::less<A>>
std::set< A, Compare > set_intersection (std::set< A, Compare > &a, std::set< A, Compare > &b)
 
template<typename A , typename Compare = std::less<A>>
std::set< A, Compare > set_union (const std::set< A, Compare > &a, const std::set< A, Compare > &b)
 
template<typename T >
std::vector< T > concat (std::vector< T > v1, const std::vector< T > &v2)
 
template<typename T >
std::vector< T > & append (std::vector< T > &v1, const std::vector< T > &v2)
 
template<typename T >
std::vector< T > remove_duplicates (std::vector< T > v)
 
template<typename T >
std::string uniqueIndex (const T &c, std::string hint)
 
bool copyStream (std::istream &in, std::ostream &out)
 
std::optional< hilti::rt::filesystem::path > cacheDirectory (const hilti::Configuration &configuration)
 

Detailed Description

API to measure execution times and frequency for code area.

Function Documentation

◆ abort_with_backtrace()

void hilti::util::abort_with_backtrace ( )

Dumps a backtrace to stderr and then aborts execution.

◆ append()

template<typename T >
std::vector<T>& hilti::util::append ( std::vector< T > &  v1,
const std::vector< T > &  v2 
)

Appends a vector to another one.

◆ atoi_n()

template<class Iter , typename Result >
auto hilti::util::atoi_n ( Iter  s,
Iter  e,
int  base,
Result result 
)
inline

Parses an string into an integer value.

◆ cacheDirectory()

std::optional< hilti::rt::filesystem::path > hilti::util::cacheDirectory ( const hilti::Configuration &  configuration)

Computes path to directory for cached artifacts

Parameters
configurationthe configuration to use
Returns
a valid path to the directory or nothing
Note
While the returned path is valid, it might not exist yet.

◆ cannot_be_reached()

void hilti::util::cannot_be_reached ( )

Aborts with an internal error saying we should not be where we are.

◆ chars_to_double()

template<typename Error >
double hilti::util::chars_to_double ( const char *  dgts,
Error  handler 
)

Converts digits to double precision floating point.

Parameters
dgtsnull-terminated chars: decimal floating-point or hexfloat format.
handleran error-handling function object or lambda.

◆ chars_to_uint64()

template<typename Error >
uint64_t hilti::util::chars_to_uint64 ( const char *  dgts,
unsigned int  base,
Error  handler 
)

Converts digits to an unsigned integer relative to a given base.

Parameters
dgtsnull-terminated chars: decimal digits, hexits or base-n-digits
basebase to use {0,2,3,...,36} (base 0 auto-detects like strtoull).
handleran error-handling function object or lambda.

◆ concat()

template<typename T >
std::vector<T> hilti::util::concat ( std::vector< T >  v1,
const std::vector< T > &  v2 
)

Concatenates two vectors into a new one.

◆ copyStream()

bool hilti::util::copyStream ( std::istream &  in,
std::ostream &  out 
)
inline

Copies the content of one stream into another one. Returns true if successful.

◆ currentExecutable()

hilti::rt::filesystem::path hilti::util::currentExecutable ( )

Returns the path of the current executable.

◆ currentTime()

double hilti::util::currentTime ( )

Returns the current time in seconds since the epoch.

◆ endsWith()

bool hilti::util::endsWith ( const std::string &  s,
const std::string &  suffix 
)

Returns true if a string ends with another.

◆ enumerate()

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

Python-style enumerate() that returns an iterable yielding pairs (index, val). From http://reedbeta.com/blog/python-like-enumerate-in-cpp17/.

◆ escapeBytesForCxx()

std::string hilti::util::escapeBytesForCxx ( std::string_view  s)
inline

Wrapper for escapeBytes that produces a valid C++ string literal.

Parameters
sstring to escape
Returns
escaped std::string

◆ filter() [1/2]

template<typename X , typename F >
auto hilti::util::filter ( const std::vector< X > &  x,
f 
)

Filters a vector through a boolean predicate.

◆ filter() [2/2]

template<typename X , typename F >
auto hilti::util::filter ( const std::set< X > &  x,
f 
)

Filters a set through a boolean predicate.

◆ findInPaths()

hilti::Result< hilti::rt::filesystem::path > hilti::util::findInPaths ( const hilti::rt::filesystem::path &  file,
const std::vector< hilti::rt::filesystem::path > &  paths 
)

Search a file name inside a given set of paths.

◆ flattenParts()

std::vector< std::string > hilti::util::flattenParts ( const std::vector< std::string > &  in)

For each string in a vector, splits them into white-space delimited pieces, then joins all pieces into a single new vector of strings.

Note
This is primarily a helper for creating our configuration files from CMake input.
Parameters
invector with strings to each splits
Returns
reassembled vector

◆ fmt()

template<typename... Args>
std::string hilti::util::fmt ( const char *  fmt,
const Args &...  args 
)

sprintf-style string formatting.

◆ hash() [1/2]

uint64_t hilti::util::hash ( const std::string &  str)

Returns a simple (non-crypto) hash value of a std::string.

◆ hash() [2/2]

uint64_t hilti::util::hash ( const char *  data,
size_t  len 
)

Returns a simple (non-crypto) hash value of a memory block.

◆ join() [1/3]

template<typename T >
std::string hilti::util::join ( const T &  l,
const std::string &  delim = "" 
)

Joins elements of a vector into a string, using a given delimiter to separate them.

◆ join() [2/3]

template<typename T >
std::string hilti::util::join ( const std::initializer_list< T > &  l,
const std::string &  delim = "" 
)

Joins elements of an initializer list into a string, using a given delimiter to separate them.

◆ join() [3/3]

template<typename iterator >
std::string hilti::util::join ( const iterator &  begin,
const iterator &  end,
const std::string &  delim = "" 
)

Joins elements of an iterable range into a string, using a given delimiter to separate then.

◆ ltrim()

std::string hilti::util::ltrim ( const std::string &  s)

Returns a string with all leading white space removed.

◆ map_keys() [1/2]

template<typename A , typename B >
std::set<A> hilti::util::map_keys ( const std::map< A, B > &  m)

Returns the keys of a map as a set.

◆ map_keys() [2/2]

template<typename A , typename B >
std::set<A> hilti::util::map_keys ( const std::unordered_map< A, B > &  m)

Returns the keys of a map as a set.

◆ map_values() [1/2]

template<typename A , typename B >
std::set<B> hilti::util::map_values ( const std::map< A, B > &  m)

Returns the values of a map as a set.

◆ map_values() [2/2]

template<typename A , typename B >
std::set<B> hilti::util::map_values ( const std::unordered_map< A, B > &  m)

Returns the values of a map as a set.

◆ prefixParts()

std::string hilti::util::prefixParts ( const std::string &  in,
const std::string &  prefix,
const std::string &  include_tag = "" 
)

Splits a string into white-space-delimited pieces, prefixes each piece with another string, and then joins it all back together.

Optionally filters out strings with a specific tag: If an inclusion tag is specified, each string is inspected if it starts with !<tag>!. If it does, it's only included if tag == include_tag. Strings without tags are always included.

Note
This is primarily a helper for creating our configuration files from CMake input.
Parameters
instring to split
prefixprefix to add to each part
include_tagfilter tags
Returns
reassembled string with parts prefixed

◆ remove_duplicates()

template<typename T >
std::vector<T> hilti::util::remove_duplicates ( std::vector< T >  v)

Remov duplicates from a vector without changing order.

◆ replace()

std::string hilti::util::replace ( const std::string &  s,
const std::string &  o,
const std::string &  n 
)

Replaces all occurrences of one string with another.

◆ rsplit1()

std::pair< std::string, std::string > hilti::util::rsplit1 ( std::string  s,
const std::string &  delim = " " 
)

Splits a string once at the last occurrence of a delimiter. Leaves the 1st element of the result pair unset if the delimiter does not occur.

◆ rtrim()

std::string hilti::util::rtrim ( const std::string &  s)

Returns a string with all trailing white space removed.

◆ set_difference()

template<typename A , typename Compare = std::less<A>>
std::set<A, Compare> hilti::util::set_difference ( const std::set< A, Compare > &  a,
const std::set< A, Compare > &  b 
)

Returns the difference of two sets. This is a convenience wrapper around std::set_difference.

◆ set_intersection()

template<typename A , typename Compare = std::less<A>>
std::set<A, Compare> hilti::util::set_intersection ( std::set< A, Compare > &  a,
std::set< A, Compare > &  b 
)

Returns the intersection of two sets. This is a convenience wrapper around std::set_intersection.

◆ set_union()

template<typename A , typename Compare = std::less<A>>
std::set<A, Compare> hilti::util::set_union ( const std::set< A, Compare > &  a,
const std::set< A, Compare > &  b 
)

Returns the union of two sets. This is a convenience wrapper around std::set_union.

◆ signed_integer_range()

constexpr std::pair<intmax_t, intmax_t> hilti::util::signed_integer_range ( int  width)

Returns the valid value range for a signed integer of a given width. Supports only standard widths 8/16/32/64.

◆ slice()

template<typename T >
std::vector<T> hilti::util::slice ( const std::vector< T > &  v,
int  begin,
int  end = -1 
)

Returns a subrange of a vector, specified through zero-based indices. If indices are out of range, they are cut back to start/end of input.

Parameters
vvector to slice
begin1st index; if negative, counts from end Python-style
endone beyond last index; if negative, counts from end Python-style

◆ split()

std::vector< std::string > hilti::util::split ( std::string  s,
const std::string &  delim = " " 
)

Splits a string at all occurrences of a delimiter.

◆ split1()

std::pair< std::string, std::string > hilti::util::split1 ( std::string  s,
const std::string &  delim = " " 
)

Splits a string once at the 1st occurrence of a delimiter. Leaves the 2nd element of the result pair unset if the delimiter does not occur.

◆ startsWith()

bool hilti::util::startsWith ( const std::string &  s,
const std::string &  prefix 
)
inline

Returns true if a string begins with another.

◆ toIdentifier()

std::string hilti::util::toIdentifier ( const std::string &  s,
bool  ensure_non_keyword = false 
)

Turns an arbitrary string into something that can be used as C-level identifier.

Parameters
sstring to convert.
ensure_non_keywordif true, the returned ID will be expanded to make sure it won't accidentally match a compiler keyword.
Returns
valid C identifier

◆ tolower()

std::string hilti::util::tolower ( const std::string &  s)

Returns a lower-case version of a string.

◆ toupper()

std::string hilti::util::toupper ( const std::string &  s)

Returns a upper-case version of a string.

◆ transform() [1/3]

template<typename X , typename F >
auto hilti::util::transform ( const std::vector< X > &  x,
f 
)

Applies a function to each element of a vector.

◆ transform() [2/3]

template<typename X , typename F >
auto hilti::util::transform ( const std::set< X > &  x,
f 
)

Applies a function to each element of a set.

◆ transform() [3/3]

template<typename X , typename F >
auto hilti::util::transform ( const std::initializer_list< X > &  x,
f 
)

Applies a function to each element of an initializer_list.

◆ transform_to_vector()

template<typename X , typename F >
auto hilti::util::transform_to_vector ( const std::set< X > &  x,
f 
)

Applies a function to each element of a set, returning a vector with the results.

◆ trim()

std::string hilti::util::trim ( const std::string &  s)

Returns a string with all leading & trailing white space removed.

◆ typename_()

template<typename T >
std::string hilti::util::typename_ ( )

Returns a type's demangled C++ name.

◆ uitoa_n()

std::string hilti::util::uitoa_n ( uint64_t  value,
unsigned int  base,
int  n = -1 
)

Converts an integer into a string relative to a given base.

Parameters
valuevalue to convert
basebase to use
nThe maximum number of characters to include. If the final string would be longer than this, it's cut off. If smaller than zero, includes all.
Returns
converted string

◆ uniqueIndex()

template<typename T >
std::string hilti::util::uniqueIndex ( const T &  c,
std::string  hint 
)

Given an associative container and an index hint, returns a new index value that doesn't exist in the container yet. If the hint itself doesn't exist yet, it's returned directly.

◆ unsigned_integer_range()

constexpr std::pair<uintmax_t, uintmax_t> hilti::util::unsigned_integer_range ( int  width)

Returns the valid value range for an unsigned integer of a given width. Supports only standard widths 8/16/32/64.

◆ zip2() [1/2]

template<typename A , typename B >
std::list<std::pair<A, B> > hilti::util::zip2 ( const std::list< A > &  lhs,
const std::list< B > &  rhs 
)

◆ zip2() [2/2]

template<typename A , typename B >
std::vector<std::pair<A, B> > hilti::util::zip2 ( const std::vector< A > &  lhs,
const std::vector< B > &  rhs 
)

Pairs up the elements of two vectors.

From http://stackoverflow.com/questions/10420380/c-zip-variadic-templates.