mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 23:18:10 +01:00
Use to_string() from STL when available (#2992)
This commit is contained in:
parent
15ce27992e
commit
5143a5b5c5
2 changed files with 19 additions and 57 deletions
|
@ -197,52 +197,6 @@ uint8_t reverse_bits_8(uint8_t x) {
|
||||||
uint16_t reverse_bits_16(uint16_t x) {
|
uint16_t reverse_bits_16(uint16_t x) {
|
||||||
return uint16_t(reverse_bits_8(x & 0xFF) << 8) | uint16_t(reverse_bits_8(x >> 8));
|
return uint16_t(reverse_bits_8(x & 0xFF) << 8) | uint16_t(reverse_bits_8(x >> 8));
|
||||||
}
|
}
|
||||||
std::string to_string(const std::string &val) { return val; }
|
|
||||||
std::string to_string(int val) {
|
|
||||||
char buf[64];
|
|
||||||
sprintf(buf, "%d", val);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
std::string to_string(long val) { // NOLINT
|
|
||||||
char buf[64];
|
|
||||||
sprintf(buf, "%ld", val);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
std::string to_string(long long val) { // NOLINT
|
|
||||||
char buf[64];
|
|
||||||
sprintf(buf, "%lld", val);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
std::string to_string(unsigned val) { // NOLINT
|
|
||||||
char buf[64];
|
|
||||||
sprintf(buf, "%u", val);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
std::string to_string(unsigned long val) { // NOLINT
|
|
||||||
char buf[64];
|
|
||||||
sprintf(buf, "%lu", val);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
std::string to_string(unsigned long long val) { // NOLINT
|
|
||||||
char buf[64];
|
|
||||||
sprintf(buf, "%llu", val);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
std::string to_string(float val) {
|
|
||||||
char buf[64];
|
|
||||||
sprintf(buf, "%f", val);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
std::string to_string(double val) {
|
|
||||||
char buf[64];
|
|
||||||
sprintf(buf, "%f", val);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
std::string to_string(long double val) {
|
|
||||||
char buf[64];
|
|
||||||
sprintf(buf, "%Lf", val);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t fnv1_hash(const std::string &str) {
|
uint32_t fnv1_hash(const std::string &str) {
|
||||||
uint32_t hash = 2166136261UL;
|
uint32_t hash = 2166136261UL;
|
||||||
|
|
|
@ -36,17 +36,6 @@ std::string get_mac_address_pretty();
|
||||||
void set_mac_address(uint8_t *mac);
|
void set_mac_address(uint8_t *mac);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::string to_string(const std::string &val);
|
|
||||||
std::string to_string(int val);
|
|
||||||
std::string to_string(long val); // NOLINT
|
|
||||||
std::string to_string(long long val); // NOLINT
|
|
||||||
std::string to_string(unsigned val); // NOLINT
|
|
||||||
std::string to_string(unsigned long val); // NOLINT
|
|
||||||
std::string to_string(unsigned long long val); // NOLINT
|
|
||||||
std::string to_string(float val);
|
|
||||||
std::string to_string(double val);
|
|
||||||
std::string to_string(long double val);
|
|
||||||
|
|
||||||
/// Compare string a to string b (ignoring case) and return whether they are equal.
|
/// Compare string a to string b (ignoring case) and return whether they are equal.
|
||||||
bool str_equals_case_insensitive(const std::string &a, const std::string &b);
|
bool str_equals_case_insensitive(const std::string &a, const std::string &b);
|
||||||
bool str_startswith(const std::string &full, const std::string &start);
|
bool str_startswith(const std::string &full, const std::string &start);
|
||||||
|
@ -266,6 +255,22 @@ uint32_t fnv1_hash(const std::string &str);
|
||||||
/// @name STL backports
|
/// @name STL backports
|
||||||
///@{
|
///@{
|
||||||
|
|
||||||
|
// std::to_string() from C++11, available from libstdc++/g++ 8+
|
||||||
|
// See https://github.com/espressif/esp-idf/issues/1445
|
||||||
|
#if _GLIBCXX_RELEASE >= 8
|
||||||
|
using std::to_string;
|
||||||
|
#else
|
||||||
|
inline std::string to_string(int value) { return str_snprintf("%d", 32, value); } // NOLINT
|
||||||
|
inline std::string to_string(long value) { return str_snprintf("%ld", 32, value); } // NOLINT
|
||||||
|
inline std::string to_string(long long value) { return str_snprintf("%lld", 32, value); } // NOLINT
|
||||||
|
inline std::string to_string(unsigned value) { return str_snprintf("%u", 32, value); } // NOLINT
|
||||||
|
inline std::string to_string(unsigned long value) { return str_snprintf("%lu", 32, value); } // NOLINT
|
||||||
|
inline std::string to_string(unsigned long long value) { return str_snprintf("%llu", 32, value); } // NOLINT
|
||||||
|
inline std::string to_string(float value) { return str_snprintf("%f", 32, value); }
|
||||||
|
inline std::string to_string(double value) { return str_snprintf("%f", 32, value); }
|
||||||
|
inline std::string to_string(long double value) { return str_snprintf("%Lf", 32, value); }
|
||||||
|
#endif
|
||||||
|
|
||||||
// std::byteswap is from C++23 and technically should be a template, but this will do for now.
|
// std::byteswap is from C++23 and technically should be a template, but this will do for now.
|
||||||
constexpr uint8_t byteswap(uint8_t n) { return n; }
|
constexpr uint8_t byteswap(uint8_t n) { return n; }
|
||||||
constexpr uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); }
|
constexpr uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); }
|
||||||
|
@ -326,6 +331,9 @@ template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> constexpr
|
||||||
/// @name Strings
|
/// @name Strings
|
||||||
///@{
|
///@{
|
||||||
|
|
||||||
|
/// Convert the value to a string (added as extra overload so that to_string() can be used on all stringifiable types).
|
||||||
|
inline std::string to_string(const std::string &val) { return val; }
|
||||||
|
|
||||||
/// Truncate a string to a specific length.
|
/// Truncate a string to a specific length.
|
||||||
std::string str_truncate(const std::string &str, size_t length);
|
std::string str_truncate(const std::string &str, size_t length);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue