refactor: more constexpr lookup tables (#38886)

* refactor: use constexpr lookup table in electron_api_web_contents.cc

* refactor: make KeyboardCodeFromStr() private

it is only used as a helper to KeyboardCodeFromStr()

* chore: savepoint

* chore: make lint happy

* fixup! refactor: make KeyboardCodeFromStr() private

* refactor: use constexpr lookup table in electron_url_loader_factory

* refactor: use constexpr lookup table in electron_api_tray

* refactor: use constexpr lookup table in web_contents_preferences.cc

* refactor: use constexpr lookup table in content_converter
This commit is contained in:
Charles Kerr 2023-06-22 07:33:44 -05:00 committed by GitHub
parent 1eb19f3078
commit 395b608dd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 349 additions and 460 deletions

View file

@ -9,6 +9,7 @@
#include <string>
#include <utility>
#include "base/containers/fixed_flat_map.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/uuid.h"
@ -45,22 +46,17 @@ struct Converter<electron::ProtocolType> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::ProtocolType* out) {
std::string type;
if (!ConvertFromV8(isolate, val, &type))
return false;
if (type == "buffer")
*out = electron::ProtocolType::kBuffer;
else if (type == "string")
*out = electron::ProtocolType::kString;
else if (type == "file")
*out = electron::ProtocolType::kFile;
else if (type == "http")
*out = electron::ProtocolType::kHttp;
else if (type == "stream")
*out = electron::ProtocolType::kStream;
else // note "free" is internal type, not allowed to be passed from user
return false;
return true;
using Val = electron::ProtocolType;
static constexpr auto Lookup =
base::MakeFixedFlatMapSorted<base::StringPiece, Val>({
// note "free" is internal type, not allowed to be passed from user
{"buffer", Val::kBuffer},
{"file", Val::kFile},
{"http", Val::kHttp},
{"stream", Val::kStream},
{"string", Val::kString},
});
return FromV8WithLookup(isolate, val, Lookup, out);
}
};