xref: /aosp_15_r20/external/perfetto/include/perfetto/ext/base/string_view_splitter.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef INCLUDE_PERFETTO_EXT_BASE_STRING_VIEW_SPLITTER_H_
18 #define INCLUDE_PERFETTO_EXT_BASE_STRING_VIEW_SPLITTER_H_
19 
20 #include "perfetto/ext/base/string_view.h"
21 
22 namespace perfetto {
23 namespace base {
24 
25 // C++ version of strtok(). Splits a StringView without making copies or any
26 // heap allocations. Supports the special case of using \0 as a delimiter.
27 // The token returned in output are valid as long as the input string is valid.
28 class StringViewSplitter {
29  public:
30   // Whether an empty string (two delimiters side-to-side) is a valid token.
31   enum class EmptyTokenMode {
32     DISALLOW_EMPTY_TOKENS,
33     ALLOW_EMPTY_TOKENS,
34 
35     DEFAULT = DISALLOW_EMPTY_TOKENS,
36   };
37 
38   // Can take ownership of the string if passed via std::move(), e.g.:
39   // StringViewSplitter(std::move(str), '\n');
40   StringViewSplitter(base::StringView,
41                      char delimiter,
42                      EmptyTokenMode empty_token_mode = EmptyTokenMode::DEFAULT);
43 
44   // Splits the current token from an outer StringViewSplitter instance. This is
45   // to chain splitters as follows: for (base::StringViewSplitter lines(x,
46   // '\n'); ss.Next();)
47   //   for (base::StringViewSplitter words(&lines, ' '); words.Next();)
48   StringViewSplitter(StringViewSplitter*,
49                      char delimiter,
50                      EmptyTokenMode empty_token_mode = EmptyTokenMode::DEFAULT);
51 
52   // Returns true if a token is found (in which case it will be stored in
53   // cur_token()), false if no more tokens are found.
54   bool Next();
55 
56   // Returns the current token iff last call to Next() returned true.
57   // In all other cases (before the 1st call to Next() and after Next() returns
58   // false) returns the empty string.
cur_token()59   base::StringView cur_token() { return cur_; }
60 
61  private:
62   StringViewSplitter(const StringViewSplitter&) = delete;
63   StringViewSplitter& operator=(const StringViewSplitter&) = delete;
64   void Initialize(base::StringView);
65 
66   base::StringView str_;
67   base::StringView cur_;
68   base::StringView next_;
69   bool end_of_input_;
70   const char delimiter_;
71   const EmptyTokenMode empty_token_mode_;
72 };
73 
74 }  // namespace base
75 }  // namespace perfetto
76 
77 #endif  // INCLUDE_PERFETTO_EXT_BASE_STRING_VIEW_SPLITTER_H_
78