cast.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. #include <util/stream/str.h>
  4. #include <util/generic/string.h>
  5. #include <util/generic/strbuf.h>
  6. #include <util/generic/typetraits.h>
  7. #include <util/generic/yexception.h>
  8. /*
  9. * specialized for all arithmetic types
  10. */
  11. template <class T>
  12. size_t ToStringImpl(T t, char* buf, size_t len);
  13. /**
  14. * Converts @c t to string writing not more than @c len bytes to output buffer @c buf.
  15. * No NULL terminator appended! Throws exception on buffer overflow.
  16. * @return number of bytes written
  17. */
  18. template <class T>
  19. inline size_t ToString(const T& t, char* buf, size_t len) {
  20. using TParam = typename TTypeTraits<T>::TFuncParam;
  21. return ToStringImpl<TParam>(t, buf, len);
  22. }
  23. /**
  24. * Floating point to string conversion mode, values are enforced by `dtoa_impl.cpp`.
  25. */
  26. enum EFloatToStringMode {
  27. /** 0.1f -> "0.1", 0.12345678f -> "0.12345678", ignores ndigits. */
  28. PREC_AUTO = 0,
  29. /** "%g" mode, writes up to the given number of significant digits:
  30. * 0.1f -> "0.1", 0.12345678f -> "0.123457" for ndigits=6, 1.2e-06f -> "1.2e-06" */
  31. PREC_NDIGITS = 2,
  32. /** "%f" mode, writes the given number of digits after decimal point:
  33. * 0.1f -> "0.100000", 1.2e-06f -> "0.000001" for ndigits=6 */
  34. PREC_POINT_DIGITS = 3,
  35. /** same as PREC_POINT_DIGITS, but stripping trailing zeroes:
  36. * 0.1f for ndgigits=6 -> "0.1" */
  37. PREC_POINT_DIGITS_STRIP_ZEROES = 4
  38. };
  39. size_t FloatToString(float t, char* buf, size_t len, EFloatToStringMode mode = PREC_AUTO, int ndigits = 0);
  40. size_t FloatToString(double t, char* buf, size_t len, EFloatToStringMode mode = PREC_AUTO, int ndigits = 0);
  41. template <typename T>
  42. inline TString FloatToString(const T& t, EFloatToStringMode mode = PREC_AUTO, int ndigits = 0) {
  43. char buf[512]; // Max<double>() with mode = PREC_POINT_DIGITS has 309 digits before the decimal point
  44. size_t count = FloatToString(t, buf, sizeof(buf), mode, ndigits);
  45. return TString(buf, count);
  46. }
  47. namespace NPrivate {
  48. template <class T, bool isSimple>
  49. struct TToString {
  50. static inline TString Cvt(const T& t) {
  51. char buf[512];
  52. return TString(buf, ToString<T>(t, buf, sizeof(buf)));
  53. }
  54. };
  55. template <class T>
  56. struct TToString<T, false> {
  57. static inline TString Cvt(const T& t) {
  58. TString s;
  59. TStringOutput o(s);
  60. o << t;
  61. return s;
  62. }
  63. };
  64. }
  65. /*
  66. * some clever implementations...
  67. */
  68. template <class T>
  69. inline TString ToString(const T& t) {
  70. using TR = std::remove_cv_t<T>;
  71. return ::NPrivate::TToString<TR, std::is_arithmetic<TR>::value>::Cvt((const TR&)t);
  72. }
  73. inline const TString& ToString(const TString& s) noexcept {
  74. return s;
  75. }
  76. inline const TString& ToString(TString& s) noexcept {
  77. return s;
  78. }
  79. inline TString ToString(const char* s) {
  80. return s;
  81. }
  82. inline TString ToString(char* s) {
  83. return s;
  84. }
  85. /*
  86. * Wrapper for wide strings.
  87. */
  88. template <class T>
  89. inline TUtf16String ToWtring(const T& t) {
  90. return TUtf16String::FromAscii(ToString(t));
  91. }
  92. inline const TUtf16String& ToWtring(const TUtf16String& w) {
  93. return w;
  94. }
  95. inline const TUtf16String& ToWtring(TUtf16String& w) {
  96. return w;
  97. }
  98. struct TFromStringException: public TBadCastException {
  99. };
  100. /*
  101. * specialized for:
  102. * bool
  103. * short
  104. * unsigned short
  105. * int
  106. * unsigned int
  107. * long
  108. * unsigned long
  109. * long long
  110. * unsigned long long
  111. * float
  112. * double
  113. * long double
  114. */
  115. template <typename T, typename TChar>
  116. T FromStringImpl(const TChar* data, size_t len);
  117. template <typename T, typename TChar>
  118. inline T FromString(const TChar* data, size_t len) {
  119. return ::FromStringImpl<T>(data, len);
  120. }
  121. template <typename T, typename TChar>
  122. inline T FromString(const TChar* data) {
  123. return ::FromString<T>(data, std::char_traits<TChar>::length(data));
  124. }
  125. template <class T>
  126. inline T FromString(const TStringBuf& s) {
  127. return ::FromString<T>(s.data(), s.size());
  128. }
  129. template <class T>
  130. inline T FromString(const TString& s) {
  131. return ::FromString<T>(s.data(), s.size());
  132. }
  133. template <class T>
  134. inline T FromString(const std::string& s) {
  135. return ::FromString<T>(s.data(), s.size());
  136. }
  137. template <>
  138. inline TString FromString<TString>(const TString& s) {
  139. return s;
  140. }
  141. template <class T>
  142. inline T FromString(const TWtringBuf& s) {
  143. return ::FromString<T, typename TWtringBuf::char_type>(s.data(), s.size());
  144. }
  145. template <class T>
  146. inline T FromString(const TUtf16String& s) {
  147. return ::FromString<T, wchar16>(s.data(), s.size());
  148. }
  149. namespace NPrivate {
  150. template <typename TChar>
  151. class TFromString {
  152. const TChar* const Data;
  153. const size_t Len;
  154. public:
  155. inline TFromString(const TChar* data, size_t len)
  156. : Data(data)
  157. , Len(len)
  158. {
  159. }
  160. template <typename T>
  161. inline operator T() const {
  162. return FromString<T, TChar>(Data, Len);
  163. }
  164. };
  165. }
  166. template <typename TChar>
  167. inline ::NPrivate::TFromString<TChar> FromString(const TChar* data, size_t len) {
  168. return ::NPrivate::TFromString<TChar>(data, len);
  169. }
  170. template <typename TChar>
  171. inline ::NPrivate::TFromString<TChar> FromString(const TChar* data) {
  172. return ::NPrivate::TFromString<TChar>(data, std::char_traits<TChar>::length(data));
  173. }
  174. template <typename T>
  175. inline ::NPrivate::TFromString<typename T::TChar> FromString(const T& s) {
  176. return ::NPrivate::TFromString<typename T::TChar>(s.data(), s.size());
  177. }
  178. // Conversion exception free versions
  179. template <typename T, typename TChar>
  180. bool TryFromStringImpl(const TChar* data, size_t len, T& result);
  181. /**
  182. * @param data Source string buffer pointer
  183. * @param len Source string length, in characters
  184. * @param result Place to store conversion result value.
  185. * If conversion error occurs, no value stored in @c result
  186. * @return @c true in case of successful conversion, @c false otherwise
  187. **/
  188. template <typename T, typename TChar>
  189. inline bool TryFromString(const TChar* data, size_t len, T& result) {
  190. return TryFromStringImpl<T>(data, len, result);
  191. }
  192. template <typename T, typename TChar>
  193. inline bool TryFromString(const TChar* data, T& result) {
  194. return TryFromString<T>(data, std::char_traits<TChar>::length(data), result);
  195. }
  196. template <class T, class TChar>
  197. inline bool TryFromString(const TChar* data, const size_t len, T& result, const T& def) {
  198. if (TryFromString<T>(data, len, result)) {
  199. return true;
  200. }
  201. result = def;
  202. return false;
  203. }
  204. template <class T>
  205. inline bool TryFromString(const TStringBuf& s, T& result) {
  206. return TryFromString<T>(s.data(), s.size(), result);
  207. }
  208. template <class T>
  209. inline bool TryFromString(const TString& s, T& result) {
  210. return TryFromString<T>(s.data(), s.size(), result);
  211. }
  212. template <class T>
  213. inline bool TryFromString(const std::string& s, T& result) {
  214. return TryFromString<T>(s.data(), s.size(), result);
  215. }
  216. template <class T>
  217. inline bool TryFromString(const TWtringBuf& s, T& result) {
  218. return TryFromString<T>(s.data(), s.size(), result);
  219. }
  220. template <class T>
  221. inline bool TryFromString(const TUtf16String& s, T& result) {
  222. return TryFromString<T>(s.data(), s.size(), result);
  223. }
  224. template <class T, class TStringType>
  225. inline bool TryFromStringWithDefault(const TStringType& s, T& result, const T& def) {
  226. return TryFromString<T>(s.data(), s.size(), result, def);
  227. }
  228. template <class T>
  229. inline bool TryFromStringWithDefault(const char* s, T& result, const T& def) {
  230. return TryFromStringWithDefault<T>(TStringBuf(s), result, def);
  231. }
  232. template <class T, class TStringType>
  233. inline bool TryFromStringWithDefault(const TStringType& s, T& result) {
  234. return TryFromStringWithDefault<T>(s, result, T());
  235. }
  236. // FromString methods with default value if data is invalid
  237. template <class T, class TChar>
  238. inline T FromString(const TChar* data, const size_t len, const T& def) {
  239. T result;
  240. TryFromString<T>(data, len, result, def);
  241. return result;
  242. }
  243. template <class T, class TStringType>
  244. inline T FromStringWithDefault(const TStringType& s, const T& def) {
  245. return FromString<T>(s.data(), s.size(), def);
  246. }
  247. template <class T>
  248. inline T FromStringWithDefault(const char* s, const T& def) {
  249. return FromStringWithDefault<T>(TStringBuf(s), def);
  250. }
  251. template <class T, class TStringType>
  252. inline T FromStringWithDefault(const TStringType& s) {
  253. return FromStringWithDefault<T>(s, T());
  254. }
  255. double StrToD(const char* b, char** se);
  256. double StrToD(const char* b, const char* e, char** se);
  257. template <int base, class T>
  258. size_t IntToString(T t, char* buf, size_t len);
  259. template <int base, class T>
  260. inline TString IntToString(T t) {
  261. static_assert(std::is_arithmetic<std::remove_cv_t<T>>::value, "expect std::is_arithmetic<std::remove_cv_t<T>>::value");
  262. char buf[256];
  263. return TString(buf, IntToString<base>(t, buf, sizeof(buf)));
  264. }
  265. template <int base, class TInt, class TChar>
  266. bool TryIntFromString(const TChar* data, size_t len, TInt& result);
  267. template <int base, class TInt, class TStringType>
  268. inline bool TryIntFromString(const TStringType& s, TInt& result) {
  269. return TryIntFromString<base>(s.data(), s.size(), result);
  270. }
  271. template <class TInt, int base, class TChar>
  272. TInt IntFromString(const TChar* str, size_t len);
  273. template <class TInt, int base, class TChar>
  274. inline TInt IntFromString(const TChar* str) {
  275. return IntFromString<TInt, base>(str, std::char_traits<TChar>::length(str));
  276. }
  277. template <class TInt, int base, class TStringType>
  278. inline TInt IntFromString(const TStringType& str) {
  279. return IntFromString<TInt, base>(str.data(), str.size());
  280. }
  281. static inline TString ToString(const TStringBuf str) {
  282. return TString(str);
  283. }
  284. static inline TUtf16String ToWtring(const TWtringBuf wtr) {
  285. return TUtf16String(wtr);
  286. }
  287. static inline TUtf32String ToUtf32String(const TUtf32StringBuf wtr) {
  288. return TUtf32String(wtr);
  289. }