Debian Codegraph @

bcftools/hex.h

raw symbols SCIP
1 // VariantKey
2 //
3 // hex.h
4 //
5 // @category Libraries
6 // @author Nicola Asuni <[email protected]>
7 // @link https://github.com/tecnickcom/variantkey
8 // @license MIT [LICENSE](https://raw.githubusercontent.com/tecnickcom/variantkey/main/LICENSE)
9 // @copyright 2017-2018 GENOMICS plc
10 //
11 // LICENSE
12 //
13 // Copyright (c) 2017-2018 GENOMICS plc
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining a copy
16 // of this software and associated documentation files (the "Software"), to deal
17 // in the Software without restriction, including without limitation the rights
18 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 // copies of the Software, and to permit persons to whom the Software is
20 // furnished to do so, subject to the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be included in
23 // all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 // THE SOFTWARE.
32
33 /**
34 * @file hex.h
35 * @brief Utility functions to manipulate strings.
36 *
37 * Collection of utility functions to manipulate strings.
38 */
39
40 #ifndef ASTRING_H
41 #define ASTRING_H
42
43 #include <inttypes.h>
44 #include <stdio.h>
45
46 /** @brief Returns uint64_t hexadecimal string (16 characters).
47 *
48 * @param n Number to parse
49 * @param str String buffer to be returned (it must be sized 17 bytes at least).
50 *
51 * @return Upon successful return, these function returns the number of characters processed
52 * (excluding the null byte used to end output to strings).
53 * If the buffer size is not sufficient, then the return value is the number of characters required for
54 * buffer string, including the terminating null byte.
55 */
56 static inline size_t hex_uint64_t(uint64_t n, char *str)
57 {
58 return sprintf(str, "%016" PRIx64, n);
59 }
60
61 /** @brief Parses a 16 chars hexadecimal string and returns the code.
62 *
63 * @param s Hexadecimal string to parse (it must contain 16 hexadecimal characters).
64 *
65 * @return uint64_t unsigned integer number.
66 */
67 static inline uint64_t parse_hex_uint64_t(const char *s)
68 {
69 uint64_t v = 0;
70 uint8_t b;
71 size_t i;
72 for (i = 0; i < 16; i++)
73 {
74 b = s[i];
75 if (b >= 'a')
76 {
77 b -= ('a' - 10); // a-f
78 }
79 else
80 {
81 if (b >= 'A')
82 {
83 b -= ('A' - 10); // A-F
84 }
85 else
86 {
87 b -= '0'; // 0-9
88 }
89 }
90 v = ((v << 4) | b);
91 }
92 return v;
93 }
94
95 #endif // ASTRING_H