Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
string_util.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file utility/string_util.cc
11 ///
12 /// @brief Some std::string helper functions.
13 /// @author Sergey Lyskov
14 #include <platform/types.hh>
15 
16 #include <utility/numbers.hh>
17 #include <utility/exit.hh>
19 #include <utility/file/FileName.hh>
20 #include <utility/io/izstream.hh>
21 #include <utility/string_util.hh>
22 #include <utility/vector1.hh>
24 #include <locale>
25 
26 // C/C++ headers
27 #include <fstream>
28 #include <iostream>
29 #include <iomanip>
30 #include <sstream>
31 #include <string>
32 #include <cmath>
33 #include <boost/uuid/sha1.hpp>
34 
35 namespace utility {
36 
37 
39 {
41  unsigned int start=0, i=0;
42  while ( start < s.size() ) {
43  if ( s[i] == ' ' /*|| i==s.size()-1 */ ) {
44  std::string add(s.begin()+start, s.begin()+i);
45  if ( add.size() != 0 ) r.push_back( add );
46  start = i+1;
47  }
48  i++;
49  if ( i == s.size() ) {
50  std::string add(s.begin()+start, s.begin()+i);
51  if ( add.size() != 0 ) r.push_back( add );
52  break;
53  }
54  }
55  return r;
56 }
57 
59  std::istringstream ss( s );
61  while ( !ss.fail() ) {
62  std::string e;
63  ss >> e;
64  if ( ss.fail() ) break;
65  r.push_back(e);
66  }
67  return r;
68 }
69 
70 /// @note This function also removes empty lines.
71 std::vector< std::string > split_by_newlines( std::string const & s )
72 {
73  std::vector< std::string > r;
74  platform::Size start=0, i=0;
75  while ( start < s.size() ) {
76  if ( s[i] == '\n' || s[i] == '\r' /* || i==s.size()-1 */ ) {
77  r.push_back( std::string(s.begin()+start, s.begin()+i) );
78  start = i+1;
79  }
80  i++;
81  if ( i == s.size() ) {
82  r.push_back( std::string(s.begin()+start, s.begin()+i) );
83  break;
84  }
85  }
86  for ( platform::SSize i=r.size()-1; i>=0; i-- ) { // removing empty lines
87  if ( r[i].size() == 0 ) r.erase( r.begin()+i );
88  }
89  return r;
90 }
91 
92 
93 std::string join(utility::vector1<std::string> const & s, std::string const & connector){
94  std::ostringstream os;
96  os << *begin++;
97  for ( ; begin != s.end(); ++begin ) {
98  os<< connector<< *begin;
99  }
100  return os.str();
101 }
102 
103 std::string join(std::vector<std::string> const & s, std::string const & connector){
104  std::ostringstream os;
106  os << *begin++;
107  for ( ; begin != s.end(); ++begin ) {
108  os<< connector<< *begin;
109  }
110  return os.str();
111 }
112 
113 std::string replace_spaces(std::string const & string_w_spaces, std::string const & replacement){
114  //std::string trimmed= trim(string_w_spaces);
115  utility::vector1<std::string> pieces= split(string_w_spaces);
116  return join(pieces, replacement);
117 }
118 
119 /// @details split given std::string using ' ' symbol.
120 std::list< std::string > split_to_list(const std::string &s) {
121  std::list<std::string> r;
122  unsigned int start=0, i=0;
123  while ( start < s.size() ) {
124  if ( s[i] == ' ' /*|| i==s.size()-1 */ ) {
125  std::string add(s.begin()+start, s.begin()+i);
126  if ( add.size() != 0 ) r.push_back( add );
127  start = i+1;
128  }
129  i++;
130  if ( i == s.size() ) {
131  std::string add(s.begin()+start, s.begin()+i);
132  if ( add.size() != 0 ) r.push_back( add );
133  break;
134  }
135  }
136  return r;
137 }
138 
139 /// @details split given std::string using ' ' symbol.
140 std::set< std::string > split_to_set(const std::string &s) {
141  std::set<std::string> r;
142  unsigned int start=0, i=0;
143  while ( start < s.size() ) {
144  if ( s[i] == ' ' /*|| i==s.size()-1 */ ) {
145  std::string add(s.begin()+start, s.begin()+i);
146  if ( add.size() != 0 ) r.insert( add );
147  start = i+1;
148  }
149  i++;
150  if ( i == s.size() ) {
151  std::string add(s.begin()+start, s.begin()+i);
152  if ( add.size() != 0 ) r.insert( add );
153  break;
154  }
155  }
156  return r;
157 }
158 
160 string_split( std::string const & in, char splitchar /* = ' ' */ )
161 {
163  size_t i(0), j(0);
164  while ( j != std::string::npos ) {
165  j = in.find( splitchar, i );
166  std::string const part = in.substr(i,j-i);
167  parts.push_back( part );
168  i = j+1;
169  }
170  return parts;
171 }
172 
173 /// @details split to vector1< std::string > using arbitrary split character, but no empty strings (closer to python string::split)
175 string_split_simple( std::string const & in, char splitchar /* = ' ' */ )
176 {
178  size_t i(0), j(0);
179  while ( j != std::string::npos ) {
180  j = in.find( splitchar, i );
181  std::string const part = in.substr(i,j-i);
182  if ( part.size() > 0 ) {
183  parts.push_back( part );
184  }
185  i = j+1;
186  }
187  return parts;
188 }
189 
190 
191 //overloaded to split on any of an array of chars, useful to split on any whitespace
193 string_split_multi_delim( std::string const & in, std::string splitchars )
194 {
196  size_t i(0), j(0);
197  while ( j != std::string::npos ) {
198  //find first instance of any of the splitchars
199  j = in.find_first_of( splitchars, i );
200  parts.push_back( in.substr(i,j-i) );
201  i = j+1;
202  }
203  return parts;
204 }
205 
206 
207 /// @details convert a string to a float
208 float string2float( std::string st ){
209  float i;
210  std::stringstream ss( st );
211  ss >> i;
212  if ( !ss ) {
213  return -1;
214  }
215  return i;
216 }
217 
218 /// @details convert a string to an int
219 int string2int( std::string st ){
220  int i;
221  std::stringstream ss( st );
222  ss >> i;
223  if ( !ss ) {
224  return -1;
225  }
226  return i;
227 }
228 
229 /// @brief convert a string to a Size, returns numeric::get_undefined_size() on failure
230 platform::Size string2Size( std::string st ){
231  platform::Size i;
232  std::stringstream ss( st );
233  ss >> i;
234  if ( !ss ) {
235  return get_undefined_size();
236  }
237  return i;
238 }
239 
240 /// @brief convert a string to a Real, returns numeric::get_undefined_real() on failure
241 platform::Real string2Real( std::string st ){
242  platform::Real i;
243  std::stringstream ss( st );
244  ss >> i;
245  if ( !ss ) {
246  return get_undefined_real();
247  }
248  return i;
249 }
250 
251 std::string Real2string ( platform::Real num, std::size_t const decimal_places)
252 {
253 
254  std::ostringstream converter;
255  converter << std::fixed << std::setprecision(decimal_places) << num;
256  return converter.str();
257 }
258 
259 std::string fmt_real ( platform::Real num, platform::Size pad_left_n, std::size_t const decimal_places)
260 {
261  std::string s = Real2string( num, decimal_places);
262  //platform::Size total_pad_left = s.length() - 1 + pad_left_n;
263  platform::Real newlen = decimal_places + pad_left_n +1;
264 
265  return pad_left(s, newlen );
266 }
267 
268 // @brief Reads an unsigned int from string <x>, writing the result
269 // to output parameter <y>, which must be non-NULL. The result is
270 // undefined if the input string is malformed.
271 void string2uint(const std::string& x, unsigned int* y) {
272  assert(y != NULL);
273  std::stringstream ss(x);
274  ss >> *y;
275 }
276 
277 /// @details compares two strings ignoring leading and trailing spaces
278 bool trimmed_compare( std::string const & s1, std::string const & s2 )
279 {
280  std::string const space( " " );
281 
282  std::size_t const s1_start( s1.find_first_not_of( space ) );
283  std::size_t const s1_end( s1.find_last_not_of( space ) );
284  std::size_t const s2_start( s2.find_first_not_of( space ) );
285  std::size_t const s2_end( s2.find_last_not_of( space ) );
286 
287  std::size_t const s1_len( s1_end - s1_start + 1 );
288  std::size_t const s2_len( s2_end - s2_start + 1 );
289 
290  return ( ( s1_len == s2_len ) && ( s1.compare( s1_start, s1_len, s2, s2_start, s2_len ) == 0 ) );
291 }
292 
293 bool startswith(std::string const & haystack, std::string const & needle)
294 {
295  if ( haystack.length() < needle.length() ) return false;
296  else return ( haystack.compare(0, needle.length(), needle) == 0 );
297 }
298 
299 bool endswith(std::string const & haystack, std::string const & needle)
300 {
301  if ( haystack.length() < needle.length() ) return false;
302  else return ( haystack.compare(haystack.size()-needle.size(),needle.size(),needle) == 0 );
303 }
304 
305 void slurp(std::istream & in, std::string & out)
306 {
307  std::string line;
308  std::ostringstream os;
309  while ( std::getline(in,line) ) {
310  os << line << std::endl;
311  }
312  out.append( os.str());
313 }
314 
315 void trim( std::string & s, const std::string & drop)
316 {
317  std::string r = s.erase( s.find_last_not_of(drop)+1 );
318  r.erase( 0, r.find_first_not_of(drop) );
319  s = r;
320 }
321 
322 std::string
323 strip( std::string const & s, std::string const & drop )
324 {
325  std::string copystr( s );
326  trim( copystr, drop );
327  return copystr;
328 }
329 
330 std::string
331 pad_left( std::string s, platform::Size const newlen, char pad_with ){
332 
333  std::ostringstream converter;
334  converter << std::setfill( pad_with ) << std::setw( newlen ) << std::right << s;
335  //std::cout << ":" << converter.str() << ":"<<std::endl;
336  return converter.str();
337 }
338 
339 std::string
340 pad_right( std::string s, platform::Size const newlen, char pad_with ){
341 
342  std::ostringstream converter;
343  converter << std::setfill( pad_with ) << std::setw( newlen ) << std::left << s;
344  //std::cout << ":" << converter.str() << ":"<<std::endl;
345  return converter.str();
346 }
347 
348 
349 // @brief Return a copy of the string with leading and trailing characters removed
350 std::string strip(std::string const & source, char c)
351 {
352  std::string::const_iterator begin = source.begin();
353  std::string::const_iterator end = source.end();
354  for ( std::string::const_iterator p = source.begin(); p!=source.end(); ++p ) {
355  if ( *p == c ) begin = p+1;
356  else break;
357  }
358 
359  for ( std::string::const_iterator p = source.end(); p!=begin; --p ) {
360  if ( *(p-1) == c ) end = p-1;
361  else break;
362  }
363 
364  return std::string(begin, end);
365 }
366 
367 /*
368 void add_spaces_left_align( std::string & st, std::size_t const newlen )
369 {
370 std::size_t const to_add = newlen - st.length();
371 if ( to_add > 0 ) {
372 std::string st_to_add("");
373 st_to_add.append(to_add,' ');
374 st = st + st_to_add;
375 }
376 }
377 
378 void add_spaces_right_align( std::string & st, std::size_t const newlen )
379 {
380 std::size_t const to_add = newlen - st.length();
381 if ( to_add > 0 ) {
382 std::string st_to_add("");
383 st_to_add.append(to_add,' ');
384 st = st_to_add + st;
385 }
386 }
387 */
388 
389 
390 bool is_string_numeric(std::string const & input)
391 {
392  std::locale loc;
393  for ( platform::Size i = 0 ; i < input.size(); ++i ) {
394  char current = input[i];
395  if ( std::isdigit(current,loc) || current == '-' || current == '+' || current =='E' ||current=='e' ) {
396  continue;
397  } else {
398  return false;
399  }
400  }
401  return true;
402 }
403 
404 std::string
405 file_contents( std::string const & file_name )
406 {
408  std::string line;
409  io::izstream textstream( file_name );
410  if ( ! textstream ) {
411  throw excn::EXCN_Msg_Exception( "Could not open file " + file_name );
412  }
413  int strsize( 0 );
414  while ( getline(textstream, line) ) {
415  text.push_back(line + "\n");
416  strsize += line.size() + 1;
417  }
418  textstream.close();
419 
420  std::string alltext;
421  alltext.reserve( strsize );
422  for ( unsigned int ii = 1; ii <= text.size(); ++ ii ) {
423  alltext += text[ii];
424  }
425  return alltext;
426 }
427 
428 std::string file_basename(const std::string& full_path) {
429  return filename(full_path);
430 }
431 
432 std::string filename(const std::string& path) {
434  return f.base() + f.extension();
435 }
436 
437 std::string pathname(const std::string& path) {
438  return utility::file::FileName(path).path();
439 }
440 
441 std::string replace_environment_variables(std::string input)
442 {
443  const std::string start("${");
444  const std::string end("}");
445 
446  platform::Size start_position = 0;
447  while ( true )
448  {
449  start_position = input.find(start);
450  if ( start_position != std::string::npos ) {
451  platform::Size end_position = input.find(end,start_position);
452  if ( start_position == std::string::npos ) {
453  utility_exit_with_message("opening ${ but no closing } around an environment variable, check your options file");
454  }
455 
456  platform::Size env_length = end_position-start_position;
457 
458  std::string env_name = input.substr(start_position+2,env_length-2);
459  char * env_value = getenv(env_name.c_str());
460  if ( !env_value ) {
461  utility_exit_with_message("environment variable "+env_name+" does not exist");
462  }
463 
464  input.replace(start_position, env_length+1,env_value);
465 
466  } else {
467  return input;
468  }
469  }
470 }
471 
472 std::string string_to_sha1(std::string const & input_string)
473 {
474  //Based on https://gist.github.com/990731
475  unsigned int digest[5];
476  boost::uuids::detail::sha1 hasher;
477  hasher.process_bytes(input_string.c_str(),input_string.size());
478 
479 
480  char hash[20];
481  std::stringstream output_hash;
482 
483  hasher.get_digest(digest);
484 
485  for ( int i = 0; i < 5; ++i ) {
486  const char* tmp = reinterpret_cast<char*>(digest);
487  hash[i*4] = tmp[i*4+3];
488  hash[i*4+1] = tmp[i*4+2];
489  hash[i*4+2] = tmp[i*4+1];
490  hash[i*4+3] = tmp[i*4];
491  }
492 
493  output_hash << std::hex;
494 
495  for ( int i = 0; i < 20 ; ++i ) {
496  output_hash << ((hash[i] & 0x000000F0) >> 4) << (hash[i] & 0x0000000F);
497  }
498 
499  return output_hash.str();
500 }
501 
502 ////////////////////////////////////////////////////////////////////////////
503 // Compactifies vectors of ints: 1 2 3 9 10 11 to "1-3 9-11"
504 // The function to go the other way (from string to vector) is available in
505 // ObjexxFCL::get_ints(), I think.
506 //
507 std::string
509  char const delimiter /* = ' ' */){
511  for ( platform::Size n = 0; n < res_vector.size(); n++ ) chains.push_back( ' ' );
512  return make_tag_with_dashes( res_vector, chains, delimiter );
513 }
514 
515 ////////////////////////////////////////////////////////////////////////////
516 // Compactifies vectors of ints and chars (resnum and chain): 1A 2A 3A 9B 10B 11B to "A:1-3 B:9-11"
517 // The function to go the other way (from string to two vectors) is available below in get_resnum_and_chain()
518 //
519 std::string
521  utility::vector1< char > chain_vector,
522  char const delimiter /* = ' ' */){
523 
524  using namespace ObjexxFCL;
525  std::string tag = "";
526 
527  if ( res_vector.size() == 0 ) return tag;
528  runtime_assert( res_vector.size() == chain_vector.size() );
529 
530  int start_segment = res_vector[1];
531  int last_res = res_vector[1];
532  int last_chain = chain_vector[1];
533  utility::vector1< std::pair<int,int> > res_vector_segments;
534  utility::vector1< char > chains_for_segments;
535  for ( platform::Size n = 2; n<= res_vector.size(); n++ ) {
536  if ( res_vector[n] != last_res+1 || chain_vector[n] != last_chain ) {
537  res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
538  chains_for_segments.push_back( last_chain );
539  start_segment = res_vector[n];
540  }
541  last_res = res_vector[n];
542  last_chain = chain_vector[n];
543  }
544  res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
545  chains_for_segments.push_back( last_chain );
546 
547  for ( platform::Size n = 1; n <= res_vector_segments.size(); n++ ) {
548  if ( n > 1 ) tag += delimiter;
549  std::pair< int, int > const & segment = res_vector_segments[n];
550  if ( chains_for_segments[n] != '\0' &&
551  chains_for_segments[n] != ' ' &&
552  chains_for_segments[n] != '_' ) tag += std::string(1,chains_for_segments[n]) + ":";
553  if ( segment.first == segment.second ) {
554  tag += string_of( segment.first );
555  } else {
556  tag += string_of( segment.first )+"-"+string_of(segment.second);
557  }
558  }
559 
560  return tag;
561 }
562 
563 /////////////////////////////////////////////////////////////////////////////////////////
564 // @brief Demands four-character seg ids. Output looks like "SEG1:3-4 SEG2:1-12 :1-3"
565 std::string
567  utility::vector1< std::string > segid_vector,
568  char const delimiter /* = ' ' */){
569 
570  using namespace ObjexxFCL;
571  std::string tag = "";
572 
573  if ( res_vector.size() == 0 ) return tag;
574  runtime_assert( res_vector.size() == segid_vector.size() );
575 
576  int start_segment = res_vector[1];
577  int last_res = res_vector[1];
578  std::string last_segid = segid_vector[1];
579  runtime_assert( segid_vector[1].size() == 4 );
580  utility::vector1< std::pair<int,int> > res_vector_segments;
581  utility::vector1< std::string > segids_for_segments;
582  for ( platform::Size n = 2; n<= res_vector.size(); n++ ) {
583  runtime_assert( segid_vector[n].size() == 4 );
584  if ( res_vector[n] != last_res+1 || segid_vector[n] != last_segid ) {
585  res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
586  segids_for_segments.push_back( last_segid );
587  start_segment = res_vector[n];
588  }
589  last_res = res_vector[n];
590  last_segid = segid_vector[n];
591  }
592  res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
593  segids_for_segments.push_back( last_segid );
594 
595  for ( platform::Size n = 1; n <= res_vector_segments.size(); n++ ) {
596  if ( n > 1 ) tag += delimiter;
597  std::pair< int, int > const & segment = res_vector_segments[n];
598  tag += segids_for_segments[n] + ":";
599  if ( segment.first == segment.second ) {
600  tag += string_of( segment.first );
601  } else {
602  tag += string_of( segment.first )+"-"+string_of(segment.second);
603  }
604  }
605 
606  return tag;
607 }
608 
609 /////////////////////////////////////////////////////////////////////////////////
610 std::string
612 
613  using namespace ObjexxFCL;
614  std::string tag = "";
615 
616  for ( platform::Size n = 1; n <= res_vector.size(); ++n ) {
617  if ( n > 1 ) tag += " ";
618  tag += string_of( res_vector[n] );
619  }
620 
621  return tag;
622 }
623 
624 /// @brief converts string like "1-3 20-22" or "A:1-5 B:20-22" to vectors containing resnums and chains.
625 /// @author rhiju
626 //
627 // #detailed several kinds of tags are OK, including "A:1-5 B:20-22" and "A1-5 B20,21,22".
628 //
629 std::pair< std::vector< int >, std::vector< char > >
630 get_resnum_and_chain( std::string const & s, bool & string_is_ok ){
631 
632  string_is_ok = true;
633  std::vector< int > resnum;
634  std::vector< char > chain;
635 
636  std::string s_nocommas = replace_in( s, ",", " " ); // order of operations issue?
638  for ( platform::Size n = 1; n <= tags.size(); n++ ) {
639  string_is_ok = get_resnum_and_chain_from_one_tag( tags[n], resnum, chain );
640  if ( !string_is_ok ) break;
641  }
642  return std::make_pair( resnum,chain );
643 }
644 
645 std::pair< std::vector< int >, std::vector< char > >
646 get_resnum_and_chain( std::string const & s ){
647  bool string_is_ok;
648  return get_resnum_and_chain( s, string_is_ok );
649 }
650 
651 std::pair< std::vector< int >, std::vector< std::string > >
652 get_resnum_and_segid( std::string const & s, bool & string_is_ok ){
653 
654  string_is_ok = true;
655  std::vector< int > resnum;
656  std::vector< std::string > segid;
657 
658  std::string s_nocommas = replace_in( s, ",", " " ); // order of operations issue?
659  // Each tag looks like
660  // abcd:n-m, bcd could be spaces...
661  // Strategy: add substring from start to colon (skips spaces!)
662  // then from the space on forward.
663  // utility::string_split( s_nocommas );
665 
666  // populate tags
667  while ( true ) {
668  // get rid of spaces (will get filled back in by get_resnum_and_segid_from_one_tag)
669  while ( s_nocommas.size() > 0 && s_nocommas[0]==' ' ) s_nocommas = s_nocommas.substr(1);
670 
671  std::string tag = s_nocommas.substr( 0, s_nocommas.find_first_of( ':' )+1 );
672  s_nocommas = s_nocommas.substr( s_nocommas.find_first_of( ':' ) +1 );
673 
674  platform::Size space_pos( s_nocommas.find_first_of( ' ' ) );
675  if ( space_pos == std::string::npos ) {
676  tag += s_nocommas;
677  tags.push_back( tag );
678  break;
679  } else {
680  tag += s_nocommas.substr( 0, space_pos );
681  tags.push_back( tag );
682  s_nocommas = s_nocommas.substr( space_pos +1 );
683  }
684  }
685 
686  for ( platform::Size n = 1; n <= tags.size(); n++ ) {
687  string_is_ok = get_resnum_and_segid_from_one_tag( tags[n], resnum, segid );
688  if ( !string_is_ok ) break;
689  }
690  return std::make_pair( resnum, segid );
691 }
692 
693 /// @brief helper function for get_resnum_and_chain
694 bool
696  std::vector< int > & resnum,
697  std::vector< char > & chains ){
698  bool string_is_ok( false );
699  std::vector< int > resnum_from_tag;
700  char chain( ' ' );
701  std::string const numerical("-0123456789");
702 
703  if ( tag.size() == 0 ) return true;
704 
705  size_t found_colon = tag.find( ":" );
706  if ( found_colon == std::string::npos ) {
707  if ( numerical.find( tag[0] ) == std::string::npos ) { // looks like a chain character at beginning
708  chain= tag[0];
709  resnum_from_tag = ObjexxFCL::ints_of( tag.substr(1), string_is_ok );
710  } else {
711  resnum_from_tag = ObjexxFCL::ints_of( tag, string_is_ok );
712  }
713  } else {
714  if ( found_colon == 0 ) {
715  chain = ' ';
716  resnum_from_tag = ObjexxFCL::ints_of( tag.substr(1), string_is_ok );
717  } else if ( found_colon == 1 ) {
718  chain = tag[0];
719  resnum_from_tag = ObjexxFCL::ints_of( tag.substr(2), string_is_ok );
720  } else {
721  return false;
722  }
723  }
724 
725  for ( platform::Size n = 0; n < resnum_from_tag.size(); n++ ) {
726  resnum.push_back( resnum_from_tag[ n ] );
727  chains.push_back( chain );
728  }
729 
730  return string_is_ok;
731 }
732 
733 bool
735  std::vector< int > & resnum,
736  std::vector< std::string > & segids ){
737  bool string_is_ok( false );
738  std::vector< int > resnum_from_tag;
739  std::string segid( "" );
740 
741  if ( tag.size() == 0 ) return true;
742 
743  size_t found_colon = tag.find( ":" );
744 
745  if ( found_colon == std::string::npos || found_colon > 4 ) return false;
746 
747  for ( size_t n = found_colon; n < 4; n++ ) {
748  segid += ' ';
749  }
750  if ( found_colon > 0 ) segid += tag.substr(0,found_colon);
751  runtime_assert( segid.size() == 4 );
752 
753  resnum_from_tag = ObjexxFCL::ints_of( tag.substr( found_colon + 1 ), string_is_ok );
754 
755  for ( platform::Size n = 0; n < resnum_from_tag.size(); n++ ) {
756  resnum.push_back( resnum_from_tag[ n ] );
757  segids.push_back( segid );
758  }
759 
760  return string_is_ok;
761 }
762 
765  return (platform::Size)ceil( std::log10(value + 1.) );
766 }
767 
768 std::string
769 replace_in( std::string const & name_in, std::string const & find_string, std::string const & replace_string ){
770  std::string name = name_in;
771  // WARNING WARNING WARNING: Do not change pos back to platform::Size. In general platform::Size type
772  // is not the same as size_t and algorithm below is sensitive to this
773  // platform::Size pos = name.find( find_string );
774  size_t pos = name.find( find_string );
775  while ( pos != std::string::npos ) {
776  name = name.replace( pos, find_string.size(), replace_string );
777 
778  //pos = name.find( find_string );
779 
780  // No, need to look for LATER instances of the replacement string.
781  pos = name.find( find_string, pos + replace_string.size() );
782  }
783  return name;
784 }
785 
786 } // namespace utility
void trim(std::string &s, const std::string &drop)
Remove any charachters in "drop" from the front and back of the string. Use strip() for the value-ret...
Definition: string_util.cc:315
#define utility_exit_with_message(m)
Exit with file + line + message.
Definition: exit.hh:47
BooleanOptionKey const in
StringOptionKey const chain
void slurp(std::istream &in, std::string &out)
Definition: string_util.cc:305
BooleanOptionKey const text("options:table:text")
Definition: OptionKeys.hh:46
platform::Size string2Size(std::string st)
convert a string to a Size, returns numeric::get_undefined_size() on failure
Definition: string_util.cc:230
FileName & extension(std::string const &ext_a)
Extension assignment.
Definition: FileName.hh:183
platform::Size get_undefined_size()
Get a numeric value for Size that represents an "undefined" value.
Definition: numbers.hh:38
utility::keys::KeyLookup< KeyType >::const_iterator const_iterator
Key collection iterators.
dictionary size
Definition: amino_acids.py:44
IntegerOptionKey const right
float string2float(std::string st)
convert a string to a float, returns -1 on failure
Definition: string_util.cc:208
FileVectorOptionKey const s
Definition: SQLPDB.py:309
void string2uint(const std::string &x, unsigned int *y)
Definition: string_util.cc:271
std::istream & getline(std::istream &stream, Fstring &s)
Get Line from Stream.
Definition: Fstring.cc:1610
StringVectorOptionKey const s1
def x
std::ssize_t SSize
Definition: types.hh:38
utility::vector1< std::string > string_split_multi_delim(std::string const &in, std::string splitchars)
Definition: string_util.cc:193
bool endswith(std::string const &haystack, std::string const &needle)
True iff haystack ends with needle.
Definition: string_util.cc:299
std::pair< std::vector< int >, std::vector< std::string > > get_resnum_and_segid(std::string const &s, bool &string_is_ok)
converts string like "1-3 20-22" or "A:1-5 B:20-22" to vectors containing resnums and chains...
Definition: string_util.cc:652
#define runtime_assert(_Expression)
Assert that the condition holds. Evaluated for both debug and release builds.
Definition: exit.hh:63
FileName & path(std::string const &path_a)
Path assignment.
Definition: FileName.cc:46
char space()
Space Character.
double Real
Definition: types.hh:45
std::string Real2string(platform::Real num, std::size_t const decimal_places)
convert a Real to string at a number of decimal places, optionally pad left.
Definition: string_util.cc:251
File name class supporting Windows and UN*X/Linux format names.
Definition: FileName.hh:37
std::set< std::string > split_to_set(const std::string &s)
split given std::string to a set using ' ' symbol.
Definition: string_util.cc:140
common derived classes for thrown exceptions
IntegerOptionKey const left
utility::vector1< std::string > string_split(std::string const &in, char splitchar)
Definition: string_util.cc:160
utility::vector1< std::string > split_whitespace(const std::string &s)
split given std::string using whitespace as a separator. Unlike string_split_multi_delim(), any group of mixed whitespace counts only as a single seperator.
Definition: string_util.cc:58
bool get_resnum_and_segid_from_one_tag(std::string const &tag, std::vector< int > &resnum, std::vector< std::string > &segids)
helper function for get_resnum_and_chain
Definition: string_util.cc:734
std::string replace_spaces(std::string const &string_w_spaces, std::string const &replacement)
replace space separations in a string with a connector such as '_'
Definition: string_util.cc:113
utility::vector1< std::string > split(const std::string &s)
split given std::string using ' ' symbol.
Definition: string_util.cc:38
std::string file_basename(const std::string &full_path)
Definition: string_util.cc:428
platform::Real string2Real(std::string st)
convert a string to a Real, returns numeric::get_undefined_real() on failure
Definition: string_util.cc:241
std::string join(utility::vector1< std::string > const &s, std::string const &connector)
combine strings with anything
Definition: string_util.cc:93
std::string string_to_sha1(std::string const &input_string)
Definition: string_util.cc:472
bool trimmed_compare(std::string const &s1, std::string const &s2)
compares two strings ignoring leading and trailing spaces
Definition: string_util.cc:278
std::list< std::string > split_to_list(const std::string &s)
split given std::string using ' ' symbol.
Definition: string_util.cc:120
member1 value
Definition: Tag.cc:296
void add(xyzTriple< T > const &a, xyzTriple< T > const &b, xyzTriple< T > &r)
Add: xyzTriple + xyzTriple.
Brief utility classes for numeric usage.
void close()
Close the ifstream and reset the state.
Definition: izstream.hh:219
std::string pad_left(std::string s, platform::Size const newlen, char pad_with)
Add char to the left of the string.
Definition: string_util.cc:331
Program exit functions and macros.
std::vector< std::string > split_by_newlines(std::string const &s)
Definition: string_util.cc:71
std::string make_segtag_with_dashes(utility::vector1< int > res_vector, utility::vector1< std::string > segid_vector, char const delimiter)
Definition: string_util.cc:566
izstream: Input file stream wrapper for uncompressed and compressed files
Definition: izstream.hh:44
std::string pad_right(std::string s, platform::Size const newlen, char pad_with)
Add char to the right of a string.
Definition: string_util.cc:340
Input file stream wrapper for uncompressed and compressed files.
std::string make_tag_with_dashes(utility::vector1< int > res_vector, char const delimiter)
Compactifies vectors of ints: 1 2 3 9 10 11 to "1-3 9-11".
Definition: string_util.cc:508
File name class supporting Windows and UN*X/Linux format names.
bool get_resnum_and_chain_from_one_tag(std::string const &tag, std::vector< int > &resnum, std::vector< char > &chains)
helper function for get_resnum_and_chain
Definition: string_util.cc:695
std::string fmt_real(platform::Real num, platform::Size pad_left_n, std::size_t const decimal_places)
convert a Real to a string, padding left with spaces until total number of char on left is equal to p...
Definition: string_util.cc:259
bool is_string_numeric(std::string const &input)
Definition: string_util.cc:390
std::string replace_in(std::string const &name_in, std::string const &find_string, std::string const &replace_string)
Generate new string from 'source' by replacing all occurrences of 'from' to 'to' string.
Definition: string_util.cc:769
platform::Real get_undefined_real()
Get a numeric value for Real that represents an "undefined" value.
Definition: numbers.hh:50
std::string filename(const std::string &path)
Definition: string_util.cc:432
super::const_iterator const_iterator
Definition: vector1.hh:62
PathVectorOptionKey const path
list resnum
if line_edit[13:14]=='P': #Nucleic acid? Skip.
utility::vector1< std::string > string_split_simple(std::string const &in, char splitchar)
split to vector1< std::string > using arbitrary split character, but no empty strings (closer to pyth...
Definition: string_util.cc:175
static char * line
Definition: Svm.cc:2683
std::vector< int > ints_of(std::string const &s)
ints of a string (e.g., allowing "5-8" to represent "5 6 7 8").
int string2int(std::string st)
convert a string to an int, returns -1 on failure
Definition: string_util.cc:219
std::string pathname(const std::string &path)
Definition: string_util.cc:437
FileName & base(std::string const &base_a)
Base assignment.
Definition: FileName.hh:163
std::string replace_environment_variables(std::string input)
find all environment variables with the form ${VARIABLE} and replace with the contents of that enviro...
Definition: string_util.cc:441
vector1: std::vector with 1-based indexing
bool startswith(std::string const &haystack, std::string const &needle)
True iff haystack starts with needle.
Definition: string_util.cc:293
platform::Size get_num_digits(platform::Size value)
Definition: string_util.cc:764
utility::keys::lookup::begin< KeyType > const begin
std::string file_contents(std::string const &file_name)
Read the entire contents of a file into a string. All end-of-line characters are replaced by "\n"...
Definition: string_util.cc:405
rule< Scanner, string_closure::context_t > name
Definition: Tag.cc:376
std::string make_tag(utility::vector1< int > res_vector)
Definition: string_util.cc:611
std::string string_of(Fstring const &s)
string of an Fstring
Definition: Fstring.hh:2889
Some std::string helper functions.
std::pair< std::vector< int >, std::vector< char > > get_resnum_and_chain(std::string const &s, bool &string_is_ok)
converts string like "1-3 20-22" or "A:1-5 B:20-22" to vectors containing resnums and chains...
Definition: string_util.cc:630
rule< Scanner, tag_closure::context_t > tag
Definition: Tag.cc:373
StringVectorOptionKey const s2
std::size_t Size
Definition: types.hh:37
def y
StringVectorOptionKey const tags
std::set< char > chains
IntegerOptionKey const j
std::string strip(std::string const &s, std::string const &drop)
Return a copy of the string with leading and trailing characters removed Any charachters in drop will...
Definition: string_util.cc:323