File: util.cpp

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (95 lines) | stat: -rw-r--r-- 2,499 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <zxcvbn/util.hpp>

#include <algorithm>
#include <string>
#include <string_view>
#include <utility>

#include <cassert>

#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "base/strings/utf_string_conversions.h"

namespace zxcvbn {

namespace util {

bool utf8_valid(std::string::const_iterator start,
                std::string::const_iterator end) {
  return base::IsStringUTF8(std::string_view(start, end));
}

bool utf8_valid(const std::string & str) {
  return utf8_valid(str.begin(), str.end());
}

std::string ascii_lower(const std::string & in) {
  return base::ToLowerASCII(in);
}

std::string reverse_string(const std::string & in) {
  if (!utf8_valid(in))
    return std::string(in.rbegin(), in.rend());

  std::wstring ret = base::UTF8ToWide(in);
  std::reverse(ret.begin(), ret.end());
  return base::WideToUTF8(ret);
}

template<class It>
std::pair<char32_t, It> _utf8_decode(It it, It end) {
  assert(it != end);
  const char* src = &*it;
  size_t src_len = static_cast<size_t>(std::distance(it, end));
  size_t char_index = 0;
  base_icu::UChar32 code_point_out;

  base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point_out);
  return {code_point_out, it + ++char_index};
}


template<class It>
It _utf8_iter(It start, It end) {
  return _utf8_decode(start, end).second;
}

std::string::iterator utf8_iter(std::string::iterator start,
                                std::string::iterator end) {
  return _utf8_iter(start, end);
}

std::string::const_iterator utf8_iter(std::string::const_iterator start,
                                      std::string::const_iterator end) {
  return _utf8_iter(start, end);
}

std::string::size_type character_len(const std::string & str,
                                     std::string::size_type start,
                                     std::string::size_type end) {
  assert(utf8_valid(str.begin() + start, str.begin() + end));

  std::string::size_type clen = 0;
  for (auto it = str.begin() + start;
        it != str.begin() + end;
        it = utf8_iter(it, str.begin() + end)) {
    clen += 1;
  }
  return clen;
}

std::string::size_type character_len(const std::string & str) {
  return character_len(str, 0, str.size());
}

char32_t utf8_decode(const std::string & start,
                     std::string::size_type & idx) {
  auto ret = _utf8_decode(start.begin() + idx, start.end());
  idx += ret.second - (start.begin() + idx);
  return ret.first;
}

}

}