The Sparta Modeling Framework
Loading...
Searching...
No Matches
StringUtils.hpp File Reference

Cool string utilities. More...

#include <cinttypes>
#include <cstring>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <memory>

Go to the source code of this file.

Classes

class  sparta::utils::TransformedString< AppliedTransform >
 function LevenshteinDistance() More...
 
struct  sparta::utils::make_lowercase
 Functor for ::tolower. More...
 
struct  sparta::utils::make_uppercase
 Functor for ::toupper. More...
 

Namespaces

namespace  sparta
 Macros for handling exponential backoff.
 

Typedefs

typedef TransformedString< make_lowercasesparta::utils::lowercase_string
 always lowercase string data type
 
typedef TransformedString< make_uppercasesparta::utils::UPPERCASE_STRING
 ALWAYS UPPERCASE STRING DATA TYPE.
 

Functions

std::string sparta::utils::uint64_to_hexstr (uint64_t val)
 
std::string sparta::utils::uint32_to_hexstr (uint32_t val)
 
std::string sparta::utils::uint64_to_str (uint64_t val)
 
std::string sparta::utils::uint32_to_str (uint32_t val)
 
std::string sparta::utils::int64_to_str (int64_t val)
 
std::string sparta::utils::int32_to_str (int32_t val)
 
std::string sparta::utils::bool_to_str (bool val)
 
std::string sparta::utils::bin_to_hexstr (const uint8_t *data, size_t size, const std::string &sep=" ")
 
std::string sparta::utils::bin_to_bitstr (const uint8_t *data, size_t size, const std::string &sep=" ")
 
std::string sparta::utils::eliminate_whitespace (const std::string &str)
 
std::string sparta::utils::strip_whitespace (const std::string &str)
 function eliminate_whitespace()
 
std::string sparta::utils::strip_string_pattern (const std::string &pat, const std::string &str)
 function strip_whitespace()
 
void sparta::utils::tokenize (const std::string &in_str, std::vector< std::string > &str_vector, const std::string &delimiters=" ")
 function strip_string_pattern()
 
void sparta::utils::split_lines_around_tokens (std::istream &in_stream, std::vector< std::vector< std::string > > &str_vectors, const std::string &delimiters=" ", const char line_separator='\n')
 function tokenize()
 
void sparta::utils::tokenize (const char *in_char_str, std::vector< std::string > &str_vector, const std::string &delimiters=" ")
 function split_lines_around_tokens()
 
void sparta::utils::tokenize_strip_whitespace (const std::string &in_str, std::vector< std::string > &str_vector, const std::string &delimiters=" ")
 function tokenize()
 
void sparta::utils::tokenize_strip_whitespace (const char *in_char_str, std::vector< std::string > &str_vector, const std::string &delimiters=" ")
 function tokenize_strip_whitespace()
 
void sparta::utils::tokenize_on_whitespace (const std::string &in_str, std::vector< std::string > &str_vector)
 function tokenize_strip_whitespace()
 
void sparta::utils::tokenize_on_whitespace (const char *in_char_str, std::vector< std::string > &str_vector)
 function tokenize()
 
bool sparta::utils::strcmp_with_null (const char *s1, const char *s2)
 function tokenize()
 
uint32_t sparta::utils::LevenshteinDistance (const char *s, size_t n, const char *t, size_t m)
 
template<class AppliedTransform >
bool sparta::utils::operator< (const TransformedString< AppliedTransform > &one, const TransformedString< AppliedTransform > &two)
 
template<class AppliedTransform >
bool sparta::utils::operator== (const std::string &one, const TransformedString< AppliedTransform > &two)
 Comparisons against std::string where the utils object is the rhs:
 
template<class AppliedTransform >
bool sparta::utils::operator!= (const std::string &one, const TransformedString< AppliedTransform > &two)
 

Detailed Description

Cool string utilities.

Definition in file StringUtils.hpp.

Typedef Documentation

◆ lowercase_string

typedef TransformedString<make_lowercase> sparta::utils::lowercase_string

always lowercase string data type

Definition at line 494 of file StringUtils.hpp.

◆ UPPERCASE_STRING

typedef TransformedString<make_uppercase> sparta::utils::UPPERCASE_STRING

ALWAYS UPPERCASE STRING DATA TYPE.

Definition at line 497 of file StringUtils.hpp.

Function Documentation

◆ bin_to_bitstr()

std::string sparta::utils::bin_to_bitstr ( const uint8_t *  data,
size_t  size,
const std::string &  sep = " " 
)
inline

=========================================================================== function bin_to_bitstr()

  • print binary data as a bit string

Definition at line 125 of file StringUtils.hpp.

Here is the call graph for this function:

◆ bin_to_hexstr()

std::string sparta::utils::bin_to_hexstr ( const uint8_t *  data,
size_t  size,
const std::string &  sep = " " 
)
inline

=========================================================================== function bin_to_hexstr()

  • print binary data as a hex string

Definition at line 108 of file StringUtils.hpp.

Here is the call graph for this function:

◆ bool_to_str()

std::string sparta::utils::bool_to_str ( bool  val)
inline

=========================================================================== function bool_to_str()

  • print a int32 as a hex string

Definition at line 98 of file StringUtils.hpp.

Here is the call graph for this function:

◆ eliminate_whitespace()

std::string sparta::utils::eliminate_whitespace ( const std::string &  str)
inline

=========================================================================== function eliminate_whitespace()

  • strip whitespace from anywhere in a string (including newlines)

Definition at line 148 of file StringUtils.hpp.

Here is the call graph for this function:

◆ int32_to_str()

std::string sparta::utils::int32_to_str ( int32_t  val)
inline

=========================================================================== function int32_to_hexstr()

  • print a int32 as a hex string

Definition at line 88 of file StringUtils.hpp.

Here is the call graph for this function:

◆ int64_to_str()

std::string sparta::utils::int64_to_str ( int64_t  val)
inline

=========================================================================== function int64_to_str()

  • print a int64 as a string

Definition at line 77 of file StringUtils.hpp.

Here is the call graph for this function:

◆ LevenshteinDistance()

uint32_t sparta::utils::LevenshteinDistance ( const char *  s,
size_t  n,
const char *  t,
size_t  m 
)
inline

=========================================================================== function LevenshteinDistance()

  • Calculate the Levenshtein distance for two strings. The returned value gives an indication of how similar the strings are to one another. This is used in error diagnostics in the import program. Implementation taken from here: https://gist.github.com/TheRayTracer/2644387

A deletion

An insertion

A substitution

Definition at line 377 of file StringUtils.hpp.

Here is the call graph for this function:

◆ operator!=()

template<class AppliedTransform >
bool sparta::utils::operator!= ( const std::string &  one,
const TransformedString< AppliedTransform > &  two 
)
inline

Definition at line 523 of file StringUtils.hpp.

◆ operator<()

template<class AppliedTransform >
bool sparta::utils::operator< ( const TransformedString< AppliedTransform > &  one,
const TransformedString< AppliedTransform > &  two 
)
inline

Less than comparison, so you can put these data types into ordered containers like std::set's

Definition at line 502 of file StringUtils.hpp.

Here is the call graph for this function:

◆ operator==()

template<class AppliedTransform >
bool sparta::utils::operator== ( const std::string &  one,
const TransformedString< AppliedTransform > &  two 
)
inline

Comparisons against std::string where the utils object is the rhs:

std::string s1(...); lowercase_string s2(...); if (s1 == s2) { ... }

Definition at line 516 of file StringUtils.hpp.

◆ split_lines_around_tokens()

void sparta::utils::split_lines_around_tokens ( std::istream &  in_stream,
std::vector< std::vector< std::string > > &  str_vectors,
const std::string &  delimiters = " ",
const char  line_separator = '\n' 
)
inline

function tokenize()

=========================================================================== function split_lines_around_tokens()

  • tokenize multi-line text around line separators and an arbirtary set of delimiters, returning a vector of tokenized lines
  • default line separator is newline ('
    ')
  • default token/delimiter applied per line is a space (' ') For example, an istream containing this text: x:foo y:bar:z:baz?w?buz Could be tokenized with this call: std::vector<std::vector<std::string>> str_vectors; utils::split_lines_around_tokens(in_stream, str_vectors, ":?"); Would return str_vectors containing: { { "x", "foo" }, { "y", "bar", "z", "baz" } } Assuming the line endings of the text were '
    ' (the default line separator)

Definition at line 248 of file StringUtils.hpp.

Here is the call graph for this function:

◆ strcmp_with_null()

bool sparta::utils::strcmp_with_null ( const char *  s1,
const char *  s2 
)
inline

function tokenize()

=========================================================================== function strcmp_with_null()

  • Compare two strings. There are two cases in which the strings comare equal; when both strings are nullptr and otherwise if the actual strings are equal.

Definition at line 355 of file StringUtils.hpp.

Here is the call graph for this function:

◆ strip_string_pattern()

std::string sparta::utils::strip_string_pattern ( const std::string &  pat,
const std::string &  str 
)
inline

function strip_whitespace()

=========================================================================== function strip_string_pattern()

  • strip the given string of the given pattern (at the end/beginning) Examples: strip_string_pattern("out", "remove_out") -> "remove_" strip_string_pattern("out", "out_remove_out") -> "_remove_"

Look for the pattern at the beginning of the string

It's at the beginning...

Look for the pattern at the end of the string

Found the beginning one OR we found a match NOT at the end

Definition at line 177 of file StringUtils.hpp.

Here is the call graph for this function:

◆ strip_whitespace()

std::string sparta::utils::strip_whitespace ( const std::string &  str)
inline

function eliminate_whitespace()

=========================================================================== function strip_whitespace()

  • strip whitespace from the front and back of a string (including newlines)

Definition at line 160 of file StringUtils.hpp.

Here is the call graph for this function:

◆ tokenize() [1/2]

void sparta::utils::tokenize ( const char *  in_char_str,
std::vector< std::string > &  str_vector,
const std::string &  delimiters = " " 
)
inline

function split_lines_around_tokens()

=========================================================================== function tokenize()

  • tokenize a character string with an arbitrary set of delimiters, return result in a string vector
  • default is space
  • note: multiple consecutive whitespaces are treated as separate delimiters, so that " x y " would tokenize as ("", "x", "", "", "y", "")

Copy char * into a string

Definition at line 269 of file StringUtils.hpp.

Here is the call graph for this function:

◆ tokenize() [2/2]

void sparta::utils::tokenize ( const std::string &  in_str,
std::vector< std::string > &  str_vector,
const std::string &  delimiters = " " 
)
inline

function strip_string_pattern()

=========================================================================== function tokenize()

  • tokenize a string with an arbitrary set of delimiters, return result in a string vector
  • default is space
  • note: multiple consecutive whitespaces are treated as separate delimiters, so that " x y " would tokenize as ("", "x", "", "", "y", "")

Definition at line 211 of file StringUtils.hpp.

Here is the call graph for this function:

◆ tokenize_on_whitespace() [1/2]

void sparta::utils::tokenize_on_whitespace ( const char *  in_char_str,
std::vector< std::string > &  str_vector 
)
inline

function tokenize()

=========================================================================== function tokenize_on_whitespace()

  • tokenize a character string on whitespace (space, tab, return)
  • note: multiple consecutive whitespaces are treated as a single delimiters so that " x y " would tokenize as ("x", "y")

Copy char * into a string

Definition at line 343 of file StringUtils.hpp.

Here is the call graph for this function:

◆ tokenize_on_whitespace() [2/2]

void sparta::utils::tokenize_on_whitespace ( const std::string &  in_str,
std::vector< std::string > &  str_vector 
)
inline

function tokenize_strip_whitespace()

=========================================================================== function tokenize_on_whitespace()

  • tokenize a string on whitespace (space, tab, return)
  • note: multiple consecutive whitespaces are treated as a single delimiters so that " x y " would tokenize as ("x", "y")

Definition at line 318 of file StringUtils.hpp.

Here is the call graph for this function:

◆ tokenize_strip_whitespace() [1/2]

void sparta::utils::tokenize_strip_whitespace ( const char *  in_char_str,
std::vector< std::string > &  str_vector,
const std::string &  delimiters = " " 
)
inline

function tokenize_strip_whitespace()

=========================================================================== function tokenize_strip_whitespace()

  • tokenize a character string with an arbitrary set of delimiters, return result in a string vector
  • default is space
  • note: multiple consecutive whitespaces are treated as separate delimiters, so that " x y " would tokenize as ("", "x", "", "", "y", "")

Copy char * into a string

Definition at line 303 of file StringUtils.hpp.

Here is the call graph for this function:

◆ tokenize_strip_whitespace() [2/2]

void sparta::utils::tokenize_strip_whitespace ( const std::string &  in_str,
std::vector< std::string > &  str_vector,
const std::string &  delimiters = " " 
)
inline

function tokenize()

=========================================================================== function tokenize_strip_whitespace()

  • same as tokenize(), but strip whitespace at beginning and end of each string

Definition at line 282 of file StringUtils.hpp.

Here is the call graph for this function:

◆ uint32_to_hexstr()

std::string sparta::utils::uint32_to_hexstr ( uint32_t  val)
inline

=========================================================================== function uint32_to_hexstr()

  • print a uint32 as a hex string

Definition at line 44 of file StringUtils.hpp.

Here is the call graph for this function:

◆ uint32_to_str()

std::string sparta::utils::uint32_to_str ( uint32_t  val)
inline

=========================================================================== function uint32_to_hexstr()

  • print a uint32 as a hex string

Definition at line 67 of file StringUtils.hpp.

Here is the call graph for this function:

◆ uint64_to_hexstr()

std::string sparta::utils::uint64_to_hexstr ( uint64_t  val)
inline

=========================================================================== function uint64_to_hexstr()

  • print a uint64 as a hex string
  • prints an "_" to separate the upper 8 digits from the lower 8 digits

Definition at line 30 of file StringUtils.hpp.

Here is the call graph for this function:

◆ uint64_to_str()

std::string sparta::utils::uint64_to_str ( uint64_t  val)
inline

=========================================================================== function uint64_to_str()

  • print a uint64 as a string

Definition at line 56 of file StringUtils.hpp.

Here is the call graph for this function: