summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorasynts <asynts@gmail.com>2020-08-27 14:21:53 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-30 09:56:10 +0200
commite7df17d1467ee5379492576c831394f4126cb92a (patch)
treeed8b7191dffdfa97025c39365b13aef2be95c6ec /AK
parentdeb85c47b54b3ea98f74ba297fe0cc0b8d6d1a28 (diff)
downloadserenity-e7df17d1467ee5379492576c831394f4126cb92a.zip
AK: Stream operators for String for generic streams.
I think this should really be a member function of InputStream instead, but I don't want to include String in Stream.h. This will do for now...
Diffstat (limited to 'AK')
-rw-r--r--AK/Stream.h2
-rw-r--r--AK/String.h40
2 files changed, 22 insertions, 20 deletions
diff --git a/AK/Stream.h b/AK/Stream.h
index a769e7f432..5b803d2367 100644
--- a/AK/Stream.h
+++ b/AK/Stream.h
@@ -181,8 +181,6 @@ inline OutputStream& operator<<(OutputStream& stream, bool value)
}
class InputMemoryStream final : public InputStream {
- friend InputMemoryStream& operator>>(InputMemoryStream& stream, String& string);
-
public:
InputMemoryStream(ReadonlyBytes bytes)
: m_bytes(bytes)
diff --git a/AK/String.h b/AK/String.h
index 52c006e0be..1fcd6de7f1 100644
--- a/AK/String.h
+++ b/AK/String.h
@@ -29,6 +29,7 @@
#include <AK/Forward.h>
#include <AK/RefPtr.h>
#include <AK/Stream.h>
+#include <AK/StringBuilder.h>
#include <AK/StringImpl.h>
#include <AK/StringUtils.h>
#include <AK/Traits.h>
@@ -277,26 +278,29 @@ bool operator<=(const char*, const String&);
String escape_html_entities(const StringView& html);
-inline InputMemoryStream& operator>>(InputMemoryStream& stream, String& string)
+inline InputStream& operator>>(InputStream& stream, String& string)
{
- // FIXME: There was some talking about a generic lexer class?
-
- const auto start = stream.offset();
-
- while (!stream.eof() && stream.m_bytes[stream.m_offset]) {
- ++stream.m_offset;
+ StringBuilder builder;
+
+ for (;;) {
+ if (stream.eof()) {
+ string = nullptr;
+
+ // FIXME: We need an InputStream::set_error_flag method.
+ stream.discard_or_error(1);
+ return stream;
+ }
+
+ char next_char;
+ stream >> next_char;
+
+ if (next_char) {
+ builder.append(next_char);
+ } else {
+ string = builder.to_string();
+ return stream;
+ }
}
-
- if (stream.eof()) {
- stream.m_error = true;
- stream.m_offset = start;
- string = nullptr;
- } else {
- string = String { stream.bytes().slice(start, stream.offset() - start) };
- ++stream.m_offset;
- }
-
- return stream;
}
}