1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <iomanip> 11 12 // template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false); 13 14 // REQUIRES: locale.en_US.UTF-8 15 16 #include <iomanip> 17 #include <cassert> 18 19 #include "platform_support.h" // locale name macros 20 21 template <class CharT> 22 class testbuf 23 : public std::basic_streambuf<CharT> 24 { 25 typedef std::basic_streambuf<CharT> base; 26 std::basic_string<CharT> str_; 27 public: 28 testbuf() 29 { 30 } 31 32 std::basic_string<CharT> str() const 33 {return std::basic_string<CharT>(base::pbase(), base::pptr());} 34 35 protected: 36 37 virtual typename base::int_type 38 overflow(typename base::int_type __c = base::traits_type::eof()) 39 { 40 if (__c != base::traits_type::eof()) 41 { 42 int n = str_.size(); 43 str_.push_back(__c); 44 str_.resize(str_.capacity()); 45 base::setp(const_cast<CharT*>(str_.data()), 46 const_cast<CharT*>(str_.data() + str_.size())); 47 base::pbump(n+1); 48 } 49 return __c; 50 } 51 }; 52 53 int main() 54 { 55 { 56 testbuf<char> sb; 57 std::ostream os(&sb); 58 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 59 showbase(os); 60 long double x = -123456789; 61 os << std::put_money(x, false); 62 assert(sb.str() == "-$1,234,567.89"); 63 } 64 { 65 testbuf<char> sb; 66 std::ostream os(&sb); 67 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 68 showbase(os); 69 long double x = -123456789; 70 os << std::put_money(x, true); 71 assert(sb.str() == "-USD 1,234,567.89"); 72 } 73 { 74 testbuf<wchar_t> sb; 75 std::wostream os(&sb); 76 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 77 showbase(os); 78 long double x = -123456789; 79 os << std::put_money(x, false); 80 assert(sb.str() == L"-$1,234,567.89"); 81 } 82 { 83 testbuf<wchar_t> sb; 84 std::wostream os(&sb); 85 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 86 showbase(os); 87 long double x = -123456789; 88 os << std::put_money(x, true); 89 assert(sb.str() == L"-USD 1,234,567.89"); 90 } 91 } 92