NSVD Reader  0.0.1
bitRange.hpp
Go to the documentation of this file.
1 
5 /*
6  * These codes are licensed under CC0.
7  * http://creativecommons.org/publicdomain/zero/1.0/
8  */
9 #ifndef __NODAMUSHI_SVD_BITRANGE_HPP__
10 #define __NODAMUSHI_SVD_BITRANGE_HPP__
11 
12 # include <cstdint>
13 # include <ostream>
15 namespace nodamushi{
16 namespace svd{
23 struct bitRange
24 {
26  uint32_t lsb;
28  uint32_t msb;
29 
30  //-----------------------------------------------
31  bitRange():lsb(0),msb(0){}
32  bitRange(uint32_t l,uint32_t m):lsb(l<m?l:m),msb(l<m?m:l){}
33 
34  bool operator==(const bitRange& b)const noexcept{
35  return lsb == b.lsb && msb == b.msb;
36  }
37  bool operator!=(const bitRange& b)const noexcept{
38  return lsb != b.lsb || msb != b.msb;
39  }
40 
46  template<typename STR> bool set(const STR& src)
47  {
48  // decode [<msb>:<lsb>]
49  size_t l = src.length();
50  if(l <5||src[0] !='[' ||src[l-1] !=']'){return false;}
51 
52  size_t msb_b=1,msb_e,colon,lsb_b,lsb_e;
53  while(is_space(src[msb_b]))
54  msb_b++;
55 
56  msb_e = msb_b;
57  while(!is_space(src[msb_e]) && src[msb_e] != ':' )
58  msb_e++;
59  colon = msb_e;
60  while(src[colon] != ':' )
61  colon++;
62  lsb_b = colon + 1;
63  while(is_space(src[lsb_b]))
64  lsb_b++;
65  lsb_e = lsb_b;
66  while(!is_space(src[lsb_e]) && src[lsb_e] != ']' )
67  lsb_e++;
68  uint32_t msb=0;
69  uint32_t lsb=0;
70  const char* ptr = src.data();
71  if(!to_int(ptr + msb_b, ptr + msb_e,msb)){
72  return false;
73  }
74  if(!to_int(ptr + lsb_b, ptr + lsb_e,lsb)){
75  return false;
76  }
77  this->lsb = lsb;
78  this->msb = msb;
79  return true;
80  }
81 
82 
83  private:
84  static constexpr bool is_space(char c)
85  {
86  return ' ' == c || '\t' == c || '\r' == c || '\n' == c;
87  }
88  static constexpr bool to_int(const char* begin,const char* const end,uint32_t& i)
89  {
90  while(begin!=end)
91  {
92  char c = *begin;
93  ++begin;
94  if('0' <= c && c <='9')
95  i = i * 10 + (c -'0');
96  else
97  return false;
98  }
99  return true;
100  }
101 
102 };
103 
104 
105 template<>struct value_setter_helper<bitRange>
106 {
107  static constexpr bool value=true;
108 
109  static bool set(const std::string& src,bitRange& dst){ return dst.set(src); }
110 # if __cplusplus >= 201703
111  static bool set(std::string_view src,bitRange& dst){return dst.set(src);}
112 # endif
113 
114 };
115 
116 
117 
118 
119 }}// end namespace nodamushi::svd
120 
121 
122 namespace std{
123 ostream& operator<<(ostream& o,const nodamushi::svd::bitRange& r)
124 {
125  o << '['<<r.msb <<':' <<r.lsb<<']';
126  return o;
127 }
128 } // end namespace std
129 
130 #endif // __NODAMUSHI_SVD_BITRANGE_HPP__
ostream & operator<<(std::ostream &os, const ::nodamushi::svd::Access &value)
Definition: Access.hpp:144
<field>.<bitRange> element
Definition: bitRange.hpp:23
uint32_t msb
msb: the bit position of the most significant bit within the register.
Definition: bitRange.hpp:28
bool operator==(const bitRange &b) const noexcept
Definition: bitRange.hpp:34
Definition: Access.hpp:143
bool operator!=(const bitRange &b) const noexcept
Definition: bitRange.hpp:37
uint32_t lsb
lsb: the bit position of the least significant bit within the register.
Definition: bitRange.hpp:26
static bool set(const std::string &src, bitRange &dst)
Definition: bitRange.hpp:109
This class reperesents SVD(xml) element / attribute.
Definition: value.hpp:53
bool set(const STR &src)
Definition: bitRange.hpp:46
bitRange(uint32_t l, uint32_t m)
Definition: bitRange.hpp:32