MEPP2 Project
StringUtilities.hpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #pragma once
12 
13 #include <string>
14 #include <sstream>
15 #include <vector>
16 #include <algorithm>
17 
18 #include <boost/algorithm/string.hpp>
19 
20 #if(_MSC_VER >= 1400)
21 #ifndef _SCL_SECURE_NO_WARNINGS
22 #define _SCL_SECURE_NO_WARNINGS
23 #endif
24 #endif
25 
26 
27 namespace FEVV {
28 namespace StrUtils {
29 
33 inline
34 std::vector< std::string >
35 split(const std::string &str,
36  const std::string &delims,
37  bool keep_empty_tokens = false)
38 {
39  std::vector< std::string > tokens, non_empty_tokens;
40 
42  tokens,
43  str,
44  boost::is_any_of(delims)); // boost string split => gives a warning: C4996
45  // std::copy::unchecked_iterators::_Deprecate
46 
47  if(keep_empty_tokens)
48  return tokens;
49  // TODO : these two codes do the same thing. The second one is faster but
50  // leave compilation warning on Windows
51  for(std::vector< std::string >::iterator it = tokens.begin();
52  it != tokens.end();
53  ++it)
54  if(!it->empty())
55  non_empty_tokens.push_back(*it);
56  /*
57  std::copy_if( tokens.begin(),
58  tokens.end(),
59  std::back_inserter(non_empty_tokens),
60  [](std::string s) {return !s.empty();
61  }); //vector copy to avoid empty tokens
62  */
63  return non_empty_tokens;
64 }
65 
69 inline
70 bool
71 is_equal(const std::string &str1, const std::string &str2)
72 {
73  return str1.compare(str2) == 0;
74 }
75 
79 template< typename ConvertType >
80 void
81 convert(const std::string &str, ConvertType &elem)
82 {
83  std::stringstream ss(str);
84  ss >> elem;
85 }
86 
90 template< typename ConvertType >
91 ConvertType
92 convert(const std::string &str)
93 {
94  ConvertType elem;
95  convert(str, elem);
96  return elem;
97 }
98 
102 template< typename ScalarType >
103 void
104 convert(const ScalarType &s, std::string &st)
105 {
106  std::ostringstream conv;
107  conv << s;
108  st = conv.str();
109 }
110 
114 template< typename ScalarType >
115 std::string
116 convert(const ScalarType &s)
117 {
118  std::string st;
119  convert< ScalarType >(s, st);
120  return st;
121 }
122 
126 inline
127 bool
128 starts_with(const std::string &str,
129  const std::string &prefix)
130 {
131  return (str.substr(0, prefix.size()) == prefix);
132 }
133 
134 } // namespace StrUtils
135 } // namespace FEVV
136 
FEVV::StrUtils::convert
void convert(const std::string &str, ConvertType &elem)
Definition: StringUtilities.hpp:81
FEVV::StrUtils::is_equal
bool is_equal(const std::string &str1, const std::string &str2)
Definition: StringUtilities.hpp:71
FEVV::StrUtils::split
std::vector< std::string > split(const std::string &str, const std::string &delims, bool keep_empty_tokens=false)
Definition: StringUtilities.hpp:35
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::StrUtils::starts_with
bool starts_with(const std::string &str, const std::string &prefix)
Definition: StringUtilities.hpp:128