Use simplify operator< implementations in tools/
Simplify the code for operator< when comparing multiple members using
a common std::tie idiom. And resolve a TODO when comparing strings.
BUG=555171
R=brettw@chromium.org
Review URL: https://codereview.chromium.org/1461623006
Cr-Original-Commit-Position: refs/heads/master@{#362254}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 850020357f5d66ac342fd387e36dd1fb5c298803
diff --git a/tools/gn/label.h b/tools/gn/label.h
index 88336ea..33cac56 100644
--- a/tools/gn/label.h
+++ b/tools/gn/label.h
@@ -71,14 +71,12 @@
return !operator==(other);
}
bool operator<(const Label& other) const {
- // TODO(brettw) could be optimized to avoid an extra full string check
- // (one for operator==, one for <).
- if (dir_ != other.dir_)
- return dir_ < other.dir_;
- if (name_ != other.name_)
- return name_ < other.name_;
- if (toolchain_dir_ != other.toolchain_dir_)
- return toolchain_dir_ < other.toolchain_dir_;
+ if (int c = dir_.value().compare(other.dir_.value()))
+ return c < 0;
+ if (int c = name_.compare(other.name_))
+ return c < 0;
+ if (int c = toolchain_dir_.value().compare(other.toolchain_dir_.value()))
+ return c < 0;
return toolchain_name_ < other.toolchain_name_;
}
diff --git a/tools/gn/location.cc b/tools/gn/location.cc
index 7a18786..59b99d6 100644
--- a/tools/gn/location.cc
+++ b/tools/gn/location.cc
@@ -4,6 +4,8 @@
#include "tools/gn/location.h"
+#include <tuple>
+
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "tools/gn/input_file.h"
@@ -36,9 +38,8 @@
bool Location::operator<(const Location& other) const {
DCHECK(file_ == other.file_);
- if (line_number_ != other.line_number_)
- return line_number_ < other.line_number_;
- return char_offset_ < other.char_offset_;
+ return std::tie(line_number_, char_offset_) <
+ std::tie(other.line_number_, other.char_offset_);
}
std::string Location::Describe(bool include_char_offset) const {