diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-08-01 07:54:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-01 07:54:20 +0200 |
commit | 44f1e540f62faf03e64411e1f52c3cb0a31d2758 (patch) | |
tree | a041630fa75e65fa16811a72a33ff84a7264a98e /core | |
parent | 4d4575d3863663174c862f37f2d56596e8ad469b (diff) | |
parent | 1418f97c70a5551bdbfeea853cbc479b32ea9e08 (diff) |
Merge pull request #63733 from akien-mga/file-get_as_text-skip-CR
File: Re-add support to skip CR (`\r`) in `File::get_as_text`
Diffstat (limited to 'core')
-rw-r--r-- | core/core_bind.cpp | 6 | ||||
-rw-r--r-- | core/core_bind.h | 2 | ||||
-rw-r--r-- | core/io/file_access.cpp | 4 | ||||
-rw-r--r-- | core/io/file_access.h | 2 | ||||
-rw-r--r-- | core/string/ustring.cpp | 10 | ||||
-rw-r--r-- | core/string/ustring.h | 2 |
6 files changed, 17 insertions, 9 deletions
diff --git a/core/core_bind.cpp b/core/core_bind.cpp index 65cc3b4c42..79c80cf7d1 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -1238,13 +1238,13 @@ Vector<uint8_t> File::get_buffer(int64_t p_length) const { return data; } -String File::get_as_text() const { +String File::get_as_text(bool p_skip_cr) const { ERR_FAIL_COND_V_MSG(f.is_null(), String(), "File must be opened before use, or is lacking read-write permission."); uint64_t original_pos = f->get_position(); const_cast<FileAccess *>(*f)->seek(0); - String text = f->get_as_utf8_string(); + String text = f->get_as_utf8_string(p_skip_cr); const_cast<FileAccess *>(*f)->seek(original_pos); @@ -1441,7 +1441,7 @@ void File::_bind_methods() { ClassDB::bind_method(D_METHOD("get_buffer", "length"), &File::get_buffer); ClassDB::bind_method(D_METHOD("get_line"), &File::get_line); ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &File::get_csv_line, DEFVAL(",")); - ClassDB::bind_method(D_METHOD("get_as_text"), &File::get_as_text); + ClassDB::bind_method(D_METHOD("get_as_text", "skip_cr"), &File::get_as_text, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_md5", "path"), &File::get_md5); ClassDB::bind_method(D_METHOD("get_sha256", "path"), &File::get_sha256); ClassDB::bind_method(D_METHOD("is_big_endian"), &File::is_big_endian); diff --git a/core/core_bind.h b/core/core_bind.h index 8f66dc03ce..45b4091ce2 100644 --- a/core/core_bind.h +++ b/core/core_bind.h @@ -412,7 +412,7 @@ public: Vector<uint8_t> get_buffer(int64_t p_length) const; // Get an array of bytes. String get_line() const; Vector<String> get_csv_line(const String &p_delim = ",") const; - String get_as_text() const; + String get_as_text(bool p_skip_cr = false) const; String get_md5(const String &p_path) const; String get_sha256(const String &p_path) const; diff --git a/core/io/file_access.cpp b/core/io/file_access.cpp index da25f23917..8ed3d40c22 100644 --- a/core/io/file_access.cpp +++ b/core/io/file_access.cpp @@ -377,7 +377,7 @@ uint64_t FileAccess::get_buffer(uint8_t *p_dst, uint64_t p_length) const { return i; } -String FileAccess::get_as_utf8_string() const { +String FileAccess::get_as_utf8_string(bool p_skip_cr) const { Vector<uint8_t> sourcef; uint64_t len = get_length(); sourcef.resize(len + 1); @@ -388,7 +388,7 @@ String FileAccess::get_as_utf8_string() const { w[len] = 0; String s; - s.parse_utf8((const char *)w); + s.parse_utf8((const char *)w, -1, p_skip_cr); return s; } diff --git a/core/io/file_access.h b/core/io/file_access.h index e2c11142d7..3386800686 100644 --- a/core/io/file_access.h +++ b/core/io/file_access.h @@ -113,7 +113,7 @@ public: virtual String get_line() const; virtual String get_token() const; virtual Vector<String> get_csv_line(const String &p_delim = ",") const; - virtual String get_as_utf8_string() const; + virtual String get_as_utf8_string(bool p_skip_cr = false) const; /** * Use this for files WRITTEN in _big_ endian machines (ie, amiga/mac) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index beefe54faf..e93375bff7 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -1656,7 +1656,7 @@ String String::utf8(const char *p_utf8, int p_len) { return ret; } -Error String::parse_utf8(const char *p_utf8, int p_len) { +Error String::parse_utf8(const char *p_utf8, int p_len, bool p_skip_cr) { if (!p_utf8) { return ERR_INVALID_DATA; } @@ -1689,6 +1689,10 @@ Error String::parse_utf8(const char *p_utf8, int p_len) { uint8_t c = *ptrtmp >= 0 ? *ptrtmp : uint8_t(256 + *ptrtmp); if (skip == 0) { + if (p_skip_cr && c == '\r') { + ptrtmp++; + continue; + } /* Determine the number of characters in sequence */ if ((c & 0x80) == 0) { skip = 0; @@ -1753,6 +1757,10 @@ Error String::parse_utf8(const char *p_utf8, int p_len) { uint8_t c = *p_utf8 >= 0 ? *p_utf8 : uint8_t(256 + *p_utf8); if (skip == 0) { + if (p_skip_cr && c == '\r') { + p_utf8++; + continue; + } /* Determine the number of characters in sequence */ if ((c & 0x80) == 0) { *(dst++) = c; diff --git a/core/string/ustring.h b/core/string/ustring.h index 7672663964..6c3169f136 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -377,7 +377,7 @@ public: CharString ascii(bool p_allow_extended = false) const; CharString utf8() const; - Error parse_utf8(const char *p_utf8, int p_len = -1); + Error parse_utf8(const char *p_utf8, int p_len = -1, bool p_skip_cr = false); static String utf8(const char *p_utf8, int p_len = -1); Char16String utf16() const; |