PRCYCoin  2.0.0.7rc1
P2P Digital Currency
util.cpp
Go to the documentation of this file.
1 #include <zxcvbn/util.hpp>
2 
3 #include <algorithm>
4 #include <codecvt>
5 #include <locale>
6 #include <string>
7 #include <utility>
8 
9 #include <cassert>
10 
11 namespace zxcvbn {
12 
13 namespace util {
14 
15 std::string ascii_lower(const std::string & in) {
16  const char A = 0x41, Z = 0x5A;
17  const char a = 0x61;
18  auto result = in;
19  std::transform(result.begin(), result.end(), result.begin(),
20  [&] (char c) {
21  return (c >= A && c <= Z
22  ? c - A + a
23  : c);
24  });
25  return result;
26 }
27 
28 std::string reverse_string(const std::string & in) {
29  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
30  auto ret = conv.from_bytes(in);
31  std::reverse(ret.begin(), ret.end());
32  return conv.to_bytes(ret);
33 }
34 
35 const std::codecvt_utf8<char32_t> char32_conv;
36 
37 bool utf8_valid(std::string::const_iterator start,
38  std::string::const_iterator end) {
39  while (start != end) {
40  std::mbstate_t st;
41 
42  const char *from = &*start;
43  const char *from_end = &*end;
44  const char *from_next;
45 
46  char32_t new_char;
47  char32_t *to_next;
48 
49  auto res = char32_conv.in(st, from, from_end, from_next,
50  &new_char, &new_char + 1, to_next);
51  if (!((res == std::codecvt_utf8<char32_t>::result::partial &&
52  from_next != from_end) ||
53  (res == std::codecvt_utf8<char32_t>::result::ok &&
54  from_next == from_end))) {
55  return false;
56  }
57  start += (from_next - from);
58  }
59  return true;
60 }
61 
62 bool utf8_valid(const std::string & str) {
63  return utf8_valid(str.begin(), str.end());
64 }
65 
66 template<class It>
67 It _utf8_iter(It start, It end) {
68  assert(start != end);
69  std::mbstate_t st;
70  auto amt = char32_conv.length(st, &*start, &*end, 1);
71  return start + amt;
72 }
73 
74 std::string::iterator utf8_iter(std::string::iterator start,
75  std::string::iterator end) {
76  return _utf8_iter(start, end);
77 }
78 
79 std::string::const_iterator utf8_iter(std::string::const_iterator start,
80  std::string::const_iterator end) {
81  return _utf8_iter(start, end);
82 }
83 
84 std::string::size_type character_len(const std::string & str,
85  std::string::size_type start,
86  std::string::size_type end) {
87  assert(utf8_valid(str.begin() + start, str.begin() + end));
88 
89  std::string::size_type clen = 0;
90  for (auto it = str.begin() + start;
91  it != str.begin() + end;
92  it = utf8_iter(it, str.begin() + end)) {
93  clen += 1;
94  }
95  return clen;
96 }
97 
98 std::string::size_type character_len(const std::string & str) {
99  return character_len(str, 0, str.size());
100 }
101 
102 template<class It>
103 std::pair<char32_t, It> _utf8_decode(It it, It end) {
104  std::mbstate_t st;
105  char32_t new_char;
106  char32_t *to_next;
107 
108  assert(it != end);
109 
110  const char *from = &*it;
111  const char *from_end = &*end;
112  const char *from_next;
113  auto res = char32_conv.in(st, from, from_end, from_next,
114  &new_char, &new_char + 1, to_next);
115  assert((res == std::codecvt_utf8<char32_t>::result::partial &&
116  from_next != from_end) ||
117  (res == std::codecvt_utf8<char32_t>::result::ok &&
118  from_next == from_end));
119  (void) res;
120 
121  return std::make_pair(new_char, it + (from_next - from));
122 }
123 
124 std::pair<char32_t, std::string::iterator> utf8_decode(std::string::iterator start,
125  std::string::iterator end) {
126  return _utf8_decode(start, end);
127 }
128 
129 std::pair<char32_t, std::string::const_iterator> utf8_decode(std::string::const_iterator start,
130  std::string::const_iterator end) {
131  return _utf8_decode(start, end);
132 }
133 
134 char32_t utf8_decode(const std::string & start,
135  std::string::size_type & idx) {
136  auto ret = _utf8_decode(start.begin() + idx, start.end());
137  idx += ret.second - (start.begin() + idx);
138  return ret.first;
139 }
140 
141 }
142 
143 }
zxcvbn::util::ascii_lower
std::string ascii_lower(const std::string &in)
Definition: util.cpp:15
zxcvbn
Definition: _frequency_lists.cpp:7
zxcvbn::util::character_len
std::string::size_type character_len(const std::string &str, std::string::size_type start, std::string::size_type end)
Definition: util.cpp:84
zxcvbn::util::reverse_string
std::string reverse_string(const std::string &in)
Definition: util.cpp:28
zxcvbn::util::utf8_valid
bool utf8_valid(std::string::const_iterator start, std::string::const_iterator end)
Definition: util.cpp:37
zxcvbn::util::utf8_decode
std::pair< char32_t, std::string::iterator > utf8_decode(std::string::iterator start, std::string::iterator end)
Definition: util.cpp:124
zxcvbn::util::utf8_iter
std::string::iterator utf8_iter(std::string::iterator start, std::string::iterator end)
Definition: util.cpp:74
zxcvbn::util::char32_conv
const std::codecvt_utf8< char32_t > char32_conv
Definition: util.cpp:35
zxcvbn::util::_utf8_iter
It _utf8_iter(It start, It end)
Definition: util.cpp:67
zxcvbn::util::_utf8_decode
std::pair< char32_t, It > _utf8_decode(It it, It end)
Definition: util.cpp:103
util
Definition: threadnames.h:10
util.hpp