1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef LIB_FIDL_CPP_STRING_VIEW_H_
6 #define LIB_FIDL_CPP_STRING_VIEW_H_
7 
8 #include <zircon/fidl.h>
9 
10 namespace fidl {
11 
12 class StringView : public fidl_string_t {
13 public:
StringView()14     StringView() : fidl_string_t{} {}
15 
size()16     uint64_t size() const { return fidl_string_t::size; }
set_size(uint64_t size)17     void set_size(uint64_t size) { fidl_string_t::size = size; }
18 
data()19     const char* data() const { return fidl_string_t::data; }
set_data(char * data)20     void set_data(char* data) { fidl_string_t::data = data; }
21 
mutable_data()22     char* mutable_data() const { return fidl_string_t::data; }
23 
is_null()24     bool is_null() const { return fidl_string_t::data == nullptr; }
empty()25     bool empty() const { return fidl_string_t::size == 0; }
26 
at(size_t offset)27     const char& at(size_t offset) const { return data()[offset]; }
at(size_t offset)28     char& at(size_t offset) { return mutable_data()[offset]; }
29 
30     const char& operator[](size_t offset) const { return at(offset); }
31     char& operator[](size_t offset) { return at(offset); }
32 
begin()33     char* begin() { return mutable_data(); }
begin()34     const char* begin() const { return data(); }
cbegin()35     const char* cbegin() const { return data(); }
36 
end()37     char* end() { return mutable_data() + size(); }
end()38     const char* end() const { return data() + size(); }
cend()39     const char* cend() const { return data() + size(); }
40 
impl()41     fidl_string_t* impl() { return this; }
42 };
43 
44 } // namespace fidl
45 
46 #endif // LIB_FIDL_CPP_STRING_VIEW_H_
47