Coverage Report

Created: 2026-06-08 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wxwidgets/include/wx/tokenzr.h
Line
Count
Source
1
/////////////////////////////////////////////////////////////////////////////
2
// Name:        wx/tokenzr.h
3
// Purpose:     String tokenizer - a C++ replacement for strtok(3)
4
// Author:      Guilhem Lavaux
5
// Modified by: (or rather rewritten by) Vadim Zeitlin
6
// Created:     04/22/98
7
// Copyright:   (c) Guilhem Lavaux
8
// Licence:     wxWindows licence
9
/////////////////////////////////////////////////////////////////////////////
10
11
#ifndef _WX_TOKENZRH
12
#define _WX_TOKENZRH
13
14
#include "wx/object.h"
15
#include "wx/string.h"
16
#include "wx/arrstr.h"
17
18
// ----------------------------------------------------------------------------
19
// constants
20
// ----------------------------------------------------------------------------
21
22
// default: delimiters are usual white space characters
23
#define wxDEFAULT_DELIMITERS (wxT(" \t\r\n"))
24
25
// wxStringTokenizer mode flags which determine its behaviour
26
enum wxStringTokenizerMode
27
{
28
    wxTOKEN_INVALID = -1,   // set by def ctor until SetString() is called
29
    wxTOKEN_DEFAULT,        // strtok() for whitespace delims, RET_EMPTY else
30
    wxTOKEN_RET_EMPTY,      // return empty token in the middle of the string
31
    wxTOKEN_RET_EMPTY_ALL,  // return trailing empty tokens too
32
    wxTOKEN_RET_DELIMS,     // return the delim with token (implies RET_EMPTY)
33
    wxTOKEN_STRTOK          // behave exactly like strtok(3)
34
};
35
36
// ----------------------------------------------------------------------------
37
// wxStringTokenizer: replaces infamous strtok() and has some other features
38
// ----------------------------------------------------------------------------
39
40
class WXDLLIMPEXP_BASE wxWARN_UNUSED wxStringTokenizer : public wxObject
41
{
42
public:
43
    // ctors and initializers
44
        // default ctor, call SetString() later
45
0
    wxStringTokenizer() { m_mode = wxTOKEN_INVALID; }
46
        // ctor which gives us the string
47
    wxStringTokenizer(const wxString& str,
48
                      const wxString& delims = wxDEFAULT_DELIMITERS,
49
                      wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
50
        // copy ctor and assignment operator
51
    wxStringTokenizer(const wxStringTokenizer& src);
52
    wxStringTokenizer& operator=(const wxStringTokenizer& src);
53
54
        // args are same as for the non default ctor above
55
    void SetString(const wxString& str,
56
                   const wxString& delims = wxDEFAULT_DELIMITERS,
57
                   wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
58
59
        // reinitialize the tokenizer with the same delimiters/mode
60
    void Reinit(const wxString& str);
61
62
    // tokens access
63
        // return the number of remaining tokens
64
    size_t CountTokens() const;
65
        // did we reach the end of the string?
66
    bool HasMoreTokens() const;
67
        // get the next token, will return empty string if !HasMoreTokens()
68
    wxString GetNextToken();
69
        // get the delimiter which terminated the token last retrieved by
70
        // GetNextToken() or NUL if there had been no tokens yet or the last
71
        // one wasn't terminated (but ran to the end of the string)
72
0
    wxChar GetLastDelimiter() const { return m_lastDelim; }
73
74
    // get current tokenizer state
75
        // returns the part of the string which remains to tokenize (*not* the
76
        // initial string)
77
0
    wxString GetString() const { return wxString(m_pos, m_string.end()); }
78
79
        // returns the current position (i.e. one index after the last
80
        // returned token or 0 if GetNextToken() has never been called) in the
81
        // original string
82
0
    size_t GetPosition() const { return m_pos - m_string.begin(); }
83
84
    // misc
85
        // get the current mode - can be different from the one passed to the
86
        // ctor if it was wxTOKEN_DEFAULT
87
0
    wxStringTokenizerMode GetMode() const { return m_mode; }
88
        // do we return empty tokens?
89
0
    bool AllowEmpty() const { return m_mode != wxTOKEN_STRTOK; }
90
91
92
    // backwards compatibility section from now on
93
    // -------------------------------------------
94
95
    // for compatibility only, use GetNextToken() instead
96
0
    wxString NextToken() { return GetNextToken(); }
97
98
    // compatibility only, don't use
99
    void SetString(const wxString& to_tokenize,
100
                   const wxString& delims,
101
                   bool WXUNUSED(ret_delim))
102
0
    {
103
0
        SetString(to_tokenize, delims, wxTOKEN_RET_DELIMS);
104
0
    }
105
106
    wxStringTokenizer(const wxString& to_tokenize,
107
                      const wxString& delims,
108
                      bool ret_delim)
109
0
    {
110
0
        SetString(to_tokenize, delims, ret_delim);
111
0
    }
112
113
protected:
114
0
    bool IsOk() const { return m_mode != wxTOKEN_INVALID; }
115
116
    bool DoHasMoreTokens() const;
117
118
    void DoCopyFrom(const wxStringTokenizer& src);
119
120
    enum MoreTokensState
121
    {
122
        MoreTokens_Unknown,
123
        MoreTokens_Yes,
124
        MoreTokens_No
125
    };
126
127
    MoreTokensState m_hasMoreTokens;
128
129
    wxString m_string;              // the string we tokenize
130
    wxString::const_iterator m_stringEnd;
131
    wxWCharBuffer m_delims;         // all possible delimiters
132
    size_t m_delimsLen;
133
134
    wxString::const_iterator m_pos; // the current position in m_string
135
136
    wxStringTokenizerMode m_mode;   // see wxTOKEN_XXX values
137
138
    wxChar   m_lastDelim;           // delimiter after last token or '\0'
139
};
140
141
// ----------------------------------------------------------------------------
142
// convenience function which returns all tokens at once
143
// ----------------------------------------------------------------------------
144
145
// the function takes the same parameters as wxStringTokenizer ctor and returns
146
// the array containing all tokens
147
wxArrayString WXDLLIMPEXP_BASE
148
wxStringTokenize(const wxString& str,
149
                 const wxString& delims = wxDEFAULT_DELIMITERS,
150
                 wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
151
152
#endif // _WX_TOKENZRH