TkN 2.4
Toolkit for Nuclei
Loading...
Searching...
No Matches
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 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
11 * SPDX-License-Identifier: MIT *
12 ********************************************************************************/
13
14#ifndef tkstring_H
15#define tkstring_H
16
17#include <iomanip>
18#include <vector>
19#include <strings.h>
20#include <cstring>
21
22#include "tkn_config.h"
23
24#ifdef HAS_ROOT
25#include "TClass.h"
26#endif
27
28namespace tkn {
29
30class tkstring : public std::string
31{
32
33public:
35
36public:
37 tkstring() : std::string() {}
38 tkstring(const char *_c) : std::string(_c) {;}
39 tkstring(const unsigned char *_c) : std::string(reinterpret_cast< const char* >(_c)) {;}
40 tkstring(const std::string &_s) : std::string(_s.data()) {;}
41// tkstring(const tkstring &_s) : std::string(_s.data()) {;}
42 tkstring(double _value, double _error);
43 tkstring(char _c) : std::string(&_c) {;}
44
45 virtual ~tkstring() {}
46
47 tkstring substr(size_type __pos = 0, size_type __n = npos) const;
48
50 bool is_digit() const;
52 bool is_float() const;
53
55 bool is_alpha() const;
60
62 int atoi() const;
64 double atof() const;
66 int64_t atoll() const { return std::atoll(data());}
67
68// //! Returns a string formated energy with error
69// static tkstring energy_to_string(double _val, int _precision);
70// //! Returns a string formated energy with error
71// static tkstring energy_error_to_string(double _val, int _precision);
72
74 static double get_precision(tkstring _st);
75
77 static double get_absolute_error(tkstring val, tkstring error);
78
80 std::vector<tkstring> tokenize(const tkstring &_delim = " ") const;
81
83 std::vector<tkstring> tokenize_from_string(const tkstring &_delim) const;
84
86 tkstring copy() const;
87
90
95
97 size_t index(const char *_s, size_t _pos=0, ECaseCompare _cmp=kExact) const;
99 size_t index(const tkstring &_pat, size_t _pos=0, ECaseCompare _cmp=kExact) const;
100
102 bool equal_to(const char *_s, ECaseCompare _cmp = kExact) const;
103 bool equal_to(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
104
105 bool begins_with(const char *_s, ECaseCompare _cmp = kExact) const;
106 bool begins_with(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
107 bool ends_with(const char *_s, ECaseCompare _cmp = kExact) const;
108 bool ends_with(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
109
110 bool contains(const char *_pat, ECaseCompare _cmp = kExact) const;
111 bool contains(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
112
113 tkstring &append(const tkstring &_st);
114 tkstring &prepend(const tkstring &_st);
115
116 tkstring &replace_all(const tkstring &_s1, const tkstring &_s2); // Find&Replace all s1 with s2 if any
117 tkstring &replace_all(const tkstring &_s1, const char *_s2); // Find&Replace all s1 with s2 if any
118 tkstring &replace_all(const char *_s1, const tkstring &_s2); // Find&Replace all s1 with s2 if any
119 tkstring &replace_all(const char *_s1, const char *_s2); // Find&Replace all s1 with s2 if any
120 tkstring &replace_all(const char *_s1, size_t _ls1, const char *_s2, size_t _ls2); // Find&Replace all s1 with s2 if any
121
122 tkstring &remove_all(const tkstring &_s1);
123 tkstring &remove_all(const char *_s1);
124
125 tkstring remove_last_occurence(const char *_s1);
126 tkstring get_last_occurence(const char *_s1);
127
128 static tkstring Form(const char * _format, ...); // return a string using the c printf method
129 static const char* form(const char * _format, ...); // return a string using the c printf method
130
133
134 std::istream &read_line(std::istream &_strm, bool _skip_white = true); // Read to EOF or newline
135
136 bool match(const char *_pattern) const;
137 bool match(const tkstring &_pattern) const;
138
139 // return true if empty or composed only of blank spaces
140 bool is_empty() const {
141 if(length()==0) return true;
142 else return (this->strip_all_extra_white_space() == "");
143 }
144
145 // count the number of occurences of the string _st
146 int count_string(const tkstring &_st) const;
147
148#ifdef HAS_ROOT
150 ClassDef(tkstring,0);
151#endif
152};
153
155
156inline tkstring tkstring::substr(size_type __pos, size_type __n) const
157{ tkstring tmp = std::string::substr(__pos,__n); return tmp;}
158
159inline bool tkstring::equal_to(const tkstring &_pat, ECaseCompare _cmp) const
160{ return equal_to(_pat.data(),_cmp); }
161
162inline bool tkstring::begins_with(const char *_s, ECaseCompare _cmp) const
163{ return index(_s, 0, _cmp) == 0; }
164
165inline bool tkstring::begins_with(const tkstring &_pat, ECaseCompare _cmp) const
166{ return index(_pat.data(), 0, _cmp) == 0; }
167
168inline bool tkstring::ends_with(const tkstring &_pat, ECaseCompare _cmp) const
169{ return ends_with(_pat.data(),_cmp); }
170
171inline bool tkstring::contains(const tkstring &_pat, ECaseCompare _cmp) const
172{ return index(_pat.data(), 0, _cmp) != npos; }
173
174inline bool tkstring::contains(const char *_s, ECaseCompare _cmp) const
175{ return index(_s, 0, _cmp) != npos; }
176
177inline size_t tkstring::index(const tkstring &_pat, size_t _pos, ECaseCompare _cmp) const
178{ return index(_pat.data(), _pos, _cmp); }
179
180inline tkstring &tkstring::replace_all(const tkstring &_s1, const tkstring &_s2)
181{ return replace_all(_s1.data(), _s1.length(), _s2.data(), _s2.length()) ; }
182
183inline tkstring &tkstring::replace_all(const tkstring &_s1, const char *_s2)
184{ return replace_all(_s1.data(), _s1.length(), _s2, _s2 ? strlen(_s2) : 0); }
185
186inline tkstring &tkstring::replace_all(const char *_s1, const tkstring &_s2)
187{ return replace_all(_s1, _s1 ? strlen(_s1) : 0, _s2.data(), _s2.length()); }
188
189inline tkstring &tkstring::replace_all(const char *_s1,const char *_s2)
190{ return replace_all(_s1, _s1 ? strlen(_s1) : 0, _s2, _s2 ? strlen(_s2) : 0); }
191
193{ return replace_all(_s1, "") ; }
194
195inline tkstring &tkstring::remove_all(const char *_s1)
196{ return replace_all(_s1, "") ; }
197
198inline bool tkstring::match(const tkstring &_pat) const
199{ return match(_pat.data()); }
200
202{ insert(0,_st); return *this;}
203
205{ std::string::append(_st); return *this;}
206
209
210}
211#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:31
tkstring extract_alpha()
Returns a tkstring composed only of the alphabetic letters of the original tkstring.
Definition tkstring.cpp:364
tkstring(const unsigned char *_c)
Definition tkstring.h:39
tkstring strip_all_extra_white_space() const
Definition tkstring.cpp:537
tkstring copy() const
Returns a copy of this string.
Definition tkstring.cpp:347
tkstring & to_lower()
Change all letters to lower case.
Definition tkstring.cpp:36
tkstring(const std::string &_s)
Definition tkstring.h:40
static const char * form(const char *_format,...)
Definition tkstring.cpp:408
bool is_float() const
Checks if string contains a floating point or integer number.
Definition tkstring.cpp:87
tkstring get_last_occurence(const char *_s1)
Definition tkstring.cpp:306
std::vector< tkstring > tokenize(const tkstring &_delim=" ") const
Create a vector of string separated by at least one delimiter.
Definition tkstring.cpp:231
static tkstring Form(const char *_format,...)
Definition tkstring.cpp:322
bool is_empty() const
Definition tkstring.h:140
tkstring & remove_all(const tkstring &_s1)
Definition tkstring.h:192
tkstring(char _c)
Definition tkstring.h:43
tkstring substr(size_type __pos=0, size_type __n=npos) const
Inlines.
Definition tkstring.h:156
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:271
tkstring(const char *_c)
Definition tkstring.h:38
bool match(const char *_pattern) const
Definition tkstring.cpp:556
bool is_alpha() const
Checks whether tkstring is only composed of alphabetic letters.
Definition tkstring.cpp:397
std::istream & read_line(std::istream &_strm, bool _skip_white=true)
tkstring::read_line
Definition tkstring.cpp:600
int atoi() const
Converts a string to integer value.
Definition tkstring.cpp:152
bool ends_with(const char *_s, ECaseCompare _cmp=kExact) const
Definition tkstring.cpp:218
virtual ~tkstring()
Definition tkstring.h:45
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:608
bool equal_to(const char *_s, ECaseCompare _cmp=kExact) const
Returns true if the string and _s are identical.
Definition tkstring.cpp:211
size_t index(const char *_s, size_t _pos=0, ECaseCompare _cmp=kExact) const
Returns the index of the substring _s.
Definition tkstring.cpp:195
tkstring remove_alpha()
Returns a tkstring composed only of the non alphabetic letters of the original tkstring.
Definition tkstring.cpp:378
tkstring & remove_all_extra_white_space()
Definition tkstring.cpp:503
bool contains(const char *_pat, ECaseCompare _cmp=kExact) const
Definition tkstring.h:174
tkstring & prepend(const tkstring &_st)
Definition tkstring.h:201
int count_string(const tkstring &_st) const
Definition tkstring.cpp:524
bool begins_with(const char *_s, ECaseCompare _cmp=kExact) const
Definition tkstring.h:162
tkstring & append(const tkstring &_st)
Definition tkstring.h:204
tkstring & capitalize()
Change first letter of string from lower to upper case.
Definition tkstring.cpp:353
static double get_precision(tkstring _st)
Extract the precision for a given ENSDF data.
Definition tkstring.cpp:626
tkstring & replace_all(const tkstring &_s1, const tkstring &_s2)
Definition tkstring.h:180
bool is_digit() const
Checks if all characters in string are digits (0-9) or whitespaces.
Definition tkstring.cpp:58
int64_t atoll() const
Converts a string to long integer value.
Definition tkstring.h:66
tkstring remove_last_occurence(const char *_s1)
Definition tkstring.cpp:314
double atof() const
Converts a string to double value.
Definition tkstring.cpp:168
tkstring & to_upper()
Change all letters to upper case.
Definition tkstring.cpp:42
Definition tklog.cpp:16
tklog & error(tklog &log)
Definition tklog.h:344
tkstring & append(const tkstring &_st)
tkstring & preprend(const tkstring &_st)