1 // Copyright (C) 2022 Google LLC 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 #include "icing/util/fingerprint-util.h" 16 17 namespace icing { 18 namespace lib { 19 20 namespace fingerprint_util { 21 22 // A formatter to properly handle a string that is actually just a hash value. GetFingerprintString(uint64_t fingerprint)23std::string GetFingerprintString(uint64_t fingerprint) { 24 std::string encoded_fprint; 25 // DynamicTrie cannot handle keys with '0' as bytes. So, we encode it in 26 // base128 and add 1 to make sure that no byte is '0'. This increases the 27 // size of the encoded_fprint from 8-bytes to 10-bytes. 28 while (fingerprint) { 29 encoded_fprint.push_back((fingerprint & 0x7F) + 1); 30 fingerprint >>= 7; 31 } 32 return encoded_fprint; 33 } 34 GetFingerprint(std::string_view fingerprint_string)35uint64_t GetFingerprint(std::string_view fingerprint_string) { 36 uint64_t fprint = 0; 37 for (int i = fingerprint_string.length() - 1; i >= 0; --i) { 38 fprint <<= 7; 39 char c = fingerprint_string[i] - 1; 40 fprint |= (c & 0x7F); 41 } 42 return fprint; 43 } 44 45 } // namespace fingerprint_util 46 47 } // namespace lib 48 } // namespace icing 49