casacore
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1// # String.h: String class
2// # Copyright (C) 2001,2002,2003
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASACORE_STRING_H_
27#define CASACORE_STRING_H_
28
29// Comment this out to suppress warnings for use of non-std::string functions
30#define CASACORE_DEPRECATE_STRING
31
32#ifdef CASACORE_DEPRECATE_STRING
33// Some of the (deprecated) code calls deprecated functions, so disable this
34#pragma GCC diagnostic push
35#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
36#define DEPRECATED(X) [[deprecated(X)]]
37#else
38#define DEPRECATED(X)
39#endif
40
41#include <algorithm>
42#include <cassert>
43#include <cctype>
44#include <sstream>
45#include <string>
46#include <string_view>
47
48namespace casacore { // # NAMESPACE CASACORE - BEGIN
49
50// # Forward Declarations
51class String;
52class Regex;
53
54// Standard white space characters: space, new line, tab and carriage return.
55inline const std::string_view kWhiteSpaceCharacters = " \n\t\r";
56
57// All the characters of kWhiteSpaceCharacters, extended with vertical tab (\v) and form feed (\f).
58inline const std::string_view kExtendedWhiteSpaceCharacters = " \n\t\r\v\f";
59
60// Return the position of the character in the string or npos if not found.
61// Searches the first index of the character if the startpos >= 0, or the reverse index if
62// startpos < 0. Note that a startpos of -1 indicates searching at the second to last character.
63// This is in line with the old index() function, but rather confusing, so do not use for new code.
64inline std::string::size_type IndexString(std::string_view str, char c, int startpos = 0) {
65 if (startpos >= 0) {
66 return str.find(c, startpos);
67 } else {
68 const int search_from = str.length() + startpos - 1;
69 return search_from >= 0 ? str.rfind(c, search_from) : std::string::npos;
70 }
71}
72
73// Return the position of the substring in the string or npos if not found.
74// Searches the first index of the substring if the startpos >= 0, or the last index if
75// startpos < 0. Note that a startpos of -1 indicates searching at the second to last character.
76// This is in line with the old index() function, but rather confusing, so do not use for new code.
77inline std::string::size_type IndexString(std::string_view str, std::string_view pattern,
78 int startpos = 0) {
79 if (startpos >= 0) {
80 return str.find(pattern, startpos);
81 } else {
82 const int search_from = str.length() + startpos - pattern.length();
83 return search_from >= 0 ? str.rfind(pattern, search_from) : std::string::npos;
84 }
85}
86
87// Replacement of old member function "matches", which has somewhat confusing semantics: two empty
88// strings do not match, and an empty pattern does also not match. Do not use this for new code and
89// try to rewrite old code.
90inline bool EqualStringsAndNotEmpty(std::string_view str1, std::string_view str2,
91 int position = 0) {
92 if (str1.empty() || str2.empty()) {
93 return false;
94 } else if (position < 0) {
95 return std::string_view(str1.begin(),
96 str1.begin() + std::min<size_t>(-position, str1.size())) == str2;
97 } else {
98 return std::string_view(str1.begin() + std::min<size_t>(position, str1.size()), str1.end()) ==
99 str2;
100 }
101}
102
103// Convert a String to a value. All characters in the string must be used.
104// It uses a shift from an ostringstream, so that operator must exist
105// for the data type used.
106// In case of an error, an exception is thrown if @p throw_on_error is set.
107// Otherwise it returns false and @p value contains the value read
108// so far.
109template <typename T>
110inline bool StringToValue(const std::string &str, T &value, bool throw_on_error = true) {
111 std::istringstream is(str);
112 is >> value;
113 if (is.fail() || !is.eof()) {
114 if (throw_on_error) {
115 if (is.fail()) {
116 throw std::runtime_error("String '" + str + "' failed to parse in StringToValue()");
117 } else {
118 std::string extra;
119 std::getline(is, extra);
120 throw std::runtime_error("Extra characters after parsing string '" + str +
121 "' in StringToValue(): '" + extra + "'");
122 }
123 }
124 return false;
125 }
126 return true;
127}
128
129// Same as other StringToValue() overload, but returns the parsed value.
130template <typename T>
131inline T StringToValue(const std::string &str, bool throw_on_error = true) {
132 T value{};
133 StringToValue(str, value, throw_on_error);
134 return value;
135}
136
137// Same as StringToValue<int>
138inline int StringToInt(const std::string &str, bool throw_on_error = false) {
139 return StringToValue<int>(str, throw_on_error);
140}
141
142// Same as StringToValue<float>
143inline float StringToFloat(const std::string &str, bool throw_on_error = false) {
144 return StringToValue<float>(str, throw_on_error);
145}
146
147// Same as StringToValue<double>
148inline double StringToDouble(const std::string &str, bool throw_on_error = false) {
149 return StringToValue<double>(str, throw_on_error);
150}
151
152template <typename T>
153std::string ValueToString(T &&value) {
154 std::ostringstream os;
155 os << std::forward<T>(value);
156 return os.str();
157}
158
159// Replace all matches of the string @p str with the regex @pat by @repl.
160int RegexReplaceAll(std::string &str, const Regex &pat, const std::string &repl);
161
162// Checks if the regex @r matches with the string @str. If @p pos is provided,
163// searching starts from that position.
164bool RegexMatches(const std::string &str, const Regex &r, int pos = 0);
165
166// Matches the regex and returns the matching part.
167std::string RegexSubStr(const std::string &str, const Regex &r, size_t startpos = 0);
168
169// Matches the regex and returns at what index in the string.
170size_t RegexIndex(const std::string &str, const Regex &r, size_t startpos = 0);
171
172// <summary> SubString help class to be used in at, before, ... </summary>
173// <synopsis>
174// The SubString class can only be used by the String class to be able to
175// operate the Casacore defined replacement operators at, before, after,
176// through, from. The class is used transparently in operations like:
177// <srcblock>
178// string.at(2,3) = "five";
179// </srcblock>
180// If the SubString starts at a position outside the length of the
181// original string (like e.g. in after(1000000)), a zero length string is
182// created (not an exception thrown like in standard string operations).
183// </synopsis>
184
186 public:
187 // # Friends
188 friend class String;
189 // Make a string
190 DEPRECATED("class SubString will be removed")
191 operator const std::string() const { return std::string(ref_p, pos_p, len_p); }
192 // Default copy constructor.
193 SubString(const SubString &) = default;
194 // Assignment
195 // <group>
196 DEPRECATED("class SubString will be removed")
197 SubString &operator=(const SubString &str);
198 DEPRECATED("class SubString will be removed")
199 SubString &operator=(const String &str);
200 DEPRECATED("class SubString will be removed")
201 SubString &operator=(const char *s);
202 DEPRECATED("class SubString will be removed")
203 SubString &operator=(const char c);
204 // </group>
205 // Get as (const) C array
206 DEPRECATED("class SubString will be removed")
207 const char *chars() const;
208 // Obtain length
209 DEPRECATED("class SubString will be removed")
210 std::string::size_type length() const { return len_p; }
211
212 private:
213 // # Constructors
214 // Constructor (there are no public constructors)
215 SubString(const std::string &str, std::string::size_type pos, std::string::size_type len);
216 // # Data
217 // Referenced string
218 const std::string &ref_p;
219 // Start of sub-string
220 std::string::size_type pos_p;
221 // Length of sub-string
222 std::string::size_type len_p;
223};
224
225// <summary>
226// String: the storage and methods of handling collections of characters.
227// </summary>
228
229// <use visibility=export>
230
231// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tString.cc" demos="">
232// </reviewed>
233
234// <prerequisite>
235// <li> Regex - the regular expressions class
236// <li> the std string class
237// </prerequisite>
238//
239// <etymology>
240// The String class name is a continuation of the "C" language custom of
241// refering to collections of characters as "strings of characters".
242// </etymology>
243//
244// <synopsis>
245// The String class is the Casacore implementation of a string class. It is
246// from the standard library string class, and all operations
247// and behaviour of strings as defined in the standard are available for
248// a String. The only difference is the extension with additional functions
249// in the Casacore String class as compared to the standard string class.
250//
251// The String class may be instantiated in many ways:
252// <ol>
253// <li> A single character - <src>String myChar('C');</src>
254// <li> A char* argument - <src>String myWord("Yowza");</src>
255// <li> The first n chararcters of a pre-existing string -
256// <src>String myFoo("fooey", 3);</src>
257// </ol> As well as the copy and default constructors and iterator based ones.
258//
259// A String may be concatinated with another object (String, or
260// char*) with either prepending or postpending. A search for the position
261// of a character within a String may return its position, a bool that it
262// is contained within or a bool confirming your guess at the character's
263// position is correct. A check of the frequency of occurance of a string
264// within a String will return the number of occurances.
265//
266// Strings may be extracted from Strings at, before, through, from and
267// after a starting position within the String. Deletion of characters is
268// possible after a given position within the String. Global substitution
269// of characters within a String is provided, as well. Splitting of Strings
270// into a carray of Strings is possible, based upon a given separator
271// character, with a return value of the number of elements split. The joining
272// together of the elements of an array of Strings into one String is possible.
273//
274// Finally, transformations of case and conversions of type are provided.
275//
276// The standard string class provides the following functionality:
277// <ol>
278// <li> Construction from (part of) String, (part of) char*,
279// (repeating) Char, iterator pair.
280// <li> Assignment from String, char*, Char
281// <li> Iterators: begin() and end(); rbegin() and rend() (Note: gcc reverse
282// iterators still weak)
283// <li> Capacity: size, length, max_size, resize, capacity, reserve, clear,
284// empty
285// <li> Special size: String::size_type, with indicator: String::npos
286// <li> Element access: [pos] and at(pos) (both const and non-const)
287// <li> Modifiers: += of String, char*, Char; append of (part of) String,
288// char*, char and iterator defined; assign() of (part of)
289// String, char* and (repeating) char and iterator;
290// insertion of same; replacing of same; erase of part of
291// String; a copy and a swap.
292// <li> C-string: get char* with c_str() or data() and get the relevant
293// Allocator used (Note: not fully supported in gcc)
294// <li> Operations: find, rfind, find_first_of, find_last_of, find_first_not_of,
295// find_last_not_of; substr (Note only readable substring);
296// compare with (part of) String, char*
297// <li> Globals: Addition operators for String, char*, Char; all comparison
298// operators for String and char*; getline; input and output
299// stream operators
300// <li> Typedef: All relevant typedefs for standard containers and iterator
301// handling
302// </ol>
303// The Casacore additions are:
304// <ol>
305// <li> To standard: some char function arguments where appropriate; Regex
306// arguments in search like methods.
307// <li> Substring additions: at, before, after, from, through functions taking
308// search String, char* as arguments can give (hidden) substrings
309// which can be assigned (as in <src> at(1,2) = ";"</src>)
310// <li> Methods: prepend (in addition to standard append); del (as erase);
311// global substitution of String and patterns;
312// freq (count of occurance); split/join of strings at separator
313// or pattern; upcase, downcase, reverse;
314// common_suffix and _prefix; replicate; case insensitive
315// compare; creation from stream
316// </ol>
317
318// </synopsis>
319//
320// <example>
321// <srcblock>
322// // Let's start with a simple string.
323// String myString("the time");
324// // add some more on the end...
325// myString += " for all good men";
326// // prepend some on the front...
327// myString.prepend("Now is ");
328// // do some concatination...
329// String evenMore;
330// evenMore += myString + " to come to";
331// // do some three way concatination
332// String allKeys, finishIt(" their country.");
333// allKeys = evenMore + "the aid of" + finishIt;
334// // find the spot where we put something earlier
335// String::size_type position = allKeys.index(finishIt);
336// // find if the word is in the String...
337// bool query = myString.contains("good men");
338// // ask if the position we think is true is correct...
339// bool answer = allKeys.matches(finishIt, position);
340// // How many spaces are in our phrase?
341// int spacesCount = allKeys.freq(" ");
342// </srcblock>
343// </example>
344//
345// <motivation>
346// The String class eases the handling of characters within the Casacore
347// environment.
348// </motivation>
349//
350// <todo asof=2000/12/05">
351// <li> if old string disappeared; remove the alloc() call.
352// <li> add more tests (for string methods) when old String disappears
353// </todo>
354
355class String : public std::string {
356 public:
357 // # Constructors
358 // Default constructor
359 String() = default;
360 // Construct from std::string
361 // Construct from (part of) other string: acts as copy constructor
362 // <thrown>
363 // <li> out_of_range if pos > str.size()
364 // </thrown>
365 String(const std::string &str, size_type pos = 0, size_type n = npos)
366 : std::string(str, pos, n) {}
367 // Construct from char* with given length
368 // <thrown>
369 // <li> length_error if n == npos
370 // </thrown>
371 String(const char *s, size_type n) : std::string(s, n) {}
372 // Construct from char array
373 String(const char *s) : std::string(s) {}
374 // Construct from a single char (repeated n times)
375 // <thrown>
376 // <li> length_error if n == npos
377 // </thrown>
378 String(size_type n, char c) : std::string(n, c) {}
379 // Construct from iterator
380 template <class InputIterator>
381 String(InputIterator begin, InputIterator end) : std::string(begin, end) {}
382 // From single char (** Casacore addition).
383 // <note role=warning> Note that there is no automatic Char-to-String
384 // conversion available. This stops inadvertent conversions of
385 // integer to string. </note>
386 DEPRECATED("Use overload String(1, c)")
387 explicit String(char c) : std::string(1, c) {}
388 // Construct from a SubString
389 String(const SubString &str) : std::string(str.ref_p, str.pos_p, str.len_p) {}
390 // Construct from a stream.
391 DEPRECATED("Use os.str()")
392 String(std::ostringstream &os);
393
394 // # Destructor
395 // Destructor
396 ~String() = default;
397
398 // # Operators
399 // Assignments (they are all deep copies according to standard)
400 // <group>
401 String &operator=(const std::string &str) {
402 return static_cast<String &>(std::string::operator=(str));
403 }
404 String &operator=(const SubString &str) { return (*this = String(str)); }
405 String &operator=(const char *s) { return static_cast<String &>(std::string::operator=(s)); }
406 String &operator=(char c) { return static_cast<String &>(std::string::operator=(c)); }
407 // </group>
408 // ** Casacore addition: synonym for at(pos, len)
409 DEPRECATED("Use substr()")
410 SubString operator()(size_type pos, size_type len);
411
412 // *** Casacore addition
413 // <group>
414 DEPRECATED("Use at()")
415 const_reference elem(size_type pos) const { return std::string::at(pos); }
416
417 DEPRECATED("Use front()")
418 char firstchar() const { return at(static_cast<size_type>(0)); }
419 DEPRECATED("Use back()")
420 char lastchar() const { return at(length() - 1); }
421
422 // ** Casacore addition -- works as a capacity(n) -- Note Int
423 DEPRECATED("Use capacity()")
424 int allocation() const { return std::string::capacity(); }
425
426 // ** Casacore addition -- works as a resize(n)
427 DEPRECATED("Use resize()")
428 void alloc(size_type n) { std::string::resize(n); }
429
430 // ** Casacore addition
431 DEPRECATED("Use append(1, c)")
432 std::string &append(char c) { return std::string::append(1, c); }
433 using std::string::append;
434
435 // ** Casacore addition
436 DEPRECATED("Use assign(1, c)")
437 String &assign(char c) { return static_cast<String &>(std::string::assign(1, c)); }
438
439 // ** Casacore addition
440 DEPRECATED("Use insert(pos, 1, c)")
442 return static_cast<String &>(std::string::insert(pos, 1, c));
443 }
444 using std::string::insert;
445
446 // ** Casacore addition
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));
450 }
451 // ** Casacore addition
452 DEPRECATED("Use other overload, e.g. String::replace(i1, i2, 1, c)")
453 String &replace(iterator i1, iterator i2, char c) {
454 return static_cast<String &>(std::string::replace(i1, i2, 1, c));
455 }
456 using std::string::replace;
457
458 // ** Casacore synonym
459 DEPRECATED("Use c_str()")
460 const char *chars() const { return std::string::c_str(); }
461
462 // Create a formatted string using the given printf format string.
464 "Use std::format if possible, or FormatString() if the format string can't be changed (e.g. "
465 "runtime determined)")
466 static String format(const char *picture, ...);
467
468 // Convert a String to a value. All characters in the string must be used.
469 // It uses a shift from an ostringstream, so that operator must exist
470 // for the data type used.
471 // <br>In case of an error, an exception is thrown if <src>chk</src> is set.
472 // Otherwise it returns false and <src>value</src> contains the value read
473 // so far.
474 // <group>
475 template <typename T>
476 DEPRECATED("Use StringToValue()")
477 inline bool fromString(T &value, bool throw_on_error = true) const {
478 return StringToValue(*this, value, throw_on_error);
479 }
480 template <typename T>
481 DEPRECATED("Use StringToValue()")
482 inline T fromString() const {
483 T value;
485 return value;
486 }
487 // </group>
488
489 // Convert a string to an Int, Float or Double.
490 // <br>In case of an error, an exception is thrown if <src>chk</src> is set.
491 // Otherwise the value read so far is returned (0 if nothing read).
492 // <group>
493 DEPRECATED("Use StringToInt()")
494 static int toInt(const String &s, bool chk = false) {
495 int v = 0;
496 StringToValue(s, v, chk);
497 return v;
498 }
499 DEPRECATED("Use StringToFloat()")
500 static float toFloat(const String &s, bool chk = false) {
501 float v = 0;
502 StringToValue(s, v, chk);
503 return v;
504 }
505 DEPRECATED("Use StringToDouble()")
506 static double toDouble(const String &s, bool chk = false) {
507 double v = 0;
508 StringToValue(s, v, chk);
509 return v;
510 }
511 // </group>
512
513 // Convert a value to a String.
514 // It uses a shift into an ostringstream, so that operator must be
515 // defined for the data type used.
516 template <typename T>
517 DEPRECATED("Use std::to_string or ValueToString()")
519 return ValueToString(value);
520 }
521
522 // Remove beginning and ending whitespace.
523 DEPRECATED("Use free function TrimInPlace()")
524 void trim();
525
526 // Remove specified chars from beginning and end of string.
527 DEPRECATED("Use free function TrimInPlace()")
528 void trim(char c[], unsigned int n);
529
530 // Remove specified character from beginning of string.
531 // If the character is repeated more than once on the left, all instances
532 // will be removed; e.g. ltrim(',') results in ",,xy" becoming "xy".
533 DEPRECATED("Use free function LTrimInPlace()")
534 void ltrim(char c);
535
536 // Remove specified character from end of string.
537 // If the character is repeated more than once on the right, all instances
538 // will be removed; e.g. rtrim(',') results in "xy,," becoming "xy".
539 DEPRECATED("Use free function RTrimInPlace()")
540 void rtrim(char c);
541
542 // Does the string start with the specified string?
543 DEPRECATED("Use String.starts_with(beginString)")
544 bool startsWith(const std::string &beginString) const { return starts_with(beginString); }
545
546 using std::string::find;
547 DEPRECATED("Use std::regex")
549
550 // Containment. ** Casacore addition
551 // <group name=contains>
552 DEPRECATED("Use find(c) != npos")
553 bool contains(char c) const { return (find(c) != npos); }
554 DEPRECATED("Use find(str) != npos or StringContains()")
555 bool contains(const std::string &str) const { return (find(str) != npos); }
556 DEPRECATED("Use find(s) != npos or StringContains()")
557 bool contains(const char *s) const { return (find(s) != npos); }
558 DEPRECATED("Use std::regex_search(str, regex);")
559 bool contains(const Regex &r) const;
560 // </group>
561 // Does the string starting at the given position contain the given substring?
562 // If the position is negative, it is counted from the end.
563 // ** Casacore addition
564 // <group name=contains_pos>
565 DEPRECATED("Use find(), rfind() or IndexString()")
566 bool contains(char c, int pos) const { return IndexString(*this, c, pos) != npos; }
567 DEPRECATED("Use find(), rfind(), IndexString() and/or StringContains()")
568 bool contains(const std::string &str, int pos) const {
569 return (IndexString(*this, str, pos) != npos);
570 }
571 DEPRECATED("Use find(), rfind() or IndexString() and/or StringContains()")
572 bool contains(const char *s, int pos) const { return (IndexString(*this, s, pos) != npos); }
573 DEPRECATED("Use std::regex_search(str.substr(pos), regex);")
574 bool contains(const Regex &r, int pos) const { return (index(r, pos) != npos); }
575 // </group>
576
577 // Matches entire string from pos, or till pos if negative pos. ** Casacore addition
578 // Returns false if either is empty and pos >= 0.
579 // The original implementation has multiple bugs and awkward behaviour.
580 // BUG: Returns false for two empty strings (except if pos < 0).
581 // BUG: Returns false when pattern is empty, whereas normally an empty pattern matches everything.
582 // BUG: When pos < 0, it does not behave as advertised; e.g. "xyxy" does not match with "x" when
583 // pos=-1 is specified. <group name=matches>
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")
587 bool matches(const std::string &str, int pos = 0) const;
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 {
592 return EqualStringsAndNotEmpty(*this, std::string_view(&c, 1), pos);
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")
597 bool matches(const char *s, int pos = 0) const { return EqualStringsAndNotEmpty(*this, s, pos); }
598 DEPRECATED("Use RegexMatches()")
599 bool matches(const Regex &r, int pos = 0) const { return RegexMatches(*this, r, pos); }
600 // </group>
601
602 DEPRECATED("Use insert(0, str)")
603 void prepend(const std::string &str);
604 DEPRECATED("Use insert(0, str)")
605 void prepend(const char *str);
606 DEPRECATED("Use insert(0, c)")
607 void prepend(char c);
609 // Return the position of the target in the string or npos for failure.
610 // Searches the first index if the startpos >= 0, or the last index if
611 // startpos < 0.
612 // ** Casacore addition
613 DEPRECATED("Use find() or rfind(), or IndexString() if the search direction is runtime-dependent")
614 size_type index(char c, int startpos = 0) const {
615 return ((startpos >= 0) ? find(c, startpos) : rfind(c, length() + startpos - 1));
616 }
617 DEPRECATED("Use find() or rfind(), or IndexString() if the search direction is runtime-dependent")
618 size_type index(const std::string &str, int startpos = 0) const {
619 return ((startpos >= 0) ? find(str, startpos) : rfind(str, length() + startpos - str.length()));
620 }
621 DEPRECATED("Use find() or rfind(), or IndexString() if the search direction is runtime-dependent")
622 size_type index(const char *s, int startpos = 0) const {
623 return ((startpos >= 0) ? find(s, startpos)
624 : rfind(s, length() + startpos - traits_type::length(s)));
625 }
626 DEPRECATED("Use RegexIndex()")
627 size_type index(const Regex &r, int startpos = 0) const;
628
629 // Return the number of occurences of target in String. ** Casacore addition
630 // <group name=freq>
631 DEPRECATED("Use std::count()")
632 int freq(char c) const;
633 DEPRECATED("Use SubStringCount()")
634 int freq(const std::string &str) const;
635 DEPRECATED("Use SubStringCount()")
636 int freq(const char *s) const;
637 // </group>
638
639 DEPRECATED("Use substr()")
641 DEPRECATED("Use substr()")
642 String at(size_type pos, size_type len) const { return String(*this, pos, len); }
643
644 DEPRECATED("Use a combination of find and/or replace")
645 SubString at(const std::string &str, int startpos = 0);
646 DEPRECATED("Use GetSubViewFrom()")
647 String at(const std::string &str, int startpos = 0) const;
648 DEPRECATED("Use a combination of find and/or replace")
649 SubString at(const char *s, int startpos = 0);
650 DEPRECATED("Use GetSubViewFrom()")
651 String at(const char *s, int startpos = 0) const;
652 DEPRECATED("Use a combination of find and/or replace")
653 SubString at(char c, int startpos = 0);
654 DEPRECATED("Use GetSubViewFrom()")
655 String at(char c, int startpos = 0) const;
656 DEPRECATED("Use RegexSubStr")
658 DEPRECATED("Use RegexSubStr")
660 // Next ones for overloading reasons.
661 // <note role=tip> It is better to use the <src>substr()</src> method
662 // in stead. </note>
663 // <group>
664 DEPRECATED("Use substr()")
665 SubString at(int pos, int len) {
666 return at(static_cast<size_type>(pos), static_cast<size_type>(len));
667 };
668 DEPRECATED("Use substr()")
669 String at(int pos, int len) const {
670 return at(static_cast<size_type>(pos), static_cast<size_type>(len));
671 };
672 DEPRECATED("Use substr()")
673 SubString at(int pos, size_type len) { return at(static_cast<size_type>(pos), len); };
674 DEPRECATED("Use substr()")
675 String at(int pos, size_type len) const { return at(static_cast<size_type>(pos), len); };
676 using std::string::at;
677 // </group>
678
679 // Start at startpos and extract the string "before" the argument's
680 // position, exclusive. ** Casacore addition
681 // <group name=before>
682 DEPRECATED("Use substr(0, pos) or create a view. Also consider if str.start_with() suffices.")
684 DEPRECATED("Use GetStringUpToExcluding()")
685 SubString before(const std::string &str, size_type startpos = 0);
686 DEPRECATED("Use GetStringUpToExcluding()")
688 DEPRECATED("Use GetStringUpToExcluding()")
689 SubString before(char c, size_type startpos = 0);
690 DEPRECATED("Use std::regex")
692 // Next one for overloading reasons
693 DEPRECATED("Use substr(0, pos)")
694 SubString before(int pos) { return before(static_cast<size_type>(pos)); };
695 // </group>
696
697 // Start at startpos and extract the SubString "through" to the argument's
698 // position, inclusive. ** Casacore addition
699 // <group name=through>
700 DEPRECATED("Use substr(0, pos+1)")
702 DEPRECATED("Use GetStringUpToIncluding()")
703 SubString through(const std::string &str, size_type startpos = 0);
704 DEPRECATED("Use GetStringUpToIncluding()")
706 DEPRECATED("Use GetStringUpToIncluding()")
707 SubString through(char c, size_type startpos = 0);
708 DEPRECATED("Use std::regex")
710 // Next one for overloading reasons
711 DEPRECATED("Use substr(0, pos+1)")
712 SubString through(int pos) { return through(static_cast<size_type>(pos)); }
713 // </group>
714
715 // Start at startpos and extract the SubString "from" the argument's
716 // position, inclusive, to the String's end. ** Casacore addition
717 // <group name=from>
718 DEPRECATED("Use substr(pos)")
720 DEPRECATED("Use GetStringFrom()")
721 SubString from(const std::string &str, size_type startpos = 0);
722 DEPRECATED("Use GetStringFrom()")
723 SubString from(const char *s, size_type startpos = 0);
724 DEPRECATED("Use GetStringFrom()")
725 SubString from(char c, size_type startpos = 0);
726 DEPRECATED("Use std::regex")
728 // Next one for overloading reasons
729 DEPRECATED("Use substr(pos)")
730 SubString from(int pos) { return from(static_cast<size_type>(pos)); };
731 // </group>
732
733 // Start at startpos and extract the SubString "after" the argument's
734 // position, exclusive, to the String's end. ** Casacore addition
735 // <group name=after>
736 DEPRECATED("Use substr(pos + 1)")
738 DEPRECATED("Use GetStringAfter()")
739 SubString after(const std::string &str, size_type startpos = 0);
740 DEPRECATED("Use GetStringAfter()")
741 SubString after(const char *s, size_type startpos = 0);
742 DEPRECATED("Use GetStringAfter()")
743 SubString after(char c, size_type startpos = 0);
744 DEPRECATED("Use std::regex")
746 // Next one for overloading reasons
747 DEPRECATED("Use substr(pos + 1)")
748 SubString after(int pos) { return after(static_cast<size_type>(pos)); };
749 // </group>
750
751 // Maybe forget some. ** Casacore addition
752 // <group>
753 // internal transformation to reverse order of String.
754 DEPRECATED("Use std::reverse(str.begin(), str.end())")
755 void reverse();
756 // internal transformation to capitalization of String.
757 DEPRECATED("Use CapitalizeStringInPlace()")
759 // internal transformation to uppercase of String
760 DEPRECATED("Use ToUpperCaseInPlace()")
761 void upcase();
762 // internal transformation to lowercase of String
763 DEPRECATED("Use ToLowerCaseInPlace()")
764 void downcase();
765 // </group>
766
767 // Delete len chars starting at pos. ** Casacore addition
768 DEPRECATED("Use erase()")
770
771 // Delete the first occurrence of target after startpos. ** Casacore addition
772 //<group name=del_after>
773 DEPRECATED("Use EraseStringFrom()")
774 void del(const std::string &str, size_type startpos = 0);
775 DEPRECATED("Use EraseStringFrom()")
776 void del(const char *s, size_type startpos = 0);
777 DEPRECATED("Use EraseStringFrom()")
778 void del(char c, size_type startpos = 0);
779 DEPRECATED("Use std::regex")
780 void del(const Regex &r, size_type startpos = 0);
781 // Overload problem
782 DEPRECATED("Use erase()")
783 void del(int pos, int len) { del(static_cast<size_type>(pos), static_cast<size_type>(len)); }
784 //</group>
785
786 // Global substitution: substitute all occurrences of pat with repl, and
787 // return the number of replacements.
788 // ** Casacore addition
789 //<group name=gsub>
790 DEPRECATED("Use ReplaceAllInPlace()")
791 int gsub(const std::string &pat, const std::string &repl);
792 DEPRECATED("Use ReplaceAllInPlace()")
793 int gsub(const char *pat, const std::string &repl);
794 DEPRECATED("Use ReplaceAllInPlace()")
795 int gsub(const char *pat, const char *repl);
796 DEPRECATED("Use RegexReplaceAll")
797 int gsub(const Regex &pat, const std::string &repl) { return RegexReplaceAll(*this, pat, repl); }
798 //</group>
799
800 private:
801 // Helper functions for at, before etc
802 // <group>
803 SubString _substr(size_type first, size_type l) const { return SubString(*this, first, l); }
804 // </group>
805
806 // Helper function for fromString.
808};
809
810// <summary>
811// Global concatenation operators
812// </summary>
813
814// <group name=concatenator>
815inline String operator+(const String &lhs, const String &rhs) {
816 String str(lhs);
817 str.append(rhs);
818 return str;
819}
820inline String operator+(const char *lhs, const String &rhs) {
821 String str(lhs);
822 str.append(rhs);
823 return str;
824}
825inline String operator+(char lhs, const String &rhs) {
826 String str(1, lhs);
827 str.append(rhs);
828 return str;
829}
830inline String operator+(const String &lhs, const char *rhs) {
831 String str(lhs);
832 str.append(rhs);
833 return str;
834}
835inline String operator+(const String &lhs, char rhs) {
836 String str(lhs);
837 str.append(String(1, rhs));
838 return str;
839}
840// </group>
841
842// <summary>
843// Global comparison operators
844// </summary>
845
846// The global comparison operators
847// <group name=comparitor>
848inline bool operator==(const String &x, const String &y) { return x.compare(y) == 0; }
849inline bool operator!=(const String &x, const String &y) { return x.compare(y) != 0; }
850inline bool operator>(const String &x, const String &y) { return x.compare(y) > 0; }
851inline bool operator>=(const String &x, const String &y) { return x.compare(y) >= 0; }
852inline bool operator<(const String &x, const String &y) { return x.compare(y) < 0; }
853inline bool operator<=(const String &x, const String &y) { return x.compare(y) <= 0; }
854inline bool operator==(const String &x, const char *t) { return x.compare(t) == 0; }
855inline bool operator!=(const String &x, const char *t) { return x.compare(t) != 0; }
856inline bool operator>(const String &x, const char *t) { return x.compare(t) > 0; }
857inline bool operator>=(const String &x, const char *t) { return x.compare(t) >= 0; }
858inline bool operator<(const String &x, const char *t) { return x.compare(t) < 0; }
859inline bool operator<=(const String &x, const char *t) { return x.compare(t) <= 0; }
860inline bool operator==(const String &x, const char t) { return x.compare(String(1, t)) == 0; }
861inline bool operator!=(const String &x, const char t) { return x.compare(String(1, t)) != 0; }
862inline bool operator>(const String &x, const char t) { return x.compare(String(1, t)) > 0; }
863inline bool operator>=(const String &x, const char t) { return x.compare(String(1, t)) >= 0; }
864inline bool operator<(const String &x, const char t) { return x.compare(String(1, t)) < 0; }
865inline bool operator<=(const String &x, const char t) { return x.compare(String(1, t)) <= 0; }
866// ** Casacore additions of global compares. Returns 0 if equal; lt or gt 0 if
867// strings unequal or of unequal lengths.
868// <group>
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)); }
872// this version ignores case. ** Casacore addition. Result is 0 if equal
873// strings of equal lengths; else lt or gt 0 to indicate differences.
874int fcompare(const String &x, const String &y);
875// </group>
876// </group>
877
878// <summary> Splitting </summary>
879// Global function which splits the String into string array res at separator
880// and returns the number of elements. ** Casacore addition
881// <group name=split>
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);
885//</group>
887// <summary> Some general functions </summary>
888// Functions to find special patterns, join and replicate
889// <group name=common>
890String common_prefix(const std::string &x, const std::string &y, int startpos = 0);
891String common_suffix(const std::string &x, const std::string &y, int startpos = -1);
892String replicate(char c, String::size_type n);
893String replicate(const std::string &str, String::size_type n);
894String join(std::string src[], int n, const std::string &sep);
895// </group>
897// <summary> Casing and related functions </summary>
898// Case conversion and rearrangement functions
899// <group name=case>
900// Global function which returns a transformation to reverse order of String.
901String reverse(const std::string &str);
902// Global function which returns a transformation to uppercase of String.
903String upcase(const std::string &str);
904// Global function which returns a transformation to lowercase of String.
905String downcase(const std::string &str);
906// Global function which returns a transformation to capitalization of
907// String.
908String capitalize(const std::string &str);
909// Global function which removes leading and trailing whitespace.
910String trim(const std::string &str);
911// </group>
913// <summary> IO </summary>
914// <group name=io>
915// Output
916std::ostream &operator<<(std::ostream &s, const String &x);
917// </group>
918
919inline SubString::SubString(const std::string &str, std::string::size_type pos,
920 std::string::size_type len)
921 : ref_p(str),
922 pos_p((pos > str.length()) ? str.length() : pos),
923 len_p((len == std::string::npos || pos_p + len > str.length()) ? str.length() - pos_p : len) {
924}
925
926inline SubString String::operator()(size_type pos, size_type len) { return at(pos, len); }
927inline const char *SubString::chars() const { return String(*this).c_str(); }
928
929inline std::ostream &operator<<(std::ostream &s, const String &x) {
930 s << x.c_str();
931 return s;
932}
933
934// Remove specified character from beginning of string.
935// If the character is repeated more than once on the left, all instances
936// will be removed; e.g. LTrimInPlace(str, ',') results in ",,xy" becoming "xy".
937inline void LTrimInPlace(std::string &str, char character) {
938 const std::size_t pos = str.find_first_not_of(character);
939 str.erase(0, pos);
940}
941
942// Remove specified characters from beginning of string.
943// If the characters are repeated more than once on the left, all instances
944// will be removed; e.g. LTrimInPlace(str, "*-") results in "-**-xy" becoming "xy".
945inline void LTrimInPlace(std::string &str, std::string_view characters) {
946 const std::size_t pos = str.find_first_not_of(characters);
947 str.erase(0, pos);
948}
949
950// Remove specified character from end of string.
951// If the character is repeated more than once on the right, all instances
952// will be removed; e.g. RTrimInPlace(str, ',') results in "xy,," becoming "xy".
953inline void RTrimInPlace(std::string &str, char character) {
954 const size_t pos = str.find_last_not_of(character);
955 if (pos == std::string::npos)
956 str.clear();
957 else
958 str.resize(pos + 1);
959}
960
961// Remove specified characters from end of string.
962// If the characters are repeated more than once on the right, all instances
963// will be removed; e.g. RTrimInPlace(str, "*-") results in "xy--**-" becoming "xy".
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)
967 str.clear();
968 else
969 str.resize(pos + 1);
970}
971
972// Remove specified char from beginning and end of string.
973inline void TrimInPlace(std::string &str, char single_character) {
974 LTrimInPlace(str, single_character);
975 RTrimInPlace(str, single_character);
976}
977
978// Remove specified chars from beginning and end of string.
979inline void TrimInPlace(std::string &str, std::string_view characters = kWhiteSpaceCharacters) {
980 LTrimInPlace(str, characters);
981 RTrimInPlace(str, characters);
982}
983
984// Like sprintf. Don't use for new code -- to be deprecated if possible.
985std::string FormatString(const char *format_string, ...);
986
987// Returns the number of times a given pattern occurs in a string.
988// E.g. SubStringCount("ababaaba", "aba") returns 3.
989inline std::size_t SubStringCount(std::string_view str, std::string_view pattern) {
990 std::size_t p = 0;
991 std::size_t count = 0;
992 while (p < str.length()) {
993 if ((p = str.find(pattern, p)) == std::string::npos) break;
994 count++;
995 p++;
996 }
997 return count;
998}
999
1000// Gets a part of a string up to and excluding a specified pattern.
1001// An optional @p start_position can be specified to skip the beginning of the string.
1002inline std::string_view GetStringUpToExcluding(std::string_view input, std::string_view pattern,
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));
1006 // input.subview() is only available from C++26, so construct manually:
1007 return std::string_view(input.begin(), input.begin() + end);
1008}
1009
1010// Rather specific function -- this is a replacement for String:through() with string argument, but
1011// maybe String::through() is rarely used, in which case this function should be removed.
1012inline std::string_view GetStringUpToIncluding(std::string_view input, std::string_view pattern,
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) {
1017 end = input.size();
1018 } else {
1019 end += pattern.size();
1020 }
1021 // input.subview() is only available from C++26, so construct manually:
1022 return std::string_view(input.begin(), input.begin() + end);
1023}
1024
1025// Get a string starting at a specific pattern that is found in the string. The @p start_position
1026// can be used to start the search at a later position.
1027inline std::string_view GetStringFrom(std::string_view input, std::string_view pattern,
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();
1032 // input.subview() is only available from C++26, so construct manually:
1033 return std::string_view(input.begin() + start, input.end());
1034}
1035
1036// Rather specific function -- this is a replacement for String:after(), but maybe String::after()
1037// is rarely used, in which case this function should be removed.
1038inline std::string_view GetStringAfter(std::string_view input, std::string_view pattern,
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();
1044 } else {
1045 start += pattern.size();
1046 }
1047 // input.subview() is only available from C++26, so construct manually:
1048 return std::string_view(input.begin() + start, input.end());
1049}
1050
1051// Converts the specified string to upper case, in place.
1052inline void ToUpperCaseInPlace(std::string &str) {
1053 std::transform(str.begin(), str.end(), str.begin(),
1054 [](unsigned char c) { return static_cast<char>(std::toupper(c)); });
1055}
1056
1057// Converts the specified string to lower case, in place.
1058inline void ToLowerCaseInPlace(std::string &str) {
1059 std::transform(str.begin(), str.end(), str.begin(),
1060 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
1061}
1062
1063// Changes the casing such that every separate word starts with an uppercase
1064// character and continues lowercase.
1065inline void CapitalizeStringInPlace(std::string &str) {
1066 std::string::iterator p = str.begin();
1067 while (p != str.end()) {
1068 bool at_word;
1069 if (std::islower(static_cast<unsigned char>(*p))) {
1070 *p = std::toupper(static_cast<unsigned char>(*p));
1071 at_word = true;
1072 } else {
1073 at_word = std::isupper(static_cast<unsigned char>(*p)) ||
1074 std::isdigit(static_cast<unsigned char>(*p));
1075 }
1076 ++p;
1077 // at_word is now true if the previous *p is a character or digit
1078 if (at_word) {
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)))
1084 break;
1085 ++p;
1086 }
1087 if (p != str.end()) ++p;
1088 }
1089 }
1090}
1091
1092// Remove the end of a string, starting from a specified pattern.
1093// A start_position can be specified to skip the beginning during the search for the pattern
1094inline void EraseStringFrom(std::string &str, std::string_view pattern, size_t start_position = 0) {
1095 const std::size_t start = str.find(pattern, start_position);
1096 if (start != std::string::npos) str.erase(start, pattern.length());
1097}
1098
1099// Replace every occurence of @c pattern in @c str by @c replacement.
1100inline size_t ReplaceAllInPlace(std::string &str, std::string_view pattern,
1101 std::string_view replacement) {
1102 std::size_t n_matches = 0;
1103 if (str.length() == 0 || pattern.length() == 0 || str.length() < pattern.length())
1104 return n_matches;
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)
1109 break;
1110 else {
1111 n_matches++;
1112 str.replace(pos, pattern.length(), replacement);
1113 search_index = pos + replacement.length();
1114 }
1115 }
1116 return n_matches;
1117}
1118
1119// Determines if a given string contains another given string.
1120inline constexpr bool StringContains(std::string_view str, std::string_view pattern) {
1121 return str.find(pattern) != std::string_view::npos;
1122}
1123
1124} // namespace casacore
1125
1126// Define the hash function for String, so unordered_set<String> can be used.
1127namespace std {
1128template <>
1129struct hash<casacore::String> {
1130 std::size_t operator()(casacore::String const &k) const noexcept {
1131 return std::hash<std::string>()(k);
1132 }
1133};
1134} // namespace std
1135
1136#ifdef CASACORE_DEPRECATE_STRING
1137#pragma GCC diagnostic pop
1138#endif
1139
1140#undef DEPRECATED
1141
1142#endif
#define DEPRECATED(X)
Some of the (deprecated) code calls deprecated functions, so disable this.
Definition String.h:36
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.
Definition String.h:355
SubString before(int pos)
Next one for overloading reasons.
Definition String.h:695
SubString at(int pos, size_type len)
Definition String.h:673
bool startsWith(const std::string &beginString) const
Does the string start with the specified string?
Definition String.h:544
void downcase()
internal transformation to lowercase of String
std::string & append(char c)
** Casacore addition
Definition String.h:432
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)
Definition String.h:506
static String toString(const T &value)
Convert a value to a String.
Definition String.h:518
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)
Definition String.h:404
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.
Definition String.h:365
void rtrim(char c)
Remove specified character from end of string.
String & insert(size_type pos, char c)
** Casacore addition
Definition String.h:441
void capitalize()
internal transformation to capitalization of String.
const_reference elem(size_type pos) const
*** Casacore addition
Definition String.h:415
String(size_type n, char c)
Construct from a single char (repeated n times).
Definition String.h:378
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.
Definition String.h:494
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.
Definition String.h:389
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
Definition String.h:437
int allocation() const
** Casacore addition – works as a capacity(n) – Note Int
Definition String.h:424
static float toFloat(const String &s, bool chk=false)
Definition String.h:500
DEPRECATED("Use find(str) != npos or StringContains()") bool contains(const std int pos const
Definition String.h:568
SubString operator()(size_type pos, size_type len)
Casacore addition: synonym for at(pos, len).
Definition String.h:929
String & operator=(const char *s)
Definition String.h:405
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.
Definition String.h:554
void alloc(size_type n)
** Casacore addition – works as a resize(n)
Definition String.h:428
String(const char *s)
Construct from char array.
Definition String.h:373
String & operator=(char c)
Definition String.h:406
SubString _substr(size_type first, size_type l) const
Helper functions for at, before etc.
Definition String.h:803
String(const char *s, size_type n)
Construct from char* with given length.
Definition String.h:371
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
Definition String.h:675
void throwFromStringError() const
Helper function for fromString.
char firstchar() const
Definition String.h:418
bool fromString(T &value, bool throw_on_error=true) const
Convert a String to a value.
Definition String.h:477
String & replace(size_type pos, size_type n1, char c)
** Casacore addition
Definition String.h:448
const char * chars() const
** Casacore synonym
Definition String.h:460
void del(int pos, int len)
Overload problem.
Definition String.h:784
String(InputIterator begin, InputIterator end)
Construct from iterator.
Definition String.h:381
SubString at(size_type pos, size_type len)
SubString through(int pos)
Next one for overloading reasons.
Definition String.h:713
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
Definition String.h:593
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
T fromString() const
Definition String.h:482
SubString from(int pos)
Next one for overloading reasons.
Definition String.h:731
char lastchar() const
Definition String.h:420
SubString after(int pos)
Next one for overloading reasons.
Definition String.h:749
std::string::size_type length() const
Obtain length.
Definition String.h:210
const char * chars() const
Get as (const) C array.
Definition String.h:930
std::string::size_type len_p
Length of sub-string.
Definition String.h:222
SubString(const SubString &)=default
Default copy constructor.
const std::string & ref_p
Referenced string.
Definition String.h:218
std::string::size_type pos_p
Start of sub-string.
Definition String.h:220
friend class String
Definition String.h:188
operator const std::string() const
Make a string.
Definition String.h:191
bool operator==(const String &x, const String &y)
Global comparison operators.
Definition String.h:849
String operator+(const String &lhs, const String &rhs)
Global concatenation operators.
Definition String.h:816
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 ...
struct Node * first
Definition malloc.h:325
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
bool StringToValue(const std::string &str, T &value, bool throw_on_error=true)
Convert a String to a value.
Definition String.h:110
TableExprNode pattern(const TableExprNode &node)
Definition ExprNode.h:1305
double StringToDouble(const std::string &str, bool throw_on_error=false)
Same as StringToValue<double>.
Definition String.h:148
void LTrimInPlace(std::string &str, char character)
Remove specified character from beginning of string.
Definition String.h:938
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.
Definition String.h:973
void ToLowerCaseInPlace(std::string &str)
Converts the specified string to lower case, in place.
Definition String.h:1058
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.
Definition String.h:1027
std::string ValueToString(T &&value)
Definition String.h:153
bool operator<(const String &x, const String &y)
Definition String.h:853
constexpr bool StringContains(std::string_view str, std::string_view pattern)
Determines if a given string contains another given string.
Definition String.h:1120
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)
Definition String.h:852
void CapitalizeStringInPlace(std::string &str)
Changes the casing such that every separate word starts with an uppercase character and continues low...
Definition String.h:1065
int compare(const std::string &x, const std::string &y)
** Casacore additions of global compares.
Definition String.h:869
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.
Definition String.h:1094
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.
Definition String.h:64
const std::string_view kWhiteSpaceCharacters
Standard white space characters: space, new line, tab and carriage return.
Definition String.h:55
size_t size_type
Definition Block.h:594
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.
Definition String.h:953
iterator begin()
Get the begin and end iterator object for this block.
Definition Block.h:599
bool operator<=(const String &x, const String &y)
Definition String.h:854
std::string FormatString(const char *format_string,...)
Like sprintf.
T * iterator
Definition Block.h:588
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.
Definition String.h:1052
const value_type & const_reference
Definition Block.h:593
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...
Definition String.h:90
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...
Definition String.h:1038
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.
Definition String.h:1100
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).
Definition String.h:58
bool operator>(const String &x, const String &y)
Definition String.h:851
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>.
Definition String.h:143
int StringToInt(const std::string &str, bool throw_on_error=false)
Same as StringToValue<int>.
Definition String.h:138
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.
Definition String.h:1002
bool operator!=(const String &x, const String &y)
Definition String.h:850
String join(std::string src[], int n, const std::string &sep)
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
iterator end()
Definition Block.h:601
std::size_t SubStringCount(std::string_view str, std::string_view pattern)
Returns the number of times a given pattern occurs in a string.
Definition String.h:989
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,...
Definition String.h:1012
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.
Definition Complex.h:344
std::size_t operator()(casacore::String const &k) const noexcept
Definition String.h:1130