Skip to content

Commit

Permalink
Merge pull request #69961 from lawnjelly/variant_parser_optional_read…
Browse files Browse the repository at this point in the history
…ahead

VariantParser make readahead optional
  • Loading branch information
akien-mga committed Dec 12, 2022
2 parents 8ab653c + 491594e commit ba4bd7f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
17 changes: 16 additions & 1 deletion core/variant/variant_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions core/variant/variant_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ class VariantParser {
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() {}
Expand All @@ -62,13 +64,14 @@ class VariantParser {
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 {
Expand All @@ -79,10 +82,11 @@ class VariantParser {

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);
Expand Down
3 changes: 2 additions & 1 deletion scene/resources/resource_format_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,8 @@ void ResourceLoaderText::set_translation_remapped(bool p_remapped) {
translation_remapped = p_remapped;
}

ResourceLoaderText::ResourceLoaderText() {}
ResourceLoaderText::ResourceLoaderText() :
stream(false) {}

void ResourceLoaderText::get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types) {
open(p_f);
Expand Down

0 comments on commit ba4bd7f

Please sign in to comment.