• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PANDA_LIBPANDABASE_UTILS_BIT_MEMORY_REGION_INL_H_
17 #define PANDA_LIBPANDABASE_UTILS_BIT_MEMORY_REGION_INL_H_
18 
19 #include "bit_memory_region.h"
20 
21 #include <iomanip>
22 
23 namespace panda {
24 
25 template <typename Base>
Dump(std::ostream & os)26 void BitMemoryRegion<Base>::Dump(std::ostream &os) const
27 {
28     static constexpr size_t BITS_PER_HEX_DIGIT = 4;
29     os << "0x";
30     static constexpr size_t BITS_PER_WORD = sizeof(size_t) * BITS_PER_BYTE;
31     if (Size() >= BITS_PER_WORD) {
32         bool is_zero = true;
33         size_t width = BITS_PER_WORD - (BITS_PER_HEX_DIGIT - Size() % BITS_PER_HEX_DIGIT);
34         for (ssize_t i = Size() - width; i >= 0; i -= width) {
35             auto val = Read(i, width);
36             if (val != 0 || !is_zero) {
37                 // CODECHECK-NOLINTNEXTLINE(C_RULE_ID_FUNCTION_NESTING_LEVEL)
38                 if (!is_zero) {
39                     os << std::setw(static_cast<int>(width / BITS_PER_HEX_DIGIT)) << std::setfill('0');
40                 }
41                 os << std::hex << val;
42                 is_zero = false;
43             }
44             if (i == 0) {
45                 break;
46             }
47             width = std::min<size_t>(i, BITS_PER_WORD);
48         }
49         if (is_zero) {
50             os << '0';
51         }
52     } else {
53         os << std::hex << ReadAll();
54     }
55     os << std::dec;
56 }
57 
58 template <typename T>
59 inline std::ostream &operator<<(std::ostream &os, const BitMemoryRegion<T> &region)
60 {
61     region.Dump(os);
62     return os;
63 }
64 
65 }  // namespace panda
66 
67 #endif  // PANDA_LIBPANDABASE_UTILS_BIT_MEMORY_REGION_INL_H_
68