1 /* 2 * Copyright (C) 2019 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 SRC_TRACE_PROCESSOR_CONTAINERS_NULL_TERM_STRING_VIEW_H_ 18 #define SRC_TRACE_PROCESSOR_CONTAINERS_NULL_TERM_STRING_VIEW_H_ 19 20 #include "perfetto/ext/base/string_view.h" 21 22 namespace perfetto { 23 namespace trace_processor { 24 25 // A string-like object that refers to a non-owned piece of memory which is 26 // null terminated. 27 class NullTermStringView : public base::StringView { 28 public: 29 // Creates an empty view. NullTermStringView()30 NullTermStringView() : StringView() {} 31 32 // Make the view copy constructible. 33 NullTermStringView(const NullTermStringView&) = default; 34 NullTermStringView& operator=(const NullTermStringView&) = default; 35 36 // Creates a NullTermStringView from a null-terminated C string. 37 // Deliberately not "explicit". NullTermStringView(const char * cstr)38 NullTermStringView(const char* cstr) : StringView(cstr) {} 39 40 // Creates a NullTermStringView from a null terminated C-string where the 41 // size is known. This allows a call to strlen() to be avoided. 42 // Note: This string MUST be null terminated i.e. data[size] == '\0' MUST hold 43 // for this constructor to be valid. NullTermStringView(const char * data,size_t size)44 NullTermStringView(const char* data, size_t size) : StringView(data, size) { 45 PERFETTO_DCHECK(data[size] == '\0'); 46 } 47 48 // This instead has to be explicit, as creating a NullTermStringView out of a 49 // std::string can be subtle. NullTermStringView(const std::string & str)50 explicit NullTermStringView(const std::string& str) : StringView(str) {} 51 52 // Returns the null terminated C-string backing this string view. The same 53 // pointer as |data()| is returned. c_str()54 const char* c_str() const { return data(); } 55 }; 56 57 } // namespace trace_processor 58 } // namespace perfetto 59 60 #endif // SRC_TRACE_PROCESSOR_CONTAINERS_NULL_TERM_STRING_VIEW_H_ 61