Skip to content

Commit

Permalink
Update switches to use StringView. (flutter#3781)
Browse files Browse the repository at this point in the history
  • Loading branch information
pylaligand authored Jun 16, 2017
1 parent 784e975 commit 07d4357
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 7 additions & 5 deletions shell/common/switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
#include <sstream>
#include <string>

#include "lib/ftl/strings/string_view.h"

// Include once for the default enum definition.
#include "flutter/shell/common/switches.h"

#undef SHELL_COMMON_SWITCHES_H_

struct SwitchDesc {
shell::Switch sw;
const char* flag;
const ftl::StringView flag;
const char* help;
};

Expand Down Expand Up @@ -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<uint32_t>(strlen(desc.flag) + 2, max_width);
max_width = std::max<uint32_t>(desc.flag.size() + 2, max_width);
}

const uint32_t help_width = column_width - max_width - 3;
Expand All @@ -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;
Expand All @@ -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<uint32_t>(Switch::Sentinel); i++) {
if (gSwitchDescs[i].sw == swtch) {
return gSwitchDescs[i].flag;
}
}
return nullptr;
return ftl::StringView();
}

} // namespace shell
4 changes: 3 additions & 1 deletion shell/common/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 07d4357

Please sign in to comment.