1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <algorithm>
6
7 #include "chrome/browser/autofill/form_group.h"
8
9 #include <iterator>
10
Label() const11 const string16 FormGroup::Label() const { return string16(); }
12
operator !=(const FormGroup & form_group) const13 bool FormGroup::operator!=(const FormGroup& form_group) const {
14 FieldTypeSet a, b, symmetric_difference;
15 GetAvailableFieldTypes(&a);
16 form_group.GetAvailableFieldTypes(&b);
17 std::set_symmetric_difference(
18 a.begin(), a.end(),
19 b.begin(), b.end(),
20 std::inserter(symmetric_difference, symmetric_difference.begin()));
21
22 if (!symmetric_difference.empty())
23 return true;
24
25 return (!IntersectionOfTypesHasEqualValues(form_group));
26 }
27
IsSubsetOf(const FormGroup & form_group) const28 bool FormGroup::IsSubsetOf(const FormGroup& form_group) const {
29 FieldTypeSet types;
30 GetAvailableFieldTypes(&types);
31
32 for (FieldTypeSet::const_iterator iter = types.begin(); iter != types.end();
33 ++iter) {
34 if (StringToLowerASCII(GetInfo(*iter)) !=
35 StringToLowerASCII(form_group.GetInfo(*iter)))
36 return false;
37 }
38
39 return true;
40 }
41
IntersectionOfTypesHasEqualValues(const FormGroup & form_group) const42 bool FormGroup::IntersectionOfTypesHasEqualValues(
43 const FormGroup& form_group) const {
44 FieldTypeSet a, b, intersection;
45 GetAvailableFieldTypes(&a);
46 form_group.GetAvailableFieldTypes(&b);
47 std::set_intersection(a.begin(), a.end(),
48 b.begin(), b.end(),
49 std::inserter(intersection, intersection.begin()));
50
51 // An empty intersection can't have equal values.
52 if (intersection.empty())
53 return false;
54
55 for (FieldTypeSet::const_iterator iter = intersection.begin();
56 iter != intersection.end(); ++iter) {
57 if (StringToLowerASCII(GetInfo(*iter)) !=
58 StringToLowerASCII(form_group.GetInfo(*iter)))
59 return false;
60 }
61
62 return true;
63 }
64
MergeWith(const FormGroup & form_group)65 void FormGroup::MergeWith(const FormGroup& form_group) {
66 FieldTypeSet a, b, intersection;
67 GetAvailableFieldTypes(&a);
68 form_group.GetAvailableFieldTypes(&b);
69 std::set_difference(b.begin(), b.end(),
70 a.begin(), a.end(),
71 std::inserter(intersection, intersection.begin()));
72
73 for (FieldTypeSet::const_iterator iter = intersection.begin();
74 iter != intersection.end(); ++iter) {
75 SetInfo(*iter, form_group.GetInfo(*iter));
76 }
77 }
78
OverwriteWith(const FormGroup & form_group)79 void FormGroup::OverwriteWith(const FormGroup& form_group) {
80 FieldTypeSet a;;
81 form_group.GetAvailableFieldTypes(&a);
82
83 for (FieldTypeSet::const_iterator iter = a.begin(); iter != a.end(); ++iter) {
84 SetInfo(*iter, form_group.GetInfo(*iter));
85 }
86 }
87