summaryrefslogtreecommitdiff
path: root/Build/source/texk/lcdf-typetools/lcdf-typetools-2.98/include/lcdf/permstr.hh
blob: 684878ed464b1547bf63f8fd70c0d5fb85948de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// -*- related-file-name: "../../liblcdf/permstr.cc" -*-
#ifndef LCDF_PERMSTR_HH
#define LCDF_PERMSTR_HH
#include <assert.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <lcdf/inttypes.h>
#include <lcdf/hashcode.hh>
class PermString;
inline bool operator==(PermString a, PermString b);
inline bool operator!=(PermString a, PermString b);

class PermString { struct Doodad; public:

    typedef Doodad *Capsule;
    // Declare a PermString::Initializer in any file in which you declare
    // static global PermStrings.
    struct Initializer { Initializer(); };

    PermString()
	: _rep(zero_char_doodad.data) {
    }
    explicit PermString(char c)
	: _rep(one_char_doodad[(unsigned char) c].data) {
    }
    inline PermString(const char *s);
    inline PermString(const char *s, int len);
    inline PermString(const char *begin, const char *end);

    typedef int (PermString::*unspecified_bool_type)() const;
    inline operator unspecified_bool_type() const;
    inline bool operator!() const;

    inline int length() const;
    char operator[](int i) const;

    inline const char *c_str() const {
	return _rep;
    }
    inline char operator*() const {
	return *_rep;
    }

    inline const char *begin() const;
    inline const char *end() const;

    inline bool equals(const char *s, int len) const;
    friend inline bool operator==(PermString a, PermString b);
    friend inline bool operator!=(PermString a, PermString b);

    inline Capsule capsule() const;
    inline static PermString decapsule(Capsule c);

    friend PermString permprintf(const char*, ...);
    friend PermString vpermprintf(const char*, va_list);
    friend PermString permcat(PermString, PermString);
    friend PermString permcat(PermString, PermString, PermString);

  private:

    struct Doodad {
	Doodad *next;
	int length;
	char data[2];
    };

    const char *_rep;

    PermString(Doodad* d)		: _rep(d->data) { }
    void initialize(const char*, int);
    Doodad* doodad() const { return (Doodad*)(_rep - offsetof(Doodad, data)); }

    friend struct PermString::Initializer;
    static void static_initialize();

    enum { NHASH = 1024 };	// must be power of 2
    static Doodad zero_char_doodad, one_char_doodad[256], *buckets[NHASH];

};


inline PermString::PermString(const char* s)
{
    initialize(s, -1);
}

inline PermString::PermString(const char* s, int len)
{
    initialize(s, len);
}

inline PermString::PermString(const char* begin, const char* end)
{
    assert(end);
    initialize(begin, end > begin ? end - begin : 0);
}

inline PermString::operator unspecified_bool_type() const
{
    return _rep != zero_char_doodad.data ? &PermString::length : 0;
}

inline bool PermString::operator!() const
{
    return _rep == zero_char_doodad.data;
}

inline int PermString::length() const
{
    return doodad()->length;
}

inline const char *PermString::begin() const
{
    return _rep;
}

inline const char *PermString::end() const
{
    return _rep + doodad()->length;
}

inline char PermString::operator[](int i) const
{
    assert((unsigned) i < (unsigned) length());
    return c_str()[i];
}

inline bool operator==(PermString a, PermString b)
{
    return a._rep == b._rep;
}

inline bool operator==(PermString a, const char *b)
{
    return (!a || !b ? !a && !b : strcmp(a.c_str(), b) == 0);
}

inline bool operator==(const char *a, PermString b)
{
    return b == a;
}

inline bool operator!=(PermString a, PermString b)
{
    return a._rep != b._rep;
}

inline bool operator!=(PermString a, const char *b)
{
    return !(a == b);
}

inline bool operator!=(const char *a, PermString b)
{
    return !(b == a);
}

inline bool operator<(PermString a, PermString b)
{
    // NOT lexicographic ordering!
    return a.begin() < b.begin();
}

inline bool PermString::equals(const char *s, int len) const
{
    if (len > 0)
	return len == length() && memcmp(s, _rep, len) == 0;
    else
	return strcmp(s, _rep) == 0;
}

inline PermString::Capsule PermString::capsule() const
{
    return doodad();
}

inline PermString PermString::decapsule(Capsule c)
{
    return PermString(c);
}

inline hashcode_t hashcode(PermString s)
{
    return (uintptr_t) s.c_str();
}

PermString permprintf(const char *format, ...);
PermString vpermprintf(const char *format, va_list val);

PermString permcat(PermString a, PermString b);

#endif