| 1 | /* |
| 2 | Copyright (C) 2011-2016 Brazil |
| 3 | Copyright (C) 2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include "string.hpp"cxx . . $ `<file>/lib/dat/string.hpp`/ |
| 23 | |
| 24 | namespace grncxx . . $ grn/ { |
| 25 | namespace datcxx . . $ grn/dat/ { |
| 26 | |
| 27 | class GRN_DAT_APIcxx . . $ `lib/dat/dat.hpp:44:12`! Keycxx . . $ grn/dat/Key# { |
| 28 | public: |
| 29 | const UInt8cxx . . $ grn/dat/UInt8# &operatorcxx . . $ grn/dat/Key#`operator[]`(ffeb5165310d7d37).[](UInt32cxx . . $ grn/dat/UInt32# i) const { |
| 30 | GRN_DAT_DEBUG_THROW_IFcxx . . $ `lib/dat/dat.hpp:242:10`!(i >= length()); |
| 31 | return buf_cxx . . $ grn/dat/Key#buf_.[i]; |
| 32 | } |
| 33 | |
| 34 | bool is_validcxx . . $ grn/dat/Key#is_valid(50ce9a9e25b4a850).() const { |
| 35 | return idcxx . . $ grn/dat/Key#id(ddd8539a2fcdd4ce).() != INVALID_KEY_IDcxx . . $ grn/dat/INVALID_KEY_ID.; |
| 36 | } |
| 37 | |
| 38 | Stringcxx . . $ grn/dat/String# strcxx . . $ grn/dat/Key#str(3f4ba625c018c01d).() const { |
| 39 | return Stringcxx . . $ grn/dat/String#(ptrcxx . . $ grn/dat/Key#ptr(3a8ef3fdc6e817ef).(), lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).()); |
| 40 | } |
| 41 | |
| 42 | const void *ptrcxx . . $ grn/dat/Key#ptr(3a8ef3fdc6e817ef).() const { |
| 43 | return buf_cxx . . $ grn/dat/Key#buf_.; |
| 44 | } |
| 45 | UInt32cxx . . $ grn/dat/UInt32# lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).() const { |
| 46 | const UInt32cxx . . $ grn/dat/UInt32# length = (length_high_cxx . . $ grn/dat/Key#length_high_. << 4) | (id_and_length_low_cxx . . $ grn/dat/Key#id_and_length_low_. & 0x0F); |
| 47 | // 4096 bytes key overflows to 0, so 0 is treated as 4096. |
| 48 | return (length == 0) ? MAX_KEY_LENGTHcxx . . $ grn/dat/MAX_KEY_LENGTH. : length; |
| 49 | } |
| 50 | UInt32cxx . . $ grn/dat/UInt32# idcxx . . $ grn/dat/Key#id(ddd8539a2fcdd4ce).() const { |
| 51 | return id_and_length_low_cxx . . $ grn/dat/Key#id_and_length_low_. >> 4; |
| 52 | } |
| 53 | |
| 54 | bool equals_tocxx . . $ grn/dat/Key#equals_to(4319e1f56ab94368).(const void *ptr, UInt32cxx . . $ grn/dat/UInt32# length, UInt32cxx . . $ grn/dat/UInt32# offset = 0) const { |
| 55 | if (length != this->lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).()) { |
| 56 | return false; |
| 57 | } |
| 58 | for ( ; offset < length; ++offset) { |
| 59 | if ((*this)[cxx . . $ grn/dat/Key#`operator[]`(ffeb5165310d7d37).offset] != static_cast<const UInt8cxx . . $ grn/dat/UInt8# *>(ptr)[offset]) { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | // Creates an object of Key from given parameters. Then, the created object |
| 67 | // is embedded into a specified buffer. |
| 68 | static const Keycxx . . $ grn/dat/Key# &createcxx . . $ grn/dat/Key#create(66d57bc96acb1d3c).(UInt32cxx . . $ grn/dat/UInt32# *buf, UInt32cxx . . $ grn/dat/UInt32# key_id, |
| 69 | const void *key_ptr, UInt32cxx . . $ grn/dat/UInt32# key_length) { |
| 70 | GRN_DAT_DEBUG_THROW_IFcxx . . $ `lib/dat/dat.hpp:242:10`!(buf == NULL); |
| 71 | GRN_DAT_DEBUG_THROW_IFcxx . . $ `lib/dat/dat.hpp:242:10`!(key_id > MAX_KEY_ID); |
| 72 | GRN_DAT_DEBUG_THROW_IFcxx . . $ `lib/dat/dat.hpp:242:10`!((key_ptr == NULL) && (key_length != 0)); |
| 73 | GRN_DAT_DEBUG_THROW_IFcxx . . $ `lib/dat/dat.hpp:242:10`!(key_length > MAX_KEY_LENGTH); |
| 74 | |
| 75 | *buf = (key_id << 4) | (key_length & 0x0F); |
| 76 | UInt8cxx . . $ grn/dat/UInt8# *ptr = reinterpret_cast<UInt8cxx . . $ grn/dat/UInt8# *>(buf + 1); |
| 77 | *ptr++ = static_cast<UInt8cxx . . $ grn/dat/UInt8#>(key_length >> 4); |
| 78 | for (UInt32cxx . . $ grn/dat/UInt32# i = 0; i < key_length; ++i) { |
| 79 | ptr[i] = static_cast<const UInt8cxx . . $ grn/dat/UInt8# *>(key_ptr)[i]; |
| 80 | } |
| 81 | return *reinterpret_cast<const Keycxx . . $ grn/dat/Key# *>(buf); |
| 82 | } |
| 83 | |
| 84 | // Calculates how many UInt32s are required for a string. It is guaranteed |
| 85 | // that the estimated size is not less than the actual size. |
| 86 | static UInt32cxx . . $ grn/dat/UInt32# estimate_sizecxx . . $ grn/dat/Key#estimate_size(2141b656ce2cc063).(UInt32cxx . . $ grn/dat/UInt32# length) { |
| 87 | return static_cast<UInt32cxx . . $ grn/dat/UInt32#>(2 + (length / sizeof(UInt32cxx . . $ grn/dat/UInt32#))); |
| 88 | } |
| 89 | |
| 90 | // Returns a reference to an invalid key. |
| 91 | static const Keycxx . . $ grn/dat/Key# &invalid_keycxx . . $ grn/dat/Key#invalid_key(741ea5814372790).() { |
| 92 | static const Keycxx . . $ grn/dat/Key# invalid_keycxx . . $ grn/dat/Key#Key(49f6e7a06ebc5aa8).; |
| 93 | return invalid_key; |
| 94 | // static const UInt32 invalid_key_buf[2] = { INVALID_KEY_ID << 4, 0 }; |
| 95 | // return *reinterpret_cast<const Key *>(invalid_key_buf); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | UInt32cxx . . $ grn/dat/UInt32# id_and_length_low_cxx . . $ grn/dat/Key#id_and_length_low_.; |
| 100 | UInt8cxx . . $ grn/dat/UInt8# length_high_cxx . . $ grn/dat/Key#length_high_.; |
| 101 | UInt8cxx . . $ grn/dat/UInt8# buf_cxx . . $ grn/dat/Key#buf_.[3]; |
| 102 | |
| 103 | // Disallows instantiation. |
| 104 | Keycxx . . $ grn/dat/Key#Key(49f6e7a06ebc5aa8).() : id_and_length_low_cxx . . $ grn/dat/Key#id_and_length_low_.(INVALID_KEY_IDcxx . . $ grn/dat/INVALID_KEY_ID. << 4), length_high_cxx . . $ grn/dat/Key#length_high_.(0) {} |
| 105 | ~cxx . . $ grn/dat/Key#`~Key`(ced63f7c635d850d).Keycxx . . $ grn/dat/Key#() {} |
| 106 | |
| 107 | // Disallows copy and assignment. |
| 108 | Keycxx . . $ grn/dat/Key#Key(5c3f5cbd7f81f1f7).(const Keycxx . . $ grn/dat/Key# &); |
| 109 | Keycxx . . $ grn/dat/Key# &operatorcxx . . $ grn/dat/Key#`operator=`(f6756e733a1ad828).=(const Keycxx . . $ grn/dat/Key# &); |
| 110 | }; |
| 111 | |
| 112 | } // namespace dat |
| 113 | } // namespace grn |