Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame^] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef SYMTAB_H_ |
| 16 | #define SYMTAB_H_ |
| 17 | |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "string_piece.h" |
| 22 | |
| 23 | using namespace std; |
| 24 | |
| 25 | extern vector<string>* g_symbols; |
| 26 | |
| 27 | class Symtab; |
| 28 | |
| 29 | class Symbol { |
| 30 | public: |
| 31 | const string& str() const { |
| 32 | return (*g_symbols)[v_]; |
| 33 | } |
| 34 | |
| 35 | const char* c_str() const { |
| 36 | return str().c_str(); |
| 37 | } |
| 38 | |
| 39 | bool empty() const { return !v_; } |
| 40 | |
| 41 | int val() const { return v_; } |
| 42 | |
| 43 | char get(size_t i) const { |
| 44 | const string& s = str(); |
| 45 | if (i >= s.size()) |
| 46 | return 0; |
| 47 | return s[i]; |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | explicit Symbol(int v); |
| 52 | |
| 53 | int v_; |
| 54 | |
| 55 | friend class Symtab; |
| 56 | }; |
| 57 | |
| 58 | inline bool operator==(const Symbol& x, const Symbol& y) { |
| 59 | return x.val() == y.val(); |
| 60 | } |
| 61 | |
| 62 | namespace std { |
| 63 | template<> struct hash<Symbol> { |
| 64 | size_t operator()(const Symbol& s) const { |
| 65 | return s.val(); |
| 66 | } |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | void InitSymtab(); |
| 71 | void QuitSymtab(); |
| 72 | Symbol Intern(StringPiece s); |
| 73 | |
| 74 | string JoinSymbols(const vector<Symbol>& syms, const char* sep); |
| 75 | |
| 76 | #endif // SYMTAB_H_ |