Skip to content

Commit

Permalink
Consistently namespace qualify size_t
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed May 7, 2020
1 parent c068514 commit 7f723fb
Show file tree
Hide file tree
Showing 39 changed files with 222 additions and 209 deletions.
17 changes: 8 additions & 9 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@ inline std::tm gmtime(std::time_t time) {
}

namespace internal {
inline std::size_t strftime(char* str, std::size_t count, const char* format,
const std::tm* time) {
inline size_t strftime(char* str, size_t count, const char* format,
const std::tm* time) {
return std::strftime(str, count, format, time);
}

inline std::size_t strftime(wchar_t* str, std::size_t count,
const wchar_t* format, const std::tm* time) {
inline size_t strftime(wchar_t* str, size_t count, const wchar_t* format,
const std::tm* time) {
return std::wcsftime(str, count, format, time);
}
} // namespace internal
Expand All @@ -414,11 +414,10 @@ template <typename Char> struct formatter<std::tm, Char> {
template <typename FormatContext>
auto format(const std::tm& tm, FormatContext& ctx) -> decltype(ctx.out()) {
basic_memory_buffer<Char> buf;
std::size_t start = buf.size();
size_t start = buf.size();
for (;;) {
std::size_t size = buf.capacity() - start;
std::size_t count =
internal::strftime(&buf[start], size, &tm_format[0], &tm);
size_t size = buf.capacity() - start;
size_t count = internal::strftime(&buf[start], size, &tm_format[0], &tm);
if (count != 0) {
buf.resize(start + count);
break;
Expand All @@ -430,7 +429,7 @@ template <typename Char> struct formatter<std::tm, Char> {
// https://github.com/fmtlib/fmt/issues/367
break;
}
const std::size_t MIN_GROWTH = 10;
const size_t MIN_GROWTH = 10;
buf.reserve(buf.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
}
return std::copy(buf.begin(), buf.end(), ctx.out());
Expand Down
4 changes: 2 additions & 2 deletions include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ template <typename Char> struct ansi_color_escape {
// 10 more.
if (is_background) value += 10u;

std::size_t index = 0;
size_t index = 0;
buffer[index++] = static_cast<Char>('\x1b');
buffer[index++] = static_cast<Char>('[');

Expand Down Expand Up @@ -398,7 +398,7 @@ template <typename Char> struct ansi_color_escape {
if (em_bits & static_cast<uint8_t>(emphasis::strikethrough))
em_codes[3] = 9;

std::size_t index = 0;
size_t index = 0;
for (int i = 0; i < 4; ++i) {
if (!em_codes[i]) continue;
buffer[index++] = static_cast<Char>('\x1b');
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n,
}

template <typename CompiledFormat, typename... Args>
std::size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
return format_to(internal::counting_iterator(), cf, args...).count();
}

Expand Down
26 changes: 13 additions & 13 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,27 +630,27 @@ namespace internal {
template <typename T> class buffer {
private:
T* ptr_;
std::size_t size_;
std::size_t capacity_;
size_t size_;
size_t capacity_;

protected:
// Don't initialize ptr_ since it is not accessed to save a few cycles.
FMT_SUPPRESS_MSC_WARNING(26495)
buffer(std::size_t sz) FMT_NOEXCEPT : size_(sz), capacity_(sz) {}
buffer(size_t sz) FMT_NOEXCEPT : size_(sz), capacity_(sz) {}

buffer(T* p = nullptr, std::size_t sz = 0, std::size_t cap = 0) FMT_NOEXCEPT
buffer(T* p = nullptr, size_t sz = 0, size_t cap = 0) FMT_NOEXCEPT
: ptr_(p),
size_(sz),
capacity_(cap) {}

/** Sets the buffer data and capacity. */
void set(T* buf_data, std::size_t buf_capacity) FMT_NOEXCEPT {
void set(T* buf_data, size_t buf_capacity) FMT_NOEXCEPT {
ptr_ = buf_data;
capacity_ = buf_capacity;
}

/** Increases the buffer capacity to hold at least *capacity* elements. */
virtual void grow(std::size_t capacity) = 0;
virtual void grow(size_t capacity) = 0;

public:
using value_type = T;
Expand All @@ -667,10 +667,10 @@ template <typename T> class buffer {
const T* end() const FMT_NOEXCEPT { return ptr_ + size_; }

/** Returns the size of this buffer. */
std::size_t size() const FMT_NOEXCEPT { return size_; }
size_t size() const FMT_NOEXCEPT { return size_; }

/** Returns the capacity of this buffer. */
std::size_t capacity() const FMT_NOEXCEPT { return capacity_; }
size_t capacity() const FMT_NOEXCEPT { return capacity_; }

/** Returns a pointer to the buffer data. */
T* data() FMT_NOEXCEPT { return ptr_; }
Expand All @@ -681,7 +681,7 @@ template <typename T> class buffer {
/**
Resizes the buffer. If T is a POD type new elements may not be initialized.
*/
void resize(std::size_t new_size) {
void resize(size_t new_size) {
reserve(new_size);
size_ = new_size;
}
Expand All @@ -690,7 +690,7 @@ template <typename T> class buffer {
void clear() { size_ = 0; }

/** Reserves space to store at least *capacity* elements. */
void reserve(std::size_t new_capacity) {
void reserve(size_t new_capacity) {
if (new_capacity > capacity_) grow(new_capacity);
}

Expand All @@ -715,7 +715,7 @@ class container_buffer : public buffer<typename Container::value_type> {
Container& container_;

protected:
void grow(std::size_t capacity) FMT_OVERRIDE {
void grow(size_t capacity) FMT_OVERRIDE {
container_.resize(capacity);
this->set(&container_[0], capacity);
}
Expand Down Expand Up @@ -869,12 +869,12 @@ constexpr bool is_arithmetic_type(type t) {

template <typename Char> struct string_value {
const Char* data;
std::size_t size;
size_t size;
};

template <typename Char> struct named_arg_value {
const named_arg_info<Char>* data;
std::size_t size;
size_t size;
};

template <typename Context> struct custom_value {
Expand Down
10 changes: 5 additions & 5 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
// Dummy implementations of strerror_r and strerror_s called if corresponding
// system functions are not available.
inline fmt::internal::null<> strerror_r(int, char*, ...) { return {}; }
inline fmt::internal::null<> strerror_s(char*, std::size_t, ...) { return {}; }
inline fmt::internal::null<> strerror_s(char*, size_t, ...) { return {}; }

FMT_BEGIN_NAMESPACE
namespace internal {
Expand Down Expand Up @@ -76,14 +76,14 @@ inline int fmt_snprintf(char* buffer, size_t size, const char* format, ...) {
// other - failure
// Buffer should be at least of size 1.
FMT_FUNC int safe_strerror(int error_code, char*& buffer,
std::size_t buffer_size) FMT_NOEXCEPT {
size_t buffer_size) FMT_NOEXCEPT {
FMT_ASSERT(buffer != nullptr && buffer_size != 0, "invalid buffer");

class dispatcher {
private:
int error_code_;
char*& buffer_;
std::size_t buffer_size_;
size_t buffer_size_;

// A noop assignment operator to avoid bogus warnings.
void operator=(const dispatcher&) {}
Expand Down Expand Up @@ -128,7 +128,7 @@ FMT_FUNC int safe_strerror(int error_code, char*& buffer,
#endif

public:
dispatcher(int err_code, char*& buf, std::size_t buf_size)
dispatcher(int err_code, char*& buf, size_t buf_size)
: error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {}

int run() { return handle(strerror_r(error_code_, buffer_, buffer_size_)); }
Expand All @@ -145,7 +145,7 @@ FMT_FUNC void format_error_code(internal::buffer<char>& out, int error_code,
static const char SEP[] = ": ";
static const char ERROR_STR[] = "error ";
// Subtract 2 to account for terminating null characters in SEP and ERROR_STR.
std::size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2;
size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2;
auto abs_value = static_cast<uint32_or_64_or_128_t<int>>(error_code);
if (internal::is_negative(error_code)) {
abs_value = 0 - abs_value;
Expand Down
Loading

5 comments on commit 7f723fb

@adromanov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask what is the reason of such change?

@vitaut
Copy link
Contributor Author

@vitaut vitaut commented on 7f723fb Jul 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency and removing redundant qualification.

@adromanov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitaut don't get me wrong, I am not pointing at what you should do and how should you write the code. I just share my vision.
This is C++ code and there are includes like #include <cstddef> instead of #include <stddef.h>. All aliases from <cstddef> are in std namespace.
I understand that these aliases work without std prefix with all major compilers, but strictly speaking there are no such guarantees.

@vitaut
Copy link
Contributor Author

@vitaut vitaut commented on 7f723fb Jul 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a problem we can change cstddef to stddef.h. The former is widely recognized as a mistake anyway.

@adromanov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no problem here, actually.
I just usually do the reverse change from size_t to std::size_t and here I wanted to hear arguments advocating the opposite point of view.
Thanks for your answers!

Please sign in to comment.