TkN 2.7
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 <istream>
19#include <cstdint>
20#include <string>
21#include <vector>
22#include <cstring>
23
24#include "tkn_config.h"
25
26#ifdef HAS_ROOT
27#include "TClass.h"
28#endif
29
30namespace tkn {
31
32class tkstring : public std::string
33{
34
35public:
37
38public:
39 tkstring() : std::string() {}
40 tkstring(const char *_c) : std::string(_c ? _c : "") {}
41 tkstring(const unsigned char *_c) : std::string(_c ? reinterpret_cast<const char *>(_c) : "") {}
42 tkstring(const std::string &_s) : std::string(_s) {}
43// tkstring(const tkstring &_s) : std::string(_s.data()) {;}
44 tkstring(double _value, double _error);
45 tkstring(char _c) : std::string(1, _c) {}
46
47 virtual ~tkstring() {}
48
49 tkstring substr(size_type __pos = 0, size_type __n = npos) const;
50
52 bool is_digit() const;
54 bool is_float() const;
55
57 bool is_alpha() const;
62
64 int atoi() const;
66 double atof() const;
68 int64_t atoll() const;
69
70// //! Returns a string formated energy with error
71// static tkstring energy_to_string(double _val, int _precision);
72// //! Returns a string formated energy with error
73// static tkstring energy_error_to_string(double _val, int _precision);
74
76 static double get_precision(tkstring _st);
77
79 static double get_absolute_error(tkstring val, tkstring error);
80
82 std::vector<tkstring> tokenize(const tkstring &_delim = " ") const;
83
85 std::vector<tkstring> tokenize_from_string(const tkstring &_delim) const;
86
88 tkstring copy() const;
89
92
97
99 size_t index(const char *_s, size_t _pos=0, ECaseCompare _cmp=kExact) const;
101 size_t index(const tkstring &_pat, size_t _pos=0, ECaseCompare _cmp=kExact) const;
102
104 bool equal_to(const char *_s, ECaseCompare _cmp = kExact) const;
105 bool equal_to(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
106
107 bool begins_with(const char *_s, ECaseCompare _cmp = kExact) const;
108 bool begins_with(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
109 bool begins_with(char _c) const { return !empty() && front() == _c; }
110 bool ends_with(const char *_s, ECaseCompare _cmp = kExact) const;
111 bool ends_with(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
112 bool ends_with(char _c) const { return !empty() && back() == _c; }
113
114 bool contains(const char *_pat, ECaseCompare _cmp = kExact) const;
115 bool contains(const tkstring &_pat, ECaseCompare _cmp = kExact) const;
116 bool contains(char _c) const { return find(_c) != npos; }
117
118 tkstring &append(const tkstring &_st);
119 tkstring &prepend(const tkstring &_st);
120
121 tkstring &replace_all(const tkstring &_s1, const tkstring &_s2); // Find&Replace all s1 with s2 if any
122 tkstring &replace_all(const tkstring &_s1, const char *_s2); // Find&Replace all s1 with s2 if any
123 tkstring &replace_all(const char *_s1, const tkstring &_s2); // Find&Replace all s1 with s2 if any
124 tkstring &replace_all(const char *_s1, const char *_s2); // Find&Replace all s1 with s2 if any
125 tkstring &replace_all(const char *_s1, size_t _ls1, const char *_s2, size_t _ls2); // Find&Replace all s1 with s2 if any
126
127 tkstring &remove_all(const tkstring &_s1);
128 tkstring &remove_all(const char *_s1);
129
130 tkstring remove_last_occurence(const char *_s1);
131 tkstring get_last_occurence(const char *_s1);
132
133 static tkstring Form(const char * _format, ...); // return a string using the c printf method
134 static const char* form(const char * _format, ...); // return a string using the c printf method
135
138
139 std::istream &read_line(std::istream &_strm, bool _skip_white = true); // Read to EOF or newline
140
141 bool match(const char *_pattern) const;
142 bool match(const tkstring &_pattern) const;
143
144 // return true if empty or composed only of blank spaces
145 bool is_empty() const {
146 return empty() || find_first_not_of(" \n\t") == npos;
147 }
148
149 // count the number of occurences of the string _st
150 int count_string(const tkstring &_st) const;
151
152#ifdef HAS_ROOT
154 ClassDef(tkstring,0);
155#endif
156};
157
159
160inline tkstring tkstring::substr(size_type __pos, size_type __n) const
161{ tkstring tmp = std::string::substr(__pos,__n); return tmp;}
162
163inline bool tkstring::equal_to(const tkstring &_pat, ECaseCompare _cmp) const
164{ return equal_to(_pat.data(),_cmp); }
165
166inline bool tkstring::begins_with(const char *_s, ECaseCompare _cmp) const
167{
168 if (!_s) return false;
169#if defined(__cpp_lib_starts_ends_with) && __cpp_lib_starts_ends_with >= 201711L
170 if (_cmp == kExact) return std::string::starts_with(_s);
171#endif
172 return index(_s, 0, _cmp) == 0;
173}
174
175inline bool tkstring::begins_with(const tkstring &_pat, ECaseCompare _cmp) const
176{ return begins_with(_pat.data(), _cmp); }
177
178inline bool tkstring::ends_with(const tkstring &_pat, ECaseCompare _cmp) const
179{ return ends_with(_pat.data(),_cmp); }
180
181inline bool tkstring::contains(const tkstring &_pat, ECaseCompare _cmp) const
182{ return contains(_pat.data(), _cmp); }
183
184inline bool tkstring::contains(const char *_s, ECaseCompare _cmp) const
185{
186 if (!_s) return false;
187#if defined(__cpp_lib_string_contains) && __cpp_lib_string_contains >= 202011L
188 if (_cmp == kExact) return std::string::contains(_s);
189#endif
190 return index(_s, 0, _cmp) != npos;
191}
192
193inline size_t tkstring::index(const tkstring &_pat, size_t _pos, ECaseCompare _cmp) const
194{ return index(_pat.data(), _pos, _cmp); }
195
196inline tkstring &tkstring::replace_all(const tkstring &_s1, const tkstring &_s2)
197{ return replace_all(_s1.data(), _s1.length(), _s2.data(), _s2.length()) ; }
198
199inline tkstring &tkstring::replace_all(const tkstring &_s1, const char *_s2)
200{ return replace_all(_s1.data(), _s1.length(), _s2, _s2 ? strlen(_s2) : 0); }
201
202inline tkstring &tkstring::replace_all(const char *_s1, const tkstring &_s2)
203{ return replace_all(_s1, _s1 ? strlen(_s1) : 0, _s2.data(), _s2.length()); }
204
205inline tkstring &tkstring::replace_all(const char *_s1,const char *_s2)
206{ return replace_all(_s1, _s1 ? strlen(_s1) : 0, _s2, _s2 ? strlen(_s2) : 0); }
207
209{ return replace_all(_s1, "") ; }
210
211inline tkstring &tkstring::remove_all(const char *_s1)
212{ return replace_all(_s1, "") ; }
213
214inline bool tkstring::match(const tkstring &_pat) const
215{ return match(_pat.data()); }
216
218{ insert(0,_st); return *this;}
219
221{ std::string::append(_st); return *this;}
222
225
226std::string wrap_text(const tkstring &_text, size_t _first_content_col, size_t _continuation_col, size_t _max_line_width = 80);
227
228}
229#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:33
tkstring extract_alpha()
Returns a tkstring composed only of the alphabetic letters of the original tkstring.
Definition tkstring.cpp:394
tkstring(const unsigned char *_c)
Definition tkstring.h:41
tkstring strip_all_extra_white_space() const
Definition tkstring.cpp:550
tkstring copy() const
Returns a copy of this string.
Definition tkstring.cpp:377
tkstring & to_lower()
Change all letters to lower case.
Definition tkstring.cpp:118
tkstring(const std::string &_s)
Definition tkstring.h:42
static const char * form(const char *_format,...)
Definition tkstring.cpp:438
bool is_float() const
Checks if string contains a floating point or integer number.
Definition tkstring.cpp:167
tkstring get_last_occurence(const char *_s1)
Definition tkstring.cpp:352
bool begins_with(char _c) const
Definition tkstring.h:109
std::vector< tkstring > tokenize(const tkstring &_delim=" ") const
Create a vector of string separated by at least one delimiter.
Definition tkstring.cpp:292
static tkstring Form(const char *_format,...)
Definition tkstring.cpp:368
bool contains(char _c) const
Definition tkstring.h:116
bool is_empty() const
Definition tkstring.h:145
tkstring & remove_all(const tkstring &_s1)
Definition tkstring.h:208
bool ends_with(char _c) const
Definition tkstring.h:112
tkstring(char _c)
Definition tkstring.h:45
tkstring substr(size_type __pos=0, size_type __n=npos) const
Inlines.
Definition tkstring.h:160
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:312
tkstring(const char *_c)
Definition tkstring.h:40
bool match(const char *_pattern) const
Definition tkstring.cpp:572
bool is_alpha() const
Checks whether tkstring is only composed of alphabetic letters.
Definition tkstring.cpp:427
std::istream & read_line(std::istream &_strm, bool _skip_white=true)
tkstring::read_line
Definition tkstring.cpp:616
int atoi() const
Converts a string to integer value.
Definition tkstring.cpp:210
bool ends_with(const char *_s, ECaseCompare _cmp=kExact) const
Definition tkstring.cpp:272
virtual ~tkstring()
Definition tkstring.h:47
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:624
bool equal_to(const char *_s, ECaseCompare _cmp=kExact) const
Returns true if the string and _s are identical.
Definition tkstring.cpp:259
size_t index(const char *_s, size_t _pos=0, ECaseCompare _cmp=kExact) const
Returns the index of the substring _s.
Definition tkstring.cpp:237
tkstring remove_alpha()
Returns a tkstring composed only of the non alphabetic letters of the original tkstring.
Definition tkstring.cpp:408
tkstring & remove_all_extra_white_space()
Definition tkstring.cpp:525
bool contains(const char *_pat, ECaseCompare _cmp=kExact) const
Definition tkstring.h:184
tkstring & prepend(const tkstring &_st)
Definition tkstring.h:217
int count_string(const tkstring &_st) const
Definition tkstring.cpp:536
bool begins_with(const char *_s, ECaseCompare _cmp=kExact) const
Definition tkstring.h:166
tkstring & append(const tkstring &_st)
Definition tkstring.h:220
tkstring & capitalize()
Change first letter of string from lower to upper case.
Definition tkstring.cpp:383
static double get_precision(tkstring _st)
Extract the precision for a given ENSDF data.
Definition tkstring.cpp:642
tkstring & replace_all(const tkstring &_s1, const tkstring &_s2)
Definition tkstring.h:196
bool is_digit() const
Checks if all characters in string are digits (0-9) or whitespaces.
Definition tkstring.cpp:140
int64_t atoll() const
Converts a string to long integer value.
Definition tkstring.cpp:216
tkstring remove_last_occurence(const char *_s1)
Definition tkstring.cpp:360
double atof() const
Converts a string to double value.
Definition tkstring.cpp:226
tkstring & to_upper()
Change all letters to upper case.
Definition tkstring.cpp:124
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:75
tklog & error(tklog &log)
Definition tklog.h:344
tkstring & append(const tkstring &_st)
tkstring & preprend(const tkstring &_st)