[llvm] 38fc680 - Replace strlen with StringRef::size

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 20 14:19:12 PDT 2023


Author: Ilyas Mustafazade
Date: 2023-03-20T14:18:51-07:00
New Revision: 38fc680c2dc31ae7c397faa919e1881fb9e8f82a

URL: https://github.com/llvm/llvm-project/commit/38fc680c2dc31ae7c397faa919e1881fb9e8f82a
DIFF: https://github.com/llvm/llvm-project/commit/38fc680c2dc31ae7c397faa919e1881fb9e8f82a.diff

LOG: Replace strlen with StringRef::size

Replace multiple strlen calls with a StringRef constructor and a
StringRef::size call.

Differential Revision: https://reviews.llvm.org/D146394

Added: 
    

Modified: 
    llvm/lib/Option/Option.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Option/Option.cpp b/llvm/lib/Option/Option.cpp
index 95edf32b2174..c570b02b08ce 100644
--- a/llvm/lib/Option/Option.cpp
+++ b/llvm/lib/Option/Option.cpp
@@ -109,9 +109,10 @@ std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
                                             StringRef Spelling,
                                             unsigned &Index) const {
   const size_t SpellingSize = Spelling.size();
+  const size_t ArgStringSize = StringRef(Args.getArgString(Index)).size();
   switch (getKind()) {
   case FlagClass: {
-    if (SpellingSize != strlen(Args.getArgString(Index)))
+    if (SpellingSize != ArgStringSize)
       return nullptr;
     return std::make_unique<Arg>(*this, Spelling, Index++);
   }
@@ -149,8 +150,7 @@ std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
   }
   case SeparateClass:
     // Matches iff this is an exact match.
-    // FIXME: Avoid strlen.
-    if (SpellingSize != strlen(Args.getArgString(Index)))
+    if (SpellingSize != ArgStringSize)
       return nullptr;
 
     Index += 2;
@@ -162,8 +162,7 @@ std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
                                  Args.getArgString(Index - 1));
   case MultiArgClass: {
     // Matches iff this is an exact match.
-    // FIXME: Avoid strlen.
-    if (SpellingSize != strlen(Args.getArgString(Index)))
+    if (SpellingSize != ArgStringSize)
       return nullptr;
 
     Index += 1 + getNumArgs();
@@ -178,8 +177,7 @@ std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
   }
   case JoinedOrSeparateClass: {
     // If this is not an exact match, it is a joined arg.
-    // FIXME: Avoid strlen.
-    if (SpellingSize != strlen(Args.getArgString(Index))) {
+    if (SpellingSize != ArgStringSize) {
       const char *Value = Args.getArgString(Index) + SpellingSize;
       return std::make_unique<Arg>(*this, Spelling, Index++, Value);
     }
@@ -205,8 +203,7 @@ std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
                                  Args.getArgString(Index - 1));
   case RemainingArgsClass: {
     // Matches iff this is an exact match.
-    // FIXME: Avoid strlen.
-    if (SpellingSize != strlen(Args.getArgString(Index)))
+    if (SpellingSize != ArgStringSize)
       return nullptr;
     auto A = std::make_unique<Arg>(*this, Spelling, Index++);
     while (Index < Args.getNumInputArgStrings() &&
@@ -216,7 +213,7 @@ std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
   }
   case RemainingArgsJoinedClass: {
     auto A = std::make_unique<Arg>(*this, Spelling, Index);
-    if (SpellingSize != strlen(Args.getArgString(Index))) {
+    if (SpellingSize != ArgStringSize) {
       // An inexact match means there is a joined arg.
       A->getValues().push_back(Args.getArgString(Index) + SpellingSize);
     }


        


More information about the llvm-commits mailing list