Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 755487bd authored by Josh Wu's avatar Josh Wu
Browse files

gd/common: Replace variable length array

Variable-length array is a C99-feature, but not always allowed by
the C++ standard.

Bug: 203730542
Tag: #gd-refactor
Test: cert/run
Change-Id: I3c1a91f2c8e76a3d3a9fbe38da93be5337a6960d
parent 91c70e5a
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -98,6 +98,7 @@ std::string ToString(uint64_t value);
std::optional<bool> BoolFromString(const std::string& str);
std::string ToString(bool value);

// Migrate this method to std::format when C++20 becomes available
// printf like formatting to std::string
// format must contains format information, to print a string use StringFormat("%s", str)
template <typename... Args>
@@ -105,8 +106,8 @@ std::string StringFormat(const std::string& format, Args... args) {
  auto size = std::snprintf(nullptr, 0, format.c_str(), args...);
  ASSERT_LOG(size >= 0, "return value %d, error %d, text '%s'", size, errno, strerror(errno));
  // Add 1 for terminating null byte
  char buffer[size + 1];
  auto actual_size = std::snprintf(buffer, sizeof(buffer), format.c_str(), args...);
  std::vector<char> buffer(size + 1);
  auto actual_size = std::snprintf(buffer.data(), buffer.size(), format.c_str(), args...);
  ASSERT_LOG(
      size == actual_size,
      "asked size %d, actual size %d, error %d, text '%s'",
@@ -115,7 +116,7 @@ std::string StringFormat(const std::string& format, Args... args) {
      errno,
      strerror(errno));
  // Exclude the terminating null byte
  return std::string(buffer, size);
  return std::string(buffer.data(), size);
}

inline std::string StringFormatTime(const std::string& format, const struct std::tm& tm) {