| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 43 | namespace hpp { | ||
| 44 | namespace util { | ||
| 45 | |||
| 46 | template <typename InputIt, typename Predicate> | ||
| 47 | 2 | bool string_split(InputIt first, InputIt last, const char& c, Predicate p) { | |
| 48 | 4 | while (true) { | |
| 49 | 6 | InputIt next = std::find(first, last, c); | |
| 50 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (p(first, next)) return true; |
| 51 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (next == last) return false; |
| 52 | 4 | first = std::next(next); | |
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | template <typename InputIt, typename Predicate> | ||
| 57 | 1 | bool string_split(InputIt first, InputIt last, const char* c, Predicate p) { | |
| 58 | 1 | auto n = std::strlen(c); | |
| 59 | 4 | while (true) { | |
| 60 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | InputIt next = std::find_if(first, last, [&c, &n](char l) -> bool { |
| 61 | 21 | return c + n != std::find(c, c + n, l); | |
| 62 | }); | ||
| 63 |
2/4✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
6 | if (p(first, next)) return true; |
| 64 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
|
5 | if (next == last) return false; |
| 65 | 4 | first = std::next(next); | |
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | template <typename InputIt> | ||
| 70 | 2 | std::vector<std::string> string_split(InputIt first, InputIt last, | |
| 71 | const char& c) { | ||
| 72 | 2 | std::vector<std::string> strings; | |
| 73 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | string_split(first, last, c, [&strings](InputIt begin, InputIt end) { |
| 74 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
12 | strings.emplace_back(&(*begin), std::distance(begin, end)); |
| 75 | 6 | return false; | |
| 76 | }); | ||
| 77 | 2 | return strings; | |
| 78 | ✗ | } | |
| 79 | |||
| 80 | template <typename InputIt> | ||
| 81 | ✗ | std::vector<std::string> string_split(InputIt first, InputIt last, | |
| 82 | const char* c) { | ||
| 83 | ✗ | std::vector<std::string> strings; | |
| 84 | ✗ | string_split(first, last, c, [&strings](InputIt begin, InputIt end) { | |
| 85 | ✗ | strings.emplace_back(&(*begin), std::distance(begin, end)); | |
| 86 | ✗ | return false; | |
| 87 | }); | ||
| 88 | ✗ | return strings; | |
| 89 | ✗ | } | |
| 90 | |||
| 91 | 3 | inline bool iequal(const std::string& a, const std::string& b) { | |
| 92 |
4/4✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
|
5 | return (a.size() == b.size()) && |
| 93 | 2 | std::equal(a.begin(), a.end(), b.begin(), [](char a, char b) -> bool { | |
| 94 | 5 | return std::tolower(a) == std::tolower(b); | |
| 95 | 3 | }); | |
| 96 | } | ||
| 97 | |||
| 98 | } // end of namespace util. | ||
| 99 | } // end of namespace hpp. | ||
| 100 | |||
| 101 | #endif //! HPP_UTIL_STRING_HH | ||
| 102 |