26#ifndef CASACORE_STRING_H_
27#define CASACORE_STRING_H_
30#define CASACORE_DEPRECATE_STRING
32#ifdef CASACORE_DEPRECATE_STRING
34#pragma GCC diagnostic push
35#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
36#define DEPRECATED(X) [[deprecated(X)]]
64inline std::string::size_type
IndexString(std::string_view str,
char c,
int startpos = 0) {
66 return str.find(c, startpos);
68 const int search_from = str.length() + startpos - 1;
69 return search_from >= 0 ? str.rfind(c, search_from) : std::string::npos;
80 return str.find(
pattern, startpos);
82 const int search_from = str.length() + startpos -
pattern.length();
83 return search_from >= 0 ? str.rfind(
pattern, search_from) : std::string::npos;
92 if (str1.empty() || str2.empty()) {
94 }
else if (position < 0) {
95 return std::string_view(str1.begin(),
96 str1.begin() + std::min<size_t>(-position, str1.size())) == str2;
98 return std::string_view(str1.begin() + std::min<size_t>(position, str1.size()), str1.end()) ==
111 std::istringstream is(str);
113 if (is.fail() || !is.eof()) {
114 if (throw_on_error) {
116 throw std::runtime_error(
"String '" + str +
"' failed to parse in StringToValue()");
119 std::getline(is, extra);
120 throw std::runtime_error(
"Extra characters after parsing string '" + str +
121 "' in StringToValue(): '" + extra +
"'");
138inline int StringToInt(
const std::string &str,
bool throw_on_error =
false) {
143inline float StringToFloat(
const std::string &str,
bool throw_on_error =
false) {
148inline double StringToDouble(
const std::string &str,
bool throw_on_error =
false) {
154 std::ostringstream os;
155 os << std::forward<T>(
value);
207 const
char *
chars() const;
215 SubString(
const std::string &str, std::string::size_type pos, std::string::size_type len);
366 :
std::string(str,
pos, n) {}
380 template <
class InputIterator>
402 return static_cast<String &
>(std::string::operator=(str));
432 std::
string &
append(
char c) {
return std::string::append(1, c); }
433 using std::string::append;
442 return static_cast<String &
>(std::string::insert(
pos, 1, c));
444 using std::string::insert;
447 DEPRECATED(
"Use other overload, e.g. String::replace(pos, n1, 1, c)")
449 return static_cast<String &
>(std::string::replace(
pos, n1, 1, c));
452 DEPRECATED(
"Use other overload, e.g. String::replace(i1, i2, 1, c)")
454 return static_cast<String &
>(std::string::replace(i1, i2, 1, c));
456 using std::string::replace;
464 "Use std::format if possible, or FormatString() if the format string can't be changed (e.g. "
465 "runtime determined)")
475 template <typename T>
480 template <
typename T>
516 template <
typename T>
517 DEPRECATED(
"Use std::to_string or ValueToString()")
528 void trim(
char c[],
unsigned int n);
533 DEPRECATED(
"Use free function LTrimInPlace()")
539 DEPRECATED(
"Use free function RTrimInPlace()")
543 DEPRECATED(
"Use String.starts_with(beginString)")
546 using std::string::find;
553 bool contains(
char c)
const {
return (
find(c) != npos); }
556 DEPRECATED(
"Use find(s) != npos or StringContains()")
558 DEPRECATED(
"Use std::regex_search(str, regex);")
565 DEPRECATED(
"Use find(), rfind() or IndexString()")
567 DEPRECATED(
"Use find(), rfind(), IndexString() and/or StringContains()")
571 DEPRECATED(
"Use find(), rfind() or IndexString() and/or StringContains()")
573 DEPRECATED(
"Use std::regex_search(str.substr(pos), regex);")
585 "Use substr(pos) == str if possible, consider ends_with() / starts_with() for "
586 "positive/negative pos or use EqualStringsAndNotEmpty() if direct replacement is necessary")
589 "Use substr(pos) == str if possible, consider ends_with() / starts_with() for "
590 "positive/negative pos or use EqualStringsAndNotEmpty() if direct replacement is necessary")
591 bool matches(
char c,
int pos = 0)
const {
595 "Use substr(pos) == str if possible, consider ends_with() / starts_with() for "
596 "positive/negative pos or use EqualStringsAndNotEmpty() if direct replacement is necessary")
613 DEPRECATED(
"Use find() or rfind(), or IndexString() if the search direction is runtime-dependent")
617 DEPRECATED(
"Use find() or rfind(), or IndexString() if the search direction is runtime-dependent")
621 DEPRECATED(
"Use find() or rfind(), or IndexString() if the search direction is runtime-dependent")
644 DEPRECATED(
"Use a combination of find and/or replace")
648 DEPRECATED(
"Use a combination of find and/or replace")
652 DEPRECATED(
"Use a combination of find and/or replace")
676 using std::string::at;
682 DEPRECATED(
"Use substr(0, pos) or create a view. Also consider if str.start_with() suffices.")
820inline String
operator+(
const char *lhs,
const String &rhs) {
825inline String
operator+(
char lhs,
const String &rhs) {
830inline String
operator+(
const String &lhs,
const char *rhs) {
835inline String
operator+(
const String &lhs,
char rhs) {
837 str.append(
String(1, rhs));
848inline bool operator==(
const String &x,
const String &y) {
return x.compare(y) == 0; }
869inline int compare(
const std::string &x,
const std::string &y) {
return x.compare(y); }
870inline int compare(
const std::string &x,
const char *y) {
return x.compare(y); }
871inline int compare(
const std::string &x,
const char y) {
return x.compare(
String(1, y)); }
882int split(
const std::string &str, std::string res[],
int maxn,
const std::string &sep);
883int split(
const std::string &str, std::string res[],
int maxn,
const char sep);
884int split(
const std::string &str, std::string res[],
int maxn,
const Regex &sep);
894String join(std::string src[],
int n,
const std::string &sep);
920 std::string::size_type len)
937inline void LTrimInPlace(std::string &str,
char character) {
938 const std::size_t pos = str.find_first_not_of(character);
945inline void LTrimInPlace(std::string &str, std::string_view characters) {
946 const std::size_t pos = str.find_first_not_of(characters);
954 const size_t pos = str.find_last_not_of(character);
955 if (pos == std::string::npos)
964inline void RTrimInPlace(std::string &str, std::string_view characters) {
965 const size_t pos = str.find_last_not_of(characters);
966 if (pos == std::string::npos)
991 std::size_t count = 0;
992 while (p < str.length()) {
993 if ((p = str.find(
pattern, p)) == std::string::npos)
break;
1003 size_t start_position = 0) {
1004 assert(start_position <= input.size());
1005 const std::size_t
end = std::min(input.size(), input.find(
pattern, start_position));
1007 return std::string_view(input.begin(), input.begin() +
end);
1013 size_t start_position = 0) {
1014 assert(start_position <= input.size());
1015 std::size_t
end = input.find(
pattern, start_position);
1016 if (
end == std::string_view::npos) {
1022 return std::string_view(input.begin(), input.begin() +
end);
1028 size_t start_position = 0) {
1029 assert(start_position <= input.size());
1030 std::size_t start = input.find(
pattern, start_position);
1031 if (start == std::string_view::npos) start = input.size();
1033 return std::string_view(input.begin() + start, input.end());
1039 size_t start_position = 0) {
1040 assert(start_position <= input.size());
1041 std::size_t start = input.find(
pattern, start_position);
1042 if (start == std::string_view::npos) {
1043 start = input.size();
1048 return std::string_view(input.begin() + start, input.end());
1053 std::transform(str.begin(), str.end(), str.begin(),
1054 [](
unsigned char c) { return static_cast<char>(std::toupper(c)); });
1059 std::transform(str.begin(), str.end(), str.begin(),
1060 [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
1066 std::string::iterator p = str.begin();
1067 while (p != str.end()) {
1069 if (std::islower(
static_cast<unsigned char>(*p))) {
1070 *p = std::toupper(
static_cast<unsigned char>(*p));
1073 at_word = std::isupper(
static_cast<unsigned char>(*p)) ||
1074 std::isdigit(
static_cast<unsigned char>(*p));
1079 while (p != str.end()) {
1080 if (std::isupper(
static_cast<unsigned char>(*p))) {
1081 *p = std::tolower(
static_cast<unsigned char>(*p));
1082 }
else if (!std::islower(
static_cast<unsigned char>(*p)) &&
1083 !std::isdigit(
static_cast<unsigned char>(*p)))
1087 if (p != str.end()) ++p;
1095 const std::size_t start = str.find(
pattern, start_position);
1096 if (start != std::string::npos) str.erase(start,
pattern.length());
1101 std::string_view replacement) {
1102 std::size_t n_matches = 0;
1103 if (str.length() == 0 ||
pattern.length() == 0 || str.length() <
pattern.length())
1105 std::size_t search_index = 0;
1106 while (str.length() - search_index >=
pattern.length()) {
1107 const std::size_t pos = str.find(
pattern, search_index);
1108 if (pos == std::string::npos)
1112 str.replace(pos,
pattern.length(), replacement);
1113 search_index = pos + replacement.length();
1121 return str.find(
pattern) != std::string_view::npos;
1131 return std::hash<std::string>()(k);
1136#ifdef CASACORE_DEPRECATE_STRING
1137#pragma GCC diagnostic pop
#define DEPRECATED(X)
Some of the (deprecated) code calls deprecated functions, so disable this.
friend LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
String: the storage and methods of handling collections of characters.
SubString before(int pos)
Next one for overloading reasons.
SubString at(int pos, size_type len)
bool startsWith(const std::string &beginString) const
Does the string start with the specified string?
void downcase()
internal transformation to lowercase of String
std::string & append(char c)
** Casacore addition
int gsub(const std::string &pat, const std::string &repl)
Global substitution: substitute all occurrences of pat with repl, and return the number of replacemen...
static double toDouble(const String &s, bool chk=false)
static String toString(const T &value)
Convert a value to a String.
size_type find(const Regex &r, size_type pos=0) const
DEPRECATED("Use substr(0, pos) or create a view. Also consider if str.start_with() suffices.") SubString before(size_type pos)
Start at startpos and extract the string "before" the argument's position, exclusive.
void prepend(const char *str)
String & operator=(const SubString &str)
String(const std::string &str, size_type pos=0, size_type n=npos)
Construct from std::string Construct from (part of) other string: acts as copy constructor.
void rtrim(char c)
Remove specified character from end of string.
String & insert(size_type pos, char c)
** Casacore addition
void capitalize()
internal transformation to capitalization of String.
const_reference elem(size_type pos) const
*** Casacore addition
String(size_type n, char c)
Construct from a single char (repeated n times).
SubString before(const std::string &str, size_type startpos=0)
static int toInt(const String &s, bool chk=false)
Convert a string to an Int, Float or Double.
void ltrim(char c)
Remove specified character from beginning of string.
String()=default
Default constructor.
DEPRECATED("Use std::format if possible, or FormatString() if the format string can't be changed (e.g. " "runtime determined)") static String format(const char *picture
Create a formatted string using the given printf format string.
String(const SubString &str)
Construct from a SubString.
SubString through(size_type pos)
Start at startpos and extract the SubString "through" to the argument's position, inclusive.
String & assign(char c)
** Casacore addition
int allocation() const
** Casacore addition – works as a capacity(n) – Note Int
static float toFloat(const String &s, bool chk=false)
DEPRECATED("Use find(str) != npos or StringContains()") bool contains(const std int pos const
SubString operator()(size_type pos, size_type len)
Casacore addition: synonym for at(pos, len).
String & operator=(const char *s)
void trim()
Remove beginning and ending whitespace.
DEPRECATED("Use std::reverse(str.begin(), str.end())") void reverse()
Maybe forget some.
SubString from(size_type pos)
Start at startpos and extract the SubString "from" the argument's position, inclusive,...
void del(size_type pos, size_type len)
Delete len chars starting at pos.
bool contains(char c) const
Containment.
void alloc(size_type n)
** Casacore addition – works as a resize(n)
String(const char *s)
Construct from char array.
String & operator=(char c)
SubString _substr(size_type first, size_type l) const
Helper functions for at, before etc.
String(const char *s, size_type n)
Construct from char* with given length.
DEPRECATED("Use find(str) != npos or StringContains()") bool contains(const std DEPRECATED("Use find(), rfind() or IndexString()") bool contains(char c
Does the string starting at the given position contain the given substring?
String at(int pos, size_type len) const
void throwFromStringError() const
Helper function for fromString.
bool fromString(T &value, bool throw_on_error=true) const
Convert a String to a value.
String & replace(size_type pos, size_type n1, char c)
** Casacore addition
const char * chars() const
** Casacore synonym
void del(int pos, int len)
Overload problem.
String(InputIterator begin, InputIterator end)
Construct from iterator.
SubString at(size_type pos, size_type len)
SubString through(int pos)
Next one for overloading reasons.
int freq(char c) const
Return the number of occurences of target in String.
DEPRECATED("Use find(), rfind(), IndexString() and/or StringContains()") bool contains(const std int pos
SubString after(size_type pos)
Start at startpos and extract the SubString "after" the argument's position, exclusive,...
void upcase()
internal transformation to uppercase of String
SubString from(int pos)
Next one for overloading reasons.
SubString after(int pos)
Next one for overloading reasons.
std::string::size_type length() const
Obtain length.
const char * chars() const
Get as (const) C array.
std::string::size_type len_p
Length of sub-string.
SubString(const SubString &)=default
Default copy constructor.
const std::string & ref_p
Referenced string.
std::string::size_type pos_p
Start of sub-string.
operator const std::string() const
Make a string.
bool operator==(const String &x, const String &y)
Global comparison operators.
String operator+(const String &lhs, const String &rhs)
Global concatenation operators.
String common_prefix(const std::string &x, const std::string &y, int startpos=0)
Some general functions Functions to find special patterns, join and replicate .
String reverse(const std::string &str)
Casing and related functions Case conversion and rearrangement functions .
int split(const std::string &str, std::string res[], int maxn, const std::string &sep)
Splitting Global function which splits the String into string array res at separator and returns the ...
For temporary backward namespace compatibility, use casa as alias for casacore.
bool StringToValue(const std::string &str, T &value, bool throw_on_error=true)
Convert a String to a value.
TableExprNode pattern(const TableExprNode &node)
double StringToDouble(const std::string &str, bool throw_on_error=false)
Same as StringToValue<double>.
void LTrimInPlace(std::string &str, char character)
Remove specified character from beginning of string.
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
void TrimInPlace(std::string &str, char single_character)
Remove specified char from beginning and end of string.
void ToLowerCaseInPlace(std::string &str)
Converts the specified string to lower case, in place.
std::string_view GetStringFrom(std::string_view input, std::string_view pattern, size_t start_position=0)
Get a string starting at a specific pattern that is found in the string.
std::string ValueToString(T &&value)
bool operator<(const String &x, const String &y)
constexpr bool StringContains(std::string_view str, std::string_view pattern)
Determines if a given string contains another given string.
String downcase(const std::string &str)
Global function which returns a transformation to lowercase of String.
String common_suffix(const std::string &x, const std::string &y, int startpos=-1)
bool operator>=(const String &x, const String &y)
void CapitalizeStringInPlace(std::string &str)
Changes the casing such that every separate word starts with an uppercase character and continues low...
int compare(const std::string &x, const std::string &y)
** Casacore additions of global compares.
void EraseStringFrom(std::string &str, std::string_view pattern, size_t start_position=0)
Remove the end of a string, starting from a specified pattern.
std::string::size_type IndexString(std::string_view str, char c, int startpos=0)
Return the position of the character in the string or npos if not found.
const std::string_view kWhiteSpaceCharacters
Standard white space characters: space, new line, tab and carriage return.
int fcompare(const String &x, const String &y)
this version ignores case.
void RTrimInPlace(std::string &str, char character)
Remove specified character from end of string.
iterator begin()
Get the begin and end iterator object for this block.
bool operator<=(const String &x, const String &y)
std::string FormatString(const char *format_string,...)
Like sprintf.
bool RegexMatches(const std::string &str, const Regex &r, int pos=0)
Checks if the regex @r matches with the string @str.
String upcase(const std::string &str)
Global function which returns a transformation to uppercase of String.
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
String replicate(char c, String::size_type n)
String capitalize(const std::string &str)
Global function which returns a transformation to capitalization of String.
void ToUpperCaseInPlace(std::string &str)
Converts the specified string to upper case, in place.
const value_type & const_reference
bool EqualStringsAndNotEmpty(std::string_view str1, std::string_view str2, int position=0)
Replacement of old member function "matches", which has somewhat confusing semantics: two empty strin...
std::string_view GetStringAfter(std::string_view input, std::string_view pattern, size_t start_position=0)
Rather specific function – this is a replacement for String:after(), but maybe String::after() is rar...
String trim(const std::string &str)
Global function which removes leading and trailing whitespace.
size_t ReplaceAllInPlace(std::string &str, std::string_view pattern, std::string_view replacement)
Replace every occurence of pattern in str by replacement.
int RegexReplaceAll(std::string &str, const Regex &pat, const std::string &repl)
Replace all matches of the string str with the regex @pat by @repl.
const std::string_view kExtendedWhiteSpaceCharacters
All the characters of kWhiteSpaceCharacters, extended with vertical tab (\v) and form feed (\f).
bool operator>(const String &x, const String &y)
size_t RegexIndex(const std::string &str, const Regex &r, size_t startpos=0)
Matches the regex and returns at what index in the string.
float StringToFloat(const std::string &str, bool throw_on_error=false)
Same as StringToValue<float>.
int StringToInt(const std::string &str, bool throw_on_error=false)
Same as StringToValue<int>.
std::string_view GetStringUpToExcluding(std::string_view input, std::string_view pattern, size_t start_position=0)
Gets a part of a string up to and excluding a specified pattern.
bool operator!=(const String &x, const String &y)
String join(std::string src[], int n, const std::string &sep)
NewDelAllocator< T > NewDelAllocator< T >::value
std::size_t SubStringCount(std::string_view str, std::string_view pattern)
Returns the number of times a given pattern occurs in a string.
std::string_view GetStringUpToIncluding(std::string_view input, std::string_view pattern, size_t start_position=0)
Rather specific function – this is a replacement for String:through() with string argument,...
std::string RegexSubStr(const std::string &str, const Regex &r, size_t startpos=0)
Matches the regex and returns the matching part.
Define real & complex conjugation for non-complex types and put comparisons into std namespace.
std::size_t operator()(casacore::String const &k) const noexcept