Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 6 additions & 52 deletions core/io/file_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "core/io/marshalls.h"
#include "core/os/os.h"
#include "core/os/time.h"
#include "core/string/char_buffer.h"

Ref<FileAccess> FileAccess::create(AccessType p_access) {
ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, nullptr);
Expand Down Expand Up @@ -415,54 +416,8 @@ String FileAccess::get_token() const {
return String::utf8(token.get_data());
}

class CharBuffer {
Vector<char> vector;
char stack_buffer[256];

char *buffer = nullptr;
int64_t capacity = 0;
int64_t written = 0;

bool grow() {
if (vector.resize(next_power_of_2((uint64_t)1 + (uint64_t)written)) != OK) {
return false;
}

if (buffer == stack_buffer) { // first chunk?

for (int64_t i = 0; i < written; i++) {
vector.write[i] = stack_buffer[i];
}
}

buffer = vector.ptrw();
capacity = vector.size();
ERR_FAIL_COND_V(written >= capacity, false);

return true;
}

public:
_FORCE_INLINE_ CharBuffer() :
buffer(stack_buffer),
capacity(std::size(stack_buffer)) {
}

_FORCE_INLINE_ void push_back(char c) {
if (written >= capacity) {
ERR_FAIL_COND(!grow());
}

buffer[written++] = c;
}

_FORCE_INLINE_ const char *get_data() const {
return buffer;
}
};

String FileAccess::get_line() const {
CharBuffer line;
CharBuffer<char, 256> line;

uint8_t c = get_8();

Expand All @@ -480,16 +435,15 @@ String FileAccess::get_line() const {
get_8();
}
}
line.push_back(0);
return String::utf8(line.get_data());
return String::utf8(line.get_terminated_buffer());
} else {
line.push_back(char(c));
line += char(c);
}

c = get_8();
}
line.push_back(0);
return String::utf8(line.get_data());

return String::utf8(line.get_terminated_buffer());
}

Vector<String> FileAccess::get_csv_line(const String &p_delim) const {
Expand Down
9 changes: 5 additions & 4 deletions core/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/config/engine.h"
#include "core/object/script_language.h"
#include "core/string/char_buffer.h"
#include "core/variant/container_type_validate.h"

const char *JSON::tk_name[TK_MAX] = {
Expand Down Expand Up @@ -224,7 +225,7 @@ Error JSON::_get_token(const char32_t *p_str, int &index, int p_len, Token &r_to
}
case '"': {
index++;
String str;
CharBuffer<char32_t> str;
while (true) {
if (p_str[index] == 0) {
r_err_str = "Unterminated string";
Expand Down Expand Up @@ -359,7 +360,7 @@ Error JSON::_get_token(const char32_t *p_str, int &index, int p_len, Token &r_to
}

r_token.type = TK_STRING;
r_token.value = str;
r_token.value = str.finalize();
return OK;

} break;
Expand All @@ -379,15 +380,15 @@ Error JSON::_get_token(const char32_t *p_str, int &index, int p_len, Token &r_to
return OK;

} else if (is_ascii_alphabet_char(p_str[index])) {
String id;
CharBuffer<char32_t> id;

while (is_ascii_alphabet_char(p_str[index])) {
id += p_str[index];
index++;
}

r_token.type = TK_IDENTIFIER;
r_token.value = id;
r_token.value = id.finalize();
return OK;
} else {
r_err_str = "Unexpected character";
Expand Down
94 changes: 94 additions & 0 deletions core/string/char_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**************************************************************************/
/* char_buffer.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#pragma once

#include "core/string/ustring.h"

template <typename T, uint32_t FIXED_BUFFER_SIZE = 64>
class CharBuffer {
T _fixed_buffer[FIXED_BUFFER_SIZE];
T *_active_buffer = _fixed_buffer;

using StringType = std::conditional_t<std::is_same_v<T, char32_t>, String, CharStringT<T>>;
StringType _string;

uint32_t _length = 0;
uint32_t _capacity = FIXED_BUFFER_SIZE;

// Size should include room for null terminator.
void _reserve(uint32_t p_size) {
if (p_size <= _capacity) {
return;
}

bool copy = _active_buffer == _fixed_buffer && _length > 0;
_capacity = next_power_of_2(p_size);
_string.reserve_exact(_capacity);
_active_buffer = const_cast<T *>(_string.ptr());
if (copy) {
memcpy(_active_buffer, _fixed_buffer, _length * sizeof(T));
}
}

public:
_FORCE_INLINE_ void operator+=(T p_char) { append(p_char); }
CharBuffer &append(T p_char) {
_reserve(_length + 2);
_active_buffer[_length++] = p_char;
return *this;
}

// Returns pointer to null-terminated string without resetting state.
const T *get_terminated_buffer() {
_active_buffer[_length] = '\0';
return _active_buffer;
}

// Returns string and resets state.
StringType finalize() {
StringType result;

if (_active_buffer == _fixed_buffer) {
result = get_terminated_buffer();
} else {
_active_buffer[_length] = '\0';
result = std::move(_string);
result.resize_uninitialized(_length + 1);

_string = StringType();
_active_buffer = _fixed_buffer;
_capacity = FIXED_BUFFER_SIZE;
}

_length = 0;
return result;
}
};
163 changes: 0 additions & 163 deletions core/string/string_buffer.h

This file was deleted.

Loading