diff options
author | lawnjelly <lawnjelly@gmail.com> | 2022-12-12 15:14:39 +0000 |
---|---|---|
committer | lawnjelly <lawnjelly@gmail.com> | 2022-12-12 15:14:39 +0000 |
commit | 491594ef0f56dcafd9a1aaba7b66ec23c9f18af4 (patch) | |
tree | 8f0b408b29a1d3c5cb9820b9d84d9096305a2894 /core/variant | |
parent | bc5d67c61345758741fe087c6b5282402b0b2465 (diff) |
VariantParser make readahead optional
It turns out some areas are independently moving / reading filepointers outside of the VariantParser, which can cause the readahead caching to get out of sync.
This PR makes the VariantParser readahead to be optional to allow for these use cases.
Diffstat (limited to 'core/variant')
-rw-r--r-- | core/variant/variant_parser.cpp | 17 | ||||
-rw-r--r-- | core/variant/variant_parser.h | 10 |
2 files changed, 23 insertions, 4 deletions
diff --git a/core/variant/variant_parser.cpp b/core/variant/variant_parser.cpp index 9f500dbf5e..f122ed5f8f 100644 --- a/core/variant/variant_parser.cpp +++ b/core/variant/variant_parser.cpp @@ -42,7 +42,7 @@ char32_t VariantParser::Stream::get_char() { } // attempt to readahead - readahead_filled = _read_buffer(readahead_buffer, READAHEAD_SIZE); + readahead_filled = _read_buffer(readahead_buffer, readahead_enabled ? READAHEAD_SIZE : 1); if (readahead_filled) { readahead_pointer = 0; } else { @@ -54,10 +54,21 @@ char32_t VariantParser::Stream::get_char() { return get_char(); } +bool VariantParser::Stream::is_eof() const { + if (readahead_enabled) { + return eof; + } + return _is_eof(); +} + bool VariantParser::StreamFile::is_utf8() const { return true; } +bool VariantParser::StreamFile::_is_eof() const { + return f->eof_reached(); +} + uint32_t VariantParser::StreamFile::_read_buffer(char32_t *p_buffer, uint32_t p_num_chars) { // The buffer is assumed to include at least one character (for null terminator) ERR_FAIL_COND_V(!p_num_chars, 0); @@ -79,6 +90,10 @@ bool VariantParser::StreamString::is_utf8() const { return false; } +bool VariantParser::StreamString::_is_eof() const { + return pos > s.length(); +} + uint32_t VariantParser::StreamString::_read_buffer(char32_t *p_buffer, uint32_t p_num_chars) { // The buffer is assumed to include at least one character (for null terminator) ERR_FAIL_COND_V(!p_num_chars, 0); diff --git a/core/variant/variant_parser.h b/core/variant/variant_parser.h index 6b1d095ab5..fdea355c4b 100644 --- a/core/variant/variant_parser.h +++ b/core/variant/variant_parser.h @@ -46,14 +46,16 @@ public: bool eof = false; protected: + bool readahead_enabled = true; virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) = 0; + virtual bool _is_eof() const = 0; public: char32_t saved = 0; char32_t get_char(); virtual bool is_utf8() const = 0; - bool is_eof() const { return eof; } + bool is_eof() const; Stream() {} virtual ~Stream() {} @@ -62,13 +64,14 @@ public: struct StreamFile : public Stream { protected: virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) override; + virtual bool _is_eof() const override; public: Ref<FileAccess> f; virtual bool is_utf8() const override; - StreamFile() {} + StreamFile(bool p_readahead_enabled = true) { readahead_enabled = p_readahead_enabled; } }; struct StreamString : public Stream { @@ -79,10 +82,11 @@ public: protected: virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) override; + virtual bool _is_eof() const override; public: virtual bool is_utf8() const override; - StreamString() {} + StreamString(bool p_readahead_enabled = true) { readahead_enabled = p_readahead_enabled; } }; typedef Error (*ParseResourceFunc)(void *p_self, Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str); |