1 #ifndef MARISA_QUERY_H_ 2 #define MARISA_QUERY_H_ 3 4 #include <string> 5 6 #include "base.h" 7 8 namespace marisa { 9 10 class Query { 11 public: Query(const char * ptr,std::size_t length)12 Query(const char *ptr, std::size_t length) : ptr_(ptr), length_(length) {} Query(const Query & query)13 Query(const Query &query) : ptr_(query.ptr_), length_(query.length_) {} 14 insert(std::string * str)15 void insert(std::string *str) const { 16 str->insert(0, ptr_, length_); 17 } 18 19 UInt8 operator[](std::size_t i) const { 20 MARISA_DEBUG_IF(i >= length_, MARISA_PARAM_ERROR); 21 return ptr_[i]; 22 } ends_at(std::size_t i)23 bool ends_at(std::size_t i) const { 24 MARISA_DEBUG_IF(i > length_, MARISA_PARAM_ERROR); 25 return i == length_; 26 } 27 28 private: 29 const char *ptr_; 30 std::size_t length_; 31 32 // Disallows assignment. 33 Query &operator=(const Query &query); 34 }; 35 36 class CQuery { 37 public: CQuery(const char * str)38 explicit CQuery(const char *str) : str_(str) {} CQuery(const CQuery & query)39 CQuery(const CQuery &query) : str_(query.str_) {} 40 insert(std::string * str)41 void insert(std::string *str) const { 42 str->insert(0, str_); 43 } 44 45 UInt8 operator[](std::size_t i) const { 46 return str_[i]; 47 } ends_at(std::size_t i)48 bool ends_at(std::size_t i) const { 49 return str_[i] == '\0'; 50 } 51 52 private: 53 const char *str_; 54 55 // Disallows assignment. 56 CQuery &operator=(const CQuery &); 57 }; 58 59 } // namespace marisa 60 61 #endif // MARISA_QUERY_H_ 62