cast.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. } // namespace
  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. template <>
  374. std::filesystem::path FromStringImpl<std::filesystem::path>(const char* data, size_t len) {
  375. return std::filesystem::path(std::string(data, len));
  376. }
  377. template <>
  378. TUtf16String FromStringImpl<TUtf16String>(const wchar16* data, size_t len) {
  379. return TUtf16String(data, len);
  380. }
  381. template <>
  382. TWtringBuf FromStringImpl<TWtringBuf>(const wchar16* data, size_t len) {
  383. return TWtringBuf(data, len);
  384. }
  385. // Try-versions
  386. template <>
  387. bool TryFromStringImpl<TStringBuf>(const char* data, size_t len, TStringBuf& result) {
  388. result = {data, len};
  389. return true;
  390. }
  391. template <>
  392. bool TryFromStringImpl<TString>(const char* data, size_t len, TString& result) {
  393. result = TString(data, len);
  394. return true;
  395. }
  396. template <>
  397. bool TryFromStringImpl<std::string>(const char* data, size_t len, std::string& result) {
  398. result.assign(data, len);
  399. return true;
  400. }
  401. template <>
  402. bool TryFromStringImpl<TWtringBuf>(const wchar16* data, size_t len, TWtringBuf& result) {
  403. result = {data, len};
  404. return true;
  405. }
  406. template <>
  407. bool TryFromStringImpl<TUtf16String>(const wchar16* data, size_t len, TUtf16String& result) {
  408. result = TUtf16String(data, len);
  409. return true;
  410. }
  411. #define DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, BASE) \
  412. template <> \
  413. TYPE IntFromString<TYPE, BASE>(const CHAR* data, size_t len) { \
  414. return ParseInt<ITYPE, ui64, BASE>(data, len, BOUNDS); \
  415. } \
  416. template <> \
  417. bool TryIntFromString<BASE>(const CHAR* data, size_t len, TYPE& result) { \
  418. ITYPE tmp; \
  419. bool status = TryParseInt<ITYPE, ui64, BASE>(data, len, BOUNDS, &tmp); \
  420. if (status) { \
  421. result = tmp; \
  422. } \
  423. return status; \
  424. }
  425. #define DEF_INT_SPEC_II(CHAR, TYPE, ITYPE, BOUNDS) \
  426. template <> \
  427. TYPE FromStringImpl<TYPE>(const CHAR* data, size_t len) { \
  428. return ParseInt<ITYPE, ui64, 10>(data, len, BOUNDS); \
  429. } \
  430. template <> \
  431. bool TryFromStringImpl<TYPE>(const CHAR* data, size_t len, TYPE& result) { \
  432. ITYPE tmp; \
  433. bool status = TryParseInt<ITYPE, ui64, 10>(data, len, BOUNDS, &tmp); \
  434. if (status) { \
  435. result = tmp; \
  436. } \
  437. return status; \
  438. } \
  439. DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 2) \
  440. DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 8) \
  441. DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 10) \
  442. DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 16)
  443. #define DEF_INT_SPEC_I(TYPE, ITYPE, BOUNDS) \
  444. DEF_INT_SPEC_II(char, TYPE, ITYPE, BOUNDS) \
  445. DEF_INT_SPEC_II(wchar16, TYPE, ITYPE, BOUNDS)
  446. #define DEF_INT_SPEC(TYPE, ID) \
  447. DEF_INT_SPEC_I(signed TYPE, i64, ID##SBounds) \
  448. DEF_INT_SPEC_I(unsigned TYPE, ui64, ID##UBounds)
  449. #define DEF_INT_SPEC_FIXED_WIDTH(TYPE, ID) \
  450. DEF_INT_SPEC_I(TYPE, i64, ID##SBounds) \
  451. DEF_INT_SPEC_I(u##TYPE, ui64, ID##UBounds)
  452. DEF_INT_SPEC_FIXED_WIDTH(i8, b)
  453. DEF_INT_SPEC(short, s)
  454. DEF_INT_SPEC(int, i)
  455. DEF_INT_SPEC(long, l)
  456. DEF_INT_SPEC(long long, ll)
  457. #undef DEF_INT_SPEC_FIXED_WIDTH
  458. #undef DEF_INT_SPEC
  459. #undef DEF_INT_SPEC_I
  460. #undef DEF_INT_SPEC_II
  461. #undef DEF_INT_SPEC_III
  462. #define DEF_FLT_SPEC(type) \
  463. template <> \
  464. type FromStringImpl<type>(const char* data, size_t len) { \
  465. return ParseFlt<type>(data, len); \
  466. }
  467. DEF_FLT_SPEC(long double)
  468. #undef DEF_FLT_SPEC
  469. // Using StrToD for float and double because it is faster than sscanf.
  470. // Exception-free, specialized for float types
  471. template <>
  472. bool TryFromStringImpl<double>(const char* data, size_t len, double& result) {
  473. if (!len) {
  474. return false;
  475. }
  476. char* se = nullptr;
  477. double d = StrToD(data, data + len, &se);
  478. if (se != data + len) {
  479. return false;
  480. }
  481. result = d;
  482. return true;
  483. }
  484. template <>
  485. bool TryFromStringImpl<float>(const char* data, size_t len, float& result) {
  486. double d;
  487. if (TryFromStringImpl<double>(data, len, d)) {
  488. result = static_cast<float>(d);
  489. return true;
  490. }
  491. return false;
  492. }
  493. template <>
  494. bool TryFromStringImpl<long double>(const char* data, size_t len, long double& result) {
  495. double d;
  496. if (TryFromStringImpl<double>(data, len, d)) {
  497. result = static_cast<long double>(d);
  498. return true;
  499. }
  500. return false;
  501. }
  502. // Exception-throwing, specialized for float types
  503. template <>
  504. double FromStringImpl<double>(const char* data, size_t len) {
  505. double d = 0.0;
  506. if (!TryFromStringImpl(data, len, d)) {
  507. ythrow TFromStringException() << TStringBuf("cannot parse float(") << TStringBuf(data, len) << TStringBuf(")");
  508. }
  509. return d;
  510. }
  511. template <>
  512. float FromStringImpl<float>(const char* data, size_t len) {
  513. return static_cast<float>(FromStringImpl<double>(data, len));
  514. }
  515. double StrToD(const char* b, const char* e, char** se) {
  516. struct TCvt: public StringToDoubleConverter {
  517. inline TCvt()
  518. : StringToDoubleConverter(ALLOW_TRAILING_JUNK | ALLOW_HEX | ALLOW_LEADING_SPACES, 0.0, NAN, nullptr, nullptr)
  519. {
  520. }
  521. };
  522. int out = 0;
  523. const auto res = SingletonWithPriority<TCvt, 0>()->StringToDouble(b, e - b, &out);
  524. if (se) {
  525. *se = (char*)(b + out);
  526. }
  527. return res;
  528. }
  529. double StrToD(const char* b, char** se) {
  530. return StrToD(b, b + strlen(b), se);
  531. }
  532. namespace {
  533. static inline DoubleToStringConverter& ToStringConverterNoPad() noexcept {
  534. struct TCvt: public DoubleToStringConverter {
  535. inline TCvt() noexcept
  536. : DoubleToStringConverter(EMIT_POSITIVE_EXPONENT_SIGN, "inf", "nan", 'e', -10, 21, 4, 0)
  537. {
  538. }
  539. };
  540. return *SingletonWithPriority<TCvt, 0>();
  541. }
  542. struct TBuilder {
  543. alignas(StringBuilder) char Store[sizeof(StringBuilder)];
  544. StringBuilder* SB;
  545. inline TBuilder(char* buf, size_t len) noexcept
  546. : SB(new (Store) StringBuilder(buf, len))
  547. {
  548. }
  549. };
  550. static inline size_t FixZeros(char* buf, size_t len) noexcept {
  551. auto end = buf + len;
  552. auto point = (char*)memchr(buf, '.', len);
  553. if (!point) {
  554. return len;
  555. }
  556. auto exp = (char*)memchr(point, 'e', end - point);
  557. if (!exp) {
  558. exp = end;
  559. }
  560. auto c = exp;
  561. c -= 1;
  562. while (point < c && *c == '0') {
  563. --c;
  564. }
  565. if (*c == '.') {
  566. --c;
  567. }
  568. memmove(c + 1, exp, end - exp);
  569. return c - buf + 1 + end - exp;
  570. }
  571. static inline size_t FixEnd(char* buf, size_t len) noexcept {
  572. if (len > 2) {
  573. auto sign = buf[len - 2];
  574. if (sign == '-' || sign == '+') {
  575. buf[len] = buf[len - 1];
  576. buf[len - 1] = '0';
  577. ++len;
  578. }
  579. }
  580. buf[len] = 0;
  581. return len;
  582. }
  583. static inline size_t DoDtoa(double d, char* buf, size_t len, int prec) noexcept {
  584. TBuilder sb(buf, len);
  585. Y_ABORT_UNLESS(ToStringConverterNoPad().ToPrecision(d, prec, sb.SB), "conversion failed");
  586. return FixEnd(buf, FixZeros(buf, sb.SB->position()));
  587. }
  588. } // namespace
  589. template <>
  590. size_t ToStringImpl<double>(double d, char* buf, size_t len) {
  591. return DoDtoa(d, buf, len, 10);
  592. }
  593. template <>
  594. size_t ToStringImpl<float>(float f, char* buf, size_t len) {
  595. return DoDtoa(f, buf, len, 6);
  596. }
  597. size_t FloatToString(float t, char* buf, size_t len, EFloatToStringMode mode, int ndigits) {
  598. if (mode == PREC_AUTO) {
  599. TBuilder sb(buf, len);
  600. Y_ABORT_UNLESS(ToStringConverterNoPad().ToShortestSingle(t, sb.SB), "conversion failed");
  601. return FixEnd(buf, sb.SB->position());
  602. }
  603. return FloatToString((double)t, buf, len, mode, ndigits);
  604. }
  605. size_t FloatToString(double t, char* buf, size_t len, EFloatToStringMode mode, int ndigits) {
  606. if (mode == PREC_NDIGITS) {
  607. auto minDigits = DoubleToStringConverter::kMinPrecisionDigits;
  608. auto maxDigits = DoubleToStringConverter::kMaxPrecisionDigits;
  609. return DoDtoa(t, buf, len, ClampVal(ndigits, minDigits, maxDigits));
  610. }
  611. TBuilder sb(buf, len);
  612. if (mode == PREC_AUTO) {
  613. Y_ABORT_UNLESS(ToStringConverterNoPad().ToShortest(t, sb.SB), "conversion failed");
  614. return FixEnd(buf, sb.SB->position());
  615. }
  616. if (!ToStringConverterNoPad().ToFixed(t, ndigits, sb.SB)) {
  617. return FloatToString(t, buf, len, PREC_AUTO);
  618. }
  619. if (mode == PREC_POINT_DIGITS_STRIP_ZEROES) {
  620. return FixZeros(buf, sb.SB->position());
  621. }
  622. return sb.SB->position();
  623. }