0

Transition PrefValueMap to use std::string_view.

No functionality changes - this is a pure replacement of const
std::string& parameters by std::string_view parameters.

Bug: 348307659
Change-Id: I5ba0658bcb4e68a5f20948d699898bfcd974bc82
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5640036
Commit-Queue: Jan Keitel <jkeitel@google.com>
Reviewed-by: Dominic Battré <battre@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1317292}
This commit is contained in:
Jan Keitel
2024-06-20 11:36:21 +00:00
committed by Chromium LUCI CQ
parent 1cd21d796a
commit b982816940
2 changed files with 49 additions and 37 deletions

@ -41,8 +41,14 @@ bool PrefValueMap::GetValue(std::string_view key, base::Value** value) {
return true;
}
bool PrefValueMap::SetValue(const std::string& key, base::Value value) {
base::Value& existing_value = prefs_[key];
bool PrefValueMap::SetValue(std::string_view key, base::Value value) {
// Once C++26 is supported, just do `base::Value& existing_value =
// prefs_[key]`.
auto it = prefs_.find(key);
if (it == prefs_.end()) {
it = prefs_.insert({std::string(key), base::Value()}).first;
}
base::Value& existing_value = it->second;
if (value == existing_value)
return false;
@ -50,19 +56,25 @@ bool PrefValueMap::SetValue(const std::string& key, base::Value value) {
return true;
}
bool PrefValueMap::RemoveValue(const std::string& key) {
return prefs_.erase(key) != 0;
bool PrefValueMap::RemoveValue(std::string_view key) {
// Once C++23 is supported, just do `return prefs_.erase(key)`;
auto it = prefs_.find(key);
if (it == prefs_.end()) {
return false;
}
prefs_.erase(it);
return true;
}
void PrefValueMap::Clear() {
prefs_.clear();
}
void PrefValueMap::ClearWithPrefix(const std::string& prefix) {
void PrefValueMap::ClearWithPrefix(std::string_view prefix) {
Map::iterator low = prefs_.lower_bound(prefix);
// Appending maximum possible character so that there will be no string with
// prefix |prefix| that we may miss.
Map::iterator high = prefs_.upper_bound(prefix + char(CHAR_MAX));
Map::iterator high = prefs_.upper_bound(std::string(prefix) + char(CHAR_MAX));
prefs_.erase(low, high);
}
@ -90,7 +102,7 @@ bool PrefValueMap::empty() const {
return prefs_.empty();
}
bool PrefValueMap::GetBoolean(const std::string& key, bool* value) const {
bool PrefValueMap::GetBoolean(std::string_view key, bool* value) const {
const base::Value* stored_value = nullptr;
if (GetValue(key, &stored_value) && stored_value->is_bool()) {
*value = stored_value->GetBool();
@ -99,11 +111,11 @@ bool PrefValueMap::GetBoolean(const std::string& key, bool* value) const {
return false;
}
void PrefValueMap::SetBoolean(const std::string& key, bool value) {
void PrefValueMap::SetBoolean(std::string_view key, bool value) {
SetValue(key, base::Value(value));
}
bool PrefValueMap::GetString(const std::string& key, std::string* value) const {
bool PrefValueMap::GetString(std::string_view key, std::string* value) const {
const base::Value* stored_value = nullptr;
if (GetValue(key, &stored_value) && stored_value->is_string()) {
*value = stored_value->GetString();
@ -112,11 +124,11 @@ bool PrefValueMap::GetString(const std::string& key, std::string* value) const {
return false;
}
void PrefValueMap::SetString(const std::string& key, const std::string& value) {
void PrefValueMap::SetString(std::string_view key, std::string_view value) {
SetValue(key, base::Value(value));
}
bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
bool PrefValueMap::GetInteger(std::string_view key, int* value) const {
const base::Value* stored_value = nullptr;
if (GetValue(key, &stored_value) && stored_value->is_int()) {
*value = stored_value->GetInt();
@ -125,11 +137,11 @@ bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
return false;
}
void PrefValueMap::SetInteger(const std::string& key, const int value) {
void PrefValueMap::SetInteger(std::string_view key, const int value) {
SetValue(key, base::Value(value));
}
void PrefValueMap::SetDouble(const std::string& key, const double value) {
void PrefValueMap::SetDouble(std::string_view key, const double value) {
SetValue(key, base::Value(value));
}

@ -27,24 +27,24 @@ class COMPONENTS_PREFS_EXPORT PrefValueMap {
virtual ~PrefValueMap();
// Gets the value for |key| and stores it in |value|. Ownership remains with
// the map. Returns true if a value is present. If not, |value| is not
// Gets the value for `key` and stores it in `value`. Ownership remains with
// the map. Returns true if a value is present. If not, `value` is not
// touched.
bool GetValue(std::string_view key, const base::Value** value) const;
bool GetValue(std::string_view key, base::Value** value);
// Sets a new |value| for |key|. Returns true if the value changed.
bool SetValue(const std::string& key, base::Value value);
// Sets a new `value` for `key`. Returns true if the value changed.
bool SetValue(std::string_view key, base::Value value);
// Removes the value for |key| from the map. Returns true if a value was
// Removes the value for `key` from the map. Returns true if a value was
// removed.
bool RemoveValue(const std::string& key);
bool RemoveValue(std::string_view key);
// Clears the map.
void Clear();
// Clear the preferences which start with |prefix|.
void ClearWithPrefix(const std::string& prefix);
// Clear the preferences which start with `prefix`.
void ClearWithPrefix(std::string_view prefix);
// Swaps the contents of two maps.
void Swap(PrefValueMap* other);
@ -55,32 +55,32 @@ class COMPONENTS_PREFS_EXPORT PrefValueMap {
const_iterator end() const;
bool empty() const;
// Gets a boolean value for |key| and stores it in |value|. Returns true if
// Gets a boolean value for `key` and stores it in `value`. Returns true if
// the value was found and of the proper type.
bool GetBoolean(const std::string& key, bool* value) const;
bool GetBoolean(std::string_view key, bool* value) const;
// Sets the value for |key| to the boolean |value|.
void SetBoolean(const std::string& key, bool value);
// Sets the value for `key` to the boolean `value`.
void SetBoolean(std::string_view key, bool value);
// Gets a string value for |key| and stores it in |value|. Returns true if
// Gets a string value for `key` and stores it in `value`. Returns true if
// the value was found and of the proper type.
bool GetString(const std::string& key, std::string* value) const;
bool GetString(std::string_view key, std::string* value) const;
// Sets the value for |key| to the string |value|.
void SetString(const std::string& key, const std::string& value);
// Sets the value for `key` to the string `value`.
void SetString(std::string_view key, std::string_view value);
// Gets an int value for |key| and stores it in |value|. Returns true if
// Gets an int value for `key` and stores it in `value`. Returns true if
// the value was found and of the proper type.
bool GetInteger(const std::string& key, int* value) const;
bool GetInteger(std::string_view key, int* value) const;
// Sets the value for |key| to the int |value|.
void SetInteger(const std::string& key, const int value);
// Sets the value for `key` to the int `value`.
void SetInteger(std::string_view key, const int value);
// Sets the value for |key| to the double |value|.
void SetDouble(const std::string& key, const double value);
// Sets the value for `key` to the double `value`.
void SetDouble(std::string_view key, const double value);
// Compares this value map against |other| and stores all key names that have
// different values in |differing_keys|. This includes keys that are present
// Compares this value map against `other` and stores all key names that have
// different values in `differing_keys`. This includes keys that are present
// only in one of the maps.
void GetDifferingKeys(const PrefValueMap* other,
std::vector<std::string>* differing_keys) const;