Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:30:28

0001 /*
0002  * Copyright 2021 Google Inc. All rights reserved.
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *     http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  */
0016 
0017 #ifndef FLATBUFFERS_STRING_H_
0018 #define FLATBUFFERS_STRING_H_
0019 
0020 #include "flatbuffers/base.h"
0021 #include "flatbuffers/vector.h"
0022 
0023 namespace flatbuffers {
0024 
0025 struct String : public Vector<char> {
0026   const char *c_str() const { return reinterpret_cast<const char *>(Data()); }
0027   std::string str() const { return std::string(c_str(), size()); }
0028 
0029   // clang-format off
0030   #ifdef FLATBUFFERS_HAS_STRING_VIEW
0031   flatbuffers::string_view string_view() const {
0032     return flatbuffers::string_view(c_str(), size());
0033   }
0034 
0035   /* implicit */
0036   operator flatbuffers::string_view() const {
0037     return flatbuffers::string_view(c_str(), size());
0038   }
0039   #endif // FLATBUFFERS_HAS_STRING_VIEW
0040   // clang-format on
0041 
0042   bool operator<(const String &o) const {
0043     return StringLessThan(this->data(), this->size(), o.data(), o.size());
0044   }
0045 };
0046 
0047 // Convenience function to get std::string from a String returning an empty
0048 // string on null pointer.
0049 static inline std::string GetString(const String *str) {
0050   return str ? str->str() : "";
0051 }
0052 
0053 // Convenience function to get char* from a String returning an empty string on
0054 // null pointer.
0055 static inline const char *GetCstring(const String *str) {
0056   return str ? str->c_str() : "";
0057 }
0058 
0059 #ifdef FLATBUFFERS_HAS_STRING_VIEW
0060 // Convenience function to get string_view from a String returning an empty
0061 // string_view on null pointer.
0062 static inline flatbuffers::string_view GetStringView(const String *str) {
0063   return str ? str->string_view() : flatbuffers::string_view();
0064 }
0065 #endif  // FLATBUFFERS_HAS_STRING_VIEW
0066 
0067 }  // namespace flatbuffers
0068 
0069 #endif  // FLATBUFFERS_STRING_H_