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