1 /*
2  * Copyright (C) 2018 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 NOGROD_STRING_TABLE_
18 #define NOGROD_STRING_TABLE_
19 
20 #include <cstdint>
21 #include <string>
22 #include <vector>
23 
24 #include <berberis/base/checks.h>
25 #include <berberis/base/macros.h>
26 
27 #include "buffer.h"
28 
29 namespace nogrod {
30 
31 class StringTable {
32  public:
33   StringTable() = default;
34 
StringTable(Buffer<char> strtab)35   StringTable(Buffer<char> strtab) : strtab_(std::move(strtab)) {
36     // string table should be \0 terminated.
37     CHECK_EQ(strtab_.data()[strtab_.size() - 1], 0);
38   }
39 
40   StringTable(const StringTable&) = delete;
41   StringTable& operator=(const StringTable&) = delete;
42   StringTable(StringTable&&) = default;
43   StringTable& operator=(StringTable&&) = default;
44 
GetString(size_t index)45   [[nodiscard]] const char* GetString(size_t index) const {
46     CHECK_LT(index, strtab_.size());
47     return strtab_.data() + index;
48   }
49 
50  private:
51   Buffer<char> strtab_;
52 };
53 
54 }  // namespace nogrod
55 #endif  // NOGROD_STRING_TABLE_
56