• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

madeso / euphoria / 13542137194

26 Feb 2025 10:46AM UTC coverage: 68.68% (+6.3%) from 62.42%
13542137194

push

github

madeso
Move more test status from build folder to root

Build folder is to be considered temporary

671 of 977 relevant lines covered (68.68%)

56.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.0
/libs/base/src/base/hash.string.h
1
#pragma once
2

3
#include "base/ints.h"
4

5
#include <string_view>
6
#include <string>
7
#include <unordered_map>
8

9
// 1 = include text in hash, 0=don't
10
// todo(Gustav): move to a config instead
11
#define USE_HASH_TEXT 1
12

13
namespace eu
14
{
15

16
/// fnv-1a hash.
17
/// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
18
constexpr u64 hash64(const std::string_view str, u64 hash = 0xcbf29ce484222325)
160✔
19
    { return str.empty() ? hash : hash64(str.substr(1), (hash ^ str[0]) * 0x100000001b3); }
160✔
20

21
/// Hash of a non-owning string.
22
/// It will be (pretty) printed as the original string if that is enabled.
23
/// Treat it as a regular `std::string_view` but without the string functionality.
24
/// @see \ref hash64
25
/// @see \ref HshO
26
struct Hsh
27
{
28
    /// The actual hash.
29
    /// Prefer to use operators on \ref Hsh instead of accessing this directly.
30
    u64 hash;
31

32
    #if USE_HASH_TEXT == 1
33
    /// Non-owning storage of the printable string, if enabled.
34
    /// Please ignore if possible and just print as normal.
35
    std::string_view text;
36
    #endif
37

38
    /// Creates a new hash object and computes the hash at compile time if possible.
39
    constexpr Hsh(const std::string_view s)
40
        : hash( hash64(s) )
41
        #if USE_HASH_TEXT == 1
42
        , text(s)
43
        #endif
44
          { }
45
};
46

47
/// Hash of an owning string.
48
/// It will be (pretty) printed as the original string if that is enabled.
49
/// Treat it as a regular `std::string` but without the string functionality.
50
/// @see \ref hash64
51
/// @see \ref Hsh
52
struct HshO
53
{
54
    /// The actual hash.
55
    /// Prefer to use operators on \ref HshO instead of accessing this directly.
56
    u64 hash;
57

58
#if USE_HASH_TEXT == 1
59
    /// Owning storage of the printable string, if enabled.
60
    /// Please ignore if possible and just print as normal.
61
    std::string text;
62
#endif
63

64
    /// Creates a new hash object and computes the hash at compile time if possible.
65
    constexpr HshO(const std::string& s)
2✔
66
        : hash(hash64(s))
2✔
67
#if USE_HASH_TEXT == 1
68
        , text(s)
2✔
69
#endif
70
    { }
2✔
71

72
    /// Creates a new hash object and computes the hash at compile time if possible.
73
    constexpr HshO(const std::string_view& s)
22✔
74
        : hash(hash64(s))
22✔
75
#if USE_HASH_TEXT == 1
76
        , text(s)
44✔
77
#endif
78
    { }
22✔
79

80
    /// Copy data from a non-owning hash.
81
    constexpr HshO(const Hsh& o)
66✔
82
        : hash(o.hash)
66✔
83
#if USE_HASH_TEXT == 1
84
        , text(o.text)
132✔
85
#endif
86
    { }
66✔
87
};
88

89
#define OP(op) \
90
    constexpr bool operator op(const Hsh& lhs, const Hsh& rhs)\
91
        { return lhs.hash op rhs.hash; }\
92
    constexpr bool operator op(const HshO& lhs, const Hsh& rhs)\
93
        { return lhs.hash op rhs.hash; }\
94
    constexpr bool operator op(const Hsh& lhs, const HshO& rhs)\
95
        { return lhs.hash op rhs.hash; }\
96
    constexpr bool operator op(const HshO& lhs, const HshO& rhs)\
97
        { return lhs.hash op rhs.hash; }
98
    OP(==) OP(!=) OP(<) OP(>) OP(<=) OP(>=)
156✔
99
#undef OP
100

101

102
template<typename TStream>
103
TStream& operator<<(TStream& s, const Hsh& hash)
×
104
{
105
    #if USE_HASH_TEXT == 1
106
    s << hash.text;
×
107
    #else
108
    s << hash.hash;
109
    #endif
110

111
    return s;
×
112
}
113

114
template<typename TStream>
115
TStream& operator<<(TStream& s, const HshO& hash)
×
116
{
117
#if USE_HASH_TEXT == 1
118
    s << hash.text;
×
119
#else
120
    s << hash.hash;
121
#endif
122

123
    return s;
×
124
}
125

126

127
}
128

129
namespace std
130
{
131

132
/// \private
133
template <> struct hash<eu::Hsh>
134
{
135
    std::size_t operator()(const eu::Hsh& x) const
8✔
136
        { return x.hash; }
8✔
137
};
138

139
/// \private
140
template <> struct hash<eu::HshO>
141
{
142
    std::size_t operator()(const eu::HshO& x) const
40✔
143
        { return x.hash; }
40✔
144
};
145

146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc