QMCPACK
ModernStringUtils.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2021 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
8 //
9 // File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include "ModernStringUtils.hpp"
13 #include <algorithm>
14 #include <cctype>
15 
16 namespace qmcplusplus
17 {
18 using std::size_t;
19 using std::string_view;
20 
21 
22 std::string lowerCase(const std::string_view s)
23 {
24  std::string lower_str{s};
25  std::transform(lower_str.begin(), lower_str.end(), lower_str.begin(),
26  [](unsigned char c) { return std::tolower(c); });
27  return lower_str;
28 }
29 
30 namespace modernstrutil
31 {
32 std::vector<std::string_view> split(const string_view s, const string_view delimiters)
33 {
34  std::vector<std::string_view> tokens;
35  size_t right = 0;
36  size_t left = 0;
37  while (true)
38  {
39  left = s.find_first_not_of(delimiters, right);
40  if (left == s.npos)
41  break;
42  else
43  right = s.find_first_of(delimiters, left);
44  if (right == s.npos)
45  right = s.size();
46  size_t count = right - left;
47  tokens.push_back(s.substr(left, count));
48  }
49  return tokens;
50 }
51 
52 std::string_view strip(const string_view s)
53 {
54  std::string_view delimiters = " \t\n\0";
55  size_t left = s.find_first_not_of(delimiters, 0);
56  if (left != s.npos) {
57  size_t right = s.find_last_not_of(delimiters, s.npos);
58  return s.substr(left, right - left + 1);
59  }
60  else
61  return std::string_view{};
62 }
63 } // namespace modernstrutil
64 
65 
66 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
std::string_view strip(const string_view s)
std::string lowerCase(const std::string_view s)
++17
std::vector< std::string_view > split(const string_view s, const string_view delimiters)