hpp-util 6.0.0
Debugging tools for the HPP project.
Loading...
Searching...
No Matches
string.hh
Go to the documentation of this file.
1// Copyright (C) 2020 by Joseph Mirabel, CNRS.
2//
3
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26// DAMAGE.
27//
28// This software is provided "as is" without warranty of any kind,
29// either expressed or implied, including but not limited to the
30// implied warranties of fitness for a particular purpose.
31//
32// See the COPYING file for more information.
33
34#ifndef HPP_UTIL_STRING_HH
35#define HPP_UTIL_STRING_HH
36#include <algorithm>
37#include <cstring>
38#include <hpp/util/config.hh>
39#include <iterator>
40#include <string>
41#include <vector>
42
43namespace hpp {
44namespace util {
45
46template <typename InputIt, typename Predicate>
47bool string_split(InputIt first, InputIt last, const char& c, Predicate p) {
48 while (true) {
49 InputIt next = std::find(first, last, c);
50 if (p(first, next)) return true;
51 if (next == last) return false;
52 first = std::next(next);
53 }
54}
55
56template <typename InputIt, typename Predicate>
57bool string_split(InputIt first, InputIt last, const char* c, Predicate p) {
58 auto n = std::strlen(c);
59 while (true) {
60 InputIt next = std::find_if(first, last, [&c, &n](char l) -> bool {
61 return c + n != std::find(c, c + n, l);
62 });
63 if (p(first, next)) return true;
64 if (next == last) return false;
65 first = std::next(next);
66 }
67}
68
69template <typename InputIt>
70std::vector<std::string> string_split(InputIt first, InputIt last,
71 const char& c) {
72 std::vector<std::string> strings;
74 strings.emplace_back(&(*begin), std::distance(begin, end));
75 return false;
76 });
77 return strings;
78}
79
80template <typename InputIt>
81std::vector<std::string> string_split(InputIt first, InputIt last,
82 const char* c) {
83 std::vector<std::string> strings;
85 strings.emplace_back(&(*begin), std::distance(begin, end));
86 return false;
87 });
88 return strings;
89}
90
91inline bool iequal(const std::string& a, const std::string& b) {
92 return (a.size() == b.size()) &&
93 std::equal(a.begin(), a.end(), b.begin(), [](char a, char b) -> bool {
94 return std::tolower(a) == std::tolower(b);
95 });
96}
97
98} // end of namespace util.
99} // end of namespace hpp.
100
101#endif
bool iequal(const std::string &a, const std::string &b)
Definition string.hh:91
bool string_split(InputIt first, InputIt last, const char &c, Predicate p)
Definition string.hh:47
Definition assertion.hh:45
Class to ease exception creation.
Definition exception-factory.hh:73