Debian Codegraph @

easystring.h

raw
1 /* BaitFisher (version 1.2.7) a program for designing DNA target enrichment baits
2 * Copyright 2013-2016 by Christoph Mayer
3 *
4 * This source file is part of the BaitFisher-package.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 * For any enquiries send an Email to Christoph Mayer
21 * [email protected]
22 *
23 * When publishing work that is based on the results please cite:
24 * Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
25 *
26 */
27
28 #ifndef EASYSTRING_H
29 #define EASYSTRING_H
30
31 #include <iostream>
32 #include <string>
33 #include <cstring> // for strchr
34 #include <cstdlib>
35 #include <fstream>
36
37 // A usefull global function split:
38 //(Code copied from the stringtok.h file on the gnu libstdc++ page.
39
40
41
42 namespace {
43 inline bool
44 isws(char c, const char* wstr="\r\n\t\v\f ")
45 {
46 return (strchr(wstr,c) != NULL);
47 }
48 }
49
50 namespace {
51 //
52 // Reads through a string. If braces are encountered we do not tokenize inside but try to read them.
53 //
54 inline void
55 skip_until_ws_respect(const std::string &s, std::string::size_type &pos, char const * const ws = "\r\n\t\v\f ")
56 {
57 char c;
58
59 unsigned count_parent = 0;
60 unsigned count_brace = 0;
61 unsigned count_curly = 0;
62 unsigned count_sq = 0;
63 unsigned count_dq = 0;
64
65 std::string::size_type size = s.size();
66
67 while (pos < size)
68 {
69 c = s[pos];
70 if (c == '(') { ++count_parent; }
71 else if (c == ')') { --count_parent; }
72 else if (c == '[') { ++count_brace; }
73 else if (c == ']') { --count_brace; }
74 else if (c == '{') { ++count_curly; }
75 else if (c == '}') { --count_curly; }
76 else if (c == '\'') { count_sq = (count_sq+1)%2; }
77 else if (c == '\"') { count_dq = (count_dq+1)%2; }
78
79 if (isws(c, ws) && count_parent==0 && count_brace==0 && count_curly==0 && count_sq==0 && count_dq==0)
80 return;
81
82 ++pos;
83 }
84 }
85 }
86
87 namespace std
88 {
89
90 /*****************************************************************
91 * Simplistic and quite Standard, but a bit slow. This should be
92 * templatized on basic_string instead, or on a more generic StringT
93 * that just happens to support ::size_type, .substr(), and so on.
94 * I had hoped that "whitespace" would be a trait, but it isn't, so
95 * the user must supply it. Enh, this lets them break up strings on
96 * different things easier than traits would anyhow.
97 */
98 template <typename Container>
99 int
100 split (Container &l, string const &s, char const * const ws = "\r\n\t\v\f ")
101 {
102 l.clear();
103 const string::size_type S = s.size();
104 string::size_type i = 0;
105
106 while (i < S) {
107 // eat leading whitespace
108 while ((i < S) && (isws(s[i],ws))) ++i;
109 if (i == S) return l.size(); // nothing left but WS
110
111 // find end of word
112 string::size_type j = i+1;
113 while ((j < S) && (!isws(s[j],ws))) ++j;
114
115 // add word
116 l.push_back(s.substr(i,j-i));
117
118 // set up for next loop
119 i = j+1;
120 }
121 return l.size();
122 }
123
124 //
125 // Tokenizer which does not split braces.
126 //
127 template <typename Container>
128 int
129 split_respect (Container &l, string const &s, char const * const ws = "\r\n\t\v\f ")
130 {
131 l.clear();
132 const string::size_type S = s.size();
133 string::size_type i = 0;
134
135 while (i < S) {
136 // eat leading whitespace
137 while ((i < S) && (isws(s[i],ws))) ++i;
138 if (i == S) return l.size(); // nothing left but WS
139
140 // find end of word
141 string::size_type j = i; // We can't skip the first char which can be a brace.
142 skip_until_ws_respect(s, j, ws);
143
144 // add word
145 l.push_back(s.substr(i,j-i));
146
147 // set up for next loop
148 i = j+1;
149 }
150 return l.size();
151 }
152
153 // Multiple successive deliminators result in multiple hits.
154 template <typename Container>
155 int
156 split_strict (Container &l, string const &s, char const * const ws = " \t\n")
157 {
158 l.clear();
159 const string::size_type S = s.size();
160 string::size_type i = 0;
161
162 while (i < S) {
163 // find end of word
164 string::size_type j = i;
165 while ((j < S) && (!isws(s[j],ws))) ++j;
166
167 // add word
168 l.push_back(s.substr(i,j-i));
169
170 // set up for next loop
171 i = j+1;
172
173 if (i==S) // Only true of last string is empty since otherwise i==S+1
174 {
175 l.push_back("");
176 }
177 }
178 return l.size();
179 }
180
181
182
183 class easystring: public string
184 {
185 public:
186 // Default constructor:
187 easystring():string(){};
188
189 // Copy constructor:
190 easystring(const string& s):string(s){}
191 easystring(const string& s, size_type pos, size_type n):string(s,pos,n){}
192
193 // Other constructors:
194 easystring(const char* s, size_type n):string(s,n){}
195 easystring(size_type n, char c):string(n,c){}
196 template <class InputIterator>
197 easystring(InputIterator first, InputIterator last):string(first, last){}
198
199 // Type conversion constructors:
200 easystring(const char* s):string(s){}
201 easystring(const char c):string(1,c){}
202 easystring(const int i):string()
203 {
204 // std::cout << "Hallo" << std::endl;
205 char tmp[21]; // Sufficient for 64 bit numbers + sign
206 sprintf(tmp, "%d", i);
207 append(tmp);
208 }
209 easystring(const long i):string()
210 {
211 // std::cout << "Hallo" << std::endl;
212 char tmp[21]; // Sufficient for 64 bit numbers + sign
213 sprintf(tmp, "%ld", i);
214 append(tmp);
215 }
216 easystring(const unsigned i):string()
217 {
218 // std::cout << "Hallo" << std::endl;
219 char tmp[21]; // Sufficient for 64 bit numbers
220 sprintf(tmp, "%u", i);
221 append(tmp);
222 }
223 easystring(const unsigned long i):string()
224 {
225 // std::cout << "Hallo" << std::endl;
226 char tmp[21]; // Sufficient for 64 bit numbers
227 sprintf(tmp, "%lu", i);
228 append(tmp);
229 }
230 easystring(const double x, int pres):string()
231 {
232 // std::cout << "Hallo" << std::endl;
233 char tmp[25]; // Sufficient for 64 bit numbers
234 sprintf(tmp, "%.*f", pres, x);
235 append(tmp);
236 }
237
238
239 void removeSpacesFront(const char* delims="\r\n\t\v\f ")
240 {
241 string::size_type i;
242 const string::size_type n = size();
243
244 for (i=0; i < n && isws((*this)[i], delims); ++i);
245 erase(0,i);
246 }
247
248
249 void removeSpacesBack(const char* delims="\r\n\t\v\f ")
250 {
251 unsigned i;
252 unsigned n = size();
253
254 if (n > 0)
255 {
256 for (i=n-1; i != 0 && isws((*this)[i], delims); --i);
257
258 if (i == 0 && isws((*this)[0], delims))
259 erase();
260 else
261 erase(i+1);
262 }
263 }
264
265
266 void ToUpper()
267 {
268 iterator it, it_end;
269
270 it = begin();
271 it_end = end();
272
273 while (it != it_end)
274 {
275 *it = toupper(*it);
276 ++it;
277 }
278 }
279
280 void ToLower()
281 {
282 string::iterator it, it_end;
283
284 it = begin();
285 it_end = end();
286
287 while (it != it_end)
288 {
289 *it = tolower(*it);
290 ++it;
291 }
292 }
293
294 // Convert to unsigned long
295 unsigned ToUnsigned() const
296 {
297 char *end;
298 return strtoul(c_str(), &end, 0);
299 }
300
301 // Convert to unsigned long
302 unsigned long ToUnsignedLong() const
303 {
304 char *end;
305 return strtoul(c_str(), &end, 0);
306 }
307
308 // Convert to int
309 int ToInt() const
310 {
311 char *end;
312 return strtol(c_str(), &end, 0);
313
314 }
315
316
317 // Convert to long
318 long ToLong() const
319 {
320 char *end;
321 return strtol(c_str(), &end, 0);
322 }
323
324 long ToLong(string::size_type beg_pos,
325 string::size_type& end_pos) const
326 {
327 const char *beg = c_str()+beg_pos;
328 char *end;
329 long result;
330
331 result = strtol(beg, &end, 0);
332 end_pos = end-c_str();
333 return result;
334 }
335
336 unsigned ToUnsigned(string::size_type beg_pos,
337 string::size_type& end_pos) const
338 {
339 const char *beg = c_str()+beg_pos;
340 char *end;
341 long result;
342
343 result = strtoul(beg, &end, 0);
344 end_pos = end-c_str();
345 return result;
346 }
347
348
349 // Convert to double
350 double ToDouble() const
351 {
352 char *end;
353 return strtod(c_str(), &end);
354 }
355
356 double ToDouble(string::size_type beg_pos,
357 string::size_type& end_pos) const
358 {
359 const char *beg = c_str()+beg_pos;
360 char *end;
361 double result;
362
363 result = strtod(beg, &end);
364 end_pos = end-c_str();
365 return result;
366 }
367
368 // not tested
369 unsigned countChar(char c)
370 {
371 unsigned n = 0;
372 string::iterator b = begin();
373 string::iterator e = end();
374
375 while (b<e)
376 {
377 if (*b == c)
378 ++n;
379 ++b;
380 }
381 return n;
382 }
383
384 // not tested
385 void unquote()
386 {
387 removeSpacesBack();
388 removeSpacesFront();
389 if (*begin() == '"' && *(end()-1) == '"' )
390 {
391 this->erase(begin());
392 this->erase(end()-1);
393 }
394 else if (*begin() == '\'' && *(end()-1) == '\'' )
395 {
396 this->erase(begin());
397 this->erase(end()-1);
398 }
399 }
400
401 // not tested
402 void unquote(char s, char e)
403 {
404 removeSpacesBack();
405 removeSpacesFront();
406 if (*begin() == s && *(end()-1) == e )
407 {
408 this->erase(begin());
409 this->erase(end()-1);
410 }
411 }
412
413 // get the next token and remove it from the called object.
414 easystring& get_next_token(easystring &str, const char * ws = "\r\n\t\v\f ")
415 {
416 const string::size_type n = size();
417 string::size_type i = 0;
418
419 str.clear();
420
421 // eat leading whitespace/deliminators -- this sould be more efficient than
422 // calling removeSpacesFront since we only need to erase in the front of the string once.
423 while ( (i < n) && ( isws( (*this)[i], ws)) ) ++i;
424
425 if (i == n) // nothing left so re remove alle spaces and the string will be empty
426 {
427 erase();
428 }
429 else
430 {
431 // find end of word
432 string::size_type j = i+1;
433 while ( (j < n) && (!isws( (*this)[j], ws)) ) ++j;
434
435 str = string(*this, i, j); // the next token
436 erase(0, j); // remove token from called object
437 }
438
439 return *this;
440 }
441
442
443 // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
444 // Be careful with keywords containing punctuations since they do not
445 // count as upper case chars. The upper case loop stops there.
446
447 // Possible bug: I think "I" abbreviates "INPUT" which is not OK.
448
449 // Use !isalpha() to as equivalent to "isupper() so we prolong the upper region.
450
451 bool is_CAPITALIZED_abbreviation_of(const easystring &keyword)
452 {
453 unsigned i = 0;
454 unsigned max = size();
455
456 // max cannot be greater than keyword.size()
457 if ( max > keyword.size() )
458 return false;
459
460 // We start comparrison in upper case region. Lets see how far we can go.
461 while (i < max && toupper( (*this)[i] ) == keyword[i] )
462 {
463 ++i;
464 }
465
466 // First we check whether both strings have the same length and we matched all chars.
467 // This means that we matched exactly the upper case region of keyword, which is the
468 // complete keyword.
469 if ( i == max && max == keyword.size() )
470 return true;
471
472 // If we get here, i must be smaller than keyword.size() since we can only have
473 // * i == max and i == keyword.size(): Not possible after previous if
474 // * i < max and i == keyword.size(): Not possible since max <= keyword.size()
475 // * i == max and i < keyword.size():
476 // * i < max and i < keyword.size():
477
478 // Thus, keyword[i] is valid and we use it to check whether we are in the upper case region
479 // (were all !alpha() should also be considered as upper case chars in this context!!).
480 // We simply check whether we are in the !islower() region.
481
482 if ( !islower( keyword[i]) ) // We are still in non-lower case region
483 return false;
484
485 // If we get here, we are in the in the lower-case region
486 // There are two possibilities:
487 // - i == max,
488 if (i==max)
489 return true;
490 // - our string is longer than the upper case region, so we have to continue
491 // our comparison.
492
493 // Let us move through lower case region
494 while (i < max && ((*this)[i] == keyword[i] || tolower((*this)[i]) == keyword[i]) )
495 ++i;
496
497 // Could we match all letters in str?
498 if (i==max)
499 return true;
500 else
501 return false;
502 }
503
504
505 };
506
507
508 }
509
510
511 #endif