Loading [MathJax]/extensions/tex2jax.js
TkN 2.1
Toolkit for Nuclei
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
tkstring.h
1/********************************************************************************
2 * Copyright (c) : Université de Lyon 1, CNRS/IN2P3, UMR5822, *
3 * IP2I, F-69622 Villeurbanne Cedex, France *
4 * Normandie Université, ENSICAEN, UNICAEN, CNRS/IN2P3, *
5 * LPC Caen, F-14000 Caen, France *
6 * Contibutor(s) : *
7 * Jérémie Dudouet jeremie.dudouet@cnrs.fr [2020] *
8 * Diego Gruyer diego.gruyer@cnrs.fr [2020] *
9 * *
10 * This software is governed by the CeCILL-B license under French law and *
11 * abiding by the rules of distribution of free software. You can use, *
12 * modify and/ or redistribute the software under the terms of the *
13 * CeCILL-B license as circulated by CEA, CNRS and INRIA at the following *
14 * URL \"http://www.cecill.info\". *
15 * *
16 * As a counterpart to the access to the source code and rights to copy, *
17 * modify and redistribute granted by the license, users are provided *
18 * only with a limited warranty and the software's author, the holder of *
19 * the economic rights, and the successive licensors have only limited *
20 * liability. *
21 * *
22 * In this respect, the user's attention is drawn to the risks associated *
23 * with loading, using, modifying and/or developing or reproducing the *
24 * software by the user in light of its specific status of free software, *
25 * that may mean that it is complicated to manipulate, and that also *
26 * therefore means that it is reserved for developers and experienced *
27 * professionals having in-depth computer knowledge. Users are therefore *
28 * encouraged to load and test the software's suitability as regards *
29 * their requirements in conditions enabling the security of their *
30 * systems and/or data to be ensured and, more generally, to use and *
31 * operate it in the same conditions as regards security. *
32 * *
33 * The fact that you are presently reading this means that you have had *
34 * knowledge of the CeCILL-B license and that you accept its terms. *
35 ********************************************************************************/
36
37#ifndef tkstring_H
38#define tkstring_H
39
40#include <iomanip>
41#include <vector>
42#include <strings.h>
43#include <cstring>
44
45#include "tkn_config.h"
46
47#ifdef HAS_ROOT
48#include "TClass.h"
49#endif
50
51namespace tkn {
52
53class tkstring : public std::string
54{
55
56public:
58
59public:
60 tkstring() : std::string() {}
61 tkstring(const char *_c) : std::string(_c) {;}
62 tkstring(const unsigned char *_c) : std::string(reinterpret_cast< const char* >(_c)) {;}
63 tkstring(const std::string &_s) : std::string(_s.data()) {;}
64// tkstring(const tkstring &_s) : std::string(_s.data()) {;}
65 tkstring(double _value, double _error);
66 tkstring(char _c) : std::string(&_c) {;}
67
68 virtual ~tkstring() {}
69
70 tkstring substr(size_type __pos = 0, size_type __n = npos) const;
71
73 bool is_digit() const;
75 bool is_float() const;
76
78 bool is_alpha() const;
83
85 int atoi() const;
87 double atof() const;
89 int64_t atoll() const { return std::atoll(data());}
90
91// //! Returns a string formated energy with error
92// static tkstring energy_to_string(double _val, int _precision);
93// //! Returns a string formated energy with error
94// static tkstring energy_error_to_string(double _val, int _precision);
95
97 static double get_precision(tkstring _st);
98
100 static double get_absolute_error(tkstring val, tkstring error);
101
103 std::vector<tkstring> tokenize(const tkstring &_delim = " ") const;
104
106 std::vector<tkstring> tokenize_from_string(const tkstring &_delim) const;
107
109 tkstring copy() const;
110
113
118
120 size_t index(const char *_s, size_t _pos=0, ECaseCompare _cmp=kExact) const;
122 size_t index(const tkstring &_pat, size_t _pos=0, ECaseCompare _cmp=kExact) const;
123
125 bool equal_to(const char *_s, ECaseCompare _cmp = kExact) const;
126 bool equal_to(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
127
128 bool begins_with(const char *_s, ECaseCompare _cmp = kExact) const;
129 bool begins_with(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
130 bool ends_with(const char *_s, ECaseCompare _cmp = kExact) const;
131 bool ends_with(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
132
133 bool contains(const char *_pat, ECaseCompare _cmp = kExact) const;
134 bool contains(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
135
136 tkstring &append(const tkstring &_st);
137 tkstring &prepend(const tkstring &_st);
138
139 tkstring &replace_all(const tkstring &_s1, const tkstring &_s2); // Find&Replace all s1 with s2 if any
140 tkstring &replace_all(const tkstring &_s1, const char *_s2); // Find&Replace all s1 with s2 if any
141 tkstring &replace_all(const char *_s1, const tkstring &_s2); // Find&Replace all s1 with s2 if any
142 tkstring &replace_all(const char *_s1, const char *_s2); // Find&Replace all s1 with s2 if any
143 tkstring &replace_all(const char *_s1, size_t _ls1, const char *_s2, size_t _ls2); // Find&Replace all s1 with s2 if any
144
145 tkstring &remove_all(const tkstring &_s1);
146 tkstring &remove_all(const char *_s1);
147
148 tkstring remove_last_occurence(const char *_s1);
149 tkstring get_last_occurence(const char *_s1);
150
151 static tkstring Form(const char * _format, ...); // return a string using the c printf method
152 static const char* form(const char * _format, ...); // return a string using the c printf method
153
156
157 std::istream &read_line(std::istream &_strm, bool _skip_white = true); // Read to EOF or newline
158
159 bool match(const char *_pattern) const;
160 bool match(const tkstring &_pattern) const;
161
162 // return true if empty or composed only of blank spaces
163 bool is_empty() const {
164 if(length()==0) return true;
165 else return (this->strip_all_extra_white_space() == "");
166 }
167
168 // count the number of occurences of the string _st
169 int count_string(const tkstring &_st) const;
170
171#ifdef HAS_ROOT
173 ClassDef(tkstring,0);
174#endif
175};
176
178
179inline tkstring tkstring::substr(size_type __pos, size_type __n) const
180{ tkstring tmp = std::string::substr(__pos,__n); return tmp;}
181
182inline bool tkstring::equal_to(const tkstring &_pat, ECaseCompare _cmp) const
183{ return equal_to(_pat.data(),_cmp); }
184
185inline bool tkstring::begins_with(const char *_s, ECaseCompare _cmp) const
186{ return index(_s, 0, _cmp) == 0; }
187
188inline bool tkstring::begins_with(const tkstring &_pat, ECaseCompare _cmp) const
189{ return index(_pat.data(), 0, _cmp) == 0; }
190
191inline bool tkstring::ends_with(const tkstring &_pat, ECaseCompare _cmp) const
192{ return ends_with(_pat.data(),_cmp); }
193
194inline bool tkstring::contains(const tkstring &_pat, ECaseCompare _cmp) const
195{ return index(_pat.data(), 0, _cmp) != npos; }
196
197inline bool tkstring::contains(const char *_s, ECaseCompare _cmp) const
198{ return index(_s, 0, _cmp) != npos; }
199
200inline size_t tkstring::index(const tkstring &_pat, size_t _pos, ECaseCompare _cmp) const
201{ return index(_pat.data(), _pos, _cmp); }
202
203inline tkstring &tkstring::replace_all(const tkstring &_s1, const tkstring &_s2)
204{ return replace_all(_s1.data(), _s1.length(), _s2.data(), _s2.length()) ; }
205
206inline tkstring &tkstring::replace_all(const tkstring &_s1, const char *_s2)
207{ return replace_all(_s1.data(), _s1.length(), _s2, _s2 ? strlen(_s2) : 0); }
208
209inline tkstring &tkstring::replace_all(const char *_s1, const tkstring &_s2)
210{ return replace_all(_s1, _s1 ? strlen(_s1) : 0, _s2.data(), _s2.length()); }
211
212inline tkstring &tkstring::replace_all(const char *_s1,const char *_s2)
213{ return replace_all(_s1, _s1 ? strlen(_s1) : 0, _s2, _s2 ? strlen(_s2) : 0); }
214
216{ return replace_all(_s1, "") ; }
217
218inline tkstring &tkstring::remove_all(const char *_s1)
219{ return replace_all(_s1, "") ; }
220
221inline bool tkstring::match(const tkstring &_pat) const
222{ return match(_pat.data()); }
223
225{ insert(0,_st); return *this;}
226
228{ std::string::append(_st); return *this;}
229
232
233}
234#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition: tkstring.h:54
tkstring extract_alpha()
Returns a tkstring composed only of the alphabetic letters of the original tkstring.
Definition: tkstring.cpp:387
tkstring(const unsigned char *_c)
Definition: tkstring.h:62
tkstring strip_all_extra_white_space() const
Definition: tkstring.cpp:560
tkstring copy() const
Returns a copy of this string.
Definition: tkstring.cpp:370
tkstring & to_lower()
Change all letters to lower case.
Definition: tkstring.cpp:59
tkstring(const std::string &_s)
Definition: tkstring.h:63
static const char * form(const char *_format,...)
Definition: tkstring.cpp:431
bool is_float() const
Checks if string contains a floating point or integer number.
Definition: tkstring.cpp:110
tkstring get_last_occurence(const char *_s1)
Definition: tkstring.cpp:329
std::vector< tkstring > tokenize(const tkstring &_delim=" ") const
Create a vector of string separated by at least one delimiter.
Definition: tkstring.cpp:254
static tkstring Form(const char *_format,...)
Definition: tkstring.cpp:345
bool is_empty() const
Definition: tkstring.h:163
tkstring & remove_all(const tkstring &_s1)
Definition: tkstring.h:215
tkstring(char _c)
Definition: tkstring.h:66
tkstring substr(size_type __pos=0, size_type __n=npos) const
Inlines.
Definition: tkstring.h:179
std::vector< tkstring > tokenize_from_string(const tkstring &_delim) const
Create a vector of string separated by a full string as delimiter.
Definition: tkstring.cpp:294
tkstring(const char *_c)
Definition: tkstring.h:61
bool match(const char *_pattern) const
Definition: tkstring.cpp:579
bool is_alpha() const
Checks whether tkstring is only composed of alphabetic letters.
Definition: tkstring.cpp:420
std::istream & read_line(std::istream &_strm, bool _skip_white=true)
tkstring::read_line
Definition: tkstring.cpp:623
int atoi() const
Converts a string to integer value.
Definition: tkstring.cpp:175
bool ends_with(const char *_s, ECaseCompare _cmp=kExact) const
Definition: tkstring.cpp:241
virtual ~tkstring()
Definition: tkstring.h:68
static double get_absolute_error(tkstring val, tkstring error)
Get absolute uncertainty from value and error strings (1.27 4 -> 0.04), returns -1 in case of empty e...
Definition: tkstring.cpp:631
bool equal_to(const char *_s, ECaseCompare _cmp=kExact) const
Returns true if the string and _s are identical.
Definition: tkstring.cpp:234
size_t index(const char *_s, size_t _pos=0, ECaseCompare _cmp=kExact) const
Returns the index of the substring _s.
Definition: tkstring.cpp:218
tkstring remove_alpha()
Returns a tkstring composed only of the non alphabetic letters of the original tkstring.
Definition: tkstring.cpp:401
tkstring & remove_all_extra_white_space()
Definition: tkstring.cpp:526
bool contains(const char *_pat, ECaseCompare _cmp=kExact) const
Definition: tkstring.h:197
tkstring & prepend(const tkstring &_st)
Definition: tkstring.h:224
int count_string(const tkstring &_st) const
Definition: tkstring.cpp:547
bool begins_with(const char *_s, ECaseCompare _cmp=kExact) const
Definition: tkstring.h:185
tkstring & append(const tkstring &_st)
Definition: tkstring.h:227
tkstring & capitalize()
Change first letter of string from lower to upper case.
Definition: tkstring.cpp:376
static double get_precision(tkstring _st)
Extract the precision for a given ENSDF data.
Definition: tkstring.cpp:649
tkstring & replace_all(const tkstring &_s1, const tkstring &_s2)
Definition: tkstring.h:203
bool is_digit() const
Checks if all characters in string are digits (0-9) or whitespaces.
Definition: tkstring.cpp:81
int64_t atoll() const
Converts a string to long integer value.
Definition: tkstring.h:89
tkstring remove_last_occurence(const char *_s1)
Definition: tkstring.cpp:337
double atof() const
Converts a string to double value.
Definition: tkstring.cpp:191
tkstring & to_upper()
Change all letters to upper case.
Definition: tkstring.cpp:65
Definition: tklog.cpp:39
tklog & error(tklog &log)
Definition: tklog.h:367
tkstring & append(const tkstring &_st)
tkstring & preprend(const tkstring &_st)