src/core/utils/text.h
Collection of string utilities. More…
Namespaces
Name |
---|
allpix Helper class to hold support layers for a detector model. |
Functions
Name | |
---|---|
std::string | to_string_impl(const std::string & inp, empty_tag ) Conversion handler for strings. |
std::string | to_string_impl(const char * inp, empty_tag ) Conversion handler for strings. |
std::string | to_string_impl(char * inp, empty_tag ) Conversion handler for strings. |
std::string | trim(const std::string & str, const std::string & delims =" \t\n\r\v") Trims leading and trailing characters from a string. |
template <typename T > T |
from_string(std::string str) Converts a string to any supported type. |
std::string | from_string_helper(std::string str) Internal helper method for checking and trimming conversions from string. |
template <typename T > std::enable_if_t< std::is_arithmetic< T >::value, T > |
from_string_impl(std::string str, type_tag< T > ) Conversion handler for all arithmetic types. |
template <typename T > std::enable_if_t< std::is_enum< T >::value, T > |
from_string_impl(std::string str, type_tag< T > ) Conversion handler for all enum types. |
std::string | from_string_impl(std::string str, type_tag< std::string > ) Conversion handler for strings. |
std::filesystem::path | from_string_impl(std::string str, type_tag< std::filesystem::path > ) Conversion handler for filesystem paths. |
bool | from_string_impl(std::string str, type_tag< bool > ) Conversion handler for booleans. |
template <typename T > std::string |
to_string(T inp) Converts any type to a string. |
template <typename T ,std::enable_if_t< std::is_arithmetic< T >::value, bool > =true> std::string |
to_string_impl(T inp, empty_tag ) Conversion handler for all arithmetic types. |
template <typename T > std::vector< T > |
split(std::string str, const std::string & delims =" \t,") Splits string into substrings at delimiters. |
template <typename T > std::string |
transform(const std::string & str, const T & op) Transforms a string and returns the transformed string. |
Detailed Description
Collection of string utilities.
Copyright: Copyright (c) 2016-2024 CERN and the Allpix Squared authors. This software is distributed under the terms of the MIT License, copied verbatim in the file “LICENSE.md”. In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an Intergovernmental Organization or submit itself to any jurisdiction. SPDX-License-Identifier: MIT
Used extensively for parsing the configuration in the allpix::ConfigReader.
Functions Documentation
function to_string_impl
inline std::string to_string_impl(
const std::string & inp,
empty_tag
)
Conversion handler for strings.
Note: Overloaded for different types of strings
Adds enclosing double quotation marks to properly store strings containing whitespace.
function to_string_impl
inline std::string to_string_impl(
const char * inp,
empty_tag
)
Conversion handler for strings.
Note: Overloaded for different types of strings
Adds enclosing double quotation marks to properly store strings containing whitespace.
function to_string_impl
inline std::string to_string_impl(
char * inp,
empty_tag
)
Conversion handler for strings.
Note: Overloaded for different types of strings
Adds enclosing double quotation marks to properly store strings containing whitespace.
function trim
std::string trim(
const std::string & str,
const std::string & delims =" \t\n\r\v"
)
Trims leading and trailing characters from a string.
Parameters:
- str String that should be trimmed
- delims List of delimiters to trim from the string (defaults to all whitespace)
function from_string
template <typename T >
T from_string(
std::string str
)
Converts a string to any supported type.
Parameters:
- str String to convert
See: String conversions
function from_string_helper
std::string from_string_helper(
std::string str
)
Internal helper method for checking and trimming conversions from string.
Parameters:
- str Input string to check and trim
Return: Trimmed string
function from_string_impl
template <typename T >
std::enable_if_t< std::is_arithmetic< T >::value, T > from_string_impl(
std::string str,
type_tag< T >
)
Conversion handler for all arithmetic types.
Exceptions:
- std::invalid_argument If the string cannot be converted to the required arithmetic type
function from_string_impl
template <typename T >
std::enable_if_t< std::is_enum< T >::value, T > from_string_impl(
std::string str,
type_tag< T >
)
Conversion handler for all enum types.
Exceptions:
- std::invalid_argument If the string cannot be converted to the required enum type
function from_string_impl
std::string from_string_impl(
std::string str,
type_tag< std::string >
)
Conversion handler for strings.
Exceptions:
- std::invalid_argument If no closing quotation mark as last character after an opening quotation mark
- std::invalid_argument If string without enclosing quotation marks, but more data after whitespace is found
If a pair of enclosing double quotation marks is found, the whole string within the quotation marks is returned. Otherwise only the first part is read until whitespace is encountered.
function from_string_impl
std::filesystem::path from_string_impl(
std::string str,
type_tag< std::filesystem::path >
)
Conversion handler for filesystem paths.
Exceptions:
- std::invalid_argument If no closing quotation mark as last character after an opening quotation mark
- std::invalid_argument If string without enclosing quotation marks, but more data after whitespace is found
First parse as normal string and then construct path from it.
function from_string_impl
bool from_string_impl(
std::string str,
type_tag< bool >
)
Conversion handler for booleans.
Exceptions:
- std::invalid_argument If the string cannot be converted to a boolean type
Both numerical (0, 1) and textual representations (“false”, “true”) are supported for booleans. No enclosing quotation marks should be used.
function to_string
template <typename T >
std::string to_string(
T inp
)
Converts any type to a string.
Note: C-strings are not supported due to allocation issues
function to_string_impl
template <typename T ,
std::enable_if_t< std::is_arithmetic< T >::value, bool > =true>
std::string to_string_impl(
T inp,
empty_tag
)
Conversion handler for all arithmetic types.
Conversion handler for all enum types.
function split
template <typename T >
std::vector< T > split(
std::string str,
const std::string & delims =" \t,"
)
Splits string into substrings at delimiters.
Parameters:
- str String to split
- delims Delimiters to split at (defaults to space, tab and comma)
Return: List of all the substrings with all empty substrings ignored (thus removed)
function transform
template <typename T >
std::string transform(
const std::string & str,
const T & op
)
Transforms a string and returns the transformed string.
Parameters:
- str String that should be transformed
- op Unary operator to act on the string
Return: Transformed string
Source code
#ifndef ALLPIX_TEXT_H
#define ALLPIX_TEXT_H
#include <cctype>
#include <filesystem>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#include "type.h"
// TODO [doc]: should possible be put in a separate namespace
namespace allpix {
std::string trim(const std::string& str, const std::string& delims = " \t\n\r\v");
template <typename T> T from_string(std::string str);
std::string from_string_helper(std::string str);
template <typename T>
typename std::enable_if_t<std::is_arithmetic<T>::value, T> from_string_impl(std::string str, type_tag<T>);
template <typename T>
typename std::enable_if_t<std::is_enum<T>::value, T> from_string_impl(std::string str, type_tag<T>);
std::string from_string_impl(std::string str, type_tag<std::string>);
std::filesystem::path from_string_impl(std::string str, type_tag<std::filesystem::path>);
bool from_string_impl(std::string str, type_tag<bool>);
template <typename T> std::string to_string(T inp);
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
std::string to_string_impl(T inp, empty_tag);
template <typename T, std::enable_if_t<std::is_enum<T>::value, bool> = true>
std::string to_string_impl(T inp, empty_tag);
inline std::string to_string_impl(const std::string& inp, empty_tag) { return '"' + inp + '"'; }
inline std::string to_string_impl(const char* inp, empty_tag) { return '"' + std::string(inp) + '"'; }
inline std::string to_string_impl(char* inp, empty_tag) { return '"' + std::string(inp) + '"'; }
template <typename T> std::vector<T> split(std::string str, const std::string& delims = " \t,");
template <typename T> std::string transform(const std::string& str, const T& op);
} // namespace allpix
// Include template definitions
#include "text.tpp"
#endif /* ALLPIX_TEXT_H */
Updated on 2024-12-13 at 08:31:37 +0000