From 1fa39f057d59aeee2b9f36cf3b152528a1197ae1 Mon Sep 17 00:00:00 2001 From: "joth@chromium.org" Date: Tue, 13 Sep 2011 15:45:34 +0000 Subject: micro optimize CommandLine.GetSwitchPrefixLength avoid searching the entire length of each switch for the -- prefix, as we know it can only exist at the start. BUG=None TEST=None Review URL: http://codereview.chromium.org/7866036 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@100918 0039d316-1c4b-4281-b951-d872f2087c98 --- base/command_line.cc | 2 +- base/command_line_unittest.cc | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'base') diff --git a/base/command_line.cc b/base/command_line.cc index b028226..22977af 100644 --- a/base/command_line.cc +++ b/base/command_line.cc @@ -37,7 +37,7 @@ const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"}; size_t GetSwitchPrefixLength(const CommandLine::StringType& string) { for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) { CommandLine::StringType prefix(kSwitchPrefixes[i]); - if (string.find(prefix) == 0) + if (string.compare(0, prefix.length(), prefix) == 0) return prefix.length(); } return 0; diff --git a/base/command_line_unittest.cc b/base/command_line_unittest.cc index 80874ac..656236e 100644 --- a/base/command_line_unittest.cc +++ b/base/command_line_unittest.cc @@ -34,6 +34,7 @@ TEST(CommandLineTest, CommandLineConstructor) { FILE_PATH_LITERAL("-spaetzle=Crepe"), FILE_PATH_LITERAL("-=loosevalue"), FILE_PATH_LITERAL("FLAN"), + FILE_PATH_LITERAL("a"), FILE_PATH_LITERAL("--input-translation=45--output-rotation"), FILE_PATH_LITERAL("--"), FILE_PATH_LITERAL("--"), @@ -74,13 +75,15 @@ TEST(CommandLineTest, CommandLineConstructor) { EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); const CommandLine::StringVector& args = cl.GetArgs(); - ASSERT_EQ(6U, args.size()); + ASSERT_EQ(7U, args.size()); std::vector::const_iterator iter = args.begin(); EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); ++iter; EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); ++iter; + EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter); + ++iter; EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); ++iter; EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); -- cgit v1.1