1 | #include <IO/WriteHelpers.h> |
2 | #include <inttypes.h> |
3 | #include <Common/hex.h> |
4 | |
5 | |
6 | namespace DB |
7 | { |
8 | |
9 | template <typename IteratorSrc, typename IteratorDst> |
10 | void formatHex(IteratorSrc src, IteratorDst dst, const size_t num_bytes) |
11 | { |
12 | size_t src_pos = 0; |
13 | size_t dst_pos = 0; |
14 | for (; src_pos < num_bytes; ++src_pos) |
15 | { |
16 | writeHexByteLowercase(src[src_pos], &dst[dst_pos]); |
17 | dst_pos += 2; |
18 | } |
19 | } |
20 | |
21 | void formatUUID(const UInt8 * src16, UInt8 * dst36) |
22 | { |
23 | formatHex(&src16[0], &dst36[0], 4); |
24 | dst36[8] = '-'; |
25 | formatHex(&src16[4], &dst36[9], 2); |
26 | dst36[13] = '-'; |
27 | formatHex(&src16[6], &dst36[14], 2); |
28 | dst36[18] = '-'; |
29 | formatHex(&src16[8], &dst36[19], 2); |
30 | dst36[23] = '-'; |
31 | formatHex(&src16[10], &dst36[24], 6); |
32 | } |
33 | |
34 | /** Function used when byte ordering is important when parsing uuid |
35 | * ex: When we create an UUID type |
36 | */ |
37 | void formatUUID(std::reverse_iterator<const UInt8 *> src16, UInt8 * dst36) |
38 | { |
39 | formatHex(src16 + 8, &dst36[0], 4); |
40 | dst36[8] = '-'; |
41 | formatHex(src16 + 12, &dst36[9], 2); |
42 | dst36[13] = '-'; |
43 | formatHex(src16 + 14, &dst36[14], 2); |
44 | dst36[18] = '-'; |
45 | formatHex(src16, &dst36[19], 2); |
46 | dst36[23] = '-'; |
47 | formatHex(src16 + 2, &dst36[24], 6); |
48 | } |
49 | |
50 | |
51 | |
52 | void writeException(const Exception & e, WriteBuffer & buf, bool with_stack_trace) |
53 | { |
54 | writeBinary(e.code(), buf); |
55 | writeBinary(String(e.name()), buf); |
56 | writeBinary(e.displayText(), buf); |
57 | |
58 | if (with_stack_trace) |
59 | writeBinary(e.getStackTrace().toString(), buf); |
60 | else |
61 | writeBinary(String(), buf); |
62 | |
63 | bool has_nested = e.nested() != nullptr; |
64 | writeBinary(has_nested, buf); |
65 | |
66 | if (has_nested) |
67 | writeException(Exception(Exception::CreateFromPoco, *e.nested()), buf, with_stack_trace); |
68 | } |
69 | } |
70 | |