gz-cpp-util 1.3
A c++20 library containing various utilities
utility.hpp File Reference

Contains utility for strings. More...

#include <string>
#include <vector>
#include <unordered_map>
#include <map>

Go to the source code of this file.

Classes

struct  gz::util::string_hash
 

Typedefs

Map with string type as key, works with strings, string_view and char*
template<typename T >
using gz::util::unordered_string_map = std::unordered_map< std::string, T, util::string_hash, std::equal_to<> >
 A unordered_map where you can use string_views to access elements. More...
 
template<typename T >
using gz::util::string_map = std::map< std::string, T, util::string_hash, std::equal_to<> >
 same as unordered_string_map, but using std::map instead of std::unordered_map
 

Functions

template<SplitStringInVectorImplemented T>
std::vector< T > gz::util::splitStringInVector (const std::string_view &s, const std::string &separator, bool skipEmptyStrings=false)
 Split a string at a separator into a vector. More...
 

Detailed Description

Contains utility for strings.

Typedef Documentation

◆ unordered_string_map

template<typename T >
using gz::util::unordered_string_map = typedef std::unordered_map<std::string, T, util::string_hash, std::equal_to<> >

A unordered_map where you can use string_views to access elements.

To retrieve an element using a string view, you need to do this:

std::string_view sv = ...;
...
if (smap.contains(sv)) {
return smap.find(sv)->second;
}
std::unordered_map< std::string, T, util::string_hash, std::equal_to<> > unordered_string_map
A unordered_map where you can use string_views to access elements.
Definition: utility.hpp:62

The at() member and [] operator do not work with the string_view.

Function Documentation

◆ splitStringInVector()

template<SplitStringInVectorImplemented T>
std::vector< T > gz::util::splitStringInVector ( const std::string_view &  s,
const std::string &  separator,
bool  skipEmptyStrings = false 
)

Split a string at a separator into a vector.

Splits a string at a separator.
Two templates exist, one where the vector will hold std::string_views into s and one where it will hold std::strings. Behavior:

  • the elements of the returned vector will not contain the separator.
  • if the separator is not in s, the vector will contain s as only element.
  • empty elements will occur if:
    • separator is the first char
    • separator is the last char
    • two or more separator appear after each other
      You can turn this behaviour off by setting skipEmptyStrings to true
      Note
      The string_view variant has the advantage of being faster, however:
      The string_views will reference the original string, so it must not be changed or destroyed as long as the string_views are used!