diff options
Diffstat (limited to 'runtime/utils.h')
-rw-r--r-- | runtime/utils.h | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/runtime/utils.h b/runtime/utils.h index 68ea47541b..eb79968e21 100644 --- a/runtime/utils.h +++ b/runtime/utils.h @@ -203,6 +203,19 @@ static inline bool NeedsEscaping(uint16_t ch) { return (ch < ' ' || ch > '~'); } +// Interpret the bit pattern of input (type U) as type V. Requires the size +// of V >= size of U (compile-time checked). +template<typename U, typename V> +static inline V bit_cast(U in) { + COMPILE_ASSERT(sizeof(U) <= sizeof(V), size_of_u_not_le_size_of_v); + union { + U u; + V v; + } tmp; + tmp.u = in; + return tmp.v; +} + std::string PrintableChar(uint16_t ch); // Returns an ASCII string corresponding to the given UTF-8 string. |