[libc-commits] [libc] [libc] Support dynamically-grown lines in FlatFileDatabase (PR #223811)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Tue Sep 15 15:50:25 PDT 2026


================
@@ -98,13 +119,67 @@ template <typename EntryType> class FlatFileDatabase {
       --bytes_read;
 
     buf[bytes_read] = '\0';
-    return ReadLineResult{bytes_read, truncated, eof};
+    return ReadLineResult{bytes_read, raw_bytes_consumed, truncated, eof};
+  }
+
+  // Reads a single line into a caller-owned buffer, growing it as needed so
+  // that arbitrarily long records can be read. Otherwise behaves as read_line;
+  // the result is never truncated.
+  LIBC_INLINE static ErrorOr<ReadLineResult>
+  read_line_growing(File *f, DynamicBuffer &buf) {
+    if (!f)
+      return Error(EINVAL);
+
+    File::FileLock lock(f);
+    size_t bytes_read = 0;
+
+    while (true) {
+      // One byte for the character about to be read, one for the terminator.
+      if (bytes_read > cpp::numeric_limits<size_t>::max() - 2 ||
+          (bytes_read + 2 > buf.capacity() && !buf.reserve(bytes_read + 2))) {
----------------
vonosmas wrote:

I don't think you need to compare against capacity here, `reserve` would already do the right thing and won't reallocate if necessary.

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


More information about the libc-commits mailing list