to_text_float.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //
  2. // $Id$
  3. //
  4. #include<__vic/to_text.h>
  5. #include<cstdio>
  6. namespace __vic {
  7. //----------------------------------------------------------------------------
  8. template<size_t Size, class T>
  9. inline size_t __vic_snprintf(char (&buf)[Size], const char *fmt, T v)
  10. {
  11. #if __cplusplus >= 201103L
  12. size_t len = std::snprintf(buf, Size, fmt, v);;
  13. return len < Size ? len : Size-1U;
  14. #else
  15. return std::sprintf(buf, fmt, v);
  16. #endif
  17. }
  18. //----------------------------------------------------------------------------
  19. void to_text_append(double n, std::string &s)
  20. {
  21. char buf[sizeof n <= 16 ? 64 : -1]; // static_assert(sizeof n <= 16)
  22. size_t len = __vic_snprintf(buf, "%G", n);
  23. s.append(buf, len);
  24. }
  25. //----------------------------------------------------------------------------
  26. void to_text_append(long double n, std::string &s)
  27. {
  28. char buf[sizeof n <= 16 ? 64 : -1]; // static_assert(sizeof n <= 16)
  29. size_t len = __vic_snprintf(buf, "%LG", n);
  30. s.append(buf, len);
  31. }
  32. //----------------------------------------------------------------------------
  33. } // namespace