Class utils#

Nested Relationships#

Nested Types#

Class Documentation#

class utils#

Class containing useful static functions.

A class containing useful static functions unrelated to other classes.

Public Static Functions

static unsigned int textDistance(std::string_view strA, std::string_view strB)#

Compute the Levenshtein distance between two strings. Can be useful for spell checking and typo detection.

static std::vector<std::string> tokenize(std::string_view str)#

Tokenize provided string_view into a vector of strings, using the same logic as bash.

  • Split by spaces unless between quotes

  • Split by quoted section and remove the quotes

  • Supported quotes are: ‘”<tt>

    - Use escaped \ quotes, spaces and escape to add them verbatim

    - Comments are supported with</tt>#`, any characters after are ignored

    - Use escaped \# to add it verbatim

    - Other escaped characters are also added verbatim

    Throw a tokenize_exception if a quoted section is not closed or if finishing with an escape

    Examples:

    <tt>set scene.up.direction +Z</tt> -> <tt>set</tt> <tt>scene.up.direction</tt> <tt>+Z</tt>

    <tt>set render.hdri.file “/path/to/file with spaces.png”</tt> -> <tt>set</tt>, <tt>render.hdri.file</tt>, <tt>/path/to/file with spaces.png</tt>

    &lsquo;set render.hdri.file &rsquo;/path/to/file with spaces.png’<tt>-\></tt>set<tt>,</tt>render.hdri.file<tt>,</tt>/path/to/file with spaces.png<tt>

    </tt>set render.hdri.file “/path/to/file’with’quotes.png”<tt>-\></tt>set<tt>,</tt>render.hdri.file<tt>,</tt>/path/to/file’with’quotes.png<tt>

    </tt>set render.hdri.file /path/to/file\ spaces\ ‘quotes”.png` -> `set`, `render.hdri.file`, `/path/to/file spaces ‘quotes”.png

    set render.hdri.file C:\path\to\windows\file.png->set,render.hdri.file,C:\path\to\windows\file.png` set scene.up.direction +Z # A comment -> set, scene.up.direction, +Z set scene.up.direction +\Z -> set, scene.up.direction, +Z set scene.up.direction "+Z -> tokenize_exception set scene.up.direction +Z\ -> tokenize_exception

static std::filesystem::path collapsePath(const std::filesystem::path &path, const std::filesystem::path &baseDirectory = {})#

Collapse a string filesystem path by:

  • Expanding tilda ~ into home dir in a cross-platform way

  • Transform relative path into an absolute path based on basedDirectory if provided, or the current directory if not

  • Remove any .. if any Rely on vtksys::SystemTools::CollapseFullPath but return empty string if the provided string is empty.

class string_template#

String template allowing substitution of variables enclosed in curly braces.

string_template("{greeting} {name}!")
  .substitute({ { "greeting", "hello" }, { "name", "World" } })
  .str() == "hello World!"

Public Functions

inline explicit string_template(const std::string &templateString)#
template<typename F>
string_template &substitute(F lookup)#

Substitute variables based on a std::string(const std::string&) function. Variables for which the function throws a string_template::lookup_error exception are left untouched.

inline string_template &substitute(const std::map<std::string, std::string> &lookup)#

Substitute variables based on a map. Variables for which the map does not contain a key are left untouched.

inline std::string str() const#

Return a string representation of the string template

inline std::vector<std::string> variables() const#

List the remaining un-substituted variables.

template<typename F>
utils::string_template &substitute(F lookup)#
struct lookup_error : public std::out_of_range#

Exception to be thrown by substitution functions to let untouched variables through.

Public Functions

inline explicit lookup_error(const std::string &what = "")#
struct tokenize_exception : public f3d::exception#

An exception that can be thrown by tokenize

Public Functions

explicit tokenize_exception(const std::string &what = "")#