From 0be6d925dc3c6413bce7a3ccb49631b8e4a6e67a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Thu, 14 May 2020 13:23:58 +0200 Subject: Style: clang-format: Disable KeepEmptyLinesAtTheStartOfBlocks Which means that reduz' beloved style which we all became used to will now be changed automatically to remove the first empty line. This makes us lean closer to 1TBS (the one true brace style) instead of hybridating it with some Allman-inspired spacing. There's still the case of braces around single-statement blocks that needs to be addressed (but clang-format can't help with that, but clang-tidy may if we agree about it). Part of #33027. --- modules/regex/regex.cpp | 48 ------------------------------------------------ 1 file changed, 48 deletions(-) (limited to 'modules/regex/regex.cpp') diff --git a/modules/regex/regex.cpp b/modules/regex/regex.cpp index 25cc580591..c0627b8f25 100644 --- a/modules/regex/regex.cpp +++ b/modules/regex/regex.cpp @@ -36,26 +36,21 @@ extern "C" { } static void *_regex_malloc(PCRE2_SIZE size, void *user) { - return memalloc(size); } static void _regex_free(void *ptr, void *user) { - memfree(ptr); } int RegExMatch::_find(const Variant &p_name) const { - if (p_name.is_num()) { - int i = (int)p_name; if (i >= data.size()) return -1; return i; } else if (p_name.get_type() == Variant::STRING) { - const Map::Element *found = names.find((String)p_name); if (found) return found->value(); @@ -65,19 +60,16 @@ int RegExMatch::_find(const Variant &p_name) const { } String RegExMatch::get_subject() const { - return subject; } int RegExMatch::get_group_count() const { - if (data.size() == 0) return 0; return data.size() - 1; } Dictionary RegExMatch::get_names() const { - Dictionary result; for (const Map::Element *i = names.front(); i != nullptr; i = i->next()) { @@ -88,13 +80,11 @@ Dictionary RegExMatch::get_names() const { } Array RegExMatch::get_strings() const { - Array result; int size = data.size(); for (int i = 0; i < size; i++) { - int start = data[i].start; if (start == -1) { @@ -111,7 +101,6 @@ Array RegExMatch::get_strings() const { } String RegExMatch::get_string(const Variant &p_name) const { - int id = _find(p_name); if (id < 0) @@ -128,7 +117,6 @@ String RegExMatch::get_string(const Variant &p_name) const { } int RegExMatch::get_start(const Variant &p_name) const { - int id = _find(p_name); if (id < 0) @@ -138,7 +126,6 @@ int RegExMatch::get_start(const Variant &p_name) const { } int RegExMatch::get_end(const Variant &p_name) const { - int id = _find(p_name); if (id < 0) @@ -148,7 +135,6 @@ int RegExMatch::get_end(const Variant &p_name) const { } void RegExMatch::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_subject"), &RegExMatch::get_subject); ClassDB::bind_method(D_METHOD("get_group_count"), &RegExMatch::get_group_count); ClassDB::bind_method(D_METHOD("get_names"), &RegExMatch::get_names); @@ -163,28 +149,22 @@ void RegExMatch::_bind_methods() { } void RegEx::_pattern_info(uint32_t what, void *where) const { - if (sizeof(CharType) == 2) { - pcre2_pattern_info_16((pcre2_code_16 *)code, what, where); } else { - pcre2_pattern_info_32((pcre2_code_32 *)code, what, where); } } void RegEx::clear() { - if (sizeof(CharType) == 2) { - if (code) { pcre2_code_free_16((pcre2_code_16 *)code); code = nullptr; } } else { - if (code) { pcre2_code_free_32((pcre2_code_32 *)code); code = nullptr; @@ -193,7 +173,6 @@ void RegEx::clear() { } Error RegEx::compile(const String &p_pattern) { - pattern = p_pattern; clear(); @@ -202,7 +181,6 @@ Error RegEx::compile(const String &p_pattern) { uint32_t flags = PCRE2_DUPNAMES; if (sizeof(CharType) == 2) { - pcre2_general_context_16 *gctx = (pcre2_general_context_16 *)general_ctx; pcre2_compile_context_16 *cctx = pcre2_compile_context_create_16(gctx); PCRE2_SPTR16 p = (PCRE2_SPTR16)pattern.c_str(); @@ -220,7 +198,6 @@ Error RegEx::compile(const String &p_pattern) { } } else { - pcre2_general_context_32 *gctx = (pcre2_general_context_32 *)general_ctx; pcre2_compile_context_32 *cctx = pcre2_compile_context_create_32(gctx); PCRE2_SPTR32 p = (PCRE2_SPTR32)pattern.c_str(); @@ -241,7 +218,6 @@ Error RegEx::compile(const String &p_pattern) { } Ref RegEx::search(const String &p_subject, int p_offset, int p_end) const { - ERR_FAIL_COND_V(!is_valid(), nullptr); Ref result = memnew(RegExMatch); @@ -251,7 +227,6 @@ Ref RegEx::search(const String &p_subject, int p_offset, int p_end) length = p_end; if (sizeof(CharType) == 2) { - pcre2_code_16 *c = (pcre2_code_16 *)code; pcre2_general_context_16 *gctx = (pcre2_general_context_16 *)general_ctx; pcre2_match_context_16 *mctx = pcre2_match_context_create_16(gctx); @@ -272,7 +247,6 @@ Ref RegEx::search(const String &p_subject, int p_offset, int p_end) result->data.resize(size); for (uint32_t i = 0; i < size; i++) { - result->data.write[i].start = ovector[i * 2]; result->data.write[i].end = ovector[i * 2 + 1]; } @@ -281,7 +255,6 @@ Ref RegEx::search(const String &p_subject, int p_offset, int p_end) pcre2_match_context_free_16(mctx); } else { - pcre2_code_32 *c = (pcre2_code_32 *)code; pcre2_general_context_32 *gctx = (pcre2_general_context_32 *)general_ctx; pcre2_match_context_32 *mctx = pcre2_match_context_create_32(gctx); @@ -304,7 +277,6 @@ Ref RegEx::search(const String &p_subject, int p_offset, int p_end) result->data.resize(size); for (uint32_t i = 0; i < size; i++) { - result->data.write[i].start = ovector[i * 2]; result->data.write[i].end = ovector[i * 2 + 1]; } @@ -324,7 +296,6 @@ Ref RegEx::search(const String &p_subject, int p_offset, int p_end) _pattern_info(PCRE2_INFO_NAMEENTRYSIZE, &entry_size); for (uint32_t i = 0; i < count; i++) { - CharType id = table[i * entry_size]; if (result->data[id].start == -1) continue; @@ -339,7 +310,6 @@ Ref RegEx::search(const String &p_subject, int p_offset, int p_end) } Array RegEx::search_all(const String &p_subject, int p_offset, int p_end) const { - int last_end = -1; Array result; Ref match = search(p_subject, p_offset, p_end); @@ -354,7 +324,6 @@ Array RegEx::search_all(const String &p_subject, int p_offset, int p_end) const } String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_all, int p_offset, int p_end) const { - ERR_FAIL_COND_V(!is_valid(), String()); // safety_zone is the number of chars we allocate in addition to the number of chars expected in order to @@ -376,7 +345,6 @@ String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_a length = p_end; if (sizeof(CharType) == 2) { - pcre2_code_16 *c = (pcre2_code_16 *)code; pcre2_general_context_16 *gctx = (pcre2_general_context_16 *)general_ctx; pcre2_match_context_16 *mctx = pcre2_match_context_create_16(gctx); @@ -401,7 +369,6 @@ String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_a return String(); } else { - pcre2_code_32 *c = (pcre2_code_32 *)code; pcre2_general_context_32 *gctx = (pcre2_general_context_32 *)general_ctx; pcre2_match_context_32 *mctx = pcre2_match_context_create_32(gctx); @@ -430,17 +397,14 @@ String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_a } bool RegEx::is_valid() const { - return (code != nullptr); } String RegEx::get_pattern() const { - return pattern; } int RegEx::get_group_count() const { - ERR_FAIL_COND_V(!is_valid(), 0); uint32_t count; @@ -451,7 +415,6 @@ int RegEx::get_group_count() const { } Array RegEx::get_names() const { - Array result; ERR_FAIL_COND_V(!is_valid(), result); @@ -465,7 +428,6 @@ Array RegEx::get_names() const { _pattern_info(PCRE2_INFO_NAMEENTRYSIZE, &entry_size); for (uint32_t i = 0; i < count; i++) { - String name = &table[i * entry_size + 1]; if (result.find(name) < 0) { result.append(name); @@ -476,26 +438,20 @@ Array RegEx::get_names() const { } RegEx::RegEx() { - if (sizeof(CharType) == 2) { - general_ctx = pcre2_general_context_create_16(&_regex_malloc, &_regex_free, nullptr); } else { - general_ctx = pcre2_general_context_create_32(&_regex_malloc, &_regex_free, nullptr); } code = nullptr; } RegEx::RegEx(const String &p_pattern) { - if (sizeof(CharType) == 2) { - general_ctx = pcre2_general_context_create_16(&_regex_malloc, &_regex_free, nullptr); } else { - general_ctx = pcre2_general_context_create_32(&_regex_malloc, &_regex_free, nullptr); } code = nullptr; @@ -503,15 +459,12 @@ RegEx::RegEx(const String &p_pattern) { } RegEx::~RegEx() { - if (sizeof(CharType) == 2) { - if (code) pcre2_code_free_16((pcre2_code_16 *)code); pcre2_general_context_free_16((pcre2_general_context_16 *)general_ctx); } else { - if (code) pcre2_code_free_32((pcre2_code_32 *)code); pcre2_general_context_free_32((pcre2_general_context_32 *)general_ctx); @@ -519,7 +472,6 @@ RegEx::~RegEx() { } void RegEx::_bind_methods() { - ClassDB::bind_method(D_METHOD("clear"), &RegEx::clear); ClassDB::bind_method(D_METHOD("compile", "pattern"), &RegEx::compile); ClassDB::bind_method(D_METHOD("search", "subject", "offset", "end"), &RegEx::search, DEFVAL(0), DEFVAL(-1)); -- cgit v1.2.3 From 0ee0fa42e6639b6fa474b7cf6afc6b1a78142185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Thu, 14 May 2020 16:41:43 +0200 Subject: Style: Enforce braces around if blocks and loops Using clang-tidy's `readability-braces-around-statements`. https://clang.llvm.org/extra/clang-tidy/checks/readability-braces-around-statements.html --- modules/regex/regex.cpp | 51 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'modules/regex/regex.cpp') diff --git a/modules/regex/regex.cpp b/modules/regex/regex.cpp index c0627b8f25..50ca01067b 100644 --- a/modules/regex/regex.cpp +++ b/modules/regex/regex.cpp @@ -46,14 +46,16 @@ static void _regex_free(void *ptr, void *user) { int RegExMatch::_find(const Variant &p_name) const { if (p_name.is_num()) { int i = (int)p_name; - if (i >= data.size()) + if (i >= data.size()) { return -1; + } return i; } else if (p_name.get_type() == Variant::STRING) { const Map::Element *found = names.find((String)p_name); - if (found) + if (found) { return found->value(); + } } return -1; @@ -64,8 +66,9 @@ String RegExMatch::get_subject() const { } int RegExMatch::get_group_count() const { - if (data.size() == 0) + if (data.size() == 0) { return 0; + } return data.size() - 1; } @@ -103,13 +106,15 @@ Array RegExMatch::get_strings() const { String RegExMatch::get_string(const Variant &p_name) const { int id = _find(p_name); - if (id < 0) + if (id < 0) { return String(); + } int start = data[id].start; - if (start == -1) + if (start == -1) { return String(); + } int length = data[id].end - start; @@ -119,8 +124,9 @@ String RegExMatch::get_string(const Variant &p_name) const { int RegExMatch::get_start(const Variant &p_name) const { int id = _find(p_name); - if (id < 0) + if (id < 0) { return -1; + } return data[id].start; } @@ -128,8 +134,9 @@ int RegExMatch::get_start(const Variant &p_name) const { int RegExMatch::get_end(const Variant &p_name) const { int id = _find(p_name); - if (id < 0) + if (id < 0) { return -1; + } return data[id].end; } @@ -223,8 +230,9 @@ Ref RegEx::search(const String &p_subject, int p_offset, int p_end) Ref result = memnew(RegExMatch); int length = p_subject.length(); - if (p_end >= 0 && p_end < length) + if (p_end >= 0 && p_end < length) { length = p_end; + } if (sizeof(CharType) == 2) { pcre2_code_16 *c = (pcre2_code_16 *)code; @@ -297,11 +305,13 @@ Ref RegEx::search(const String &p_subject, int p_offset, int p_end) for (uint32_t i = 0; i < count; i++) { CharType id = table[i * entry_size]; - if (result->data[id].start == -1) + if (result->data[id].start == -1) { continue; + } String name = &table[i * entry_size + 1]; - if (result->names.has(name)) + if (result->names.has(name)) { continue; + } result->names.insert(name, id); } @@ -314,8 +324,9 @@ Array RegEx::search_all(const String &p_subject, int p_offset, int p_end) const Array result; Ref match = search(p_subject, p_offset, p_end); while (match.is_valid()) { - if (last_end == match->get_end(0)) + if (last_end == match->get_end(0)) { break; + } result.push_back(match); last_end = match->get_end(0); match = search(p_subject, match->get_end(0), p_end); @@ -337,12 +348,14 @@ String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_a output.resize(olength + safety_zone); uint32_t flags = PCRE2_SUBSTITUTE_OVERFLOW_LENGTH; - if (p_all) + if (p_all) { flags |= PCRE2_SUBSTITUTE_GLOBAL; + } PCRE2_SIZE length = p_subject.length(); - if (p_end >= 0 && (uint32_t)p_end < length) + if (p_end >= 0 && (uint32_t)p_end < length) { length = p_end; + } if (sizeof(CharType) == 2) { pcre2_code_16 *c = (pcre2_code_16 *)code; @@ -365,8 +378,9 @@ String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_a pcre2_match_data_free_16(match); pcre2_match_context_free_16(mctx); - if (res < 0) + if (res < 0) { return String(); + } } else { pcre2_code_32 *c = (pcre2_code_32 *)code; @@ -389,8 +403,9 @@ String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_a pcre2_match_data_free_32(match); pcre2_match_context_free_32(mctx); - if (res < 0) + if (res < 0) { return String(); + } } return String(output.ptr(), olength); @@ -460,13 +475,15 @@ RegEx::RegEx(const String &p_pattern) { RegEx::~RegEx() { if (sizeof(CharType) == 2) { - if (code) + if (code) { pcre2_code_free_16((pcre2_code_16 *)code); + } pcre2_general_context_free_16((pcre2_general_context_16 *)general_ctx); } else { - if (code) + if (code) { pcre2_code_free_32((pcre2_code_32 *)code); + } pcre2_general_context_free_32((pcre2_general_context_32 *)general_ctx); } } -- cgit v1.2.3