xref: /freebsd-src/contrib/llvm-project/llvm/lib/Support/DJB.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===-- Support/DJB.cpp ---DJB Hash -----------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains support for the DJ Bernstein hash function.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/Support/DJB.h"
140b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
150b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
160b57cec5SDimitry Andric #include "llvm/Support/ConvertUTF.h"
170b57cec5SDimitry Andric #include "llvm/Support/Unicode.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric 
chopOneUTF32(StringRef & Buffer)210b57cec5SDimitry Andric static UTF32 chopOneUTF32(StringRef &Buffer) {
220b57cec5SDimitry Andric   UTF32 C;
230b57cec5SDimitry Andric   const UTF8 *const Begin8Const =
240b57cec5SDimitry Andric       reinterpret_cast<const UTF8 *>(Buffer.begin());
250b57cec5SDimitry Andric   const UTF8 *Begin8 = Begin8Const;
260b57cec5SDimitry Andric   UTF32 *Begin32 = &C;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric   // In lenient mode we will always end up with a "reasonable" value in C for
290b57cec5SDimitry Andric   // non-empty input.
300b57cec5SDimitry Andric   assert(!Buffer.empty());
310b57cec5SDimitry Andric   ConvertUTF8toUTF32(&Begin8, reinterpret_cast<const UTF8 *>(Buffer.end()),
320b57cec5SDimitry Andric                      &Begin32, &C + 1, lenientConversion);
330b57cec5SDimitry Andric   Buffer = Buffer.drop_front(Begin8 - Begin8Const);
340b57cec5SDimitry Andric   return C;
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric 
toUTF8(UTF32 C,MutableArrayRef<UTF8> Storage)370b57cec5SDimitry Andric static StringRef toUTF8(UTF32 C, MutableArrayRef<UTF8> Storage) {
380b57cec5SDimitry Andric   const UTF32 *Begin32 = &C;
390b57cec5SDimitry Andric   UTF8 *Begin8 = Storage.begin();
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   // The case-folded output should always be a valid unicode character, so use
420b57cec5SDimitry Andric   // strict mode here.
430b57cec5SDimitry Andric   ConversionResult CR = ConvertUTF32toUTF8(&Begin32, &C + 1, &Begin8,
440b57cec5SDimitry Andric                                            Storage.end(), strictConversion);
450b57cec5SDimitry Andric   assert(CR == conversionOK && "Case folding produced invalid char?");
460b57cec5SDimitry Andric   (void)CR;
470b57cec5SDimitry Andric   return StringRef(reinterpret_cast<char *>(Storage.begin()),
480b57cec5SDimitry Andric                    Begin8 - Storage.begin());
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
foldCharDwarf(UTF32 C)510b57cec5SDimitry Andric static UTF32 foldCharDwarf(UTF32 C) {
520b57cec5SDimitry Andric   // DWARF v5 addition to the unicode folding rules.
530b57cec5SDimitry Andric   // Fold "Latin Small Letter Dotless I" and "Latin Capital Letter I With Dot
540b57cec5SDimitry Andric   // Above" into "i".
550b57cec5SDimitry Andric   if (C == 0x130 || C == 0x131)
560b57cec5SDimitry Andric     return 'i';
570b57cec5SDimitry Andric   return sys::unicode::foldCharSimple(C);
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric 
fastCaseFoldingDjbHash(StringRef Buffer,uint32_t H)60*bdd1243dSDimitry Andric static std::optional<uint32_t> fastCaseFoldingDjbHash(StringRef Buffer,
61*bdd1243dSDimitry Andric                                                       uint32_t H) {
620b57cec5SDimitry Andric   bool AllASCII = true;
630b57cec5SDimitry Andric   for (unsigned char C : Buffer) {
640b57cec5SDimitry Andric     H = H * 33 + ('A' <= C && C <= 'Z' ? C - 'A' + 'a' : C);
650b57cec5SDimitry Andric     AllASCII &= C <= 0x7f;
660b57cec5SDimitry Andric   }
670b57cec5SDimitry Andric   if (AllASCII)
680b57cec5SDimitry Andric     return H;
69*bdd1243dSDimitry Andric   return std::nullopt;
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric 
caseFoldingDjbHash(StringRef Buffer,uint32_t H)720b57cec5SDimitry Andric uint32_t llvm::caseFoldingDjbHash(StringRef Buffer, uint32_t H) {
73*bdd1243dSDimitry Andric   if (std::optional<uint32_t> Result = fastCaseFoldingDjbHash(Buffer, H))
740b57cec5SDimitry Andric     return *Result;
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   std::array<UTF8, UNI_MAX_UTF8_BYTES_PER_CODE_POINT> Storage;
770b57cec5SDimitry Andric   while (!Buffer.empty()) {
780b57cec5SDimitry Andric     UTF32 C = foldCharDwarf(chopOneUTF32(Buffer));
790b57cec5SDimitry Andric     StringRef Folded = toUTF8(C, Storage);
800b57cec5SDimitry Andric     H = djbHash(Folded, H);
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric   return H;
830b57cec5SDimitry Andric }
84