MiKTeX 26.5-next
A scalable TeX distribution
Loading...
Searching...
No Matches
hash_icase.h
1/* miktex/Core/hash_icase: -*- C++ -*-
2
3 Copyright (C) 1996-2018 Christian Schenk
4
5 This file is part of the MiKTeX Core Library.
6
7 The MiKTeX Core Library is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2, or
10 (at your option) any later version.
11
12 The MiKTeX Core Library is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with the MiKTeX Core Library; if not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22#pragma once
23
24#if !defined(BE4560F0052846B09E80BD56810BD934)
25#define BE4560F0052846B09E80BD56810BD934
26
27#include <miktex/Core/config.h>
28
29#include <cstddef>
30
31#include <string>
32
33#include "Debug.h"
34
35MIKTEX_CORE_BEGIN_NAMESPACE;
36
38{
39public:
40 std::size_t operator() (const std::string& str) const
41 {
42 // see http://www.isthe.com/chongo/tech/comp/fnv/index.html
43#if defined(_M_AMD64) || defined(_M_X64) || defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(__aarch64__)
44 MIKTEX_ASSERT(sizeof(std::size_t) == 8);
45 const std::size_t FNV_prime = 1099511628211ULL;
46 const std::size_t offset_basis = 14695981039346656037ULL;
47#else
48 MIKTEX_ASSERT(sizeof(std::size_t) == 4);
49 const std::size_t FNV_prime = 16777619;
50 const std::size_t offset_basis = 2166136261;
51#endif
52 std::size_t hash = offset_basis;
53 for (char ch : str)
54 {
55 if (static_cast<unsigned>(ch) >= 128)
56 {
57 // ignore UTF-8 chars
58 continue;
59 }
60 else if (ch >= 'a' && ch <= 'z')
61 {
62 ch = 'A' + (ch - 'a');
63 }
64 hash ^= (std::size_t)ch;
65 hash *= FNV_prime;
66 }
67 return hash;
68 }
69};
70
71MIKTEX_CORE_END_NAMESPACE;
72
73#endif
Definition hash_icase.h:38