From 07d4357b1ac4c7aa8c13f1627071e43096ef3cc5 Mon Sep 17 00:00:00 2001 From: "P.Y. Laligand" Date: Fri, 16 Jun 2017 12:51:26 -0700 Subject: [PATCH] Update switches to use StringView. (#3781) See https://fuchsia-review.googlesource.com/#/c/31931/. --- shell/common/switches.cc | 12 +++++++----- shell/common/switches.h | 4 +++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/shell/common/switches.cc b/shell/common/switches.cc index 9a3168f73..6ea1bb3db 100644 --- a/shell/common/switches.cc +++ b/shell/common/switches.cc @@ -7,6 +7,8 @@ #include #include +#include "lib/ftl/strings/string_view.h" + // Include once for the default enum definition. #include "flutter/shell/common/switches.h" @@ -14,7 +16,7 @@ struct SwitchDesc { shell::Switch sw; - const char* flag; + const ftl::StringView flag; const char* help; }; @@ -46,7 +48,7 @@ void PrintUsage(const std::string& executable_name) { uint32_t max_width = 2; for (uint32_t i = 0; i < flags_count; i++) { auto desc = gSwitchDescs[i]; - max_width = std::max(strlen(desc.flag) + 2, max_width); + max_width = std::max(desc.flag.size() + 2, max_width); } const uint32_t help_width = column_width - max_width - 3; @@ -56,7 +58,7 @@ void PrintUsage(const std::string& executable_name) { auto desc = gSwitchDescs[i]; std::cerr << std::setw(max_width) - << std::string("--") + std::string(desc.flag) << " : "; + << std::string("--") + desc.flag.ToString() << " : "; std::istringstream stream(desc.help); int32_t remaining = help_width; @@ -78,13 +80,13 @@ void PrintUsage(const std::string& executable_name) { std::cerr << std::string(column_width, '-') << std::endl; } -const char* FlagForSwitch(Switch swtch) { +const ftl::StringView FlagForSwitch(Switch swtch) { for (uint32_t i = 0; i < static_cast(Switch::Sentinel); i++) { if (gSwitchDescs[i].sw == swtch) { return gSwitchDescs[i].flag; } } - return nullptr; + return ftl::StringView(); } } // namespace shell diff --git a/shell/common/switches.h b/shell/common/switches.h index fe68fe13d..d528ce623 100644 --- a/shell/common/switches.h +++ b/shell/common/switches.h @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "lib/ftl/strings/string_view.h" + #ifndef SHELL_COMMON_SWITCHES_H_ #define SHELL_COMMON_SWITCHES_H_ @@ -106,7 +108,7 @@ DEF_SWITCHES_END void PrintUsage(const std::string& executable_name); -const char* FlagForSwitch(Switch sw); +const ftl::StringView FlagForSwitch(Switch sw); } // namespace shell -- GitLab