Skip to content

Commit

Permalink
PR #380: Simplify, speed-up and error-proof op<< for PartitionPolicyEnum
Browse files Browse the repository at this point in the history
Using switch/case to output enum values is the best for readability and performance-wise
(about 8x speed-up vs the map implementation).

Also, this makes sure it is immune to future issues if the enum
is extended, as the compiler will warn about a new value not handled
in the switch.

Signed-off-by: Henner Zeller <[email protected]>

GitHub PR #380

Copybara import of the project:

  - aad06af Simplify, speed-up and error-proof op<< for PartitionPoli... by Henner Zeller <[email protected]>
  - d2f98bc Address review comments. by Henner Zeller <[email protected]>

Closes #380

PiperOrigin-RevId: 325152616
  • Loading branch information
hzeller committed Aug 6, 2020
1 parent 2b70655 commit c1db566
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
1 change: 0 additions & 1 deletion common/formatting/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ cc_library(
"//common/text:symbol",
"//common/text:tree_utils",
"//common/util:container_iterator_range",
"//common/util:container_util",
"//common/util:spacer",
"@com_google_absl//absl/strings",
],
Expand Down
29 changes: 15 additions & 14 deletions common/formatting/unwrapped_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,26 @@
#include "common/strings/display_utils.h"
#include "common/text/tree_utils.h"
#include "common/util/container_iterator_range.h"
#include "common/util/container_util.h"
#include "common/util/spacer.h"

namespace verible {

using verible::container::FindOrDie;

std::ostream& operator<<(std::ostream& stream, PartitionPolicyEnum p) {
static const auto* enum_names =
new std::map<PartitionPolicyEnum, const char*>{
{PartitionPolicyEnum::kUninitialized, "uninitialized"},
{PartitionPolicyEnum::kAlwaysExpand, "always-expand"},
{PartitionPolicyEnum::kFitOnLineElseExpand, "fit-else-expand"},
{PartitionPolicyEnum::kTabularAlignment, "tabular-alignment"},
{PartitionPolicyEnum::kSuccessfullyAligned, "aligned-success"},
{PartitionPolicyEnum::kAppendFittingSubPartitions,
"append-fitting-sub-partitions"},
};
return stream << FindOrDie(*enum_names, p);
switch (p) {
case PartitionPolicyEnum::kUninitialized:
return stream << "uninitialized";
case PartitionPolicyEnum::kAlwaysExpand:
return stream << "always-expand";
case PartitionPolicyEnum::kFitOnLineElseExpand:
return stream << "fit-else-expand";
case PartitionPolicyEnum::kTabularAlignment:
return stream << "tabular-alignment";
case PartitionPolicyEnum::kSuccessfullyAligned:
return stream << "aligned-success";
case PartitionPolicyEnum::kAppendFittingSubPartitions:
return stream << "append-fitting-sub-partitions";
}
LOG(FATAL) << "Unknown partition policy " << int(p);
}

static void TokenFormatter(std::string* out, const PreFormatToken& token,
Expand Down

0 comments on commit c1db566

Please sign in to comment.