Kea 2.5.8
isc::util::str Namespace Reference

Classes

class  StringSanitizer
 Implements a regular expression based string scrubber. More...
 
class  StringSanitizerImpl
 
class  StringTokenError
 A Set of C++ Utilities for Manipulating Strings. More...
 

Typedefs

using StringSanitizerImplPtr = std::shared_ptr< StringSanitizerImpl >
 Type representing the pointer to the StringSanitizerImpl.
 
using StringSanitizerPtr = std::unique_ptr< StringSanitizer >
 Type representing the pointer to the StringSanitizer.
 

Functions

void decodeColonSeparatedHexString (const std::string &hex_string, std::vector< uint8_t > &binary)
 Converts a string of hexadecimal digits with colons into a vector.
 
void decodeFormattedHexString (const std::string &hex_string, std::vector< uint8_t > &binary)
 Converts a formatted string of hexadecimal digits into a vector.
 
void decodeSeparatedHexString (const std::string &hex_string, const std::string &sep, std::vector< uint8_t > &binary)
 Converts a string of separated hexadecimal digits into a vector.
 
string dumpAsHex (const uint8_t *data, size_t length)
 Dumps a buffer of bytes as a string of hexadecimal digits.
 
bool isPrintable (const std::string &content)
 Check if a string is printable.
 
bool isPrintable (const std::vector< uint8_t > &content)
 Check if a byte vector is printable.
 
void lowercase (std::string &text)
 Convert string to lowercase.
 
vector< uint8_t > quotedStringToBinary (const std::string &quoted_string)
 Converts a string in quotes into vector.
 
template<typename Iterator >
Iterator seekTrimmed (Iterator const &begin, Iterator end, uint8_t const trim_val)
 Finds the "trimmed" end of a buffer.
 
vector< string > tokens (const std::string &text, const std::string &delim=" \t\n", bool escape=false)
 Split string into tokens.
 
char toLower (char const chr)
 Convert character to lowercase.
 
char toUpper (char const chr)
 Convert character to uppercase.
 
string trim (const std::string &input)
 Trim leading and trailing spaces.
 
void uppercase (std::string &text)
 Convert string to uppercase.
 

Typedef Documentation

◆ StringSanitizerImplPtr

Type representing the pointer to the StringSanitizerImpl.

Definition at line 218 of file str.h.

◆ StringSanitizerPtr

using isc::util::str::StringSanitizerPtr = typedef std::unique_ptr<StringSanitizer>

Type representing the pointer to the StringSanitizer.

Definition at line 262 of file str.h.

Function Documentation

◆ decodeColonSeparatedHexString()

void isc::util::str::decodeColonSeparatedHexString ( const std::string &  hex_string,
std::vector< uint8_t > &  binary 
)

Converts a string of hexadecimal digits with colons into a vector.

Parameters
hex_stringInput string.
binaryVector receiving converted string into binary.
Exceptions
isc::BadValueif the format of the input string is invalid.

Definition at line 158 of file str.cc.

References decodeSeparatedHexString().

Referenced by isc::dhcp::HWAddr::fromText().

+ Here is the call graph for this function:

◆ decodeFormattedHexString()

void isc::util::str::decodeFormattedHexString ( const std::string &  hex_string,
std::vector< uint8_t > &  binary 
)

Converts a formatted string of hexadecimal digits into a vector.

This function supports the following formats:

  • yy:yy:yy:yy or yy yy yy yy - octets delimited by colons or spaces, see decodeSeparatedHexString
  • yyyyyyyyyy
  • 0xyyyyyyyyyy

If there is an odd number of hexadecimal digits in the input string, the '0' is prepended to the string before decoding.

Parameters
hex_stringInput string.
binaryVector receiving converted string into binary.
Exceptions
isc::BadValueif the format of the input string is invalid.

Definition at line 212 of file str.cc.

References isc::util::encode::decodeHex(), decodeSeparatedHexString(), and isc_throw.

Referenced by isc::dhcp::OptionDataParser::createOption(), isc::dhcp::IdentifierType< min_size, max_size >::fromText(), isc::dhcp::Host::setIdentifier(), isc::dhcp::LeaseMgr::upgradeLease4ExtendedInfo(), and isc::dhcp::LeaseMgr::upgradeLease6ExtendedInfo().

+ Here is the call graph for this function:

◆ decodeSeparatedHexString()

void isc::util::str::decodeSeparatedHexString ( const std::string &  hex_string,
const std::string &  sep,
std::vector< uint8_t > &  binary 
)

Converts a string of separated hexadecimal digits into a vector.

Octets may contain 1 or 2 digits. For example, using a colon for a separator all of the following are valid:

  • yy:yy:yy:yy:yy
  • y:y:y:y:y
  • y:yy:yy:y:y

If the decoded string doesn't match any of the supported formats, an exception is thrown.

Parameters
hex_stringInput string.
sepcharacter to use as a separator.
binaryVector receiving converted string into binary.
Exceptions
isc::BadValueif the format of the input string is invalid.

Definition at line 163 of file str.cc.

References isc_throw.

Referenced by decodeColonSeparatedHexString(), and decodeFormattedHexString().

◆ dumpAsHex()

std::string isc::util::str::dumpAsHex ( const uint8_t *  data,
size_t  length 
)

Dumps a buffer of bytes as a string of hexadecimal digits.

Parameters
dataPointer to the data to dump.
lengthNumber of bytes to dump. Caller should ensure the length does not exceed the buffer.

Definition at line 330 of file str.cc.

Referenced by isc::tcp::TcpStreamRequest::logFormatRequest(), and isc::dhcp::Lease4::toText().

◆ isPrintable() [1/2]

bool isc::util::str::isPrintable ( const std::string &  content)

Check if a string is printable.

Parameters
contentString to check for printable characters.
Returns
True if empty or contains only printable characters, False otherwise.

Definition at line 310 of file str.cc.

Referenced by isc::flex_option::FlexOptionImpl::logAction(), and isc::flex_option::FlexOptionImpl::logSubAction().

◆ isPrintable() [2/2]

bool isc::util::str::isPrintable ( const std::vector< uint8_t > &  content)

Check if a byte vector is printable.

Parameters
contentVector to check for printable characters.
Returns
True if empty or contains only printable characters, False otherwise.

Definition at line 320 of file str.cc.

◆ lowercase()

void isc::util::str::lowercase ( std::string &  text)

Convert string to lowercase.

Parameters
textString to be lower-cased.

Definition at line 134 of file str.cc.

References toLower().

Referenced by isc::http::BasicHttpAuthConfig::checkAuth(), isc::http::HttpHeader::getLowerCaseName(), isc::http::HttpHeader::getLowerCaseValue(), and isc::http::HttpHeader::isValueEqual().

+ Here is the call graph for this function:

◆ quotedStringToBinary()

std::vector< uint8_t > isc::util::str::quotedStringToBinary ( const std::string &  quoted_string)

Converts a string in quotes into vector.

A converted string is first trimmed. If a trimmed string is in quotes, the quotes are removed and the resulting string is copied into a vector. If the string is not in quotes, an empty vector is returned.

The resulting string is copied to a vector and returned.

This function is intended to be used by the server configuration parsers to convert string values surrounded with quotes into binary form.

Parameters
quoted_stringString to be converted.
Returns
Vector containing converted string or empty string if input string didn't contain expected quote characters.

Definition at line 139 of file str.cc.

References trim().

Referenced by isc::dhcp::OptionDataParser::createOption(), isc::yang::AdaptorHost::quoteIdentifier(), and isc::dhcp::Host::setIdentifier().

+ Here is the call graph for this function:

◆ seekTrimmed()

template<typename Iterator >
Iterator isc::util::str::seekTrimmed ( Iterator const &  begin,
Iterator  end,
uint8_t const  trim_val 
)

Finds the "trimmed" end of a buffer.

Works backward from the end of the buffer, looking for the first character not equal to the trim value, and returns an iterator pointing to that position.

Parameters
begin- Forward iterator pointing to the beginning of the buffer to trim.
end- Forward iterator pointing to the untrimmed end of the buffer to trim.
trim_val- byte value to trim off
Returns
Iterator pointing the first character from the end of the buffer not equal to the trim value.

Definition at line 65 of file str.h.

Referenced by isc::dhcp::OptionDataTypeUtil::readString(), isc::dhcp::OptionString::setValue(), and isc::dhcp::OptionString::unpack().

◆ tokens()

std::vector< std::string > isc::util::str::tokens ( const std::string &  text,
const std::string &  delim = " \t\n",
bool  escape = false 
)

Split string into tokens.

Splits a string into tokens (the tokens being delimited by one or more of the delimiter characters) and returns the tokens in a vector. Adjacent delimiters are considered to be a single delimiter.

Special cases are:

  1. The empty string is considered to be zero tokens.
  2. A string comprising nothing but delimiters is considered to be zero tokens.

The reasoning behind this is that the string can be thought of as having invisible leading and trailing delimiter characters. Therefore both cases reduce to a set of contiguous delimiters, which are considered a single delimiter (so getting rid of the string). Optional escape allows to escape delimiter characters (and only them and the escape character itself) using backslash.

We could use Boost for this, but this (simple) function eliminates one dependency in the code.

Parameters
textString to be split. Passed by value as the internal copy is altered during the processing.
delimDelimiter characters
escapeUse backslash to escape delimiter characters
Returns
Vector of tokens.

Definition at line 52 of file str.cc.

Referenced by isc::dhcp::OptionDataParser::createOption(), isc::dhcp::OptionDefParser::parse(), isc::dhcp::DnrInstance::parseDnrInstanceConfigData(), isc::flex_option::FlexOptionImpl::process(), and splitNamespace().

◆ toLower()

char isc::util::str::toLower ( char const  chr)

Convert character to lowercase.

Used in lowercase() to pass as a parameter to std::transform(). The function std::tolower() can't be used as it takes an "int" as its parameter; this confuses the template expansion mechanism because dereferencing a string::iterator returns a char.

Parameters
chrCharacter to be lower-cased.
Returns
Lowercase version of the input character.

Definition at line 129 of file str.cc.

Referenced by lowercase().

◆ toUpper()

char isc::util::str::toUpper ( char const  chr)

Convert character to uppercase.

Used in uppercase() to pass as a parameter to std::transform(). The function std::toupper() can't be used as it takes an "int" as its parameter; this confuses the template expansion mechanism because dereferencing a string::iterator returns a char.

Parameters
chrCharacter to be upper-cased.
Returns
Uppercase version of the input character.

Definition at line 119 of file str.cc.

Referenced by uppercase().

◆ trim()

◆ uppercase()

void isc::util::str::uppercase ( std::string &  text)

Convert string to uppercase.

Parameters
textString to be upper-cased.

Definition at line 124 of file str.cc.

References toUpper().

Referenced by sentinel().

+ Here is the call graph for this function: