[llvm] [TableGen] Use std::string::find (NFC) (PR #139681)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Tue May 13 00:27:07 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/139681

This patch partially reverts #139661 for a better solution.
Specifically, we can take advantage of the fact that std::string::find
accepts anything that can be converted to std::string_view, including
StringRef, starting with C++17.  This way, we do not need to cast Val
to StringRef or LHSs->getValue() to std::string.


>From 92f0e76d89d6c21b59303eaf9760062b3677bad4 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 13 May 2025 00:16:04 -0700
Subject: [PATCH] [TableGen] Use std::string::find (NFC)

This patch partially reverts #139661 for a better solution.
Specifically, we can take advantage of the fact that std::string::find
accepts anything that can be converted to std::string_view, including
StringRef, starting with C++17.  This way, we do not need to cast Val
to StringRef or LHSs->getValue() to std::string.
---
 llvm/lib/TableGen/Record.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 0d9fcb0e63dae..bc0ccfd64d155 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -1796,11 +1796,11 @@ const Init *TernOpInit::Fold(const Record *CurRec) const {
     if (LHSs && MHSs && RHSs) {
       std::string Val = RHSs->getValue().str();
 
-      StringRef::size_type found;
-      StringRef::size_type idx = 0;
+      std::string::size_type found;
+      std::string::size_type idx = 0;
       while (true) {
-        found = StringRef(Val).find(LHSs->getValue(), idx);
-        if (found == StringRef::npos)
+        found = Val.find(LHSs->getValue(), idx);
+        if (found == std::string::npos)
           break;
         Val.replace(found, LHSs->getValue().size(), MHSs->getValue().str());
         idx = found + MHSs->getValue().size();



More information about the llvm-commits mailing list