Class utils#
Defined in File utils.h
Nested Relationships#
Nested Types#
Class Documentation#
-
class utils#
Class containing useful static functions.
A class containing useful static functions unrelated to other classes.
Public Types
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>
‘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>
</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 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_exceptionset 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 wayTransform 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.
-
static std::string globToRegex(std::string_view glob, char pathSeparator = '/')#
Converts a glob expression to a regular expression. When a glob contains
**
, thepathSeparator
(/
by default) is used to help generate the regular expression.It handles the following glob features:
*
: Matches zero or more characters (except path separators when also using**
)?
: Matches exactly one character (except path separators when also using**
)[...]
: Character class, matches any of the given characters[!...]
or[^...]
: Negated character class, matches none of the given characters{a,b,c}
: Alternation, matches any of the given comma-separated patterns**
: Matches any number of characters including path separators
Throws a
utils::glob_exception
if a character class or alternation is not closed or the expression ends with an escape.
-
static std::optional<std::string> getEnv(const std::string &env)#
Get an environment variable value, returns std::nullopt if not set
-
static std::optional<std::string> getKnownFolder(KnownFolder knownFolder)#
Get an Windows known folder, returns std::nullopt in case of error. Return std::nullopt on non-Windows platforms.
-
struct glob_exception : public f3d::exception#
An exception that can be thrown by globToRegex
Public Functions
-
explicit glob_exception(const std::string &what = "")#
-
explicit glob_exception(const std::string &what = "")#
-
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 astring_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)#
-
inline explicit string_template(const std::string &templateString)#
-
static unsigned int textDistance(std::string_view strA, std::string_view strB)#