char_utils.hpp

Simple functions and tools to manipulate individual characters.

Available Class CharSet - A collection of characters for use in other functions.

Available Functions bool is_whitespace(char test_char) bool is_upper_letter(char test_char) bool is_lower_letter(char test_char) bool is_letter(char test_char) bool is_digit(char test_char) bool is_alphanumeric(char test_char) bool is_idchar(char test_char) bool is_one_of(char test_char, const std::string & char_set) bool is_valid(char test_char ) bool is_valid(char test_char, std::function<bool(char)> fun1, FUNS… funs)

Note

Status: BETA

Typedefs

using CharSet = CharSetBase<>

Functions

static CharSet CharSetRange(char c1, char c2)
static const CharSet &WhitespaceCharSet()
static const CharSet &UpperCharSet()
static const CharSet &LowerCharSet()
static const CharSet &LetterCharSet()
static const CharSet &DigitCharSet()
static const CharSet &AlphanumericCharSet()
static const CharSet &IDCharSet()
static const CharSet &PunctuationCharSet()
static const CharSet &EscapeCodeCharSet()

Which characters can come after a backslash in a string?

inline bool is_whitespace(char test_char)
inline bool is_upper_letter(char test_char)
inline bool is_lower_letter(char test_char)
inline bool is_letter(char test_char)
inline bool is_digit(char test_char)
inline bool is_alphanumeric(char test_char)
inline bool is_idchar(char test_char)
inline bool is_punctuation(char test_char)
inline bool is_escape_code(char test_char)
static inline bool is_one_of(char test_char, const std::string &char_set)

Determine if a character is in a set of characters (represented as a string)

inline bool is_valid(char)

If no functions are provided to is_valid(), always return false as base case.

template<typename ...FUNS>
inline bool is_valid(char test_char, std::function<bool(char)> fun1, FUNS... funs)

Determine if a character passes any of the test functions provided.

inline char ToEscapeChar(char c)

Convert a char after a backslash to its escaped version.

template<typename CHAR_T = char, size_t MAX_CHAR = 128>
class CharSetBase
#include <char_utils.hpp>

A char_set is a fast true/false lookup table to identify which ASCII chars are in a set.

Public Functions

inline CharSetBase()
inline CharSetBase(CHAR_T c)
inline CharSetBase(const std::string &in_chars)
inline CharSetBase(const char *in_chars)
CharSetBase(const this_t&) = default
this_t &operator=(const this_t&) = default
inline this_t &operator=(char c)
inline this_t &operator=(const std::string &in_chars)
inline CharSetBase &Reset()
inline size_t GetMaxChar() const noexcept
inline bool Has(CHAR_T index) const
inline bool Has(const std::string &str) const
inline bool HasAny(const std::string &str) const
inline bool HasOnly(const std::string &str) const
inline bool HasAt(const std::string &str, size_t pos) const
inline size_t FindIn(const std::string &str, size_t pos = 0) const
inline bool operator[](CHAR_T index) const
inline size_t GetSize() const noexcept

Count up the number of characters in the set.

inline CharSetBase<CHAR_T, MAX_CHAR> &Set(CHAR_T c)
inline CharSetBase<CHAR_T, MAX_CHAR> &Clear(CHAR_T c)
inline CharSetBase<CHAR_T, MAX_CHAR> &SetRange(CHAR_T c1, CHAR_T c2)

Set a range of characters INCLUSIVE of c1 and c2.

inline CharSetBase<CHAR_T, MAX_CHAR> &ClearRange(CHAR_T c1, CHAR_T c2)
inline CharSetBase<CHAR_T, MAX_CHAR> operator+(const this_t &other) const
inline CharSetBase<CHAR_T, MAX_CHAR> operator!() const
inline iterator_t begin() const
inline iterator_t end() const
inline size_t CountMatches(const std::string &str) const

Count the number of matches that occur in a string.

inline size_t CountMatches(const std::string &str, size_t start, size_t end) const

Count the number of matches that occur in a sub-string.

inline size_t CountFrontMatches(const std::string &str, size_t start = 0) const

Count the number of matches that occur at the beginning of a string.

inline size_t CountBackMatches(const std::string &str) const
inline std::string AsString() const

Convert this set of characters into a regex-style character set.

Private Types

using this_t = CharSetBase<CHAR_T, MAX_CHAR>
using char_set_t = array<CHAR_T, MAX_CHAR>

Private Members

char_set_t char_set = {}
class iterator_t
#include <char_utils.hpp>

Public Functions

inline iterator_t(Ptr<const this_t> _ptr, size_t _index)
iterator_t(const iterator_t&) = default
iterator_t &operator=(const iterator_t&) = default
inline iterator_t &MakeValid()
inline bool operator==(const iterator_t &in) const noexcept
inline bool operator!=(const iterator_t &in) const noexcept
inline bool operator<(const iterator_t &in) const noexcept
inline bool operator>(const iterator_t &in) const noexcept
inline bool operator<=(const iterator_t &in) const noexcept
inline bool operator>=(const iterator_t &in) const noexcept
inline CHAR_T operator*() const
inline iterator_t &operator++()

Private Members

Ptr<const this_t> ptr
size_t index