Add to_string functions for intx, uintx, bool, void*
This commit is contained in:
parent
efd61da7d0
commit
f13352975c
@ -156,6 +156,99 @@ public:
|
|||||||
|
|
||||||
bool operator==(const string_view& rhs) const;
|
bool operator==(const string_view& rhs) const;
|
||||||
bool operator!=(const string_view& rhs) const;
|
bool operator!=(const string_view& rhs) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Convert the signed integer to a string
|
||||||
|
*
|
||||||
|
* The value in str is overwritten.
|
||||||
|
*/
|
||||||
|
template <typename T, std::enable_if_t<std::is_signed_v<T>, bool> = true>
|
||||||
|
friend istring& to_string(T x, istring& str, bool hex = false) {
|
||||||
|
size_t num_char = 0;
|
||||||
|
#ifdef __ARM_ARCH_4T__
|
||||||
|
if (hex) {
|
||||||
|
num_char = mtl_itostrx(x, str.data());
|
||||||
|
} else {
|
||||||
|
num_char = mtl_itostr(x, str.data());
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
itoa(x, str.data(), hex ? 16 : 10);
|
||||||
|
num_char = strlen(str.data());
|
||||||
|
#endif
|
||||||
|
str.m_size = num_char;
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Convert the unsigned integer to a string
|
||||||
|
*
|
||||||
|
* The value in str is overwritten.
|
||||||
|
*
|
||||||
|
* The template must check that the integer is not a boolean because
|
||||||
|
* std::is_unsigned counts booleans as unsigned integers.
|
||||||
|
*/
|
||||||
|
template <typename T, std::enable_if_t<std::is_unsigned_v<T>
|
||||||
|
&& !std::is_same_v<T, bool>, bool> = true>
|
||||||
|
friend istring& to_string(T x, istring& str, bool hex = false) {
|
||||||
|
size_t num_char = 0;
|
||||||
|
#ifdef __ARM_ARCH_4T__
|
||||||
|
if (hex) {
|
||||||
|
num_char = mtl_utostrx(x, str.data());
|
||||||
|
} else {
|
||||||
|
num_char = mtl_utostr(x, str.data());
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
utoa(x, str.data(), hex ? 16 : 10);
|
||||||
|
num_char = strlen(str.data());
|
||||||
|
#endif
|
||||||
|
str.m_size = num_char;
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Convert the boolean to a string, using the provided buffer
|
||||||
|
*
|
||||||
|
* Writes the boolean as either "true" or "false". Depending on how
|
||||||
|
* this function is used, `x ? "true" : "false"` may be faster.
|
||||||
|
* Ex. mtl::string_stream does not use this function under the hood.
|
||||||
|
*
|
||||||
|
* The value in str is overwritten.
|
||||||
|
*
|
||||||
|
* This function needs to be templated otherwise non-boolean arguments
|
||||||
|
* could be implicitly converted to boolean.
|
||||||
|
*/
|
||||||
|
template <typename T, std::enable_if_t<std::is_same_v<T, bool>, bool> = true>
|
||||||
|
friend istring& to_string(T x, istring& str) {
|
||||||
|
str = x ? "true" : "false";
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Convert the address value to a string, using the provided buffer
|
||||||
|
*
|
||||||
|
* Always writes the address in hexidecimal format. Does not pad the
|
||||||
|
* value to the address width.
|
||||||
|
*
|
||||||
|
* The value in str is overwritten.
|
||||||
|
*
|
||||||
|
* The template must check that the pointer is not a character pointer
|
||||||
|
* otherwise C strings will be converted to their address string, not
|
||||||
|
* the C string.
|
||||||
|
*/
|
||||||
|
template <typename T, std::enable_if_t<std::is_pointer_v<T>
|
||||||
|
&& !std::is_same_v<T, char*>
|
||||||
|
&& !std::is_same_v<T, const char*>, bool> = true>
|
||||||
|
friend istring& to_string(T x, istring& str) {
|
||||||
|
size_t num_char = 0;
|
||||||
|
#ifdef __ARM_ARCH_4T__
|
||||||
|
num_char = mtl_utostrx(reinterpret_cast<uint32_t>(x), str.data());
|
||||||
|
#else
|
||||||
|
utoa(x, str.data(), 16);
|
||||||
|
num_char = strlen(str.data());
|
||||||
|
#endif
|
||||||
|
str.m_size = num_char;
|
||||||
|
return str;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <size_t C>
|
template <size_t C>
|
||||||
@ -183,4 +276,5 @@ public:
|
|||||||
|
|
||||||
using istring::operator=;
|
using istring::operator=;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ml
|
} // namespace ml
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user