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 next token if, found (in which case it will be stored in 57 // cur_token()), and the empty string if no more tokens are found. NextToken()58 base::StringView NextToken() { return Next() ? cur_token() : ""; } 59 60 // Returns the current token iff last call to Next() returned true. 61 // In all other cases (before the 1st call to Next() and after Next() returns 62 // false) returns the empty string. cur_token()63 base::StringView cur_token() { return cur_; } 64 65 // Returns the remainder of the current input string that has not yet been 66 // tokenized. remainder()67 base::StringView remainder() { return next_; } 68 69 private: 70 StringViewSplitter(const StringViewSplitter&) = delete; 71 StringViewSplitter& operator=(const StringViewSplitter&) = delete; 72 void Initialize(base::StringView); 73 74 base::StringView str_; 75 base::StringView cur_; 76 base::StringView next_; 77 bool end_of_input_; 78 const char delimiter_; 79 const EmptyTokenMode empty_token_mode_; 80 }; 81 82 } // namespace base 83 } // namespace perfetto 84 85 #endif // INCLUDE_PERFETTO_EXT_BASE_STRING_VIEW_SPLITTER_H_ 86