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