Kea: str.h Source File

| Kea 3.2.0-git

str.h
Go to the documentation of this file.
1// Copyright (C) 2011-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef KEA_UTIL_STR_H
8#define KEA_UTIL_STR_H
9
10#include <exceptions/exceptions.h>
11
12#include <algorithm>
13#include <cstddef>
14#include <cstdint>
15#include <memory>
16#include <sstream>
17#include <string>
18#include <vector>
19
20#include <boost/lexical_cast.hpp>
21
22namespace isc {
23namespace util {
24namespace str {
25
27
31 class StringTokenError : public Exception {
32public:
33 StringTokenError(const char* file, size_t line, const char* what)
34 : isc::Exception(file, line, what) {
35 }
36};
37
46std::string
47 trim(const std::string& input);
48
63template <typename Iterator>
64Iterator
65seekTrimmed(Iterator const& begin, Iterator end, uint8_t const trim_val) {
66 while (end != begin && *(end - 1) == trim_val) {
67 --end;
68 }
69
70 return (end);
71}
72
100std::vector<std::string>
101 tokens(const std::string& text, const std::string& delim = " \t\n", bool escape = false);
102
113char
114 toUpper(char const chr);
115
119void
120 uppercase(std::string& text);
121
132char
133 toLower(char const chr);
134
138void
139 lowercase(std::string& text);
140
158std::vector<uint8_t>
159 quotedStringToBinary(const std::string& quoted_string);
160
179void
180 decodeSeparatedHexString(const std::string& hex_string,
181 const std::string& sep,
182 std::vector<uint8_t>& binary);
183
191void
192 decodeColonSeparatedHexString(const std::string& hex_string, std::vector<uint8_t>& binary);
193
212void
213 decodeFormattedHexString(const std::string& hex_string, std::vector<uint8_t>& binary);
214
216class StringSanitizerImpl;
217
219 using StringSanitizerImplPtr = std::shared_ptr<StringSanitizerImpl>;
220
222 class StringSanitizer {
223public:
238 StringSanitizer(const std::string& char_set, const std::string& char_replacement);
239
248 std::string scrub(const std::string& original);
249
255 static const uint32_t MAX_DATA_SIZE;
256
257private:
259 StringSanitizerImplPtr impl_;
260};
261
263 using StringSanitizerPtr = std::unique_ptr<StringSanitizer>;
264
270bool
271 isPrintable(const std::string& content);
272
278bool
279 isPrintable(const std::vector<uint8_t>& content);
280
286std::string
287 dumpAsHex(const uint8_t* data, size_t length);
288
294std::string dumpDouble(double val, size_t precision = 5);
295
308std::string
309 printOrDump(const std::vector<uint8_t>& data, size_t max_dump);
310
311} // namespace str
312} // namespace util
313} // namespace isc
314
315#endif // KEA_UTIL_STR_H
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::util::str::StringSanitizer::MAX_DATA_SIZE
static const uint32_t MAX_DATA_SIZE
The maximum size for regex parameters.
Definition str.h:255
isc::util::str::StringSanitizer::StringSanitizer
StringSanitizer(const std::string &char_set, const std::string &char_replacement)
Constructor.
Definition str.cc:300
isc::util::str::StringSanitizer::scrub
std::string scrub(const std::string &original)
Returns a scrubbed copy of a given string.
Definition str.cc:305
isc::util::str::StringTokenError::StringTokenError
StringTokenError(const char *file, size_t line, const char *what)
Definition str.h:33
exceptions.h
isc::data
Definition base_stamped_element.cc:12
isc::util::file
Definition filesystem.cc:29
isc::util::str
Definition str.cc:29
isc::util::str::StringSanitizerPtr
std::unique_ptr< StringSanitizer > StringSanitizerPtr
Type representing the pointer to the StringSanitizer.
Definition str.h:263
isc::util::str::seekTrimmed
Iterator seekTrimmed(Iterator const &begin, Iterator end, uint8_t const trim_val)
Finds the "trimmed" end of a buffer.
Definition str.h:65
isc::util::str::dumpAsHex
string dumpAsHex(const uint8_t *data, size_t length)
Dumps a buffer of bytes as a string of hexadecimal digits.
Definition str.cc:330
isc::util::str::lowercase
void lowercase(string &text)
Convert string to lowercase.
Definition str.cc:134
isc::util::str::decodeSeparatedHexString
void decodeSeparatedHexString(const string &hex_string, const string &sep, vector< uint8_t > &binary)
Converts a string of separated hexadecimal digits into a vector.
Definition str.cc:163
isc::util::str::printOrDump
std::string printOrDump(const std::vector< uint8_t > &data, size_t max_dump)
Outputs the contents of a vector as either a string printable characters or a hex dump.
Definition str.cc:351
isc::util::str::toUpper
char toUpper(char const chr)
Convert character to uppercase.
Definition str.cc:119
isc::util::str::decodeFormattedHexString
void decodeFormattedHexString(const string &hex_string, vector< uint8_t > &binary)
Converts a formatted string of hexadecimal digits into a vector.
Definition str.cc:212
isc::util::str::isPrintable
bool isPrintable(const string &content)
Check if a string is printable.
Definition str.cc:310
isc::util::str::toLower
char toLower(char const chr)
Convert character to lowercase.
Definition str.cc:129
isc::util::str::tokens
vector< string > tokens(const string &text, const string &delim, bool escape)
Split string into tokens.
Definition str.cc:52
isc::util::str::StringSanitizerImplPtr
std::shared_ptr< StringSanitizerImpl > StringSanitizerImplPtr
Type representing the pointer to the StringSanitizerImpl.
Definition str.h:219
isc::util::str::dumpDouble
string dumpDouble(double val, size_t precision)
Converts a double to a string with given precision.
Definition str.cc:344
isc::util::str::quotedStringToBinary
vector< uint8_t > quotedStringToBinary(const string &quoted_string)
Converts a string in quotes into vector.
Definition str.cc:139
isc::util::str::decodeColonSeparatedHexString
void decodeColonSeparatedHexString(const string &hex_string, vector< uint8_t > &binary)
Converts a string of hexadecimal digits with colons into a vector.
Definition str.cc:158
isc::util::str::trim
string trim(const string &input)
Trim leading and trailing spaces.
Definition str.cc:32
isc::util::str::uppercase
void uppercase(string &text)
Convert string to uppercase.
Definition str.cc:124
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18