File: string_util.cpp

package info (click to toggle)
trinityrnaseq 2.11.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 417,528 kB
  • sloc: perl: 48,420; cpp: 17,749; java: 12,695; python: 3,124; sh: 1,030; ansic: 983; makefile: 688; xml: 62
file content (51 lines) | stat: -rw-r--r-- 1,580 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "string_util.hpp"

namespace string_util {
    
    void tokenize(const string& str, vector<string>& tokens, const string& delimiters) {
        
        /* ************************************************************************************/
        /* from: http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html **/
        /**************************************************************************************/
        
        // Skip delimiters at beginning.
        string::size_type lastPos = str.find_first_not_of(delimiters, 0);
        // Find first "non-delimiter".
        string::size_type pos     = str.find_first_of(delimiters, lastPos);
        
        while (string::npos != pos || string::npos != lastPos) {
            // Found a token, add it to the vector.
            tokens.push_back(str.substr(lastPos, pos - lastPos));
            // Skip delimiters.  Note the "not_of"
            lastPos = str.find_first_not_of(delimiters, pos);
            // Find next "non-delimiter"
            pos = str.find_first_of(delimiters, lastPos);
        }
        
        return;
    }
    

    /* rest is my stuff */

    string join(const vector<string>& tokens, const string& delimiter) {
        
        string concat = "";
        
        for (unsigned int i = 0; i < tokens.size(); i++) {
            concat = concat + tokens[i];
            
            if (i != tokens.size()-1) {
                concat = concat + delimiter;
            }
        }

        return(concat);
    }
    




} // end of string_util namespace