aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/tokeniser.hh
blob: 627d28e6c5d16ea682a36bef38102319a9346f99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef PALUDIS_GUARD_PALUDIS_TOKENISER_HH
#define PALUDIS_GUARD_PALUDIS_TOKENISER_HH 1

#include <iterator>
#include <paludis/util/instantiation_policy.hh>
#include <string>

/** \file
 * Declarations for Tokeniser and related utilities.
 *
 * \ingroup g_strings
 *
 * \section Examples
 *
 * - None at this time.
 */

namespace paludis
{
    /**
     * Delimiter policy for Tokeniser.
     *
     * \ingroup g_strings
     */
    namespace delim_kind
    {
        /**
         * Any of the characters split, and the delimiter is discarded.
         *
         * \ingroup g_strings
         */
        struct AnyOfTag;
    }

    /**
     * Delimiter mode for Tokeniser.
     *
     * \ingroup g_strings
     */
    namespace delim_mode
    {
        /**
         * Discard the delimiters.
         *
         * \ingroup g_strings
         */
        struct DelimiterTag;

        /**
         * Keep the delimiters.
         *
         * \ingroup g_strings
         */
        struct BoundaryTag;
    }

    /**
     * Tokeniser internal use only.
     *
     * \ingroup g_strings
     */
    namespace tokeniser_internals
    {
        /**
         * A Writer handles Tokeniser's writes.
         *
         * \ingroup g_strings
         */
        template <typename DelimMode_, typename Char_, typename Iter_>
        struct Writer;

        /**
         * A Writer handles Tokeniser's writes (specialisation for
         * delim_mode::DelimiterTag).
         *
         * \ingroup g_strings
         */
        template <typename Char_, typename Iter_>
        struct Writer<delim_mode::DelimiterTag, Char_, Iter_>
        {
            /**
             * Handle a token.
             */
            static void handle_token(const std::basic_string<Char_> & s, Iter_ & i)
            {
                *i++ = s;
            }

            /**
             * Handle a delimiter.
             */
            static void handle_delim(const std::basic_string<Char_> &, const Iter_ &)
            {
            }
        };

        /**
         * A Writer handles Tokeniser's writes (specialisation for
         * delim_mode::BoundaryTag).
         *
         * \ingroup g_strings
         */
        template <typename Char_, typename Iter_>
        struct Writer<delim_mode::BoundaryTag, Char_, Iter_>
        {
            /**
             * Handle a token.
             */
            static void handle_token(const std::basic_string<Char_> & s, Iter_ & i)
            {
                *i++ = s;
            }

            /**
             * Handle a delimiter.
             */
            static void handle_delim(const std::basic_string<Char_> & s, Iter_ & i)
            {
                *i++ = s;
            }
        };

    }

    /**
     * Tokeniser splits up strings into smaller strings.
     *
     * \ingroup g_strings
     */
    template <typename DelimKind_, typename DelimMode_ = delim_mode::DelimiterTag,
             typename Char_ = std::string::value_type>
    struct Tokeniser;

    /**
     * Tokeniser: specialisation for delim_kind::AnyOfTag.
     *
     * \ingroup g_strings
     * \nosubgrouping
     */
    template <typename DelimMode_, typename Char_>
    class Tokeniser<delim_kind::AnyOfTag, DelimMode_, Char_>
    {
        private:
            Tokeniser();

        public:
            ///\name Basic operations
            ///\{

            /**
             * Do the tokenisation.
             */
            template <typename Iter_>
            static void tokenise(const std::basic_string<Char_> & s,
                    const std::basic_string<Char_> & delims, Iter_ iter);
    };

    template <typename DelimMode_, typename Char_>
    template <typename Iter_>
    void
    Tokeniser<delim_kind::AnyOfTag, DelimMode_, Char_>::tokenise(
            const std::basic_string<Char_> & s, const std::basic_string<Char_> & delims, Iter_ iter)
    {
        typename std::basic_string<Char_>::size_type p(0), old_p(0);
        bool in_delim((! s.empty()) && std::basic_string<Char_>::npos != delims.find(s[0]));

        for ( ; p < s.length() ; ++p)
        {
            if (in_delim)
            {
                if (std::basic_string<Char_>::npos == delims.find(s[p]))
                {
                    tokeniser_internals::Writer<DelimMode_, Char_, Iter_>::handle_delim(
                            s.substr(old_p, p - old_p), iter);
                    in_delim = false;
                    old_p = p;
                }
            }
            else
            {
                if (std::basic_string<Char_>::npos != delims.find(s[p]))
                {
                    tokeniser_internals::Writer<DelimMode_, Char_, Iter_>::handle_token(
                            s.substr(old_p, p - old_p), iter);
                    in_delim = true;
                    old_p = p;
                }
            }
        }

        if (old_p != p)
        {
            if (in_delim)
                tokeniser_internals::Writer<DelimMode_, Char_, Iter_>::handle_delim(
                        s.substr(old_p, p - old_p), iter);
            else
                tokeniser_internals::Writer<DelimMode_, Char_, Iter_>::handle_token(
                        s.substr(old_p, p - old_p), iter);
        }
    }

    /**
     * Convenience class for tokenising on whitespace.
     *
     * \ingroup g_strings
     */
    class PALUDIS_VISIBLE WhitespaceTokeniser
    {
        public:
            template <typename Iter_>
            static void tokenise(const std::string & s, Iter_ iter)
            {
                Tokeniser<delim_kind::AnyOfTag>::tokenise(s, " \t\r\n", iter);
            }
    };
}

#endif