TkN 2.5
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 <string>
19#include <vector>
20#include <strings.h>
21#include <cstring>
22
23#include "tkn_config.h"
24
25#ifdef HAS_ROOT
26#include "TClass.h"
27#endif
28
29namespace tkn {
30
31class tkstring : public std::string
32{
33
34public:
36
37public:
38 tkstring() : std::string() {}
39 tkstring(const char *_c) : std::string(_c) {;}
40 tkstring(const unsigned char *_c) : std::string(reinterpret_cast< const char* >(_c)) {;}
41 tkstring(const std::string &_s) : std::string(_s.data()) {;}
42// tkstring(const tkstring &_s) : std::string(_s.data()) {;}
43 tkstring(double _value, double _error);
44 tkstring(char _c) : std::string(&_c) {;}
45
46 virtual ~tkstring() {}
47
48 tkstring substr(size_type __pos = 0, size_type __n = npos) const;
49
51 bool is_digit() const;
53 bool is_float() const;
54
56 bool is_alpha() const;
61
63 int atoi() const;
65 double atof() const;
67 int64_t atoll() const { return std::atoll(data());}
68
69// //! Returns a string formated energy with error
70// static tkstring energy_to_string(double _val, int _precision);
71// //! Returns a string formated energy with error
72// static tkstring energy_error_to_string(double _val, int _precision);
73
75 static double get_precision(tkstring _st);
76
78 static double get_absolute_error(tkstring val, tkstring error);
79
81 std::vector<tkstring> tokenize(const tkstring &_delim = " ") const;
82
84 std::vector<tkstring> tokenize_from_string(const tkstring &_delim) const;
85
87 tkstring copy() const;
88
91
96
98 size_t index(const char *_s, size_t _pos=0, ECaseCompare _cmp=kExact) const;
100 size_t index(const tkstring &_pat, size_t _pos=0, ECaseCompare _cmp=kExact) const;
101
103 bool equal_to(const char *_s, ECaseCompare _cmp = kExact) const;
104 bool equal_to(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
105
106 bool begins_with(const char *_s, ECaseCompare _cmp = kExact) const;
107 bool begins_with(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
108 bool ends_with(const char *_s, ECaseCompare _cmp = kExact) const;
109 bool ends_with(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
110
111 bool contains(const char *_pat, ECaseCompare _cmp = kExact) const;
112 bool contains(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
113
114 tkstring &append(const tkstring &_st);
115 tkstring &prepend(const tkstring &_st);
116
117 tkstring &replace_all(const tkstring &_s1, const tkstring &_s2); // Find&Replace all s1 with s2 if any
118 tkstring &replace_all(const tkstring &_s1, const char *_s2); // Find&Replace all s1 with s2 if any
119 tkstring &replace_all(const char *_s1, const tkstring &_s2); // Find&Replace all s1 with s2 if any
120 tkstring &replace_all(const char *_s1, const char *_s2); // Find&Replace all s1 with s2 if any
121 tkstring &replace_all(const char *_s1, size_t _ls1, const char *_s2, size_t _ls2); // Find&Replace all s1 with s2 if any
122
123 tkstring &remove_all(const tkstring &_s1);
124 tkstring &remove_all(const char *_s1);
125
126 tkstring remove_last_occurence(const char *_s1);
127 tkstring get_last_occurence(const char *_s1);
128
129 static tkstring Form(const char * _format, ...); // return a string using the c printf method
130 static const char* form(const char * _format, ...); // return a string using the c printf method
131
134
135 std::istream &read_line(std::istream &_strm, bool _skip_white = true); // Read to EOF or newline
136
137 bool match(const char *_pattern) const;
138 bool match(const tkstring &_pattern) const;
139
140 // return true if empty or composed only of blank spaces
141 bool is_empty() const {
142 if(length()==0) return true;
143 else return (this->strip_all_extra_white_space() == "");
144 }
145
146 // count the number of occurences of the string _st
147 int count_string(const tkstring &_st) const;
148
149#ifdef HAS_ROOT
151 ClassDef(tkstring,0);
152#endif
153};
154
156
157inline tkstring tkstring::substr(size_type __pos, size_type __n) const
158{ tkstring tmp = std::string::substr(__pos,__n); return tmp;}
159
160inline bool tkstring::equal_to(const tkstring &_pat, ECaseCompare _cmp) const
161{ return equal_to(_pat.data(),_cmp); }
162
163inline bool tkstring::begins_with(const char *_s, ECaseCompare _cmp) const
164{ return index(_s, 0, _cmp) == 0; }
165
166inline bool tkstring::begins_with(const tkstring &_pat, ECaseCompare _cmp) const
167{ return index(_pat.data(), 0, _cmp) == 0; }
168
169inline bool tkstring::ends_with(const tkstring &_pat, ECaseCompare _cmp) const
170{ return ends_with(_pat.data(),_cmp); }
171
172inline bool tkstring::contains(const tkstring &_pat, ECaseCompare _cmp) const
173{ return index(_pat.data(), 0, _cmp) != npos; }
174
175inline bool tkstring::contains(const char *_s, ECaseCompare _cmp) const
176{ return index(_s, 0, _cmp) != npos; }
177
178inline size_t tkstring::index(const tkstring &_pat, size_t _pos, ECaseCompare _cmp) const
179{ return index(_pat.data(), _pos, _cmp); }
180
181inline tkstring &tkstring::replace_all(const tkstring &_s1, const tkstring &_s2)
182{ return replace_all(_s1.data(), _s1.length(), _s2.data(), _s2.length()) ; }
183
184inline tkstring &tkstring::replace_all(const tkstring &_s1, const char *_s2)
185{ return replace_all(_s1.data(), _s1.length(), _s2, _s2 ? strlen(_s2) : 0); }
186
187inline tkstring &tkstring::replace_all(const char *_s1, const tkstring &_s2)
188{ return replace_all(_s1, _s1 ? strlen(_s1) : 0, _s2.data(), _s2.length()); }
189
190inline tkstring &tkstring::replace_all(const char *_s1,const char *_s2)
191{ return replace_all(_s1, _s1 ? strlen(_s1) : 0, _s2, _s2 ? strlen(_s2) : 0); }
192
194{ return replace_all(_s1, "") ; }
195
196inline tkstring &tkstring::remove_all(const char *_s1)
197{ return replace_all(_s1, "") ; }
198
199inline bool tkstring::match(const tkstring &_pat) const
200{ return match(_pat.data()); }
201
203{ insert(0,_st); return *this;}
204
206{ std::string::append(_st); return *this;}
207
210
211std::string wrap_text(const tkstring &_text, size_t _first_content_col, size_t _continuation_col, size_t _max_line_width = 80);
212
213}
214#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:32
tkstring extract_alpha()
Returns a tkstring composed only of the alphabetic letters of the original tkstring.
Definition tkstring.cpp:408
tkstring(const unsigned char *_c)
Definition tkstring.h:40
tkstring strip_all_extra_white_space() const
Definition tkstring.cpp:581
tkstring copy() const
Returns a copy of this string.
Definition tkstring.cpp:391
tkstring & to_lower()
Change all letters to lower case.
Definition tkstring.cpp:80
tkstring(const std::string &_s)
Definition tkstring.h:41
static const char * form(const char *_format,...)
Definition tkstring.cpp:452
bool is_float() const
Checks if string contains a floating point or integer number.
Definition tkstring.cpp:131
tkstring get_last_occurence(const char *_s1)
Definition tkstring.cpp:350
std::vector< tkstring > tokenize(const tkstring &_delim=" ") const
Create a vector of string separated by at least one delimiter.
Definition tkstring.cpp:275
static tkstring Form(const char *_format,...)
Definition tkstring.cpp:366
bool is_empty() const
Definition tkstring.h:141
tkstring & remove_all(const tkstring &_s1)
Definition tkstring.h:193
tkstring(char _c)
Definition tkstring.h:44
tkstring substr(size_type __pos=0, size_type __n=npos) const
Inlines.
Definition tkstring.h:157
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:315
tkstring(const char *_c)
Definition tkstring.h:39
bool match(const char *_pattern) const
Definition tkstring.cpp:600
bool is_alpha() const
Checks whether tkstring is only composed of alphabetic letters.
Definition tkstring.cpp:441
std::istream & read_line(std::istream &_strm, bool _skip_white=true)
tkstring::read_line
Definition tkstring.cpp:644
int atoi() const
Converts a string to integer value.
Definition tkstring.cpp:196
bool ends_with(const char *_s, ECaseCompare _cmp=kExact) const
Definition tkstring.cpp:262
virtual ~tkstring()
Definition tkstring.h:46
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:652
bool equal_to(const char *_s, ECaseCompare _cmp=kExact) const
Returns true if the string and _s are identical.
Definition tkstring.cpp:255
size_t index(const char *_s, size_t _pos=0, ECaseCompare _cmp=kExact) const
Returns the index of the substring _s.
Definition tkstring.cpp:239
tkstring remove_alpha()
Returns a tkstring composed only of the non alphabetic letters of the original tkstring.
Definition tkstring.cpp:422
tkstring & remove_all_extra_white_space()
Definition tkstring.cpp:547
bool contains(const char *_pat, ECaseCompare _cmp=kExact) const
Definition tkstring.h:175
tkstring & prepend(const tkstring &_st)
Definition tkstring.h:202
int count_string(const tkstring &_st) const
Definition tkstring.cpp:568
bool begins_with(const char *_s, ECaseCompare _cmp=kExact) const
Definition tkstring.h:163
tkstring & append(const tkstring &_st)
Definition tkstring.h:205
tkstring & capitalize()
Change first letter of string from lower to upper case.
Definition tkstring.cpp:397
static double get_precision(tkstring _st)
Extract the precision for a given ENSDF data.
Definition tkstring.cpp:670
tkstring & replace_all(const tkstring &_s1, const tkstring &_s2)
Definition tkstring.h:181
bool is_digit() const
Checks if all characters in string are digits (0-9) or whitespaces.
Definition tkstring.cpp:102
int64_t atoll() const
Converts a string to long integer value.
Definition tkstring.h:67
tkstring remove_last_occurence(const char *_s1)
Definition tkstring.cpp:358
double atof() const
Converts a string to double value.
Definition tkstring.cpp:212
tkstring & to_upper()
Change all letters to upper case.
Definition tkstring.cpp:86
Definition tklog.cpp:16
std::string wrap_text(const tkstring &_text, size_t _first_content_col, size_t _continuation_col, size_t _max_line_width=80)
Definition tkstring.cpp:37
tklog & error(tklog &log)
Definition tklog.h:344
tkstring & append(const tkstring &_st)
tkstring & preprend(const tkstring &_st)