summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorSahan Fernando <sahan.h.fernando@gmail.com>2021-12-07 01:16:01 +1100
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-12-12 14:06:38 +0330
commit2c43eaa50ced53571de8a719946d2078fc8ea202 (patch)
treeeebc49fc2564af9f041fa6b044affecdd221fdfa /Userland/Libraries
parent6d948c1a9242efd8fa5c450540cf0f55740158ab (diff)
downloadserenity-2c43eaa50ced53571de8a719946d2078fc8ea202.zip
LibCore: Add support for range-based for loops on LineIterators
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibCore/IODevice.cpp4
-rw-r--r--Userland/Libraries/LibCore/IODevice.h20
2 files changed, 24 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/IODevice.cpp b/Userland/Libraries/LibCore/IODevice.cpp
index 0df99b0814..1911c8936e 100644
--- a/Userland/Libraries/LibCore/IODevice.cpp
+++ b/Userland/Libraries/LibCore/IODevice.cpp
@@ -319,4 +319,8 @@ LineIterator& LineIterator::operator++()
m_buffer = m_device->read_line();
return *this;
}
+
+LineIterator LineRange::begin() { return m_device.line_begin(); }
+LineIterator LineRange::end() { return m_device.line_end(); }
+
}
diff --git a/Userland/Libraries/LibCore/IODevice.h b/Userland/Libraries/LibCore/IODevice.h
index 1caa82a5c7..752963483b 100644
--- a/Userland/Libraries/LibCore/IODevice.h
+++ b/Userland/Libraries/LibCore/IODevice.h
@@ -12,6 +12,8 @@
namespace Core {
+class IODevice;
+
// This is not necessarily a valid iterator in all contexts,
// if we had concepts, this would be InputIterator, not Copyable, Movable.
class LineIterator {
@@ -34,6 +36,20 @@ private:
String m_buffer;
};
+class LineRange {
+public:
+ LineRange() = delete;
+ explicit LineRange(IODevice& device)
+ : m_device(device)
+ {
+ }
+ LineIterator begin();
+ LineIterator end();
+
+private:
+ IODevice& m_device;
+};
+
enum class OpenMode : unsigned {
NotOpen = 0,
ReadOnly = 1,
@@ -91,6 +107,10 @@ public:
LineIterator line_begin() & { return LineIterator(*this); }
LineIterator line_end() { return LineIterator(*this, true); }
+ LineRange lines()
+ {
+ return LineRange(*this);
+ }
protected:
explicit IODevice(Object* parent = nullptr);