Skip to content

Commit c937b6d

Browse files
committed
Merge pull request godotengine#102419 from Ivorforce/std-size
Use `std::size` instead of `sizeof(a) / sizeof(a[0])` pattern throughout the codebase.
2 parents 3022157 + e34f1f5 commit c937b6d

33 files changed

+71
-67
lines changed

core/error/error_list.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#include "error_list.h"
3232

33+
#include <iterator>
34+
3335
const char *error_names[] = {
3436
"OK", // OK
3537
"Failed", // FAILED
@@ -82,4 +84,4 @@ const char *error_names[] = {
8284
"Printer on fire", // ERR_PRINTER_ON_FIRE
8385
};
8486

85-
static_assert(sizeof(error_names) / sizeof(*error_names) == ERR_MAX);
87+
static_assert(std::size(error_names) == ERR_MAX);

core/input/input_map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static const _BuiltinActionDisplayName _builtin_action_display_names[] = {
407407
};
408408

409409
String InputMap::get_builtin_display_name(const String &p_name) const {
410-
int len = sizeof(_builtin_action_display_names) / sizeof(_BuiltinActionDisplayName);
410+
constexpr int len = std::size(_builtin_action_display_names);
411411

412412
for (int i = 0; i < len; i++) {
413413
if (_builtin_action_display_names[i].name == p_name) {

core/io/file_access.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ class CharBuffer {
429429
public:
430430
_FORCE_INLINE_ CharBuffer() :
431431
buffer(stack_buffer),
432-
capacity(sizeof(stack_buffer) / sizeof(char)) {
432+
capacity(std::size(stack_buffer)) {
433433
}
434434

435435
_FORCE_INLINE_ void push_back(char c) {

core/io/resource_uid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ String ResourceUID::get_cache_file() {
4646
}
4747

4848
static constexpr uint8_t uuid_characters[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', '0', '1', '2', '3', '4', '5', '6', '7', '8' };
49-
static constexpr uint32_t uuid_characters_element_count = (sizeof(uuid_characters) / sizeof(*uuid_characters));
49+
static constexpr uint32_t uuid_characters_element_count = std::size(uuid_characters);
5050
static constexpr uint8_t max_uuid_number_length = 13; // Max 0x7FFFFFFFFFFFFFFF (uid://d4n4ub6itg400) size is 13 characters.
5151

5252
String ResourceUID::id_to_text(ID p_id) const {

core/math/color.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ int Color::find_named_color(const String &p_name) {
439439
}
440440

441441
int Color::get_named_color_count() {
442-
return sizeof(named_colors) / sizeof(NamedColor);
442+
return std::size(named_colors);
443443
}
444444

445445
String Color::get_named_color_name(int p_idx) {

core/string/char_utils.h

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,25 @@
3535

3636
#include "char_range.inc"
3737

38-
#define BSEARCH_CHAR_RANGE(m_array) \
39-
int low = 0; \
40-
int high = sizeof(m_array) / sizeof(m_array[0]) - 1; \
41-
int middle = (low + high) / 2; \
42-
\
43-
while (low <= high) { \
44-
if (p_char < m_array[middle].start) { \
45-
high = middle - 1; \
46-
} else if (p_char > m_array[middle].end) { \
47-
low = middle + 1; \
48-
} else { \
49-
return true; \
50-
} \
51-
\
52-
middle = (low + high) / 2; \
53-
} \
54-
\
38+
#include <iterator>
39+
40+
#define BSEARCH_CHAR_RANGE(m_array) \
41+
int low = 0; \
42+
int high = std::size(m_array) - 1; \
43+
int middle = (low + high) / 2; \
44+
\
45+
while (low <= high) { \
46+
if (p_char < m_array[middle].start) { \
47+
high = middle - 1; \
48+
} else if (p_char > m_array[middle].end) { \
49+
low = middle + 1; \
50+
} else { \
51+
return true; \
52+
} \
53+
\
54+
middle = (low + high) / 2; \
55+
} \
56+
\
5557
return false
5658

5759
constexpr bool is_unicode_identifier_start(char32_t p_char) {

core/string/translation_domain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ const char32_t *TranslationDomain::_get_accented_version(char32_t p_character) c
182182
return nullptr;
183183
}
184184

185-
for (unsigned int i = 0; i < sizeof(_character_to_accented) / sizeof(_character_to_accented[0]); i++) {
185+
for (unsigned int i = 0; i < std::size(_character_to_accented); i++) {
186186
if (_character_to_accented[i].character == p_character) {
187187
return _character_to_accented[i].accented_character;
188188
}

drivers/d3d12/rendering_context_driver_d3d12.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686

8787
using Microsoft::WRL::ComPtr;
8888

89-
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
89+
#define ARRAY_SIZE(a) std::size(a)
9090

9191
class RenderingContextDriverD3D12 : public RenderingContextDriver {
9292
ComPtr<ID3D12DeviceFactory> device_factory;

drivers/vulkan/rendering_device_driver_vulkan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include "thirdparty/swappy-frame-pacing/swappyVk.h"
4646
#endif
4747

48-
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
48+
#define ARRAY_SIZE(a) std::size(a)
4949

5050
#define PRINT_NATIVE_COMMANDS 0
5151

editor/code_editor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ CodeTextEditor::CodeTextEditor() {
19081908
zoom_button->set_text("100 %");
19091909

19101910
PopupMenu *zoom_menu = zoom_button->get_popup();
1911-
int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);
1911+
constexpr int preset_count = std::size(ZOOM_FACTOR_PRESETS);
19121912

19131913
for (int i = 0; i < preset_count; i++) {
19141914
float z = ZOOM_FACTOR_PRESETS[i];

0 commit comments

Comments
 (0)