Commit ac8353dc authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[Passwords] Simplify CSVPasswordSequence

This change simplifies the NameToLabel helper inside CSVPasswordSequence
to use the new base::fixed_flat_map and return a simple pointer instead
of a base::Optional.

Bug: 934326
Change-Id: I1325a7470151ee31af37dcc21919d696d829edbb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2544606Reviewed-by: default avatarFriedrich [CET] <fhorschig@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828807}
parent 5f174dae
......@@ -8,10 +8,9 @@
#include <string>
#include <utility>
#include "base/containers/flat_map.h"
#include "base/containers/fixed_flat_map.h"
#include "base/containers/flat_set.h"
#include "base/no_destructor.h"
#include "base/optional.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "components/password_manager/core/browser/import/csv_field_parser.h"
#include "components/password_manager/core/browser/import/csv_password.h"
......@@ -21,13 +20,13 @@ namespace password_manager {
namespace {
// Given a CSV column |name|, returns the matching CSVPassword::Label or nothing
// if the column name is not recognised.
base::Optional<CSVPassword::Label> NameToLabel(base::StringPiece name) {
// Given a CSV column |name|, returns a pointer to the matching
// CSVPassword::Label or nullptr if the column name is not recognised.
const CSVPassword::Label* NameToLabel(base::StringPiece name) {
using Label = CSVPassword::Label;
// Recognised column names for origin URL, usernames and passwords.
static const base::NoDestructor<base::flat_map<base::StringPiece, Label>>
kLabelMap({
static constexpr auto kLabelMap =
base::MakeFixedFlatMap<base::StringPiece, Label>({
{"url", Label::kOrigin},
{"website", Label::kOrigin},
{"origin", Label::kOrigin},
......@@ -40,9 +39,8 @@ base::Optional<CSVPassword::Label> NameToLabel(base::StringPiece name) {
{"password", Label::kPassword},
});
auto it = kLabelMap->find(base::ToLowerASCII(name));
return it != kLabelMap->end() ? base::make_optional(it->second)
: base::nullopt;
auto* it = kLabelMap.find(base::ToLowerASCII(name));
return it != kLabelMap.end() ? &it->second : nullptr;
}
} // namespace
......@@ -65,10 +63,9 @@ CSVPasswordSequence::CSVPasswordSequence(std::string csv)
result_ = CSVPassword::Status::kSyntaxError;
return;
}
base::Optional<CSVPassword::Label> label = NameToLabel(name);
if (label) {
if (const CSVPassword::Label* label = NameToLabel(name))
map_[col_index] = *label;
}
}
// Check that each of the three labels is assigned to exactly one column.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment