summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/autofill_scanner.cc
blob: 6526fcc0812b0c78cbb239cea7c95a4e7a7183d1 (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/autofill/autofill_scanner.h"

#include "base/logging.h"
#include "chrome/browser/autofill/autofill_field.h"
#include "unicode/regex.h"

AutofillScanner::AutofillScanner(
    const std::vector<const AutofillField*>& fields)
    : cursor_(fields.begin()),
      end_(fields.end()) {
}

AutofillScanner::~AutofillScanner() {
}

void AutofillScanner::Advance() {
  DCHECK(!IsEnd());
  ++cursor_;
}

const AutofillField* AutofillScanner::Cursor() const {
  if (IsEnd()) {
    NOTREACHED();
    return NULL;
  }

  return *cursor_;
}

bool AutofillScanner::IsEnd() const {
  return cursor_ == end_;
}

void AutofillScanner::Rewind() {
  DCHECK(!saved_cursors_.empty());
  cursor_ = saved_cursors_.back();
  saved_cursors_.pop_back();
}

void AutofillScanner::SaveCursor() {
  saved_cursors_.push_back(cursor_);
}

namespace autofill {

bool MatchString(const string16& input, const string16& pattern) {
  UErrorCode status = U_ZERO_ERROR;
  icu::UnicodeString icu_pattern(pattern.data(), pattern.length());
  icu::UnicodeString icu_input(input.data(), input.length());
  icu::RegexMatcher matcher(icu_pattern, icu_input,
                            UREGEX_CASE_INSENSITIVE, status);
  DCHECK(U_SUCCESS(status));

  UBool match = matcher.find(0, status);
  DCHECK(U_SUCCESS(status));
  return !!match;
}

}  // namespace autofill