antioch-0.4.0
string_utils.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // Antioch - A Gas Dynamics Thermochemistry Library
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Benjamin S. Kirk,
7 // Sylvain Plessis, Roy H. Stonger
8 //
9 // Copyright (C) 2013 The PECOS Development Team
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the Version 2.1 GNU Lesser General
13 // Public License as published by the Free Software Foundation.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
23 // Boston, MA 02110-1301 USA
24 //
25 //-----------------------------------------------------------------------el-
26 
27 #include "antioch/string_utils.h"
28 
29 // C++
30 #include <algorithm>
31 
32 namespace Antioch
33 {
34  void split_string( const std::string& input,
35  const std::string& delimiter,
36  std::vector<std::string>& results )
37  {
38  // Skip delimiters at beginning.
39  std::string::size_type first_pos = input.find_first_not_of(delimiter, 0);
40 
41  std::string::size_type pos = input.find_first_of(delimiter, first_pos);
42 
43  while (std::string::npos != pos || std::string::npos != first_pos)
44  {
45  // Found a token, add it to the vector.
46  results.push_back(input.substr(first_pos, pos - first_pos));
47 
48  // Skip delimiters. Note the "not_of"
49  first_pos = input.find_first_not_of(delimiter, pos);
50 
51  // Find next delimiter
52  pos = input.find_first_of(delimiter, first_pos);
53  }
54  }
55 
56  void remove_newline_from_strings( std::vector<std::string>& strings )
57  {
58  std::string newline_str = "\n";
59 
60  // First, detect any elements that are purely newlines, cache their iterator position,
61  // and them remove them.
62  std::vector<std::vector<std::string>::iterator> its_to_be_removed;
63 
64  for( std::vector<std::string>::iterator it = strings.begin(); it != strings.end(); ++it )
65  if( (*it) == newline_str )
66  its_to_be_removed.push_back(it);
67 
68  for( std::vector<std::vector<std::string>::iterator>::iterator it = its_to_be_removed.begin();
69  it != its_to_be_removed.end(); ++it )
70  strings.erase( *it );
71 
72  // Now, strip any newline characters in the remaining elements
73  for( std::vector<std::string>::iterator it = strings.begin(); it != strings.end(); ++it )
74  {
75  std::string& elem = *it;
76 
77  // Compiler will not accept newline_str as the third argument to std::remove
78  elem.erase( std::remove(elem.begin(), elem.end(), '\n'), elem.end() );
79  }
80  }
81 
82 }
void remove_newline_from_strings(std::vector< std::string > &strings)
Strips newline characters from strings in the input vector, strings.
Definition: string_utils.C:56
void split_string(const std::string &input, const std::string &delimiter, std::vector< std::string > &results)
All characters in delimiter will be treated as a delimiter.
Definition: string_utils.C:34
The parameters are reduced parameters.

Generated on Thu Jul 7 2016 11:09:46 for antioch-0.4.0 by  doxygen 1.8.8