File: autofill_regexes.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (94 lines) | stat: -rw-r--r-- 3,455 bytes parent folder | download | duplicates (5)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CORE_COMMON_AUTOFILL_REGEXES_H_
#define COMPONENTS_AUTOFILL_CORE_COMMON_AUTOFILL_REGEXES_H_

#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

#include "base/no_destructor.h"
#include "base/sequence_checker.h"
#include "base/synchronization/lock.h"
#include "base/types/strong_alias.h"
#include "third_party/icu/source/i18n/unicode/regex.h"

namespace autofill {

using ThreadSafe = base::StrongAlias<struct ThreadSafeTag, bool>;

// Compiles a case-insensitive regular expression.
//
// The return icu::RegexPattern is thread-safe (because it's const and icu
// guarantees thread-safety of the const functions). In particularly this
// includes icu::RegexPattern::matcher().
//
// May also be used to initialize `static base::NoDestructor<icu::RegexPattern>`
// function-scope variables.
std::unique_ptr<const icu::RegexPattern> CompileRegex(
    std::u16string_view regex);

// Returns true if `regex` is found in `input`.
// If `groups` is non-null, it gets resized and the found capture groups
// are written into it.
// Thread-safe.
bool MatchesRegex(std::u16string_view input,
                  const icu::RegexPattern& regex_pattern,
                  std::vector<std::u16string>* groups = nullptr);

// Calls MatchesRegex() after compiling the `regex` on the first call and
// retrieving it from a static variable in subsequent calls.
//
// This function is thread-safe.
template <const char16_t regex[]>
bool MatchesRegex(std::u16string_view input,
                  std::vector<std::u16string>* groups = nullptr) {
  static base::NoDestructor<std::unique_ptr<const icu::RegexPattern>>
      regex_pattern(CompileRegex(regex));
  return MatchesRegex(input, **regex_pattern, groups);
}

// Splits `input` into up to `max_groups` segments. Returns a vector of segments
// in case of success or `std::nullopt` otherwise.
std::optional<std::vector<std::u16string>> SplitByRegex(
    std::u16string_view input,
    const icu::RegexPattern& regex_pattern,
    size_t max_groups);

// A cache of compiled regex patterns. It can be configured to be thread-safe
// (using a mutex) or not (in which case it uses a sequence checker).
class AutofillRegexCache {
 public:
  explicit AutofillRegexCache(ThreadSafe thread_safe);
  ~AutofillRegexCache();

  AutofillRegexCache(const AutofillRegexCache&) = delete;
  AutofillRegexCache& operator=(const AutofillRegexCache&) = delete;

  // Returns the compiled regex corresponding to `regex`.
  // This function is thread-safe if `thread_safe_`.
  // The returned object is thread-safe in any case (because it's const).
  // Although the returned pointer is guaranteed to be non-nullptr, we do not
  // return a reference to avoid accidental copies.
  const icu::RegexPattern* GetRegexPattern(std::u16string_view regex);

 private:
  // `MatchesPattern()` uses the lock if `thread_safe_`. Otherwise, it validates
  // the sequence.
  ThreadSafe thread_safe_{false};
  base::Lock lock_;
  SEQUENCE_CHECKER(sequence_checker_);

  // Maps regex strings to their corresponding compiled regex patterns.
  std::
      map<std::u16string, std::unique_ptr<const icu::RegexPattern>, std::less<>>
          cache_;
};

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_COMMON_AUTOFILL_REGEXES_H_