The Vish Visualization Shell 0.3
Vish
Classes | Typedefs | Functions
String Utility functions

Classes

class  MemCore::simplestring
 

Typedefs

using MemCore::StringUtil::SUBST = const std::tuple< const char *, std::string >
 A type definition that in conjunction with the string division operators allows a compact syntax for string substitutions such as:
 

Functions

template<typename E >
std::basic_string< E > MemCore::StringUtil::basename (const std::basic_string< E > &url)
 Return the basic name of the URL, which is without extension and path information.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_after_first (std::basic_string< E > const &s, const E c)
 Crop all what follows after first occurance of character 'c'.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_after_last (std::basic_string< E > const &s, const E c)
 Crop all what follows after last occurance of character 'c'.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_before_first (std::basic_string< E > const &s, const E c)
 Crop all what follows after first occurance of character 'c'.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_before_last (std::basic_string< E > const &s, const E c)
 Crop all what is before last occurance of character 'c'.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_first (std::basic_string< E > const &s, int n)
 Crop n characters from the beginning of this string.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_last (std::basic_string< E > const &s, int n)
 Crop n characters from the end of this string.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_postfix (const std::basic_string< E > &text, const std::basic_string< E > &postfix)
 Remove some trailing text from a string.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_prefix (const std::basic_string< E > &text, const std::basic_string< E > &prefix)
 Crop a prefix from some text, returning the unmodified text if not possible.
 
std::string MemCore::StringUtil::crop_prefix (const std::string &text, const char *prefix)
 Crop a prefix from some text, returning the unmodified text if not possible.
 
template<typename E >
bool MemCore::StringUtil::ends_with (const std::basic_string< E > &who, const std::basic_string< E > &what)
 Determine if a given string ends with a specific sequence.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::extname (const std::basic_string< E > &url)
 Return the filename extension, which is the string after the last '.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::extract_left (std::basic_string< E > &text, const E c)
 Modify the given text string, cutting off all what is left of the occurance of that first given character, and return whatever is left of it.
 
template<typename E >
bool MemCore::StringUtil::left_crop (std::basic_string< E > &text, const E *what)
 Remove some leading text from a string.
 
template<typename E >
bool MemCore::StringUtil::left_crop (std::basic_string< E > &text, const std::basic_string< E > &what)
 Remove some leading text from a string.
 
std::string MemCore::StringUtil::ltrim (const std::string &input)
 Remove leading white space.
 
std::string MemCore::StringUtil::pad_left (const std::string &str, size_t result_length, const char padchar=' ')
 Add spaces to the left such that the resulting string is at least of the given length.
 
std::string MemCore::StringUtil::percent_encode (const std::string &s, bool EncodeSpaceAsPlus=true)
 https://en.wikipedia.org/wiki/Percent-encoding
 
template<typename E >
bool MemCore::StringUtil::right_crop (std::basic_string< E > &text, const std::basic_string< E > &postfix)
 Remove some trailing text from a string.
 
std::string MemCore::StringUtil::rtrim (const std::string &input)
 Remove trailing tabs and new line charactes, but not spaces.
 
template<typename E , typename C >
size_t MemCore::StringUtil::split (std::basic_string< E > const &s, C &container, E const delimiter, bool keepBlankFields=true, E const terminator=0)
 Splitting a string using a given delimiter.
 
template<typename E , typename C >
size_t MemCore::StringUtil::split (std::basic_string< E > const &s, C &container, std::basic_string< E > delimiter, bool keepBlankFields=true, E const terminator=0)
 Splitting a string using a given set of delimiters.
 
template<typename E >
std::tuple< std::basic_string< E >, std::basic_string< E > > MemCore::StringUtil::split_at_first (std::basic_string< E > const &s, const E c)
 Split a string into two at first occurance of character 'c'.
 
template<typename E >
std::tuple< std::basic_string< E >, std::basic_string< E > > MemCore::StringUtil::split_at_last (std::basic_string< E > const &s, const E c)
 Split a string into two at last occurance of character 'c'.
 
template<typename E , typename C >
size_t MemCore::StringUtil::split_string (std::basic_string< E > const &s, C &container, E const delimiter, bool keepBlankFields=true)
 Splitting a string using a given delimiter but also handling 0-bytes in the string correctly.
 
template<typename E >
bool MemCore::StringUtil::starts_with (const std::basic_string< E > &who, const std::basic_string< E > &what)
 Determine if a given string starts with a specific sequence.
 
template<typename CHAR >
constexpr std::basic_string< CHAR > MemCore::StringUtil::str (const CHAR *text)
 A "safe" conversion operation from C strings to std::string which also checks for nullptr , which is forbidden to be passed to a std::string by the C++ standard according to https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2166r1.html .
 
template<typename CHAR , size_t N>
constexpr std::basic_string< CHAR > MemCore::StringUtil::str (const CHAR(&text)[N])
 An optimized version to construct a std::string from a static character array where the length is known.
 
template<typename E >
std::basic_string< E > MemCore::StringUtil::subst (std::basic_string< E > const &s, const E whom, const E withwhat)
 Substitute a character in a string by another one.
 

Detailed Description

Typedef Documentation

◆ SUBST

using MemCore::StringUtil::SUBST = typedef const std::tuple<const char*, std::string>

A type definition that in conjunction with the string division operators allows a compact syntax for string substitutions such as:

string s = " if (value = $Value) then return;"
/ SUBST{"$Value", ValueName};
const std::tuple< const char *, std::string > SUBST
A type definition that in conjunction with the string division operators allows a compact syntax for ...
Definition stringutil.hpp:958

Function Documentation

◆ basename()

template<typename E >
std::basic_string< E > MemCore::StringUtil::basename ( const std::basic_string< E > &  url)
inline

Return the basic name of the URL, which is without extension and path information.

This is the text between the last '/' or '\' and the last '.'

Referenced by Wizt::VNutrition::computeExpectedOutput().

◆ crop_after_first()

template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_after_first ( std::basic_string< E > const &  s,
const E  c 
)

Crop all what follows after first occurance of character 'c'.

This is equivalent to the shell function ${s%c.*}

◆ crop_after_last()

template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_after_last ( std::basic_string< E > const &  s,
const E  c 
)

Crop all what follows after last occurance of character 'c'.

This is equivalent to the shell function ${sc.*}

Referenced by MemCore::DirName(), Wizt::VScriptLoader::exec(), MemCore::getExecutableDir(), and Wizt::openModules().

◆ crop_before_first()

template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_before_first ( std::basic_string< E > const &  s,
const E  c 
)

Crop all what follows after first occurance of character 'c'.

This is equivalent to the shell function ${s#.*c}.

◆ crop_before_last()

template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_before_last ( std::basic_string< E > const &  s,
const E  c 
)

Crop all what is before last occurance of character 'c'.

This is equivalent to the shell function ${s##*c}

◆ crop_postfix()

template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_postfix ( const std::basic_string< E > &  text,
const std::basic_string< E > &  postfix 
)
inline

Remove some trailing text from a string.

Returns
The original text will be returned if there was no postfix found.
See also
right_crop()

◆ crop_prefix() [1/2]

template<typename E >
std::basic_string< E > MemCore::StringUtil::crop_prefix ( const std::basic_string< E > &  text,
const std::basic_string< E > &  prefix 
)
inline

Crop a prefix from some text, returning the unmodified text if not possible.

See also
left_crop()

Referenced by Wizt::createVScript(), and MemCore::StringUtil::crop_prefix().

◆ crop_prefix() [2/2]

std::string MemCore::StringUtil::crop_prefix ( const std::string &  text,
const char *  prefix 
)
inline

Crop a prefix from some text, returning the unmodified text if not possible.

See also
left_crop()

References MemCore::StringUtil::crop_prefix().

◆ extname()

template<typename E >
std::basic_string< E > MemCore::StringUtil::extname ( const std::basic_string< E > &  url)
inline

Return the filename extension, which is the string after the last '.

' - excluding the '.' !

Referenced by VishScriptSaver::save().

◆ split()

template<typename E , typename C >
size_t MemCore::StringUtil::split ( std::basic_string< E > const &  s,
C &  container,
E const  delimiter,
bool  keepBlankFields = true,
E const  terminator = 0 
)
inline

Splitting a string using a given delimiter.

Unfortunately the STL doesn't provide any direct way to achieve this. This split implementation is templatised and uses type deduction to determine what kind of string and container are being passed as arguments, which must must be some form of basic_string<> and any container that implements the push_back() method.

Note that the string type is not fully specified, allowing the traits and allocator to default. In some situations this may not be what you want and some compilers might not like it.

FROM: http://www.codeproject.com/KB/stl/Split_string.aspx

Example code:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
string tests[3] = {
string(""),
string("||three||five|"),
string("|two|three||five|")
};
char* delimiter = "|";
for (int i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i)
{
vector< string > x;
size_t n = split(tests[i], x, delimiter[0], true);
std::cout << n << "==" << x.size() << " fields" << std::endl;
if (n)
{
copy(x.begin(), x.end(),
ostream_iterator<string>(cout, delimiter));
cout << endl;
}
}
size_t split(std::basic_string< E > const &s, C &container, E const delimiter, bool keepBlankFields=true, E const terminator=0)
Splitting a string using a given delimiter.
Definition stringutil.hpp:115
STL namespace.
Examples
WebVish.cpp.

Referenced by Wizt::WebVish::communicate(), Wizt::ProgrammableGeometry::CompileGeometryShader(), Wizt::createVScript(), Wizt::VEnumSelection::fromString(), Wizt::CommandLineParser::getDataDirs(), Wizt::Slottify::getObjectInfo(), and MemCore::StringList::StringList().

◆ str()

template<typename CHAR , size_t N>
constexpr std::basic_string< CHAR > MemCore::StringUtil::str ( const CHAR(&)  text[N])
inlineconstexpr

An optimized version to construct a std::string from a static character array where the length is known.

This avoids determining the string length.

Referenced by MemCore::StringUtil::pad_left().