cast.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. #include <util/system/defaults.h>
  2. #if defined(_freebsd_) && !defined(__LONG_LONG_SUPPORTED)
  3. #define __LONG_LONG_SUPPORTED
  4. #endif
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <filesystem>
  8. #include <string>
  9. #include <util/string/type.h>
  10. #include <util/string/cast.h>
  11. #include <util/string/escape.h>
  12. #include <contrib/libs/double-conversion/double-conversion/double-conversion.h>
  13. #include <util/generic/string.h>
  14. #include <util/system/yassert.h>
  15. #include <util/generic/yexception.h>
  16. #include <util/generic/typetraits.h>
  17. #include <util/generic/ylimits.h>
  18. #include <util/generic/singleton.h>
  19. #include <util/generic/utility.h>
  20. using double_conversion::DoubleToStringConverter;
  21. using double_conversion::StringBuilder;
  22. using double_conversion::StringToDoubleConverter;
  23. /*
  24. * ------------------------------ formatters ------------------------------
  25. */
  26. namespace {
  27. // clang-format off
  28. constexpr int LetterToIntMap[] = {
  29. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  30. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  31. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  32. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  33. 20, 20, 20, 20, 20, 20, 20, 20, 0, 1,
  34. 2, 3, 4, 5, 6, 7, 8, 9, 20, 20,
  35. 20, 20, 20, 20, 20, 10, 11, 12, 13, 14,
  36. 15, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  37. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  38. 20, 20, 20, 20, 20, 20, 20, 10, 11, 12,
  39. 13, 14, 15,
  40. };
  41. // clang-format on
  42. template <class T>
  43. std::make_signed_t<T> NegatePositiveSigned(T value) noexcept {
  44. return value > 0 ? (-std::make_signed_t<T>(value - 1) - 1) : 0;
  45. }
  46. template <class T>
  47. struct TFltModifiers;
  48. template <class T, int base, class TChar>
  49. Y_NO_INLINE size_t FormatInt(T value, TChar* buf, size_t len) {
  50. return TIntStringBuf<T, base, TChar>::Convert(value, buf, len);
  51. }
  52. template <class T>
  53. inline size_t FormatFlt(T t, char* buf, size_t len) {
  54. const int ret = snprintf(buf, len, TFltModifiers<T>::ModifierWrite, t);
  55. Y_ENSURE(ret >= 0 && (size_t)ret <= len, TStringBuf("cannot format float"));
  56. return (size_t)ret;
  57. }
  58. enum EParseStatus {
  59. PS_OK = 0,
  60. PS_EMPTY_STRING,
  61. PS_PLUS_STRING,
  62. PS_MINUS_STRING,
  63. PS_BAD_SYMBOL,
  64. PS_OVERFLOW,
  65. };
  66. constexpr ui8 SAFE_LENS[4][17] = {
  67. {0, 0, 7, 5, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
  68. {0, 0, 15, 10, 7, 6, 6, 5, 5, 5, 4, 4, 4, 4, 4, 4, 3},
  69. {0, 0, 31, 20, 15, 13, 12, 11, 10, 10, 9, 9, 8, 8, 8, 8, 7},
  70. {0, 0, 63, 40, 31, 27, 24, 22, 21, 20, 19, 18, 17, 17, 16, 16, 15},
  71. };
  72. inline constexpr ui8 ConstLog2(ui8 x) noexcept {
  73. return x == 1 ? 0 : 1 + ConstLog2(x / 2);
  74. }
  75. template <unsigned BASE, class TChar, class T>
  76. inline std::enable_if_t<(BASE > 10), bool> CharToDigit(TChar c, T* digit) noexcept {
  77. unsigned uc = c;
  78. if (uc >= Y_ARRAY_SIZE(LetterToIntMap)) {
  79. return false;
  80. }
  81. *digit = LetterToIntMap[uc];
  82. return *digit < BASE;
  83. }
  84. template <unsigned BASE, class TChar, class T>
  85. inline std::enable_if_t<(BASE <= 10), bool> CharToDigit(TChar c, T* digit) noexcept {
  86. return (c >= '0') && ((*digit = (c - '0')) < BASE);
  87. }
  88. template <class T, unsigned base, class TChar>
  89. struct TBasicIntParser {
  90. static_assert(1 < base && base < 17, "Expect 1 < base && base < 17.");
  91. static_assert(std::is_unsigned<T>::value, "TBasicIntParser can only handle unsigned integers.");
  92. enum : unsigned {
  93. BASE_POW_2 = base * base,
  94. };
  95. static inline EParseStatus Parse(const TChar** ppos, const TChar* end, T max, T* target) noexcept {
  96. Y_ASSERT(*ppos != end); /* This check should be somewhere up the stack. */
  97. const size_t maxSafeLen = SAFE_LENS[ConstLog2(sizeof(T))][base];
  98. // can parse without overflow
  99. if (size_t(end - *ppos) <= maxSafeLen) {
  100. T result;
  101. if (ParseFast(*ppos, end, &result) && result <= max) {
  102. *target = result;
  103. return PS_OK;
  104. }
  105. }
  106. return ParseSlow(ppos, end, max, target);
  107. }
  108. static inline bool ParseFast(const TChar* pos, const TChar* end, T* target) noexcept {
  109. T result = T();
  110. T d1;
  111. T d2;
  112. // we have end > pos
  113. auto beforeEnd = end - 1;
  114. while (pos < beforeEnd && CharToDigit<base>(*pos, &d1) && CharToDigit<base>(*(pos + 1), &d2)) {
  115. result = result * BASE_POW_2 + d1 * base + d2;
  116. pos += 2;
  117. }
  118. while (pos != end && CharToDigit<base>(*pos, &d1)) {
  119. result = result * base + d1;
  120. ++pos;
  121. }
  122. *target = result;
  123. return pos == end;
  124. }
  125. static inline EParseStatus ParseSlow(const TChar** ppos, const TChar* end, T max, T* target) noexcept {
  126. T result = T();
  127. T preMulMax = max / base;
  128. const TChar* pos = *ppos;
  129. while (pos != end) {
  130. T digit;
  131. if (!CharToDigit<base>(*pos, &digit)) {
  132. *ppos = pos;
  133. return PS_BAD_SYMBOL;
  134. }
  135. if (result > preMulMax) {
  136. return PS_OVERFLOW;
  137. }
  138. result *= base;
  139. if (result > max - digit) {
  140. return PS_OVERFLOW;
  141. }
  142. result += digit;
  143. pos++;
  144. }
  145. *target = result;
  146. return PS_OK;
  147. }
  148. };
  149. template <class T>
  150. struct TBounds {
  151. T PositiveMax;
  152. T NegativeMax;
  153. };
  154. template <class T, unsigned base, class TChar>
  155. struct TIntParser {
  156. static_assert(1 < base && base < 17, "Expect 1 < base && base < 17.");
  157. static_assert(std::is_integral<T>::value, "T must be an integral type.");
  158. enum {
  159. IsSigned = std::is_signed<T>::value
  160. };
  161. using TUnsigned = std::make_unsigned_t<T>;
  162. static inline EParseStatus Parse(const TChar** ppos, const TChar* end, const TBounds<TUnsigned>& bounds, T* target) {
  163. const TChar* pos = *ppos;
  164. if (pos == end) {
  165. return PS_EMPTY_STRING;
  166. }
  167. bool negative = false;
  168. TUnsigned max;
  169. if (*pos == '+') {
  170. pos++;
  171. max = bounds.PositiveMax;
  172. if (pos == end) {
  173. return PS_PLUS_STRING;
  174. }
  175. } else if (IsSigned && *pos == '-') {
  176. pos++;
  177. max = bounds.NegativeMax;
  178. negative = true;
  179. if (pos == end) {
  180. return PS_MINUS_STRING;
  181. }
  182. } else {
  183. max = bounds.PositiveMax;
  184. }
  185. TUnsigned result;
  186. EParseStatus error = TBasicIntParser<TUnsigned, base, TChar>::Parse(&pos, end, max, &result);
  187. if (error != PS_OK) {
  188. *ppos = pos;
  189. return error;
  190. }
  191. if (IsSigned) {
  192. *target = negative ? NegatePositiveSigned(result) : static_cast<T>(result);
  193. } else {
  194. *target = result;
  195. }
  196. return PS_OK;
  197. }
  198. };
  199. template <class TChar>
  200. [[noreturn]] static Y_NO_INLINE void ThrowParseError(EParseStatus status, const TChar* data, size_t len, const TChar* pos) {
  201. Y_ASSERT(status != PS_OK);
  202. typedef TBasicString<TChar> TStringType;
  203. switch (status) {
  204. case PS_EMPTY_STRING:
  205. ythrow TFromStringException() << TStringBuf("Cannot parse empty string as number. ");
  206. case PS_PLUS_STRING:
  207. ythrow TFromStringException() << TStringBuf("Cannot parse string \"+\" as number. ");
  208. case PS_MINUS_STRING:
  209. ythrow TFromStringException() << TStringBuf("Cannot parse string \"-\" as number. ");
  210. case PS_BAD_SYMBOL:
  211. ythrow TFromStringException() << TStringBuf("Unexpected symbol \"") << EscapeC(*pos) << TStringBuf("\" at pos ") << (pos - data) << TStringBuf(" in string ") << TStringType(data, len).Quote() << TStringBuf(". ");
  212. case PS_OVERFLOW:
  213. ythrow TFromStringException() << TStringBuf("Integer overflow in string ") << TStringType(data, len).Quote() << TStringBuf(". ");
  214. default:
  215. ythrow yexception() << TStringBuf("Unknown error code in string converter. ");
  216. }
  217. }
  218. template <typename T, typename TUnsigned, int base, typename TChar>
  219. Y_NO_INLINE T ParseInt(const TChar* data, size_t len, const TBounds<TUnsigned>& bounds) {
  220. T result;
  221. const TChar* pos = data;
  222. EParseStatus status = TIntParser<T, base, TChar>::Parse(&pos, pos + len, bounds, &result);
  223. if (status == PS_OK) {
  224. return result;
  225. } else {
  226. ThrowParseError(status, data, len, pos);
  227. }
  228. }
  229. template <typename T, typename TUnsigned, int base, typename TChar>
  230. Y_NO_INLINE bool TryParseInt(const TChar* data, size_t len, const TBounds<TUnsigned>& bounds, T* result) {
  231. return TIntParser<T, base, TChar>::Parse(&data, data + len, bounds, result) == PS_OK;
  232. }
  233. template <class T>
  234. inline T ParseFlt(const char* data, size_t len) {
  235. /*
  236. * TODO
  237. */
  238. if (len > 256) {
  239. len = 256;
  240. }
  241. char* c = (char*)alloca(len + 1);
  242. memcpy(c, data, len);
  243. c[len] = 0;
  244. T ret;
  245. char ec;
  246. // try to read a value and an extra character in order to catch cases when
  247. // the string start with a valid float but is followed by unexpected characters
  248. if (sscanf(c, TFltModifiers<T>::ModifierReadAndChar, &ret, &ec) == 1) {
  249. return ret;
  250. }
  251. ythrow TFromStringException() << TStringBuf("cannot parse float(") << TStringBuf(data, len) << TStringBuf(")");
  252. }
  253. #define DEF_FLT_MOD(type, modifierWrite, modifierRead) \
  254. template <> \
  255. struct TFltModifiers<type> { \
  256. static const char* const ModifierWrite; \
  257. static const char* const ModifierReadAndChar; \
  258. }; \
  259. \
  260. const char* const TFltModifiers<type>::ModifierWrite = modifierWrite; \
  261. const char* const TFltModifiers<type>::ModifierReadAndChar = modifierRead "%c";
  262. DEF_FLT_MOD(long double, "%.10Lg", "%Lg")
  263. #undef DEF_FLT_MOD
  264. /* The following constants are initialized in terms of <climits> constants to make
  265. * sure they go into binary as actual values and there is no associated
  266. * initialization code.
  267. * */
  268. constexpr TBounds<ui64> bSBounds = {static_cast<ui64>(SCHAR_MAX), static_cast<ui64>(UCHAR_MAX - SCHAR_MAX)};
  269. constexpr TBounds<ui64> bUBounds = {static_cast<ui64>(UCHAR_MAX), 0};
  270. constexpr TBounds<ui64> sSBounds = {static_cast<ui64>(SHRT_MAX), static_cast<ui64>(USHRT_MAX - SHRT_MAX)};
  271. constexpr TBounds<ui64> sUBounds = {static_cast<ui64>(USHRT_MAX), 0};
  272. constexpr TBounds<ui64> iSBounds = {static_cast<ui64>(INT_MAX), static_cast<ui64>(UINT_MAX - INT_MAX)};
  273. constexpr TBounds<ui64> iUBounds = {static_cast<ui64>(UINT_MAX), 0};
  274. constexpr TBounds<ui64> lSBounds = {static_cast<ui64>(LONG_MAX), static_cast<ui64>(ULONG_MAX - LONG_MAX)};
  275. constexpr TBounds<ui64> lUBounds = {static_cast<ui64>(ULONG_MAX), 0};
  276. constexpr TBounds<ui64> llSBounds = {static_cast<ui64>(LLONG_MAX), static_cast<ui64>(ULLONG_MAX - LLONG_MAX)};
  277. constexpr TBounds<ui64> llUBounds = {static_cast<ui64>(ULLONG_MAX), 0};
  278. }
  279. #define DEF_INT_SPEC_II(TYPE, ITYPE, BASE) \
  280. template <> \
  281. size_t IntToString<BASE, TYPE>(TYPE value, char* buf, size_t len) { \
  282. return FormatInt<ITYPE, BASE, char>(value, buf, len); \
  283. }
  284. #define DEF_INT_SPEC_I(TYPE, ITYPE) \
  285. template <> \
  286. size_t ToStringImpl<TYPE>(TYPE value, char* buf, size_t len) { \
  287. return FormatInt<ITYPE, 10, char>(value, buf, len); \
  288. } \
  289. DEF_INT_SPEC_II(TYPE, ITYPE, 2) \
  290. DEF_INT_SPEC_II(TYPE, ITYPE, 8) \
  291. DEF_INT_SPEC_II(TYPE, ITYPE, 10) \
  292. DEF_INT_SPEC_II(TYPE, ITYPE, 16)
  293. #define DEF_INT_SPEC(TYPE) \
  294. DEF_INT_SPEC_I(signed TYPE, i64) \
  295. DEF_INT_SPEC_I(unsigned TYPE, ui64)
  296. DEF_INT_SPEC(char)
  297. DEF_INT_SPEC(short)
  298. DEF_INT_SPEC(int)
  299. DEF_INT_SPEC(long)
  300. DEF_INT_SPEC(long long)
  301. #ifdef __cpp_char8_t
  302. template <>
  303. size_t ToStringImpl<char8_t>(char8_t value, char* buf, size_t len) {
  304. return FormatInt<ui64, 10, char>(value, buf, len);
  305. }
  306. #endif
  307. using TCharIType = std::conditional_t<std::is_signed<char>::value, i64, ui64>;
  308. using TWCharIType = std::conditional_t<std::is_signed<wchar_t>::value, i64, ui64>;
  309. DEF_INT_SPEC_I(char, TCharIType)
  310. DEF_INT_SPEC_I(wchar_t, TWCharIType)
  311. DEF_INT_SPEC_I(wchar16, ui64) // wchar16 is always unsigned
  312. DEF_INT_SPEC_I(wchar32, ui64) // wchar32 is always unsigned
  313. #undef DEF_INT_SPEC
  314. #undef DEF_INT_SPEC_I
  315. #undef DEF_INT_SPEC_II
  316. #define DEF_FLT_SPEC(type) \
  317. template <> \
  318. size_t ToStringImpl<type>(type t, char* buf, size_t len) { \
  319. return FormatFlt<type>(t, buf, len); \
  320. }
  321. DEF_FLT_SPEC(long double)
  322. #undef DEF_FLT_SPEC
  323. template <>
  324. size_t ToStringImpl<bool>(bool t, char* buf, size_t len) {
  325. Y_ENSURE(len, TStringBuf("zero length"));
  326. *buf = t ? '1' : '0';
  327. return 1;
  328. }
  329. /*
  330. * ------------------------------ parsers ------------------------------
  331. */
  332. template <>
  333. bool TryFromStringImpl<bool>(const char* data, size_t len, bool& result) {
  334. if (len == 1) {
  335. if (data[0] == '0') {
  336. result = false;
  337. return true;
  338. } else if (data[0] == '1') {
  339. result = true;
  340. return true;
  341. }
  342. }
  343. TStringBuf buf(data, len);
  344. if (IsTrue(buf)) {
  345. result = true;
  346. return true;
  347. } else if (IsFalse(buf)) {
  348. result = false;
  349. return true;
  350. }
  351. return false;
  352. }
  353. template <>
  354. bool FromStringImpl<bool>(const char* data, size_t len) {
  355. bool result;
  356. if (!TryFromStringImpl<bool>(data, len, result)) {
  357. ythrow TFromStringException() << TStringBuf("Cannot parse bool(") << TStringBuf(data, len) << TStringBuf("). ");
  358. }
  359. return result;
  360. }
  361. template <>
  362. TString FromStringImpl<TString>(const char* data, size_t len) {
  363. return TString(data, len);
  364. }
  365. template <>
  366. TStringBuf FromStringImpl<TStringBuf>(const char* data, size_t len) {
  367. return TStringBuf(data, len);
  368. }
  369. template <>
  370. std::string FromStringImpl<std::string>(const char* data, size_t len) {
  371. return std::string(data, len);
  372. }
  373. #ifndef USE_STL_SYSTEM
  374. // FIXME thegeorg@: remove #ifndef upon raising minimal macOS version to 10.15 in https://st.yandex-team.ru/DTCC-836
  375. template <>
  376. std::filesystem::path FromStringImpl<std::filesystem::path>(const char* data, size_t len) {
  377. return std::filesystem::path(std::string(data, len));
  378. }
  379. #endif
  380. template <>
  381. TUtf16String FromStringImpl<TUtf16String>(const wchar16* data, size_t len) {
  382. return TUtf16String(data, len);
  383. }
  384. template <>
  385. TWtringBuf FromStringImpl<TWtringBuf>(const wchar16* data, size_t len) {
  386. return TWtringBuf(data, len);
  387. }
  388. // Try-versions
  389. template <>
  390. bool TryFromStringImpl<TStringBuf>(const char* data, size_t len, TStringBuf& result) {
  391. result = {data, len};
  392. return true;
  393. }
  394. template <>
  395. bool TryFromStringImpl<TString>(const char* data, size_t len, TString& result) {
  396. result = TString(data, len);
  397. return true;
  398. }
  399. template <>
  400. bool TryFromStringImpl<std::string>(const char* data, size_t len, std::string& result) {
  401. result.assign(data, len);
  402. return true;
  403. }
  404. template <>
  405. bool TryFromStringImpl<TWtringBuf>(const wchar16* data, size_t len, TWtringBuf& result) {
  406. result = {data, len};
  407. return true;
  408. }
  409. template <>
  410. bool TryFromStringImpl<TUtf16String>(const wchar16* data, size_t len, TUtf16String& result) {
  411. result = TUtf16String(data, len);
  412. return true;
  413. }
  414. #define DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, BASE) \
  415. template <> \
  416. TYPE IntFromString<TYPE, BASE>(const CHAR* data, size_t len) { \
  417. return ParseInt<ITYPE, ui64, BASE>(data, len, BOUNDS); \
  418. } \
  419. template <> \
  420. bool TryIntFromString<BASE>(const CHAR* data, size_t len, TYPE& result) { \
  421. ITYPE tmp; \
  422. bool status = TryParseInt<ITYPE, ui64, BASE>(data, len, BOUNDS, &tmp); \
  423. if (status) { \
  424. result = tmp; \
  425. } \
  426. return status; \
  427. }
  428. #define DEF_INT_SPEC_II(CHAR, TYPE, ITYPE, BOUNDS) \
  429. template <> \
  430. TYPE FromStringImpl<TYPE>(const CHAR* data, size_t len) { \
  431. return ParseInt<ITYPE, ui64, 10>(data, len, BOUNDS); \
  432. } \
  433. template <> \
  434. bool TryFromStringImpl<TYPE>(const CHAR* data, size_t len, TYPE& result) { \
  435. ITYPE tmp; \
  436. bool status = TryParseInt<ITYPE, ui64, 10>(data, len, BOUNDS, &tmp); \
  437. if (status) { \
  438. result = tmp; \
  439. } \
  440. return status; \
  441. } \
  442. DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 2) \
  443. DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 8) \
  444. DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 10) \
  445. DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 16)
  446. #define DEF_INT_SPEC_I(TYPE, ITYPE, BOUNDS) \
  447. DEF_INT_SPEC_II(char, TYPE, ITYPE, BOUNDS) \
  448. DEF_INT_SPEC_II(wchar16, TYPE, ITYPE, BOUNDS)
  449. #define DEF_INT_SPEC(TYPE, ID) \
  450. DEF_INT_SPEC_I(signed TYPE, i64, ID##SBounds) \
  451. DEF_INT_SPEC_I(unsigned TYPE, ui64, ID##UBounds)
  452. #define DEF_INT_SPEC_FIXED_WIDTH(TYPE, ID) \
  453. DEF_INT_SPEC_I(TYPE, i64, ID##SBounds) \
  454. DEF_INT_SPEC_I(u##TYPE, ui64, ID##UBounds)
  455. DEF_INT_SPEC_FIXED_WIDTH(i8, b)
  456. DEF_INT_SPEC(short, s)
  457. DEF_INT_SPEC(int, i)
  458. DEF_INT_SPEC(long, l)
  459. DEF_INT_SPEC(long long, ll)
  460. #undef DEF_INT_SPEC_FIXED_WIDTH
  461. #undef DEF_INT_SPEC
  462. #undef DEF_INT_SPEC_I
  463. #undef DEF_INT_SPEC_II
  464. #undef DEF_INT_SPEC_III
  465. #define DEF_FLT_SPEC(type) \
  466. template <> \
  467. type FromStringImpl<type>(const char* data, size_t len) { \
  468. return ParseFlt<type>(data, len); \
  469. }
  470. DEF_FLT_SPEC(long double)
  471. #undef DEF_FLT_SPEC
  472. // Using StrToD for float and double because it is faster than sscanf.
  473. // Exception-free, specialized for float types
  474. template <>
  475. bool TryFromStringImpl<double>(const char* data, size_t len, double& result) {
  476. if (!len) {
  477. return false;
  478. }
  479. char* se = nullptr;
  480. double d = StrToD(data, data + len, &se);
  481. if (se != data + len) {
  482. return false;
  483. }
  484. result = d;
  485. return true;
  486. }
  487. template <>
  488. bool TryFromStringImpl<float>(const char* data, size_t len, float& result) {
  489. double d;
  490. if (TryFromStringImpl<double>(data, len, d)) {
  491. result = static_cast<float>(d);
  492. return true;
  493. }
  494. return false;
  495. }
  496. template <>
  497. bool TryFromStringImpl<long double>(const char* data, size_t len, long double& result) {
  498. double d;
  499. if (TryFromStringImpl<double>(data, len, d)) {
  500. result = static_cast<long double>(d);
  501. return true;
  502. }
  503. return false;
  504. }
  505. // Exception-throwing, specialized for float types
  506. template <>
  507. double FromStringImpl<double>(const char* data, size_t len) {
  508. double d = 0.0;
  509. if (!TryFromStringImpl(data, len, d)) {
  510. ythrow TFromStringException() << TStringBuf("cannot parse float(") << TStringBuf(data, len) << TStringBuf(")");
  511. }
  512. return d;
  513. }
  514. template <>
  515. float FromStringImpl<float>(const char* data, size_t len) {
  516. return static_cast<float>(FromStringImpl<double>(data, len));
  517. }
  518. double StrToD(const char* b, const char* e, char** se) {
  519. struct TCvt: public StringToDoubleConverter {
  520. inline TCvt()
  521. : StringToDoubleConverter(ALLOW_TRAILING_JUNK | ALLOW_HEX | ALLOW_LEADING_SPACES, 0.0, NAN, nullptr, nullptr)
  522. {
  523. }
  524. };
  525. int out = 0;
  526. const auto res = SingletonWithPriority<TCvt, 0>()->StringToDouble(b, e - b, &out);
  527. if (se) {
  528. *se = (char*)(b + out);
  529. }
  530. return res;
  531. }
  532. double StrToD(const char* b, char** se) {
  533. return StrToD(b, b + strlen(b), se);
  534. }
  535. namespace {
  536. static inline DoubleToStringConverter& ToStringConverterNoPad() noexcept {
  537. struct TCvt: public DoubleToStringConverter {
  538. inline TCvt() noexcept
  539. : DoubleToStringConverter(EMIT_POSITIVE_EXPONENT_SIGN, "inf", "nan", 'e', -10, 21, 4, 0)
  540. {
  541. }
  542. };
  543. return *SingletonWithPriority<TCvt, 0>();
  544. }
  545. struct TBuilder {
  546. alignas(StringBuilder) char Store[sizeof(StringBuilder)];
  547. StringBuilder* SB;
  548. inline TBuilder(char* buf, size_t len) noexcept
  549. : SB(new (Store) StringBuilder(buf, len))
  550. {
  551. }
  552. };
  553. static inline size_t FixZeros(char* buf, size_t len) noexcept {
  554. auto end = buf + len;
  555. auto point = (char*)memchr(buf, '.', len);
  556. if (!point) {
  557. return len;
  558. }
  559. auto exp = (char*)memchr(point, 'e', end - point);
  560. if (!exp) {
  561. exp = end;
  562. }
  563. auto c = exp;
  564. c -= 1;
  565. while (point < c && *c == '0') {
  566. --c;
  567. }
  568. if (*c == '.') {
  569. --c;
  570. }
  571. memmove(c + 1, exp, end - exp);
  572. return c - buf + 1 + end - exp;
  573. }
  574. static inline size_t FixEnd(char* buf, size_t len) noexcept {
  575. if (len > 2) {
  576. auto sign = buf[len - 2];
  577. if (sign == '-' || sign == '+') {
  578. buf[len] = buf[len - 1];
  579. buf[len - 1] = '0';
  580. ++len;
  581. }
  582. }
  583. buf[len] = 0;
  584. return len;
  585. }
  586. static inline size_t DoDtoa(double d, char* buf, size_t len, int prec) noexcept {
  587. TBuilder sb(buf, len);
  588. Y_VERIFY(ToStringConverterNoPad().ToPrecision(d, prec, sb.SB), "conversion failed");
  589. return FixEnd(buf, FixZeros(buf, sb.SB->position()));
  590. }
  591. }
  592. template <>
  593. size_t ToStringImpl<double>(double d, char* buf, size_t len) {
  594. return DoDtoa(d, buf, len, 10);
  595. }
  596. template <>
  597. size_t ToStringImpl<float>(float f, char* buf, size_t len) {
  598. return DoDtoa(f, buf, len, 6);
  599. }
  600. size_t FloatToString(float t, char* buf, size_t len, EFloatToStringMode mode, int ndigits) {
  601. if (mode == PREC_AUTO) {
  602. TBuilder sb(buf, len);
  603. Y_VERIFY(ToStringConverterNoPad().ToShortestSingle(t, sb.SB), "conversion failed");
  604. return FixEnd(buf, sb.SB->position());
  605. }
  606. return FloatToString((double)t, buf, len, mode, ndigits);
  607. }
  608. size_t FloatToString(double t, char* buf, size_t len, EFloatToStringMode mode, int ndigits) {
  609. if (mode == PREC_NDIGITS) {
  610. auto minDigits = DoubleToStringConverter::kMinPrecisionDigits;
  611. auto maxDigits = DoubleToStringConverter::kMaxPrecisionDigits;
  612. return DoDtoa(t, buf, len, ClampVal(ndigits, minDigits, maxDigits));
  613. }
  614. TBuilder sb(buf, len);
  615. if (mode == PREC_AUTO) {
  616. Y_VERIFY(ToStringConverterNoPad().ToShortest(t, sb.SB), "conversion failed");
  617. return FixEnd(buf, sb.SB->position());
  618. }
  619. if (!ToStringConverterNoPad().ToFixed(t, ndigits, sb.SB)) {
  620. return FloatToString(t, buf, len, PREC_AUTO);
  621. }
  622. if (mode == PREC_POINT_DIGITS_STRIP_ZEROES) {
  623. return FixZeros(buf, sb.SB->position());
  624. }
  625. return sb.SB->position();
  626. }