• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #include <berberis/base/checks.h>
24 #include <berberis/base/macros.h>
25 
26 namespace nogrod {
27 
28 class StringTable {
29  public:
StringTable()30   StringTable() : strtab_(nullptr), strtab_size_(0) {}
31 
StringTable(const char * strtab,size_t strtab_size)32   StringTable(const char* strtab, size_t strtab_size) : strtab_(strtab), strtab_size_(strtab_size) {
33     // string table should be \0 terminated.
34     CHECK(strtab_[strtab_size_ - 1] == 0);
35   }
36 
37   StringTable(const StringTable&) = default;
38   StringTable& operator=(const StringTable&) = default;
39   StringTable(StringTable&&) = default;
40   StringTable& operator=(StringTable&&) = default;
41 
GetString(size_t index)42   [[nodiscard]] const char* GetString(size_t index) const {
43     CHECK(index < strtab_size_);
44     return strtab_ + index;
45   }
46 
47  private:
48   const char* strtab_;
49   size_t strtab_size_;
50 };
51 
52 }  // namespace nogrod
53 #endif  // __NOGROD_STRING_TABLE_
54