Branch data Line data Source code
1 : : // Copyright (c) 2019-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef BITCOIN_TEST_UTIL_STR_H
6 : : #define BITCOIN_TEST_UTIL_STR_H
7 : :
8 : : #include <string>
9 : :
10 : : /**
11 : : * Increment a string. Useful to enumerate all fixed length strings with
12 : : * characters in [min_char, max_char].
13 : : */
14 : : template <typename CharType, size_t StringLength>
15 : 2854 : bool NextString(CharType (&string)[StringLength], CharType min_char, CharType max_char)
16 : : {
17 [ + + ]: 3558 : for (CharType& elem : string) {
18 : 3504 : bool has_next = elem != max_char;
19 [ + + + + ]: 3504 : elem = elem < min_char || elem >= max_char ? min_char : CharType(elem + 1);
20 [ + + ]: 3504 : if (has_next) return true;
21 : : }
22 : : return false;
23 : : }
24 : :
25 : : /**
26 : : * Iterate over string values and call function for each string without
27 : : * successive duplicate characters.
28 : : */
29 : : template <typename CharType, size_t StringLength, typename Fn>
30 : 54 : void ForEachNoDup(CharType (&string)[StringLength], CharType min_char, CharType max_char, Fn&& fn) {
31 [ + + ]: 2908 : for (bool has_next = true; has_next; has_next = NextString(string, min_char, max_char)) {
32 : 2854 : int prev = -1;
33 : 2854 : bool skip_string = false;
34 [ + + ]: 8154 : for (CharType c : string) {
35 [ + + ]: 6506 : if (c == prev) skip_string = true;
36 [ + + + - ]: 6506 : if (skip_string || c < min_char || c > max_char) break;
37 : 5300 : prev = c;
38 : : }
39 [ + + ]: 2854 : if (!skip_string) fn();
40 : : }
41 : 54 : }
42 : :
43 : : #endif // BITCOIN_TEST_UTIL_STR_H
|