1 /*
2 * Implementations of the string-operation functions
3 *
4 * 2010 by Jian Yang <jian.yang@qimr.edu.au>
5 *
6 * This file is distributed under the GNU General Public
7 * License, Version 2. Please see the file COPYING for more
8 * details
9 */
10
11 #include "StrFunc.h"
12
split_string(const string & str,vector<string> & vec_str,string separator)13 int StrFunc::split_string(const string &str, vector<string> &vec_str, string separator)
14 {
15 if(str.empty()) return 0;
16 vec_str.clear();
17
18 int i=0;
19 bool look=false;
20 string str_buf;
21 string symbol_pool="`1234567890-=~!@#$%^&*()_+qwertyuiop[]\\asdfghjkl;'zxcvbnm,./QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>? \t\n";
22 string::size_type pos;
23
24 for(i=0; i<separator.size(); i++){
25 pos=symbol_pool.find(separator[i]);
26 if( pos!=string::npos ) symbol_pool.erase(symbol_pool.begin()+pos);
27 }
28
29 for(i=0; i<str.size(); i++){
30 if( symbol_pool.find(str[i])!=string::npos ){
31 if(!look) look=true;
32 str_buf += str[i];
33 }
34 else{
35 if(look){
36 look=false;
37 vec_str.push_back(str_buf);
38 str_buf.erase(str_buf.begin(), str_buf.end());
39 }
40 }
41 }
42 if(look) vec_str.push_back(str_buf);
43
44 return vec_str.size();
45 }
46
first_string(const string & str,const char separator)47 string StrFunc::first_string(const string &str, const char separator)
48 {
49 int pos=str.find(separator);
50 if(pos!=-1) return string(str.begin(), str.begin()+pos);
51 return string("");
52 }
53
last_string(const string & str,const char separator)54 string StrFunc::last_string(const string &str, const char separator)
55 {
56 int pos=str.find_last_of(separator);
57 if(pos!=-1) return string(str.begin()+pos+1, str.end());
58 return string("");
59 }
60
to_upper(string & str)61 void StrFunc::to_upper(string &str)
62 {
63 int i=0;
64 for(i=0; i<str.size(); i++){
65 if(str[i]>='a' && str[i]<='z') str[i]+='A'-'a';
66 }
67 }
68
to_lower(string & str)69 void StrFunc::to_lower(string &str)
70 {
71 int i=0;
72 for(i=0; i<str.size(); i++){
73 if(str[i]>='A' && str[i]<='Z') str[i]-='A'-'a';
74 }
75 }
76
get_sub_str(const string & rst,int pos)77 string StrFunc::get_sub_str(const string & rst, int pos)
78 {
79 vector<string> vs_buf;
80 StrFunc::split_string(rst, vs_buf);
81 return vs_buf[pos];
82 }
83
StrEqual(const string & StrA,const string & StrB,bool NoCaseSens)84 bool StrFunc::StrEqual(const string &StrA, const string &StrB, bool NoCaseSens)
85 {
86 if(!NoCaseSens) return StrA==StrB;
87 string StrBufA=StrA, StrBufB=StrB;
88 to_upper(StrBufA);
89 to_upper(StrBufB);
90 return StrBufA==StrBufB;
91 }
92
StrVecEqual(const vector<string> & VsBufA,const vector<string> & VsBufB,int Pos)93 bool StrFunc::StrVecEqual(const vector<string> &VsBufA, const vector<string> &VsBufB, int Pos)
94 {
95 int SizeA=VsBufA.size(), SizeB=VsBufB.size();
96 if(SizeA!=SizeB) return false;
97 if(Pos>=SizeA) throw("Invalid Pos! StrFunc::StrVecEqual");
98
99 int i=0;
100 for(i=Pos; i<SizeA; i++){
101 if(VsBufA[i]!=VsBufB[i]) return false;
102 }
103
104 return true;
105 }
106
str_within_quto(const string & str,string & str_buf)107 bool StrFunc::str_within_quto(const string &str, string &str_buf)
108 {
109 unsigned int begin=str.find_first_of("\"");
110 unsigned int end=str.find_last_of("\"");
111 if(begin==string::npos || end==string::npos || begin==end) return false;
112
113 str_buf="";
114 str_buf.insert(str_buf.begin(), str.begin()+begin+1, str.begin()+end);
115 return true;
116 }
117
find(vector<string> & target_vs,const string & target_str)118 vector<string>::iterator StrFunc::find(vector<string> &target_vs, const string &target_str)
119 {
120 string str_buf=target_str;
121 vector<string> vs_buf=target_vs;
122
123 int i=0;
124 for(i=0; i<vs_buf.size(); i++) to_upper(vs_buf[i]);
125 to_upper(str_buf);
126 return target_vs.begin()+(std::find(vs_buf.begin(), vs_buf.end(), str_buf)-vs_buf.begin());
127 }
128
find(string & target_str,const char target_ch)129 string::iterator StrFunc::find(string &target_str, const char target_ch)
130 {
131 char ch_buf=target_ch;
132 string str_buf=target_str;
133 to_upper(str_buf);
134 if(ch_buf>'a' && ch_buf<'z') ch_buf+='A'-'a';
135 return target_str.begin()+(std::find(str_buf.begin(), str_buf.end(), ch_buf)-str_buf.begin());
136 }
137
goto_str(std::istream & in_file,const string & str)138 bool StrFunc::goto_str(std::istream &in_file, const string &str)
139 {
140 string str_buf;
141 string query_str=str;
142 vector<string> vs_buf;
143 StrFunc::to_upper(query_str);
144 while(in_file>>str_buf){
145 if( StrFunc::split_string(str_buf, vs_buf)>0 ) str_buf=vs_buf[0];
146 else continue;
147 StrFunc::to_upper(str_buf);
148 if(str_buf=="#") { getline(in_file, str_buf); continue; }
149 if(str_buf==query_str) return true;
150 }
151
152 return false;
153 }
154
rewind_if(std::istream & in_file)155 void StrFunc::rewind_if(std::istream &in_file)
156 {
157 in_file.clear(ios::goodbit);
158 in_file.seekg(ios::beg);
159 }
160
match(const vector<string> & VecA,const vector<string> & VecB,vector<int> & VecC)161 void StrFunc::match(const vector<string> &VecA, const vector<string> &VecB, vector<int> &VecC)
162 {
163 int i=0;
164 map<string, int> id_map;
165 map<string, int>::iterator iter;
166 VecC.clear();
167 for(i=0; i<VecB.size(); i++) id_map.insert(pair<string,int>(VecB[i], i));
168 for(i=0; i<VecA.size(); i++){
169 iter=id_map.find(VecA[i]);
170 if(iter==id_map.end()) VecC.push_back(-9);
171 else VecC.push_back(iter->second);
172 }
173 }
174
175