acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
hex.h
1#pragma once
2
3
4
5
6
7CLASS_DECL_ACME bool ishexdigit(::i8 ch);
8
9
10namespace hex
11{
12
13
14 CLASS_DECL_ACME string to_asc(const ::scoped_string & scopedstrHex);
15 CLASS_DECL_ACME string from_asc(const ::scoped_string & scopedstrAsc);
16
17
18 CLASS_DECL_ACME ::i64 to_i64(const ::scoped_string & scopedstr);
19
20 CLASS_DECL_ACME ::u64 to_u64(const ::scoped_string & scopedstr);
21
22 CLASS_DECL_ACME ::u32 to_u32(const ::scoped_string & scopedstr);
23
24 inline CLASS_DECL_ACME ::i32 to_nibble(::i8 ch)
25 {
26
27 return (ch >= '0' && ch <= '9') ? ch - '0' :
28 (
29 (ch >= 'a' && ch <= 'f') ? ch - 'a' + 10 :
30 (
31 (ch >= 'A' && ch <= 'F') ? ch - 'A' + 10 :
32 -1
33 )
34 );
35
36 }
37
38
39 constexpr CLASS_DECL_ACME ::i8 nibble_lower_case_from(::u8 b)
40 {
41
42 if(b >= 10)
43 {
44 return b + 'a' - 10;
45 }
46 else
47 {
48 return b + '0';
49 }
50
51 }
52
53
54 constexpr CLASS_DECL_ACME ::i8 nibble_upper_case_from(::u8 b)
55 {
56
57 if(b >= 10)
58 {
59 return b + 'A' - 10;
60 }
61 else
62 {
63 return b + '0';
64 }
65
66 }
67
68
69 constexpr CLASS_DECL_ACME void lower_case_from(char_pointer sz, ::u8 b)
70 {
71
72 sz[0] = nibble_lower_case_from((b >> 4) & (::i8) 0x0f);
73 sz[1] = nibble_lower_case_from(b & (::i8) 0x0f);
74
75 }
76
77
78 constexpr CLASS_DECL_ACME void upper_case_from(char_pointer sz, ::u8 b)
79 {
80
81 sz[0] = nibble_upper_case_from((b >> 4) & (::i8) 0x0f);
82 sz[1] = nibble_upper_case_from(b & (::i8) 0x0f);
83
84 }
85
86
87 // sz buffer should have twice i32_size of s (p)
88 constexpr CLASS_DECL_ACME void lower_case_from(char_pointer sz, const void * p, memsize s)
89 {
90 const ::u8 * pb = (const ::u8 *) p;
91 while(s)
92 {
93 lower_case_from(sz, *pb);
94 s--;
95 sz+=2;
96 pb++;
97 }
98 }
99
100
101 constexpr CLASS_DECL_ACME void upper_case_from(char_pointer sz, const void * p, memsize s)
102 {
103 const ::u8 * pb = (const ::u8 *) p;
104 while(s)
105 {
106 upper_case_from(sz, *pb);
107 s--;
108 sz+=2;
109 pb++;
110 }
111 }
112
113
114 // sz buffer should have twice i32_size of s (p)
115 inline string lower_case_from(const void * p, memsize s)
116 {
117 string str;
118 lower_case_from(str.get_buffer(s * 2), p, s);
119 str.release_buffer(s * 2);
120 return str;
121 }
122
123
124 inline string upper_case_from(const void * p, memsize s)
125 {
126 string str;
127 upper_case_from(str.get_buffer(s * 2), p, s);
128 str.release_buffer(s * 2);
129 return str;
130 }
131
132
133 template < prototype_integral INTEGRAL >
134 constexpr auto upper_case_from(INTEGRAL i)
135 {
136
137 ::inline_number_string numberstring;
138
139 __tosz(i, numberstring.m_end, 16, e_digit_case_upper);
140
141 return numberstring;
142
143 }
144
145
146 template < prototype_integral INTEGRAL >
147 constexpr auto lower_case_from(INTEGRAL i)
148 {
149
150 ::inline_number_string numberstring;
151
152 __tosz(i, numberstring.m_end, 16, e_digit_case_lower);
153
154 return numberstring;
155
156 }
157
158
159 template < character_count s_iWidth >
160 inline auto padded_from(::u64 u, enum_digit_case edigitcase)
161 {
162
163 ::inline_string < ::i8, s_iWidth > numberstring;
164
165 if (u != 0)
166 {
167
168 __rear_tosz(u, numberstring.m_end, 16, edigitcase);
169
170 }
171
172 while (numberstring.size() < s_iWidth)
173 {
174
175 *((char_pointer &)numberstring.m_end)++ = '0';
176
177 }
178
179 reverse(((char_pointer )numberstring.m_begin), ((char_pointer )(numberstring.m_end)));
180
181 *((char_pointer &)numberstring.m_end + 1) = '\0';
182
183 return numberstring;
184
185 }
186
187
188 template < character_count s_iWidth >
189 inline auto lower_case_padded_from(::u64 u)
190 {
191
192 return padded_from < s_iWidth >(u, e_digit_case_lower);
193
194 }
195
196
197 template < character_count s_iWidth >
198 inline auto upper_case_padded_from(::u64 u)
199 {
200
201 return padded_from < s_iWidth >(u, e_digit_case_upper);
202
203 }
204
205
206 inline void to(::u32 & u,const ::scoped_string & scopedstr) { u = to_u32(scopedstr); }
207 inline void to(::u64 & u,const ::scoped_string & scopedstr) { u = to_u64(scopedstr); }
208#ifdef __APPLE__
209 inline void to(ulong & u,const ::scoped_string & scopedstr) { u = to_u64(scopedstr); }
210#endif
211 inline uptr to_uptr(const ::scoped_string & scopedstr) { uptr u; to(u,scopedstr); return u; }
212
213 ::u16 parse_u16_exc(::const_ansi_range & range);
214
215
216} // namespace hex
217
218
219
220
221inline bool ishexdigit(::i8 ch)
222{
223 return (ch >= '0' && ch <= '9')
224 || (ch >= 'a' && ch <= 'f')
225 || (ch >= 'A' && ch <= 'F');
226 // return true;
227 //return false;
228}
229
230
231namespace hex
232{
233
234
235 CLASS_DECL_ACME string lower_case_from(const block & block);
236
237 CLASS_DECL_ACME string upper_case_from(const block & block);
238
239
240} // namespace hex
241