From 8a7a05814c70d84eace161d3eed07f5b5cfdd8d6 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 20 Sep 2009 02:02:24 +0000 Subject: [PATCH] convert argname to StringRef, simplifying LookupOption. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82352 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/CommandLine.cpp | 43 +++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index f44ed41aa3b..e35ad93b195 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -151,23 +151,25 @@ static void GetOptionInfo(std::vector &PositionalOpts, /// LookupOption - Lookup the option specified by the specified option on the /// command line. If there is a value specified (after an equal sign) return /// that as well. -static Option *LookupOption(const char *&Arg, StringRef &Value, - StringMap &OptionsMap) { - while (*Arg == '-') ++Arg; // Eat leading dashes - - const char *ArgEnd = Arg; - while (*ArgEnd && *ArgEnd != '=') - ++ArgEnd; // Scan till end of argument name. - +static Option *LookupOption(StringRef &Arg, StringRef &Value, + const StringMap &OptionsMap) { + // Eat leading dashes. + while (!Arg.empty() && Arg[0] == '-') + Arg = Arg.substr(1); + + // Reject all dashes. + if (Arg.empty()) return 0; + + size_t EqualPos = Arg.find('='); + // If we have an equals sign, remember the value. - if (*ArgEnd == '=') - Value = ArgEnd+1; - - if (*Arg == 0) return 0; + if (EqualPos != StringRef::npos) { + Value = Arg.substr(EqualPos+1); + Arg = Arg.substr(0, EqualPos); + } // Look up the option. - StringMap::iterator I = - OptionsMap.find(llvm::StringRef(Arg, ArgEnd-Arg)); + StringMap::const_iterator I = OptionsMap.find(Arg); return I != OptionsMap.end() ? I->second : 0; } @@ -486,7 +488,7 @@ void cl::ParseCommandLineOptions(int argc, char **argv, for (int i = 1; i < argc; ++i) { Option *Handler = 0; StringRef Value; - const char *ArgName = ""; + StringRef ArgName = ""; // If the option list changed, this means that some command line // option has just been registered or deregistered. This can occur in @@ -551,17 +553,16 @@ void cl::ParseCommandLineOptions(int argc, char **argv, StringRef RealName(ArgName); if (RealName.size() > 1) { size_t Length = 0; - Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping, - Opts); + Option *PGOpt = + getOptionPred(RealName, Length, isPrefixedOrGrouping, Opts); // If the option is a prefixed option, then the value is simply the // rest of the name... so fall through to later processing, by // setting up the argument name flags and value fields. - // if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) { - Value = ArgName+Length; - assert(Opts.count(StringRef(ArgName, Length)) && - Opts[StringRef(ArgName, Length)] == PGOpt); + Value = ArgName.substr(Length); + assert(Opts.count(ArgName.substr(0, Length)) && + Opts[ArgName.substr(0, Length)] == PGOpt); Handler = PGOpt; } else if (PGOpt) { // This must be a grouped option... handle them now. -- 2.34.1