summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2021-01-11 21:40:09 +0330
committerAndreas Kling <kling@serenityos.org>2021-01-11 21:09:36 +0100
commit711ced80c0c2a62ae18e12f4d828c7f7316dd935 (patch)
treefcb185375dbdde8d571acb719ed64695d93b2d2e /Libraries
parent7029a8f605bdccc85db25ce4ef0b8e1dbe89f941 (diff)
downloadserenity-711ced80c0c2a62ae18e12f4d828c7f7316dd935.zip
LibCore: Add line iterators to IODevice
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibCore/IODevice.cpp18
-rw-r--r--Libraries/LibCore/IODevice.h25
2 files changed, 43 insertions, 0 deletions
diff --git a/Libraries/LibCore/IODevice.cpp b/Libraries/LibCore/IODevice.cpp
index 4efda65ee4..453526e408 100644
--- a/Libraries/LibCore/IODevice.cpp
+++ b/Libraries/LibCore/IODevice.cpp
@@ -315,4 +315,22 @@ bool IODevice::write(const StringView& v)
return write((const u8*)v.characters_without_null_termination(), v.length());
}
+LineIterator::LineIterator(IODevice& device, bool is_end)
+ : m_device(device)
+ , m_is_end(is_end)
+{
+ ++*this;
+}
+
+bool LineIterator::at_end() const
+{
+ return m_device->eof();
+}
+
+LineIterator& LineIterator::operator++()
+{
+ m_buffer = m_device->read_line();
+ return *this;
+}
+
}
diff --git a/Libraries/LibCore/IODevice.h b/Libraries/LibCore/IODevice.h
index 6071a4c912..70a75532a1 100644
--- a/Libraries/LibCore/IODevice.h
+++ b/Libraries/LibCore/IODevice.h
@@ -31,6 +31,28 @@
namespace Core {
+// This is not necessarily a valid iterator in all contexts,
+// if we had concepts, this would be InputIterator, not Copyable, Movable.
+class LineIterator {
+ AK_MAKE_NONCOPYABLE(LineIterator);
+
+public:
+ explicit LineIterator(IODevice&, bool is_end = false);
+
+ bool operator==(const LineIterator& other) const { return &other == this || (at_end() && other.is_end()) || (other.at_end() && is_end()); }
+ bool is_end() const { return m_is_end; }
+ bool at_end() const;
+
+ LineIterator& operator++();
+
+ StringView operator*() const { return m_buffer; }
+
+private:
+ NonnullRefPtr<IODevice> m_device;
+ bool m_is_end { false };
+ String m_buffer;
+};
+
class IODevice : public Object {
C_OBJECT_ABSTRACT(IODevice)
public:
@@ -84,6 +106,9 @@ public:
int printf(const char*, ...);
+ LineIterator line_begin() & { return LineIterator(*this); }
+ LineIterator line_end() { return LineIterator(*this, true); }
+
protected:
explicit IODevice(Object* parent = nullptr);