Debian Codegraph @

inc/tag.hpp

raw symbols SCIP
1 /* Copyright 2024 Maxim Iorsh <[email protected]>
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 * Redistributions of source code must retain the above copyright notice, this
6 * list of conditions and the following disclaimer.
7 * Redistributions in binary form must reproduce the above copyright notice,
8 * this list of conditions and the following disclaimer in the documentation
9 * and/or other materials provided with the distribution.
10 * The name of the author may not be used to endorse or promote products
11 * derived from this software without specific prior written permission.
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
15 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
16 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
18 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
19 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
20 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
21 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 #pragma once
24
25 #include <array>
26 #include <cstdint>
27 #include <cstring>
28 #include <string>
29
30 namespace ff {
31
32 class Tag {
33 public:
34 Tag(uint32_t val)
35 : arr{(char)((val >> 24) & 0xff), (char)((val >> 16) & 0xff),
36 (char)((val >> 8) & 0xff), (char)(val & 0xff), '\0'} {}
37
38 Tag(const char* val) : arr{val[0], val[1], val[2], val[3], '\0'} {}
39
40 explicit operator const char*() const { return arr.data(); }
41 operator uint32_t() const {
42 return (arr[0] << 24) | (arr[1] << 16) | (arr[2] << 8) | arr[3];
43 }
44
45 bool operator==(const char* rhs) const {
46 for (int i = 0; i < 5; ++i) {
47 // We shall usually continue here.
48 if (arr[i] == rhs[i]) continue;
49
50 // If tags differ, it's either a real difference or a space vs zero
51 // terminator.
52 return ((arr[i] == ' ') && (rhs[i] == '\0'));
53 }
54 return true;
55 }
56
57 bool operator==(const std::string& rhs) const {
58 return operator==(rhs.c_str());
59 }
60
61 private:
62 // OpenType tags normally have four characters, the fifth is the trailing
63 // zero.
64 std::array<char, 5> arr;
65 };
66
67 const Tag REQUIRED_FEATURE = Tag(" RQD");
68
69 } // namespace ff