PRCYCoin  2.0.0.7rc1
P2P Digital Currency
common.hpp
Go to the documentation of this file.
1 #ifndef __ZXCVBN__COMMON_HPP
2 #define __ZXCVBN__COMMON_HPP
3 
4 #include <zxcvbn/zxcvbn.h>
7 
8 #include <regex>
9 #include <string>
10 
11 #include <cassert>
12 
13 namespace zxcvbn {
14 
16 using guesses_log10_t = int;
17 using score_t = unsigned;
18 using idx_t = std::string::size_type;
19 
20 // Add new match types here
21 #define MATCH_RUN() \
22  MATCH_FN(Dictionary, DICTIONARY, dictionary) \
23  MATCH_FN(Spatial, SPATIAL, spatial) \
24  MATCH_FN(Repeat, REPEAT, repeat) \
25  MATCH_FN(Sequence, SEQUENCE, sequence) \
26  MATCH_FN(Regex, REGEX, regex) \
27  MATCH_FN(Date, DATE, date) \
28  MATCH_FN(Bruteforce, BRUTEFORCE, bruteforce) \
29  MATCH_FN(Unknown, UNKNOWN, unknown)
30 
31 enum class RegexTag {
35 };
36 
37 enum class SequenceTag {
38  UPPER,
39  LOWER,
40  DIGITS,
41  UNICODE,
42 };
43 
45  std::vector<std::string> matches;
46  std::size_t index;
47 
48  explicit
49  PortableRegexMatch(const std::smatch & b) {
50  std::copy(b.begin(), b.end(), std::back_inserter(matches));
51  index = b.position();
52  }
53 
54  PortableRegexMatch(std::vector<std::string> matches_,
55  std::size_t index_)
56  : matches(std::move(matches_))
57  , index(index_)
58  {}
59 };
60 
61 #define MATCH_FN(_, e, __) e,
62 enum class MatchPattern {
63  MATCH_RUN()
64 };
65 #undef MATCH_FN
66 
68  static constexpr auto pattern = MatchPattern::DICTIONARY;
69 
71  std::string matched_word;
73  bool l33t;
74  bool reversed;
75 
76  // for l33t matches
77  std::unordered_map<std::string, std::string> sub;
78  std::string sub_display;
79 };
80 
81 struct SpatialMatch {
82  static constexpr auto pattern = MatchPattern::SPATIAL;
83 
85  unsigned turns;
87 };
88 
89 class Match;
90 
91 struct RepeatMatch {
92  static constexpr auto pattern = MatchPattern::REPEAT;
93 
94  std::string base_token;
96  std::vector<Match> base_matches;
97  std::size_t repeat_count;
98 };
99 
101  static constexpr auto pattern = MatchPattern::SEQUENCE;
102 
104  unsigned sequence_space;
105  bool ascending;
106 };
107 
108 struct RegexMatch {
109  static constexpr auto pattern = MatchPattern::REGEX;
110 
113 };
114 
115 struct DateMatch {
116  static constexpr auto pattern = MatchPattern::DATE;
117 
118  std::string separator;
119  unsigned year, month, day;
121 };
122 
124  static constexpr auto pattern = MatchPattern::BRUTEFORCE;
125 };
126 
127 struct UnknownMatch {
128  static constexpr auto pattern = MatchPattern::UNKNOWN;
129 };
130 
131 // Define new match types here
132 
133 class Match {
134 private:
136 #define MATCH_FN(title, upper, lower) title##Match _##lower;
137  union {
138  MATCH_RUN()
139  };
140 #undef MATCH_FN
141 
142  template<class T>
143  void _init(T && val) {
144  i = val.i;
145  j = val.j;
146  token = std::forward<T>(val).token;
147  guesses = val.guesses;
148  guesses_log10 = val.guesses_log10;
149  idx = val.idx;
150  jdx = val.jdx;
151  _pattern = val._pattern;
152 
153 #define MATCH_FN(title, upper, lower) \
154  case MatchPattern::upper: \
155  new (&_##lower) title##Match(std::forward<T>(val)._##lower); \
156  break;
157 
158  switch (_pattern) {
159  MATCH_RUN()
160  default:
161  assert(false);
162  }
163 #undef MATCH_FN
164  }
165 
166  template<class T>
167  Match & _assign(T && val) {
168  this->~Match();
169  new (this) Match(std::forward<T>(val));
170  return *this;
171  }
172 
173 public:
174  // these are character offsets: [i, j]
176  std::string token;
179  // these are byte offsets into original string: [idx, jdx)
181 
182  template<class T>
183  Match(idx_t i_, idx_t j_, std::string token,
184  T && val);
185 
186  Match(const Match & m) {
187  _init(m);
188  }
189 
190  Match(Match && m) {
191  _init(std::move(m));
192  }
193 
194  Match & operator=(const Match & m) {
195  return _assign(m);
196  }
197 
199  return _assign(std::move(m));
200  }
201 
202  ~Match() {
203 #define MATCH_FN(title, upper, lower) \
204  case MatchPattern::upper:\
205  _##lower.~title##Match(); \
206  break;
207 
208  switch (_pattern) {
209  MATCH_RUN()
210  default:
211  assert(false);
212  }
213 #undef MATCH_FN
214  }
215 
217  return _pattern;
218  }
219 
220 #define MATCH_FN(title, upper, lower) \
221  title##Match & get_##lower() { \
222  assert(get_pattern() == MatchPattern::upper); \
223  return _##lower; \
224  } \
225  \
226  const title##Match & get_##lower() const { \
227  assert(get_pattern() == MatchPattern::upper); \
228  return _##lower; \
229  }
230 
231 MATCH_RUN()
232 
233 #undef MATCH_FN
234 
235  template<class T>
236  friend struct pattern_type_to_pmc;
237 };
238 
239 template<class T>
241 
242 #define MATCH_FN(title, upper, lower) \
243  template<> \
244  struct pattern_type_to_pmc<title##Match> : \
245  public std::integral_constant<decltype(&Match::_##lower), &Match::_##lower> {};
246 
247 MATCH_RUN()
248 
249 #undef MATCH_FN
250 
251 template<class T>
252 Match::Match(idx_t i_, idx_t j_, std::string token,
253  T && val) :
254  i(i_), j(j_), token(std::move(token)),
255  guesses(), guesses_log10(), idx(), jdx() {
256  _pattern = T::pattern;
257  new (&(this->*pattern_type_to_pmc<std::decay_t<T>>::value)) std::decay_t<T>(std::forward<T>(val));
258 }
259 
260 }
261 
262 #endif
zxcvbn::MatchPattern
MatchPattern
Definition: common.hpp:62
zxcvbn::SpatialMatch::shifted_count
idx_t shifted_count
Definition: common.hpp:86
zxcvbn::SequenceTag
SequenceTag
Definition: common.hpp:37
zxcvbn::SequenceMatch::pattern
static constexpr auto pattern
Definition: common.hpp:101
zxcvbn::Match::_pattern
MatchPattern _pattern
Definition: common.hpp:135
zxcvbn::DictionaryMatch::sub_display
std::string sub_display
Definition: common.hpp:78
zxcvbn::score_t
unsigned score_t
Definition: common.hpp:17
zxcvbn::RepeatMatch::base_guesses
guesses_t base_guesses
Definition: common.hpp:95
b
void const uint64_t * b
Definition: field_5x52_asm_impl.h:10
zxcvbn::Match::Match
Match(idx_t i_, idx_t j_, std::string token, T &&val)
Definition: common.hpp:252
zxcvbn::DictionaryMatch::matched_word
std::string matched_word
Definition: common.hpp:71
zxcvbn::Match::guesses_log10
guesses_log10_t guesses_log10
Definition: common.hpp:178
zxcvbn::SequenceTag::UNICODE
@ UNICODE
zxcvbn::PortableRegexMatch::matches
std::vector< std::string > matches
Definition: common.hpp:45
zxcvbn::pattern_type_to_pmc
Definition: common.hpp:240
zxcvbn::UnknownMatch
Definition: common.hpp:127
zxcvbn::PortableRegexMatch::index
std::size_t index
Definition: common.hpp:46
zxcvbn::Match::guesses
guesses_t guesses
Definition: common.hpp:177
zxcvbn::SpatialMatch::turns
unsigned turns
Definition: common.hpp:85
zxcvbn::MATCH_RUN
MATCH_RUN()
zxcvbn::DateMatch::has_full_year
bool has_full_year
Definition: common.hpp:120
zxcvbn::Match::Match
Match(const Match &m)
Definition: common.hpp:186
zxcvbn::DictionaryMatch
Definition: common.hpp:67
zxcvbn.h
zxcvbn
Definition: _frequency_lists.cpp:7
zxcvbn::RegexTag::ALPHANUMERIC
@ ALPHANUMERIC
zxcvbn::Match::j
idx_t j
Definition: common.hpp:175
zxcvbn::Match::token
std::string token
Definition: common.hpp:176
zxcvbn::Match::Match
Match(Match &&m)
Definition: common.hpp:190
zxcvbn::DateMatch::day
unsigned day
Definition: common.hpp:119
zxcvbn::BruteforceMatch::pattern
static constexpr auto pattern
Definition: common.hpp:124
zxcvbn::RepeatMatch::repeat_count
std::size_t repeat_count
Definition: common.hpp:97
zxcvbn::SequenceMatch::sequence_space
unsigned sequence_space
Definition: common.hpp:104
zxcvbn::SpatialMatch
Definition: common.hpp:81
zxcvbn::SequenceMatch::ascending
bool ascending
Definition: common.hpp:105
zxcvbn::RegexTag::RECENT_YEAR
@ RECENT_YEAR
zxcvbn::RegexTag::ALPHA_LOWER
@ ALPHA_LOWER
zxcvbn::RegexTag
RegexTag
Definition: common.hpp:31
zxcvbn::guesses_log10_t
int guesses_log10_t
Definition: common.hpp:16
zxcvbn::RepeatMatch::base_token
std::string base_token
Definition: common.hpp:94
zxcvbn::Match::~Match
~Match()
Definition: common.hpp:202
zxcvbn::DictionaryMatch::pattern
static constexpr auto pattern
Definition: common.hpp:68
zxcvbn::SequenceTag::LOWER
@ LOWER
zxcvbn::DateMatch
Definition: common.hpp:115
zxcvbn::RepeatMatch
Definition: common.hpp:91
zxcvbn::Match::i
idx_t i
Definition: common.hpp:175
zxcvbn::guesses_t
zxcvbn_guesses_t guesses_t
Definition: common.hpp:15
zxcvbn::DateMatch::separator
std::string separator
Definition: common.hpp:118
zxcvbn::RegexMatch::regex_match
PortableRegexMatch regex_match
Definition: common.hpp:112
zxcvbn::Match::_init
void _init(T &&val)
Definition: common.hpp:143
zxcvbn::RegexMatch::pattern
static constexpr auto pattern
Definition: common.hpp:109
zxcvbn::rank_t
std::size_t rank_t
Definition: frequency_lists_common.hpp:12
zxcvbn_guesses_t
double zxcvbn_guesses_t
Definition: zxcvbn.h:10
zxcvbn::UnknownMatch::pattern
static constexpr auto pattern
Definition: common.hpp:128
zxcvbn::RepeatMatch::pattern
static constexpr auto pattern
Definition: common.hpp:92
zxcvbn::SpatialMatch::pattern
static constexpr auto pattern
Definition: common.hpp:82
zxcvbn::SequenceTag::UPPER
@ UPPER
zxcvbn::GraphTag
GraphTag
Definition: adjacency_graphs.hpp:16
zxcvbn::DictionaryMatch::l33t
bool l33t
Definition: common.hpp:73
zxcvbn::Match::get_pattern
MatchPattern get_pattern() const
Definition: common.hpp:216
zxcvbn::SequenceMatch::sequence_tag
SequenceTag sequence_tag
Definition: common.hpp:103
zxcvbn::Match::_assign
Match & _assign(T &&val)
Definition: common.hpp:167
zxcvbn::SpatialMatch::graph
GraphTag graph
Definition: common.hpp:84
zxcvbn::RegexMatch::regex_tag
RegexTag regex_tag
Definition: common.hpp:111
zxcvbn::DateMatch::month
unsigned month
Definition: common.hpp:119
std
Definition: adjacency_graphs.hpp:25
zxcvbn::Match::operator=
Match & operator=(Match &&m)
Definition: common.hpp:198
zxcvbn::DictionaryMatch::dictionary_tag
DictionaryTag dictionary_tag
Definition: common.hpp:70
zxcvbn::Match::jdx
idx_t jdx
Definition: common.hpp:180
zxcvbn::_frequency_lists::DictionaryTag
DictionaryTag
Definition: _frequency_lists.hpp:14
zxcvbn::idx_t
std::string::size_type idx_t
Definition: common.hpp:18
zxcvbn::DictionaryMatch::rank
rank_t rank
Definition: common.hpp:72
zxcvbn::Match::operator=
Match & operator=(const Match &m)
Definition: common.hpp:194
zxcvbn::SequenceTag::DIGITS
@ DIGITS
zxcvbn::DateMatch::pattern
static constexpr auto pattern
Definition: common.hpp:116
zxcvbn::DictionaryMatch::sub
std::unordered_map< std::string, std::string > sub
Definition: common.hpp:77
frequency_lists.hpp
zxcvbn::RegexMatch
Definition: common.hpp:108
zxcvbn::Match::idx
idx_t idx
Definition: common.hpp:180
zxcvbn::RepeatMatch::base_matches
std::vector< Match > base_matches
Definition: common.hpp:96
adjacency_graphs.hpp
zxcvbn::DictionaryMatch::reversed
bool reversed
Definition: common.hpp:74
zxcvbn::BruteforceMatch
Definition: common.hpp:123
zxcvbn::PortableRegexMatch::PortableRegexMatch
PortableRegexMatch(const std::smatch &b)
Definition: common.hpp:49
zxcvbn::SequenceMatch
Definition: common.hpp:100
zxcvbn::PortableRegexMatch
Definition: common.hpp:44
zxcvbn::Match
Definition: common.hpp:133
zxcvbn::PortableRegexMatch::PortableRegexMatch
PortableRegexMatch(std::vector< std::string > matches_, std::size_t index_)
Definition: common.hpp:54
zxcvbn::DateMatch::year
unsigned year
Definition: common.hpp:119