192ac9d3eSJonas Devlieghere //===-- Support/DJB.cpp ---DJB Hash -----------------------------*- C++ -*-===//
292ac9d3eSJonas Devlieghere //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
692ac9d3eSJonas Devlieghere //
792ac9d3eSJonas Devlieghere //===----------------------------------------------------------------------===//
892ac9d3eSJonas Devlieghere //
992ac9d3eSJonas Devlieghere // This file contains support for the DJ Bernstein hash function.
1092ac9d3eSJonas Devlieghere //
1192ac9d3eSJonas Devlieghere //===----------------------------------------------------------------------===//
1292ac9d3eSJonas Devlieghere
1392ac9d3eSJonas Devlieghere #include "llvm/Support/DJB.h"
143b17b84bSPavel Labath #include "llvm/ADT/ArrayRef.h"
153b17b84bSPavel Labath #include "llvm/Support/Compiler.h"
163b17b84bSPavel Labath #include "llvm/Support/ConvertUTF.h"
173b17b84bSPavel Labath #include "llvm/Support/Unicode.h"
183b17b84bSPavel Labath
193b17b84bSPavel Labath using namespace llvm;
203b17b84bSPavel Labath
chopOneUTF32(StringRef & Buffer)213b17b84bSPavel Labath static UTF32 chopOneUTF32(StringRef &Buffer) {
223b17b84bSPavel Labath UTF32 C;
233b17b84bSPavel Labath const UTF8 *const Begin8Const =
243b17b84bSPavel Labath reinterpret_cast<const UTF8 *>(Buffer.begin());
253b17b84bSPavel Labath const UTF8 *Begin8 = Begin8Const;
263b17b84bSPavel Labath UTF32 *Begin32 = &C;
273b17b84bSPavel Labath
283b17b84bSPavel Labath // In lenient mode we will always end up with a "reasonable" value in C for
293b17b84bSPavel Labath // non-empty input.
303b17b84bSPavel Labath assert(!Buffer.empty());
313b17b84bSPavel Labath ConvertUTF8toUTF32(&Begin8, reinterpret_cast<const UTF8 *>(Buffer.end()),
323b17b84bSPavel Labath &Begin32, &C + 1, lenientConversion);
333b17b84bSPavel Labath Buffer = Buffer.drop_front(Begin8 - Begin8Const);
343b17b84bSPavel Labath return C;
353b17b84bSPavel Labath }
363b17b84bSPavel Labath
toUTF8(UTF32 C,MutableArrayRef<UTF8> Storage)373b17b84bSPavel Labath static StringRef toUTF8(UTF32 C, MutableArrayRef<UTF8> Storage) {
383b17b84bSPavel Labath const UTF32 *Begin32 = &C;
393b17b84bSPavel Labath UTF8 *Begin8 = Storage.begin();
403b17b84bSPavel Labath
413b17b84bSPavel Labath // The case-folded output should always be a valid unicode character, so use
423b17b84bSPavel Labath // strict mode here.
433b17b84bSPavel Labath ConversionResult CR = ConvertUTF32toUTF8(&Begin32, &C + 1, &Begin8,
443b17b84bSPavel Labath Storage.end(), strictConversion);
453b17b84bSPavel Labath assert(CR == conversionOK && "Case folding produced invalid char?");
463b17b84bSPavel Labath (void)CR;
473b17b84bSPavel Labath return StringRef(reinterpret_cast<char *>(Storage.begin()),
483b17b84bSPavel Labath Begin8 - Storage.begin());
493b17b84bSPavel Labath }
503b17b84bSPavel Labath
foldCharDwarf(UTF32 C)513b17b84bSPavel Labath static UTF32 foldCharDwarf(UTF32 C) {
523b17b84bSPavel Labath // DWARF v5 addition to the unicode folding rules.
533b17b84bSPavel Labath // Fold "Latin Small Letter Dotless I" and "Latin Capital Letter I With Dot
543b17b84bSPavel Labath // Above" into "i".
553b17b84bSPavel Labath if (C == 0x130 || C == 0x131)
563b17b84bSPavel Labath return 'i';
573b17b84bSPavel Labath return sys::unicode::foldCharSimple(C);
583b17b84bSPavel Labath }
593b17b84bSPavel Labath
fastCaseFoldingDjbHash(StringRef Buffer,uint32_t H)60*b1df3a2cSFangrui Song static std::optional<uint32_t> fastCaseFoldingDjbHash(StringRef Buffer,
61*b1df3a2cSFangrui Song uint32_t H) {
62795c00b2SFangrui Song bool AllASCII = true;
6350dcd8bfSFangrui Song for (unsigned char C : Buffer) {
6450dcd8bfSFangrui Song H = H * 33 + ('A' <= C && C <= 'Z' ? C - 'A' + 'a' : C);
65795c00b2SFangrui Song AllASCII &= C <= 0x7f;
6650dcd8bfSFangrui Song }
67795c00b2SFangrui Song if (AllASCII)
6850dcd8bfSFangrui Song return H;
69aadaafacSKazu Hirata return std::nullopt;
703b17b84bSPavel Labath }
713b17b84bSPavel Labath
caseFoldingDjbHash(StringRef Buffer,uint32_t H)723b17b84bSPavel Labath uint32_t llvm::caseFoldingDjbHash(StringRef Buffer, uint32_t H) {
73*b1df3a2cSFangrui Song if (std::optional<uint32_t> Result = fastCaseFoldingDjbHash(Buffer, H))
7450dcd8bfSFangrui Song return *Result;
7550dcd8bfSFangrui Song
7650dcd8bfSFangrui Song std::array<UTF8, UNI_MAX_UTF8_BYTES_PER_CODE_POINT> Storage;
773b17b84bSPavel Labath while (!Buffer.empty()) {
7850dcd8bfSFangrui Song UTF32 C = foldCharDwarf(chopOneUTF32(Buffer));
7950dcd8bfSFangrui Song StringRef Folded = toUTF8(C, Storage);
8050dcd8bfSFangrui Song H = djbHash(Folded, H);
813b17b84bSPavel Labath }
8292ac9d3eSJonas Devlieghere return H;
8392ac9d3eSJonas Devlieghere }
84