[llvm] [llvm-strings] Use small buffer instead of reading whole file (PR #163073)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 26 01:31:50 PDT 2026


================
@@ -129,40 +137,65 @@ static void strings(raw_ostream &OS, StringRef FileName,
              << errorToErrorCode(ReadBytesOrErr.takeError()).message() << '\n';
       return;
     }
-    std::size_t CurSize = *ReadBytesOrErr;
-    if (CurSize == 0)
+    std::size_t ChunkSize = *ReadBytesOrErr;
+    if (ChunkSize == 0)
       break;
 
-    std::size_t I = 0;
-    while (I != CurSize) {
+    const char *const B = Buffer.data();
+    const char *const E = B + ChunkSize;
+    const char *P = B;
+
+    if (InString || !Candidate.empty()) {
+      while (P != E && isStringChar(*P))
+        ++P;
+      size_t Len = P - B;
       if (InString) {
-        std::size_t Start = I;
-        while (I != CurSize && isStringChar(Buffer[I]))
-          ++I;
-        OS << StringRef(Buffer.data() + Start, I - Start);
-        Offset += I - Start;
-        if (I != CurSize) {
-          OS << '\n';
-          InString = false;
-        }
-      } else if (isStringChar(Buffer[I])) {
-        if (Candidate.empty())
-          StringStart = Offset;
-        Candidate.push_back(Buffer[I]);
-        ++I;
-        ++Offset;
-        if (Candidate.size() >= static_cast<size_t>(MinLength)) {
-          printHeader(StringStart);
-          OS << Candidate;
-          Candidate.clear();
-          InString = true;
-        }
+        OS << StringRef(B, Len);
+      } else if (Candidate.size() + Len >= Min) {
+        printHeader(ChunkOffset - Candidate.size());
+        OS << Candidate << StringRef(B, Len);
+        Candidate.clear();
+        InString = true;
+      } else if (P == E) {
+        Candidate.append(B, E);
       } else {
         Candidate.clear();
-        ++I;
-        ++Offset;
+      }
+      if (P == E) {
----------------
jh7370 wrote:

Nit: I'd put blank lines between the if blocks for readability.

https://github.com/llvm/llvm-project/pull/163073


More information about the llvm-commits mailing list