1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <locale>
10 
11 // Not a portable test
12 
13 // __scan_keyword
14 // Scans [__b, __e) until a match is found in the basic_strings range
15 //  [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
16 //  __b will be incremented (visibly), consuming CharT until a match is found
17 //  or proved to not exist.  A keyword may be "", in which will match anything.
18 //  If one keyword is a prefix of another, and the next CharT in the input
19 //  might match another keyword, the algorithm will attempt to find the longest
20 //  matching keyword.  If the longer matching keyword ends up not matching, then
21 //  no keyword match is found.  If no keyword match is found, __ke is returned.
22 //  Else an iterator pointing to the matching keyword is found.  If more than
23 //  one keyword matches, an iterator to the first matching keyword is returned.
24 //  If on exit __b == __e, eofbit is set in __err.  If __case_sensitive is false,
25 //  __ct is used to force to lower case before comparing characters.
26 //  Examples:
27 //  Keywords:  "a", "abb"
28 //  If the input is "a", the first keyword matches and eofbit is set.
29 //  If the input is "abc", no match is found and "ab" are consumed.
30 //
31 // template <class _InputIterator, class _ForwardIterator, class _Ctype>
32 // _ForwardIterator
33 // __scan_keyword(_InputIterator& __b, _InputIterator __e,
34 //                _ForwardIterator __kb, _ForwardIterator __ke,
35 //                const _Ctype& __ct, ios_base::iostate& __err,
36 //                bool __case_sensitive = true);
37 
38 #include <locale>
39 #include <cassert>
40 
41 #include "test_macros.h"
42 
main(int,char **)43 int main(int, char**)
44 {
45     const std::ctype<char>& ct = std::use_facet<std::ctype<char> >(std::locale::classic());
46     std::ios_base::iostate err = std::ios_base::goodbit;
47     {
48         const char input[] = "a";
49         const char* in = input;
50         std::string keys[] = {"a", "abb"};
51         err = std::ios_base::goodbit;
52         std::string* k = std::__scan_keyword(in, input+sizeof(input)-1,
53                                              keys, keys+sizeof(keys)/sizeof(keys[0]),
54                                              ct, err);
55         assert(k - keys == 0);
56         assert(in == input+1);
57         assert(err == std::ios_base::eofbit);
58     }
59     {
60         const char input[] = "abc";
61         const char* in = input;
62         std::string keys[] = {"a", "abb"};
63         err = std::ios_base::goodbit;
64         std::string* k = std::__scan_keyword(in, input+sizeof(input)-1,
65                                              keys, keys+sizeof(keys)/sizeof(keys[0]),
66                                              ct, err);
67         assert(k - keys == 2);
68         assert(in == input+2);
69         assert(err == std::ios_base::failbit);
70     }
71     {
72         const char input[] = "abb";
73         const char* in = input;
74         std::string keys[] = {"a", "abb"};
75         err = std::ios_base::goodbit;
76         std::string* k = std::__scan_keyword(in, input+sizeof(input)-1,
77                                              keys, keys+sizeof(keys)/sizeof(keys[0]),
78                                              ct, err);
79         assert(k - keys == 1);
80         assert(in == input+3);
81         assert(err == std::ios_base::eofbit);
82     }
83     {
84         const char input[] = "Tue ";
85         const char* in = input;
86         std::string keys[] = {"Mon", "Monday", "Tue", "Tuesday"};
87         err = std::ios_base::goodbit;
88         std::string* k = std::__scan_keyword(in, input+sizeof(input)-1,
89                                              keys, keys+sizeof(keys)/sizeof(keys[0]),
90                                              ct, err);
91         assert(k - keys == 2);
92         assert(in == input+3);
93         assert(err == std::ios_base::goodbit);
94     }
95     {
96         const char input[] = "tue ";
97         const char* in = input;
98         std::string keys[] = {"Mon", "Monday", "Tue", "Tuesday"};
99         err = std::ios_base::goodbit;
100         std::string* k = std::__scan_keyword(in, input+sizeof(input)-1,
101                                              keys, keys+sizeof(keys)/sizeof(keys[0]),
102                                              ct, err);
103         assert(k - keys == 4);
104         assert(in == input+0);
105         assert(err == std::ios_base::failbit);
106     }
107     {
108         const char input[] = "tue ";
109         const char* in = input;
110         std::string keys[] = {"Mon", "Monday", "Tue", "Tuesday"};
111         err = std::ios_base::goodbit;
112         std::string* k = std::__scan_keyword(in, input+sizeof(input)-1,
113                                              keys, keys+sizeof(keys)/sizeof(keys[0]),
114                                              ct, err, false);
115         assert(k - keys == 2);
116         assert(in == input+3);
117         assert(err == std::ios_base::goodbit);
118     }
119 
120   return 0;
121 }
122