retdec
Namespaces | Classes | Typedefs | Enumerations | Functions
retdec::utils Namespace Reference

Namespaces

 io
 

Classes

class  ByteValueStorage
 
class  NonIterableSet
 
struct  EnumClassKeyHash
 
class  CRC32
 compute CRC32 hash, based on Intel's Slicing-by-8 algorithm More...
 
class  DynamicBuffer
 The class for dynamic buffered data manipulation taking the endianness of the data in account. More...
 
class  FilterIterator
 An adapter of an iterator range in which some elements of the range are skipped. More...
 
class  NonCopyable
 A mixin to make classes non-copyable. More...
 
class  ScopeExitGuard
 
struct  ScopeExitGuardHelper
 

Typedefs

using WideCharType = std::uint32_t
 
using WideStringType = std::basic_string< WideCharType >
 

Enumerations

enum class  Endianness { UNKNOWN , LITTLE , BIG }
 

Functions

bool isAligned (std::uint64_t value, std::uint64_t alignment, std::uint64_t &remainder)
 
std::uint64_t alignDown (std::uint64_t value, std::uint64_t alignment)
 
std::uint64_t alignUp (std::uint64_t value, std::uint64_t alignment)
 
template<typename T , std::size_t N>
constexpr std::size_t arraySize (T(&)[N]) noexcept
 Returns the number of elements of the given array as a compile-time constant. More...
 
fs::path getThisBinaryPath ()
 
fs::path getThisBinaryDirectoryPath ()
 
template<typename N >
bool readFile (std::istream &fileStream, std::vector< N > &result, std::size_t start=0, std::size_t desiredSize=0)
 
template<typename N >
bool readFile (const std::string &fileName, std::vector< N > &result, std::size_t start=0, std::size_t desiredSize=0)
 
template<typename N >
bool writeFile (std::ostream &fileStream, const std::vector< N > &data, std::size_t start=0, std::size_t desiredSize=0)
 
template<typename N >
bool writeFile (const std::string &fileName, const std::vector< N > &data, std::size_t start=0, std::size_t desiredSize=0)
 
template<typename N >
bool isPowerOfTwo (N number)
 Check if number is power of two. More...
 
template<typename N >
bool isPowerOfTwoOrZero (N number)
 Check if number is power of two or zero. More...
 
unsigned countBits (unsigned long long n)
 Counts all 1 bits in the given number. More...
 
unsigned bitSizeOfNumber (unsigned long long v)
 Returns the number of bits needed to encode the given number. More...
 
std::size_t getTotalSystemMemory ()
 Returns the total size of system RAM (in bytes). More...
 
bool limitSystemMemory (std::size_t limit)
 Limits system memory to the given size (in bytes). More...
 
bool limitSystemMemoryToHalfOfTotalSystemMemory ()
 Limits system memory to half of the total memory. More...
 
template<typename Function >
auto operator+ (ScopeExitGuardHelper, Function &&f)
 
bool hasOnlyDecimalDigits (const std::string &str)
 Returns true if the given string is formed only by decimal digits. More...
 
bool hasOnlyHexadecimalDigits (const std::string &str)
 Returns true if the given string is formed only by hexadecimal digits. More...
 
bool hasNonprintableChars (const std::string &str)
 Returns true if the given string contains at least one non-printable character. More...
 
bool hasNonasciiChars (const std::string &str)
 Returns true if the given string contains at least one non-ASCII character. More...
 
bool isLowerThanCaseInsensitive (const std::string &str1, const std::string &str2)
 Checks if str1 < str2 (case-insensitively). More...
 
bool areEqualCaseInsensitive (const std::string &str1, const std::string &str2)
 Checks if str1 == str2 (case-insensitively). More...
 
bool isShorterPrefixOfCaseInsensitive (const std::string &str1, const std::string &str2)
 Checks if the shorter string of str1 and str2 is a case-insensitive prefix of the longer string. More...
 
bool contains (const std::string &str, const std::string &sub)
 Checks if str contains sub. More...
 
bool containsAny (const std::string &str, const std::vector< std::string > &subs)
 Check if at least one string from subs is contained in str. More...
 
bool containsCaseInsensitive (const std::string &str, const std::string &sub)
 Find out if string contains another string, no matter the case. More...
 
bool containsAnyOfChars (const std::string &str, const std::string &chars)
 Returns true if str contains at least one character from chars, false otherwise. More...
 
bool containsAnyOfChars (const std::string &str, std::string::value_type c)
 Returns true if str contains c, false otherwise. More...
 
std::string toLower (std::string str)
 Converts all characters in str to lower case. More...
 
std::string toUpper (std::string str)
 Converts all characters in str to upper case. More...
 
std::string toWide (const std::string &str, std::string::size_type length)
 Converts str to wide string. More...
 
std::string unicodeToAscii (const std::uint8_t *bytes, std::size_t nBytes)
 Converts unicode bytes to ASCII string. More...
 
std::string unicodeToAscii (const std::uint8_t *bytes, std::size_t nBytes, std::size_t &nRead)
 Converts unicode bytes to ASCII string. More...
 
std::string readNullTerminatedAscii (const std::uint8_t *bytes, std::size_t bytesLen, std::size_t offset, std::size_t maxBytes, bool failOnExceed)
 Read up to maxBytes bytes as ASCII string. More...
 
std::string trim (std::string str, const std::string &toTrim)
 Trims the given string. More...
 
std::vector< std::string > split (const std::string &str, char sep, bool trimWhitespace)
 Splits the given string by a separator. More...
 
std::string unifyLineEnds (const std::string &str)
 Unifies line ends in the given string to LF. More...
 
template<typename Container >
std::string joinStrings (const Container &strings, const std::string &separator=", ")
 Joins all the strings in strings into a single string. More...
 
std::string addSlashes (const std::string &str, const std::string &toBackslash)
 Returns str with backslashes before characters that need to be quoted, specified in toBackslash. More...
 
std::string replaceCharsWithStrings (const std::string &str, char what, const std::string &withWhat)
 Replaces all occurrences of what with withWhat in str and returns the resulting string. More...
 
template<typename String >
bool startsWith (const std::string &str, const String &withWhat)
 Returns true if str starts with the prefix withWhat, false otherwise. More...
 
bool endsWith (const std::string &str, const std::string &withWhat)
 Retruns true if str ends with the suffix withWhat, false otherwise. More...
 
bool endsWith (const std::string &str, char withWhat)
 Retruns true if str ends with the suffix withWhat, false otherwise. More...
 
bool endsWith (const std::string &str, const std::set< std::string > &withWhat)
 
bool hasSubstringOnPosition (const std::string &str, const std::string &withWhat, std::string::size_type position)
 Returns true if str has substring withWhat on index position. More...
 
bool hasSubstringInArea (const std::string &str, const std::string &withWhat, std::string::size_type start, std::string::size_type stop)
 Returns true if str has substring withWhat in area bordered by offsets start and stop. More...
 
bool isComposedOnlyOfChars (const std::string &str, const std::string &chars)
 Returns true if str is composed solely of chars in chars, false otherwise. More...
 
bool isComposedOnlyOfChars (const std::string &str, std::string::value_type c)
 Returns true if str is composed solely of char c, false otherwise. More...
 
bool isComposedOnlyOfStrings (const std::string &str, const std::string &ss)
 Returns true if str is composed solely of strings ss, false otherwise. More...
 
std::string stripDirs (const std::string &path)
 Strips all directories from the given path. More...
 
std::string replaceAll (const std::string &str, const std::string &from, const std::string &to)
 Replaces all occurrences of from in str with to and returns the string obtained in this way. More...
 
std::string replaceNonprintableChars (const std::string &str)
 Replaces non-printable characters in str with their hexadecimal values. More...
 
std::string replaceNonasciiChars (const std::string &str)
 Replaces non-ASCII characters in str with their hexadecimal values. More...
 
std::string replaceNonalnumCharsWith (const std::string &str, std::string::value_type c)
 Replaces non-alphanumeric characters in str with c. More...
 
std::string removeWhitespace (std::string s)
 Removes all whitespace from the given string. More...
 
std::pair< std::size_t, std::size_t > getLineAndColumnFromPosition (const std::string &json, std::size_t position)
 Transform position in json into line and column location. More...
 
bool isNumber (const std::string &str)
 Checks if the string is a number. More...
 
bool isIdentifier (const std::string &str)
 Checks if the string is a valid C language identifier. More...
 
bool isPrintable (const std::string &str)
 Checks if the string is printable. More...
 
std::string removeLeadingCharacter (const std::string &s, char leading, std::size_t n)
 Removes n leading characters from the given string s and returns the result. More...
 
bool isContolCharacter (char c)
 
bool isNiceCharacter (unsigned char c)
 
bool isNiceString (const std::string &str, double minRatio)
 Does the provided string seem nice, i.e ratio of printable characters and escape sequences in the string is at least minRatio. More...
 
bool isNiceAsciiWideCharacter (unsigned long long c)
 
bool isNiceAsciiWideString (const std::vector< unsigned long long > &str, double minRatio)
 Does the provided wide string consist only from ASCII characters and is nice? Nice string have ration of printable characters and escape sequences in the string is at least minRatio. Empty string is never nice. More...
 
std::string getIndentation (std::size_t count, char c)
 Returns an indentation string containing the specified number of characters. More...
 
void appendHex (std::string &n, const long long a)
 Appends hexadecimal address to string (typically object name). More...
 
void appendDec (std::string &n, const long long a)
 Appends hexadecimal address to string (typically object name). More...
 
std::string appendHexRet (const std::string &n, const long long a)
 Appends hexadecimal address to string and return new string. More...
 
std::string appendDecRet (const std::string &n, const long long a)
 Appends hexadecimal address to string and return new string. More...
 
void removeSuffix (std::string &n, const std::string &suffix)
 Finds the last occurrence of the specified suffix and removes everything from its start to the end. More...
 
std::string removeSuffixRet (const std::string &n, const std::string &suffix)
 Finds the last occurrence of the specified suffix and removes everything from its start to the end. More...
 
std::string normalizeName (const std::string &name)
 Replaces all special symbols by their normalized equivalent. More...
 
std::string normalizeNamePrefix (const std::string &name)
 
bool findFirstInEmbeddedLists (std::size_t &pos, const std::string &str, char c, const std::vector< std::pair< char, char >> &pairs)
 Finds the first occurrence of c character in string str that is outside of embedded lists delimited by pairs. More...
 
std::string removeConsecutiveSpaces (const std::string &str)
 
std::string asEscapedCString (const WideStringType &value, std::size_t charSize)
 Returns the constant's value as an escaped C string. More...
 
std::string removeComments (const std::string &str, char commentChar)
 
std::string extractVersion (const std::string &input)
 
bool isLittleEndian ()
 Finds out if the runtime architecture is little endian. More...
 
bool systemHasLongDouble ()
 Finds out if the runtime system supports long double (at least 10 bytes long). More...
 
std::tm * getCurrentTimestamp ()
 Returns the current timestamp. More...
 
std::string getCurrentDate ()
 Returns the current date in the form YYYY-MM-DD. More...
 
std::string getCurrentTime ()
 Returns the current time in the form HH:MM:SS. More...
 
std::string getCurrentYear ()
 Returns the current year in the form YYYY. More...
 
std::string timestampToDate (std::tm *tm)
 Returns date in human readable form. More...
 
std::string timestampToDate (std::time_t timestamp)
 Returns date in human readable form. More...
 
double getElapsedTime ()
 Returns how much time has elapsed since the program was started (in seconds). More...
 
General Operations with Containers
template<class Container , typename Item >
bool hasItem (const Container &container, const Item &item)
 Returns true if container contains item, false otherwise. More...
 
template<typename Item >
bool hasItem (const std::list< Item > &container, const Item &item)
 A "specialization" of hasItem<>() for std::list. More...
 
template<typename Item >
bool hasItem (const std::vector< Item > &container, const Item &item)
 A "specialization" of hasItem<>() for std::vector. More...
 
template<typename Item >
const Item & getNthItem (const std::vector< Item > &container, std::size_t n)
 Returns the n-th item in container. More...
 
template<typename Item >
const Item & getNthItem (const std::list< Item > &container, std::size_t n)
 Returns the n-th item in container. More...
 
template<class Container , typename Item >
Item getValueOrDefault (const Container &container, const Item &item, Item defaultValue=Item())
 Returns the found value if container contains item, defaultValue otherwise. More...
 
template<typename Item >
Item getValueOrDefault (const std::list< Item > &container, const Item &item, Item defaultValue=Item())
 A "specialization" of getValueOrDefault<>() for std::list. More...
 
template<typename Item >
Item getValueOrDefault (const std::vector< Item > &container, const Item &item, Item defaultValue=Item())
 A "specialization" of getValueOrDefault<>() for std::vector. More...
 
template<typename Item >
void removeItem (std::vector< Item > &v, const Item &item)
 Removes all occurrences of the given item from the given vector. More...
 
template<class Container >
void clear (Container &container)
 Clears the given container. More...
 
template<typename Item >
void clear (std::queue< Item > &q)
 A "specialization" of clear<>() for std::queue. More...
 
template<typename Item >
void clear (std::stack< Item > &s)
 A "specialization" of clear<>() for std::stack. More...
 
template<typename OutputContainer , typename InputContainer , typename Predicate >
OutputContainer filterTo (const InputContainer &input, const Predicate &predicate)
 Returns OutputContainer with items from input that satistfy predicate. More...
 
template<typename Container , typename Predicate >
Container filter (const Container &input, const Predicate &predicate)
 Returns Container with items from input that satistfy predicate. More...
 
Operations with Sets
template<typename T >
void addToSet (const std::set< T > &from, std::set< T > &to)
 Adds all values from from into to. More...
 
template<typename T >
std::set< T > setUnion (const std::set< T > &s1, const std::set< T > &s2)
 Returns the set union s1 \cup s2. More...
 
template<typename T >
std::set< T > setIntersection (const std::set< T > &s1, const std::set< T > &s2)
 Returns the set intersection s1 \cap s2. More...
 
template<typename T >
std::set< T > setDifference (const std::set< T > &s1, const std::set< T > &s2)
 Returns the set difference s1 \setminus s2. More...
 
template<typename T >
void removeFromSet (std::set< T > &from, const std::set< T > &toRemove)
 Removes all values that are in toRemove from from. More...
 
template<typename T >
bool areDisjoint (const std::set< T > &s1, const std::set< T > &s2)
 Returns true if s1 is disjoint with s2. More...
 
template<typename T >
bool shareSomeItem (const std::set< T > &s1, const std::set< T > &s2)
 Returns true if s1 and s2 have at least one item in common. More...
 
Operations with Maps
template<typename Map >
std::set< typename Map::key_type > getKeysFromMap (const Map &m)
 Returns all keys in the given map m. More...
 
template<typename Map >
std::set< typename Map::mapped_type > getValuesFromMap (const Map &m)
 Returns all values in the given map m. More...
 
template<typename Map >
bool mapHasKey (const Map &m, const typename Map::key_type &k)
 Returns true if the given map m has a key k, false otherwise. More...
 
template<typename Map >
bool mapHasValue (const Map &m, const typename Map::mapped_type &v)
 Returns true if the given map m has a value v, false otherwise. More...
 
template<typename Map >
Map::mapped_type mapGetValueOrDefault (const Map &m, const typename Map::key_type &key, typename Map::mapped_type defaultValue=typename Map::mapped_type())
 Returns the value associated to the given key in m, or defaultValue if there is no key in m. More...
 
template<typename Map >
Map::mapped_type mapGetMaxValue (const Map &m)
 Returns the maximum value from m. More...
 
template<typename Map >
Map::mapped_type & addToMap (const typename Map::key_type &key, const typename Map::mapped_type &value, Map &m)
 Adds the pair <key, value> to map m. More...
 
template<typename K , typename V >
std::map< V, K > getMapWithSwappedKeysAndValues (const std::map< K, V > &m)
 Returns a new map that has swapped keys and values. More...
 
Conversions
char * byteToHexString (uint8_t b, bool uppercase=true)
 
template<typename N >
void bytesToHexString (const N *data, std::size_t dataSize, std::string &result, std::size_t offset=0, std::size_t size=0, bool uppercase=true, bool spacing=false)
 
template<typename N >
void bytesToHexString (const std::vector< N > &bytes, std::string &result, std::size_t offset=0, std::size_t size=0, bool uppercase=true, bool spacing=false)
 
template<typename I >
std::string intToHexString (I w, bool addBase=false, unsigned fillToN=0)
 Converts the given integer into its hexadecimal representation. More...
 
std::vector< uint8_t > hexStringToBytes (const std::string &hexIn)
 
template<typename N >
bool strToNum (const std::string &str, N &number, std::ios_base &(*format)(std::ios_base &)=std::dec)
 Converts the given string into a number. More...
 
template<typename N >
std::string bytesToBits (const N *data, std::size_t dataSize)
 Converts the given array of numbers into a bits. More...
 
template<typename N >
std::string bytesToBits (const std::vector< N > &bytes)
 Converts the given vector of numbers into a bits. More...
 
template<typename N >
void bytesToString (const N *data, std::size_t dataSize, std::string &result, std::size_t offset=0, std::size_t size=0)
 
template<typename N >
void bytesToString (const std::vector< N > &bytes, std::string &result, std::size_t offset=0, std::size_t size=0)
 
void double10ToDouble8 (std::vector< unsigned char > &dest, const std::vector< unsigned char > &src)
 Convert 80-bit (10-byte) long double binary data (byte array) into 64-bit (8-byte) double binary data. More...
 
unsigned short byteSwap16 (unsigned short val)
 Swap bytes for Intel x86 16-bit little-endian immediate. More...
 
unsigned int byteSwap32 (unsigned int val)
 Swap bytes for Intel x86 32-bit little-endian immediate. More...
 
std::string byteSwap16 (const std::string &val)
 Swap bytes for Intel x86 16-bit little-endian immediate. More...
 
std::string byteSwap32 (const std::string &val)
 Swap bytes for Intel x86 32-bit little-endian immediate. More...
 
Equality of Values
template<typename T >
bool areEqual (const T &x, const T &y)
 Returns true if x is equal to y, false otherwise. More...
 
template<>
bool areEqual< float > (const float &x, const float &y)
 
template<>
bool areEqual< double > (const double &x, const double &y)
 
template<>
bool areEqual< long double > (const long double &x, const long double &y)
 

Typedef Documentation

◆ WideCharType

using retdec::utils::WideCharType = typedef std::uint32_t

◆ WideStringType

using retdec::utils::WideStringType = typedef std::basic_string<WideCharType>

Enumeration Type Documentation

◆ Endianness

Endianness.

Enumerator
UNKNOWN 
LITTLE 
BIG 

Function Documentation

◆ addSlashes()

std::string retdec::utils::addSlashes ( const std::string &  str,
const std::string &  toBackslash 
)

Returns str with backslashes before characters that need to be quoted, specified in toBackslash.

Parameters
[in]strString to be backslashed.
[in]toBackslashList of characters to be backslashed in str.

By default, toBackslash include a single quote ('), double quote ("), backslash () and NUL (the zero byte).

◆ addToMap()

template<typename Map >
Map::mapped_type& retdec::utils::addToMap ( const typename Map::key_type &  key,
const typename Map::mapped_type &  value,
Map &  m 
)

Adds the pair <key, value> to map m.

Returns
Reference to the added value.
Template Parameters
MapType of the map (std::map or std::unordered_map).

If the key already exists in the map, its value is overwritten.

The behavior of this function is similar to m[key] = value, but does not require values in the map to have the default constructor. To use operator[] in a map, values in the map must have a default constructor. If this is not the case, you cannot use operator[].

◆ addToSet()

template<typename T >
void retdec::utils::addToSet ( const std::set< T > &  from,
std::set< T > &  to 
)

Adds all values from from into to.

Template Parameters
TType of elements in the sets.

◆ alignDown()

std::uint64_t retdec::utils::alignDown ( std::uint64_t  value,
std::uint64_t  alignment 
)

Aligns given value down by specified alignment. Alignment must be power of 2.

Parameters
valueValue to align.
alignmentAlignment to use.
Returns
Value aligned down. If alignment is not power of 2, return value is undefined.

◆ alignUp()

std::uint64_t retdec::utils::alignUp ( std::uint64_t  value,
std::uint64_t  alignment 
)

Aligns given value up by specified alignment. Alignment must be power of 2.

Parameters
valueValue to align.
alignmentAlignment to use.
Returns
Value aligned up. If alignment is not power of 2, return value is undefined.

◆ appendDec()

void retdec::utils::appendDec ( std::string &  n,
const long long  a 
)

Appends hexadecimal address to string (typically object name).

Parameters
nReference to string.
aAddress to append.

◆ appendDecRet()

std::string retdec::utils::appendDecRet ( const std::string &  n,
const long long  a 
)

Appends hexadecimal address to string and return new string.

Parameters
nOriginal to string.
aAddress to append.
Returns
Copy of original string with hexadecimal address.

◆ appendHex()

void retdec::utils::appendHex ( std::string &  n,
const long long  a 
)

Appends hexadecimal address to string (typically object name).

Parameters
nReference to string.
aAddress to append.

◆ appendHexRet()

std::string retdec::utils::appendHexRet ( const std::string &  n,
const long long  a 
)

Appends hexadecimal address to string and return new string.

Parameters
nOriginal to string.
aAddress to append.
Returns
Copy of original string with hexadecimal address.

◆ areDisjoint()

template<typename T >
bool retdec::utils::areDisjoint ( const std::set< T > &  s1,
const std::set< T > &  s2 
)

Returns true if s1 is disjoint with s2.

Template Parameters
TType of elements in the sets.

◆ areEqual()

template<typename T >
bool retdec::utils::areEqual ( const T &  x,
const T &  y 
)
inline

Returns true if x is equal to y, false otherwise.

Template Parameters
TType of x and y.

By default, it returns x == y.

◆ areEqual< double >()

template<>
bool retdec::utils::areEqual< double > ( const double &  x,
const double &  y 
)
inline

◆ areEqual< float >()

template<>
bool retdec::utils::areEqual< float > ( const float &  x,
const float &  y 
)
inline

◆ areEqual< long double >()

template<>
bool retdec::utils::areEqual< long double > ( const long double &  x,
const long double &  y 
)
inline

◆ areEqualCaseInsensitive()

bool retdec::utils::areEqualCaseInsensitive ( const std::string &  str1,
const std::string &  str2 
)

Checks if str1 == str2 (case-insensitively).

◆ arraySize()

template<typename T , std::size_t N>
constexpr std::size_t retdec::utils::arraySize ( T(&)  [N])
constexprnoexcept

Returns the number of elements of the given array as a compile-time constant.

Usage example:

int array[10];
int other_array[arraySize(array)]; // will have 10 elements
constexpr std::size_t arraySize(T(&)[N]) noexcept
Returns the number of elements of the given array as a compile-time constant.
Definition: array.h:28

◆ asEscapedCString()

std::string retdec::utils::asEscapedCString ( const WideStringType value,
std::size_t  charSize 
)

Returns the constant's value as an escaped C string.

◆ bitSizeOfNumber()

unsigned retdec::utils::bitSizeOfNumber ( unsigned long long  v)

Returns the number of bits needed to encode the given number.

◆ bytesToBits() [1/2]

template<typename N >
std::string retdec::utils::bytesToBits ( const N *  data,
std::size_t  dataSize 
)

Converts the given array of numbers into a bits.

Parameters
[in]dataArray of numbers.
[in]dataSizeSize of array.
Returns
Resulting string.

◆ bytesToBits() [2/2]

template<typename N >
std::string retdec::utils::bytesToBits ( const std::vector< N > &  bytes)

Converts the given vector of numbers into a bits.

Parameters
[in]bytesVector to be converted into a bits.
Returns
Resulting string.

◆ bytesToHexString() [1/2]

template<typename N >
void retdec::utils::bytesToHexString ( const N *  data,
std::size_t  dataSize,
std::string &  result,
std::size_t  offset = 0,
std::size_t  size = 0,
bool  uppercase = true,
bool  spacing = false 
)

Converts the given array of numbers into a hexadecimal string representation

Parameters
dataArray to be converted into a hexadecimal string
dataSizeSize of array
resultInto this parameter the result is stored
offsetFirst byte from data which will be converted
sizeNumber of bytes from data for conversion (0 means all bytes from offset)
uppercasetrue if hex letters (A-F) should be uppercase
spacinginsert ' ' between every byte

◆ bytesToHexString() [2/2]

template<typename N >
void retdec::utils::bytesToHexString ( const std::vector< N > &  bytes,
std::string &  result,
std::size_t  offset = 0,
std::size_t  size = 0,
bool  uppercase = true,
bool  spacing = false 
)

Converts the given vector of numbers into a hexadecimal string representation

Parameters
bytesVector to be converted into a hexadecimal string
resultInto this parameter the result is stored
offsetFirst byte from bytes which will be converted
sizeNumber of bytes from bytes for conversion (0 means all bytes from offset)
uppercasetrue if hex letters (A-F) should be uppercase
spacinginsert ' ' between every byte

◆ bytesToString() [1/2]

template<typename N >
void retdec::utils::bytesToString ( const N *  data,
std::size_t  dataSize,
std::string &  result,
std::size_t  offset = 0,
std::size_t  size = 0 
)

Converts the given array of numbers into a string

Parameters
dataArray to be converted into a string
dataSizeSize of array
resultInto this parameter the result is stored
offsetFirst byte from data which will be converted to string
sizeNumber of bytes from data for conversion (0 means all bytes from offset)

◆ bytesToString() [2/2]

template<typename N >
void retdec::utils::bytesToString ( const std::vector< N > &  bytes,
std::string &  result,
std::size_t  offset = 0,
std::size_t  size = 0 
)

Converts the given vector of numbers into a string

Parameters
bytesVector to be converted into a string
resultInto this parameter the result is stored
offsetFirst byte from bytes which will be converted to string
sizeNumber of bytes from bytes for conversion (0 means all bytes from offset)

◆ byteSwap16() [1/2]

std::string retdec::utils::byteSwap16 ( const std::string &  val)

Swap bytes for Intel x86 16-bit little-endian immediate.

Parameters
valOriginal value.
Returns
Value with swapped bytes or original value if its size is not 16.

◆ byteSwap16() [2/2]

unsigned short retdec::utils::byteSwap16 ( unsigned short  val)

Swap bytes for Intel x86 16-bit little-endian immediate.

Parameters
valOriginal value.
Returns
Value with swapped bytes

◆ byteSwap32() [1/2]

std::string retdec::utils::byteSwap32 ( const std::string &  val)

Swap bytes for Intel x86 32-bit little-endian immediate.

Parameters
valOriginal value.
Returns
Value with swapped bytes or original value if its size is not 32.

◆ byteSwap32() [2/2]

unsigned int retdec::utils::byteSwap32 ( unsigned int  val)

Swap bytes for Intel x86 32-bit little-endian immediate.

Parameters
valOriginal value.
Returns
Value with swapped bytes

◆ byteToHexString()

char * retdec::utils::byteToHexString ( uint8_t  b,
bool  uppercase 
)

◆ clear() [1/3]

template<class Container >
void retdec::utils::clear ( Container &  container)

Clears the given container.

Template Parameters
ContainerType of the container.

◆ clear() [2/3]

template<typename Item >
void retdec::utils::clear ( std::queue< Item > &  q)

A "specialization" of clear<>() for std::queue.

◆ clear() [3/3]

template<typename Item >
void retdec::utils::clear ( std::stack< Item > &  s)

A "specialization" of clear<>() for std::stack.

◆ contains()

bool retdec::utils::contains ( const std::string &  str,
const std::string &  sub 
)

Checks if str contains sub.

◆ containsAny()

bool retdec::utils::containsAny ( const std::string &  str,
const std::vector< std::string > &  subs 
)

Check if at least one string from subs is contained in str.

◆ containsAnyOfChars() [1/2]

bool retdec::utils::containsAnyOfChars ( const std::string &  str,
const std::string &  chars 
)

Returns true if str contains at least one character from chars, false otherwise.

If chars is the empty string, it returns false.

◆ containsAnyOfChars() [2/2]

bool retdec::utils::containsAnyOfChars ( const std::string &  str,
std::string::value_type  c 
)

Returns true if str contains c, false otherwise.

◆ containsCaseInsensitive()

bool retdec::utils::containsCaseInsensitive ( const std::string &  str,
const std::string &  sub 
)

Find out if string contains another string, no matter the case.

Parameters
strString to search in.
subString to search for.
Returns
true if string contains another string, false otherwise.

◆ countBits()

unsigned retdec::utils::countBits ( unsigned long long  n)

Counts all 1 bits in the given number.

◆ double10ToDouble8()

void retdec::utils::double10ToDouble8 ( std::vector< unsigned char > &  dest,
const std::vector< unsigned char > &  src 
)

Convert 80-bit (10-byte) long double binary data (byte array) into 64-bit (8-byte) double binary data.

Parameters
[out]dest64-bit double to create.
[in]src80-bit long double to convert.

◆ endsWith() [1/3]

bool retdec::utils::endsWith ( const std::string &  str,
char  withWhat 
)

Retruns true if str ends with the suffix withWhat, false otherwise.

◆ endsWith() [2/3]

bool retdec::utils::endsWith ( const std::string &  str,
const std::set< std::string > &  withWhat 
)
Returns
true if str ends with any of the suffixes in withWhat, false otherwise

◆ endsWith() [3/3]

bool retdec::utils::endsWith ( const std::string &  str,
const std::string &  withWhat 
)

Retruns true if str ends with the suffix withWhat, false otherwise.

◆ extractVersion()

std::string retdec::utils::extractVersion ( const std::string &  input)

Search for version stored in input string

Parameters
inputInput string
Returns
Found version or empty string if no version found.

A version is considered to be a substring which consisting of numbers (and dots). If input string contains more versions, result contains only the first one.

◆ filter()

template<typename Container , typename Predicate >
Container retdec::utils::filter ( const Container &  input,
const Predicate &  predicate 
)

Returns Container with items from input that satistfy predicate.

It is a shortcut for filterTo<Container>(input, predicate).

Usage example:

auto result = filter(
std::vector<int>{1, 2, 3, 4, 5},
[](auto i) { return i % 2 == 0; }
);
Container filter(const Container &input, const Predicate &predicate)
Returns Container with items from input that satistfy predicate.
Definition: container.h:223

The type of result is Container, i.e. std::vector<int>.

◆ filterTo()

template<typename OutputContainer , typename InputContainer , typename Predicate >
OutputContainer retdec::utils::filterTo ( const InputContainer &  input,
const Predicate &  predicate 
)

Returns OutputContainer with items from input that satistfy predicate.

This function is an implementation of the standard functional filter() function.

Usage example:

auto result = filterTo<std::set<int>>(
std::vector<int>{1, 2, 3, 4, 5},
[](auto i) { return i % 2 == 0; }
);

The type of result is OutputContainer, i.e. std::set<int>.

◆ findFirstInEmbeddedLists()

bool retdec::utils::findFirstInEmbeddedLists ( std::size_t &  pos,
const std::string &  str,
char  c,
const std::vector< std::pair< char, char >> &  pairs 
)

Finds the first occurrence of c character in string str that is outside of embedded lists delimited by pairs.

Parameters
[out]posCharacter position of std::string::npos if character not found. Position is left unchanged, if true is returned.
strString to find occerrences in.
cCharacter to find.
pairsVector of delimiter pairs: <delim_start,delim_end>.
Returns
False if substring before the first found occurrence is ok (delimiter pairs match each other), true otherwise.

The check is performed only before the occurrence is found, after that, string may not be ok. Only delimiter numbers are checked, not their correct positions. For example, this is malformed string because delimiters ("{}") do not match before character ',' we search for: "{abc{},def". However, this is currently ok (search for ',', delimiters "{}()"): "{a(b}c),def".

◆ getCurrentDate()

std::string retdec::utils::getCurrentDate ( )

Returns the current date in the form YYYY-MM-DD.

◆ getCurrentTime()

std::string retdec::utils::getCurrentTime ( )

Returns the current time in the form HH:MM:SS.

◆ getCurrentTimestamp()

std::tm * retdec::utils::getCurrentTimestamp ( )

Returns the current timestamp.

◆ getCurrentYear()

std::string retdec::utils::getCurrentYear ( )

Returns the current year in the form YYYY.

◆ getElapsedTime()

double retdec::utils::getElapsedTime ( )

Returns how much time has elapsed since the program was started (in seconds).

◆ getIndentation()

std::string retdec::utils::getIndentation ( std::size_t  count,
char  c = '\t' 
)

Returns an indentation string containing the specified number of characters.

◆ getKeysFromMap()

template<typename Map >
std::set<typename Map::key_type> retdec::utils::getKeysFromMap ( const Map &  m)

Returns all keys in the given map m.

Template Parameters
MapType of the map (std::map or std::unordered_map).

◆ getLineAndColumnFromPosition()

std::pair< std::size_t, std::size_t > retdec::utils::getLineAndColumnFromPosition ( const std::string &  json,
std::size_t  position 
)

Transform position in json into line and column location.

Parameters
jsonJSON string.
positionByte distance from start of JSON string.
Returns
Pair (line, column).

◆ getMapWithSwappedKeysAndValues()

template<typename K , typename V >
std::map<V, K> retdec::utils::getMapWithSwappedKeysAndValues ( const std::map< K, V > &  m)

Returns a new map that has swapped keys and values.

Template Parameters
KType of objects serving as keys.
VType of objects serving as values.

For example, if you have std::map<int, std::string>, this function returns std::map<std::string, int>.

You have to ensure that all values in m are distinct; otherwise, the returned map may have less elements than m.

◆ getNthItem() [1/2]

template<typename Item >
const Item& retdec::utils::getNthItem ( const std::list< Item > &  container,
std::size_t  n 
)

Returns the n-th item in container.

Template Parameters
ItemType of the items that container holds.
Preconditions
  • 1 <= n <= container.size()

◆ getNthItem() [2/2]

template<typename Item >
const Item& retdec::utils::getNthItem ( const std::vector< Item > &  container,
std::size_t  n 
)

Returns the n-th item in container.

Template Parameters
ItemType of the items that container holds.
Preconditions
  • 1 <= n <= container.size()

◆ getThisBinaryDirectoryPath()

fs::path retdec::utils::getThisBinaryDirectoryPath ( )

Get absolute path to directory of currently running binary.

Returns
absolute path

◆ getThisBinaryPath()

fs::path retdec::utils::getThisBinaryPath ( )

Get absolute path of currently running binary.

Returns
absolute path

◆ getTotalSystemMemory()

std::size_t retdec::utils::getTotalSystemMemory ( )

Returns the total size of system RAM (in bytes).

When the size cannot be obtained, it returns 0.

◆ getValueOrDefault() [1/3]

template<class Container , typename Item >
Item retdec::utils::getValueOrDefault ( const Container &  container,
const Item &  item,
Item  defaultValue = Item() 
)

Returns the found value if container contains item, defaultValue otherwise.

Template Parameters
ContainerType of the container.
ItemType of the items that container holds.

If Container is a map, the searched and returned values are pairs. To get a value corresponding to a given key from a map, use mapGetValueOrDefault<>().

◆ getValueOrDefault() [2/3]

template<typename Item >
Item retdec::utils::getValueOrDefault ( const std::list< Item > &  container,
const Item &  item,
Item  defaultValue = Item() 
)

A "specialization" of getValueOrDefault<>() for std::list.

◆ getValueOrDefault() [3/3]

template<typename Item >
Item retdec::utils::getValueOrDefault ( const std::vector< Item > &  container,
const Item &  item,
Item  defaultValue = Item() 
)

A "specialization" of getValueOrDefault<>() for std::vector.

◆ getValuesFromMap()

template<typename Map >
std::set<typename Map::mapped_type> retdec::utils::getValuesFromMap ( const Map &  m)

Returns all values in the given map m.

Template Parameters
MapType of the map (std::map or std::unordered_map).

◆ hasItem() [1/3]

template<class Container , typename Item >
bool retdec::utils::hasItem ( const Container &  container,
const Item &  item 
)

Returns true if container contains item, false otherwise.

Template Parameters
ContainerType of the container.
ItemType of the items that container holds.

If Container is a map, Item has to be a key. To check whether a map contains a value, use mapHasValue<>().

◆ hasItem() [2/3]

template<typename Item >
bool retdec::utils::hasItem ( const std::list< Item > &  container,
const Item &  item 
)

A "specialization" of hasItem<>() for std::list.

◆ hasItem() [3/3]

template<typename Item >
bool retdec::utils::hasItem ( const std::vector< Item > &  container,
const Item &  item 
)

A "specialization" of hasItem<>() for std::vector.

◆ hasNonasciiChars()

bool retdec::utils::hasNonasciiChars ( const std::string &  str)

Returns true if the given string contains at least one non-ASCII character.

◆ hasNonprintableChars()

bool retdec::utils::hasNonprintableChars ( const std::string &  str)

Returns true if the given string contains at least one non-printable character.

◆ hasOnlyDecimalDigits()

bool retdec::utils::hasOnlyDecimalDigits ( const std::string &  str)

Returns true if the given string is formed only by decimal digits.

The empty string is considered to be composed only by digits.

◆ hasOnlyHexadecimalDigits()

bool retdec::utils::hasOnlyHexadecimalDigits ( const std::string &  str)

Returns true if the given string is formed only by hexadecimal digits.

The empty string is considered to be composed only by hexadecimal digits.

◆ hasSubstringInArea()

bool retdec::utils::hasSubstringInArea ( const std::string &  str,
const std::string &  withWhat,
std::string::size_type  start,
std::string::size_type  stop 
)

Returns true if str has substring withWhat in area bordered by offsets start and stop.

◆ hasSubstringOnPosition()

bool retdec::utils::hasSubstringOnPosition ( const std::string &  str,
const std::string &  withWhat,
std::string::size_type  position 
)

Returns true if str has substring withWhat on index position.

◆ hexStringToBytes()

std::vector< uint8_t > retdec::utils::hexStringToBytes ( const std::string &  hexIn)

Convert hexadecimal string hexIn string into bytes. There might be whitespaces in the string, e.g. "0b 84 d1 a0 80 60 40" is the same as "0b84d1a0806040".

◆ intToHexString()

template<typename I >
std::string retdec::utils::intToHexString ( w,
bool  addBase = false,
unsigned  fillToN = 0 
)

Converts the given integer into its hexadecimal representation.

Parameters
[in]wNumber to be converted.
[in]addBasePrepends "0x" before the result.
[in]fillToNIf needed, prepends "0" before the result to get at least fillToN characters long string.

All letters in the result are lowercase.

◆ isAligned()

bool retdec::utils::isAligned ( std::uint64_t  value,
std::uint64_t  alignment,
std::uint64_t &  remainder 
)

Checks whether given value is aligned based on alignment value. Alignment must be power of 2.

Parameters
valueValue to be checked.
alignmentAlignment to check. Must be power of 2.
remainderOutput value that is non-zero if value is not aligned and zero if it is aligned. It contains value modulo alignment. Contains undefined value if alignment is not power of 2.
Returns
True if value is aligned to given alignment, false otherwise. If alignment is not power of 2 the return value is undefined.

◆ isComposedOnlyOfChars() [1/2]

bool retdec::utils::isComposedOnlyOfChars ( const std::string &  str,
const std::string &  chars 
)

Returns true if str is composed solely of chars in chars, false otherwise.

If chars is the empty string, it returns false.

◆ isComposedOnlyOfChars() [2/2]

bool retdec::utils::isComposedOnlyOfChars ( const std::string &  str,
std::string::value_type  c 
)

Returns true if str is composed solely of char c, false otherwise.

◆ isComposedOnlyOfStrings()

bool retdec::utils::isComposedOnlyOfStrings ( const std::string &  str,
const std::string &  ss 
)

Returns true if str is composed solely of strings ss, false otherwise.

Examples:

isComposedOnlyOfStrings("abcd", "abcd") -> true
isComposedOnlyOfStrings("ababab", "ab") -> true
isComposedOnlyOfStrings("aaaaa", "aa") -> true
isComposedOnlyOfStrings("", "") -> true
isComposedOnlyOfStrings("ababab", "ba") -> false
isComposedOnlyOfStrings("hello", "") -> false
bool isComposedOnlyOfStrings(const std::string &str, const std::string &ss)
Returns true if str is composed solely of strings ss, false otherwise.
Definition: string.cpp:758

◆ isContolCharacter()

bool retdec::utils::isContolCharacter ( char  c)
Returns
True if character c is a control character, false otherwise.

◆ isIdentifier()

bool retdec::utils::isIdentifier ( const std::string &  str)

Checks if the string is a valid C language identifier.

Empty string is not valid identifier.

◆ isLittleEndian()

bool retdec::utils::isLittleEndian ( )

Finds out if the runtime architecture is little endian.

◆ isLowerThanCaseInsensitive()

bool retdec::utils::isLowerThanCaseInsensitive ( const std::string &  str1,
const std::string &  str2 
)

Checks if str1 < str2 (case-insensitively).

This function doesn't consider non-ASCII character sets.

◆ isNiceAsciiWideCharacter()

bool retdec::utils::isNiceAsciiWideCharacter ( unsigned long long  c)
Returns
True if character c is a nice ASCII wide character. False otherwise.

◆ isNiceAsciiWideString()

bool retdec::utils::isNiceAsciiWideString ( const std::vector< unsigned long long > &  str,
double  minRatio 
)

Does the provided wide string consist only from ASCII characters and is nice? Nice string have ration of printable characters and escape sequences in the string is at least minRatio. Empty string is never nice.

Parameters
strWide string to check.
minRatioMinimum ratio of printable characters.
Returns
True if the string seems nice, false otherwise.

◆ isNiceCharacter()

bool retdec::utils::isNiceCharacter ( unsigned char  c)
Returns
True if character c is a nice character (printable or control). False otherwise.

◆ isNiceString()

bool retdec::utils::isNiceString ( const std::string &  str,
double  minRatio 
)

Does the provided string seem nice, i.e ratio of printable characters and escape sequences in the string is at least minRatio.

Parameters
strString to check.
minRatioMinimum ratio of printable characters.
Returns
True if the string seems nice, false otherwise.

Empty string is never nice.

◆ isNumber()

bool retdec::utils::isNumber ( const std::string &  str)

Checks if the string is a number.

◆ isPowerOfTwo()

template<typename N >
bool retdec::utils::isPowerOfTwo ( number)

Check if number is power of two.

Parameters
[in]numberValue which will be checked.
Template Parameters
NType of number.

◆ isPowerOfTwoOrZero()

template<typename N >
bool retdec::utils::isPowerOfTwoOrZero ( number)

Check if number is power of two or zero.

Parameters
[in]numberValue which will be checked.
Template Parameters
NType of number.

◆ isPrintable()

bool retdec::utils::isPrintable ( const std::string &  str)

Checks if the string is printable.

Empty string is considered printable. This is different from isNiceString because this function considers only printable characters, not control ones.

Parameters
strinput string
Returns
true if input string is printable, false otherwise.

◆ isShorterPrefixOfCaseInsensitive()

bool retdec::utils::isShorterPrefixOfCaseInsensitive ( const std::string &  str1,
const std::string &  str2 
)

Checks if the shorter string of str1 and str2 is a case-insensitive prefix of the longer string.

◆ joinStrings()

template<typename Container >
std::string retdec::utils::joinStrings ( const Container &  strings,
const std::string &  separator = ", " 
)

Joins all the strings in strings into a single string.

Parameters
[in]stringsStrings to be joined.
[in]separatorSeparator to separate individual strings.
Template Parameters
ContainerType of strings.

If Container is an unordered container, the order depends on the implementation of the container.

◆ limitSystemMemory()

bool retdec::utils::limitSystemMemory ( std::size_t  limit)

Limits system memory to the given size (in bytes).

Returns
true if the limiting succeeded, false otherwise.

When limit is 0, it immediately returns false.

This function is not reentrant, i.e. it is not safe to call it simultaneously from multiple threads.

◆ limitSystemMemoryToHalfOfTotalSystemMemory()

bool retdec::utils::limitSystemMemoryToHalfOfTotalSystemMemory ( )

Limits system memory to half of the total memory.

◆ mapGetMaxValue()

template<typename Map >
Map::mapped_type retdec::utils::mapGetMaxValue ( const Map &  m)

Returns the maximum value from m.

If m is empty, this function returns Map::mapped_type() (default-constructed value).

◆ mapGetValueOrDefault()

template<typename Map >
Map::mapped_type retdec::utils::mapGetValueOrDefault ( const Map &  m,
const typename Map::key_type &  key,
typename Map::mapped_type  defaultValue = typename Map::mapped_type() 
)

Returns the value associated to the given key in m, or defaultValue if there is no key in m.

Template Parameters
MapType of the map (std::map or std::unordered_map).

◆ mapHasKey()

template<typename Map >
bool retdec::utils::mapHasKey ( const Map &  m,
const typename Map::key_type &  k 
)

Returns true if the given map m has a key k, false otherwise.

Template Parameters
MapType of the map (std::map or std::unordered_map).

◆ mapHasValue()

template<typename Map >
bool retdec::utils::mapHasValue ( const Map &  m,
const typename Map::mapped_type &  v 
)

Returns true if the given map m has a value v, false otherwise.

Template Parameters
MapType of the map (std::map or std::unordered_map).

◆ normalizeName()

std::string retdec::utils::normalizeName ( const std::string &  name)

Replaces all special symbols by their normalized equivalent.

Parameters
[in]nameInput string.
Returns
String with substituted special symbols.

◆ normalizeNamePrefix()

std::string retdec::utils::normalizeNamePrefix ( const std::string &  name)

TODO: Is this and normalizeName() really needed/wanted? If so, can they be merged into one. If not remove them.

◆ operator+()

template<typename Function >
auto retdec::utils::operator+ ( ScopeExitGuardHelper  ,
Function &&  f 
)

◆ readFile() [1/2]

template<typename N >
bool retdec::utils::readFile ( const std::string &  fileName,
std::vector< N > &  result,
std::size_t  start = 0,
std::size_t  desiredSize = 0 
)

Read bytes from file

Parameters
fileNameName of the file
resultInto this parameter the resulting bytes are stored
startStart offset of read
desiredSizeNumber of bytes for read. If this parameter is set to zero, function will read all bytes from start until end of file.
Returns
true if operation went OK, otherwise false

If function returns false, bytes is set to empty vector

◆ readFile() [2/2]

template<typename N >
bool retdec::utils::readFile ( std::istream &  fileStream,
std::vector< N > &  result,
std::size_t  start = 0,
std::size_t  desiredSize = 0 
)

Read bytes from file stream

Parameters
fileStreamRepresentation of input file
resultInto this parameter the resulting bytes are stored
startStart offset of read
desiredSizeNumber of bytes for read. If this parameter is set to zero, function will read all bytes from start until end of file.
Returns
true if operation went OK, otherwise false

If function returns false, bytes is set to empty vector

◆ readNullTerminatedAscii()

std::string retdec::utils::readNullTerminatedAscii ( const std::uint8_t *  bytes,
std::size_t  bytesLen,
std::size_t  offset,
std::size_t  maxBytes,
bool  failOnExceed 
)

Read up to maxBytes bytes as ASCII string.

Parameters
[in]bytesBytes to read from.
[in]bytesLenLength of bytes
[in]offsetOffset in bytes.
[in]maxBytesMaximum of bytes to read. Zero indicates as much as possible.
[in]failOnExceedIf string isn't null terminated until maxBytes, an empty string is returned
Returns
Converted string in ASCII.

◆ removeComments()

std::string retdec::utils::removeComments ( const std::string &  str,
char  commentChar 
)

Remove comments from string. Comment must start with a single commentChar character and end on new line (i.e. '
') character. For example LLVM comment: a = add i32 0, 0 ; this part will be removed

Parameters
strString from which to remove comments.
commentCharCharacter used to start the comment (e.g. ';').
Returns
String without comments.

◆ removeConsecutiveSpaces()

std::string retdec::utils::removeConsecutiveSpaces ( const std::string &  str)

◆ removeFromSet()

template<typename T >
void retdec::utils::removeFromSet ( std::set< T > &  from,
const std::set< T > &  toRemove 
)

Removes all values that are in toRemove from from.

Template Parameters
TType of elements in the sets.

◆ removeItem()

template<typename Item >
void retdec::utils::removeItem ( std::vector< Item > &  v,
const Item &  item 
)

Removes all occurrences of the given item from the given vector.

Template Parameters
ItemType of the items that the vector holds.

◆ removeLeadingCharacter()

std::string retdec::utils::removeLeadingCharacter ( const std::string &  s,
char  leading,
std::size_t  n 
)

Removes n leading characters from the given string s and returns the result.

Parameters
sString from which leading characters are tp be removed.
leadingLeading character to remove.
nMax number of characters to remove. If not set, remove as much as possible.

◆ removeSuffix()

void retdec::utils::removeSuffix ( std::string &  n,
const std::string &  suffix 
)

Finds the last occurrence of the specified suffix and removes everything from its start to the end.

Parameters
[out]nReference to string.
[in]suffixSuffix to find and remove.

◆ removeSuffixRet()

std::string retdec::utils::removeSuffixRet ( const std::string &  n,
const std::string &  suffix 
)

Finds the last occurrence of the specified suffix and removes everything from its start to the end.

Parameters
[in]nSource string.
[in]suffixSuffix to find and remove.
Returns
Copy of source string without suffix address.

◆ removeWhitespace()

std::string retdec::utils::removeWhitespace ( std::string  s)

Removes all whitespace from the given string.

◆ replaceAll()

std::string retdec::utils::replaceAll ( const std::string &  str,
const std::string &  from,
const std::string &  to 
)

Replaces all occurrences of from in str with to and returns the string obtained in this way.

If from is the empty string, it returns str.

◆ replaceCharsWithStrings()

std::string retdec::utils::replaceCharsWithStrings ( const std::string &  str,
char  what,
const std::string &  withWhat 
)

Replaces all occurrences of what with withWhat in str and returns the resulting string.

◆ replaceNonalnumCharsWith()

std::string retdec::utils::replaceNonalnumCharsWith ( const std::string &  str,
std::string::value_type  c 
)

Replaces non-alphanumeric characters in str with c.

◆ replaceNonasciiChars()

std::string retdec::utils::replaceNonasciiChars ( const std::string &  str)

Replaces non-ASCII characters in str with their hexadecimal values.

◆ replaceNonprintableChars()

std::string retdec::utils::replaceNonprintableChars ( const std::string &  str)

Replaces non-printable characters in str with their hexadecimal values.

◆ setDifference()

template<typename T >
std::set<T> retdec::utils::setDifference ( const std::set< T > &  s1,
const std::set< T > &  s2 
)

Returns the set difference s1 \setminus s2.

In other words, this function returns the set whose elements are in s1 but are not in s2.

Template Parameters
TType of elements in the sets.

◆ setIntersection()

template<typename T >
std::set<T> retdec::utils::setIntersection ( const std::set< T > &  s1,
const std::set< T > &  s2 
)

Returns the set intersection s1 \cap s2.

In other words, this function returns the set whose elements are in both s1 and s2.

Template Parameters
TType of elements in the sets.

◆ setUnion()

template<typename T >
std::set<T> retdec::utils::setUnion ( const std::set< T > &  s1,
const std::set< T > &  s2 
)

Returns the set union s1 \cup s2.

In other words, this function returns the set whose elements are in s1 or in s2.

Template Parameters
TType of elements in the sets.

◆ shareSomeItem()

template<typename T >
bool retdec::utils::shareSomeItem ( const std::set< T > &  s1,
const std::set< T > &  s2 
)

Returns true if s1 and s2 have at least one item in common.

Template Parameters
TType of elements in the sets.

◆ split()

std::vector< std::string > retdec::utils::split ( const std::string &  str,
char  sep,
bool  trimWhitespace 
)

Splits the given string by a separator.

Parameters
[in]strString to be splitted.
[in]sepSeparator to be used.
[in]trimWhitespaceIf true, trims whitespace around the separated strings.

For example,

split("; a ; b ; c ;", ';', true)
std::vector< std::string > split(const std::string &str, char sep=',', bool trimWhitespace=true)
Splits the given string by a separator.
Definition: string.cpp:599

returns

["", "a", "b", "c", ""]

◆ startsWith()

template<typename String >
bool retdec::utils::startsWith ( const std::string &  str,
const String &  withWhat 
)

Returns true if str starts with the prefix withWhat, false otherwise.

Template Parameters
StringEither std::string or char *.

◆ stripDirs()

std::string retdec::utils::stripDirs ( const std::string &  path)

Strips all directories from the given path.

For example, stripDirs("/home/user/test.c") returns "test.c".

TODO Implement the following functionality:

  • paths on MS Windows ('\' vs '/')
  • allow backslashed '/' in file names

◆ strToNum()

template<typename N >
bool retdec::utils::strToNum ( const std::string &  str,
N &  number,
std::ios_base &(*)(std::ios_base &)  format = std::dec 
)
inline

Converts the given string into a number.

Parameters
[in]strString to be converted into a number.
[out]numberInto this parameter the resulting number is stored.
[in]formatNumber format (e.g. std::dec, std::hex).
Returns
true if the conversion went ok, false otherwise.

If the conversion fails, number is left unchanged.

◆ systemHasLongDouble()

bool retdec::utils::systemHasLongDouble ( )

Finds out if the runtime system supports long double (at least 10 bytes long).

◆ timestampToDate() [1/2]

std::string retdec::utils::timestampToDate ( std::time_t  timestamp)

Returns date in human readable form.

Parameters
timestampTimestamp for conversion.

◆ timestampToDate() [2/2]

std::string retdec::utils::timestampToDate ( std::tm *  tm)

Returns date in human readable form.

Parameters
tmTimestamp for conversion.

◆ toLower()

std::string retdec::utils::toLower ( std::string  str)

Converts all characters in str to lower case.

For example, "Crazy Willy" is converted into "crazy willy".

◆ toUpper()

std::string retdec::utils::toUpper ( std::string  str)

Converts all characters in str to upper case.

For example, "Crazy Willy" is converted into "CRAZY WILLY".

◆ toWide()

std::string retdec::utils::toWide ( const std::string &  str,
std::string::size_type  length 
)

Converts str to wide string.

Parameters
[in]strString for conversion.
[in]lengthLength in bytes of one character in output string. If length is zero, function returns empty string.
Returns
Converted string.

◆ trim()

std::string retdec::utils::trim ( std::string  str,
const std::string &  toTrim 
)

Trims the given string.

Parameters
[in]strString to be trimmed.
[in]toTrimString of characters to be trimmed (removed) from the beginning and the end of str. By default, it contains all whitespace characters from the ASCII set.
Returns
Trimmed string.

For example, trim(" hey there ", " ") returns "hey there".

◆ unicodeToAscii() [1/2]

std::string retdec::utils::unicodeToAscii ( const std::uint8_t *  bytes,
std::size_t  nBytes 
)

Converts unicode bytes to ASCII string.

Parameters
[in]bytesBytes for conversion.
[in]nBytesNumber of bytes.
Returns
Converted string in ASCII.

◆ unicodeToAscii() [2/2]

std::string retdec::utils::unicodeToAscii ( const std::uint8_t *  bytes,
std::size_t  nBytes,
std::size_t &  nRead 
)

Converts unicode bytes to ASCII string.

Parameters
[in]bytesBytes for conversion.
[in]nBytesNumber of bytes.
[in]nReadNumber of bytes read. Note that this doesn't have to be the length of returned string
Returns
Converted string in ASCII.

◆ unifyLineEnds()

std::string retdec::utils::unifyLineEnds ( const std::string &  str)

Unifies line ends in the given string to LF.

Returns
String str with unified line ends.

In a greater detail, this function converts CRLF and CR inside str to LF.

◆ writeFile() [1/2]

template<typename N >
bool retdec::utils::writeFile ( const std::string &  fileName,
const std::vector< N > &  data,
std::size_t  start = 0,
std::size_t  desiredSize = 0 
)

Write bytes to file

Parameters
fileNameName of the file
dataData to write into the file
startStart offset of write
desiredSizeNumber of bytes to write. If this parameter is set to zero, function will write all bytes from data.
Returns
true if operation went OK, otherwise false

◆ writeFile() [2/2]

template<typename N >
bool retdec::utils::writeFile ( std::ostream &  fileStream,
const std::vector< N > &  data,
std::size_t  start = 0,
std::size_t  desiredSize = 0 
)

Write bytes to file

Parameters
fileStreamRepresentation of output file
dataData to write into the file
startStart offset of write
desiredSizeNumber of bytes to write. If this parameter is set to zero, function will write all bytes from data.
Returns
true if operation went OK, otherwise false