Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"sign=space & pad_0s_until_width & alignment=right/internal/center" do not work together ("sign=space" is ignored) #81

Open
jhcarl0814 opened this issue Feb 16, 2021 · 0 comments

Comments

@jhcarl0814
Copy link

#include<string>
#include<iostream>
#include<iomanip>
#include<boost/format.hpp>

class Rational {
public:
    Rational(int n, unsigned int d) : n_(n), d_(d) {}
    Rational(int n, int d);    // convert denominator to unsigned
    friend std::ostream& operator<<(std::ostream&, const Rational&);
private:
    int n_;               // numerator
    unsigned int d_;      // denominator
};

Rational::Rational(int n, int d) : n_(n)
{
    if (d < 0) { n_ = -n_; d = -d; } // make the denominator always non-negative.
    d_ = static_cast<unsigned int>(d);
}

std::ostream& operator<<(std::ostream& os, const Rational& r) {
    using namespace std;
    streamsize w = os.width(0); // width has to be zeroed before saving state.

    boost::io::basic_oaltstringstream<char> oss;
    oss.copyfmt(os);
    oss << r.n_ << "/" << noshowpos << r.d_;

    os.write(oss.begin(), oss.size());
    return os;
}

int main(int argc, char* argv[])
{
    using namespace std;
    using boost::format;
    using boost::io::group;
    using boost::io::str;

    std::printf("[%8s][%8s]\n", "format", "printf");

    std::printf("\nright:\n");//'+'/' ', right
    std::cerr << boost::format("[%+8d]") % 12; std::printf("[%+8d]\n", 12);
    std::cerr << boost::format("[% 8d]") % 12; std::printf("[% 8d]\n", 12);
    std::cerr << boost::format("[%+08d]") % 12; std::printf("[%+08d]\n", 12);
    std::cerr << boost::format("[% 08d]") % 12; std::printf("[% 08d] !!!\n", 12);

    std::printf("\ninternal:\n");//'+'/' ', internal('_')
    std::cerr << boost::format("[%_+8d]\n") % 12;
    std::cerr << boost::format("[%_ 8d]\n") % 12;
    std::cerr << boost::format("[%_+08d]\n") % 12;
    std::cerr << boost::format("[%_ 08d] !!!\n") % 12;

    std::printf("\ncenter:\n");//'+'/' ', center('=')
    std::cerr << boost::format("[%=+8d]\n") % 12;
    std::cerr << boost::format("[%= 8d]\n") % 12;
    std::cerr << boost::format("[%=+08d]\n") % 12;
    std::cerr << boost::format("[%= 08d] !!!\n") % 12;

    std::printf("\nleft:\n");//'+'/' ', left('-')
    std::cerr << boost::format("[%-+8d]") % 12; std::printf("[%-+8d]\n", 12);
    std::cerr << boost::format("[%- 8d]") % 12; std::printf("[%- 8d]\n", 12);
    std::cerr << boost::format("[%-+08d]") % 12; std::printf("[%-+08d]\n", 12);
    std::cerr << boost::format("[%- 08d]") % 12; std::printf("[%- 08d]\n", 12);

    Rational  r(16, 9);
    std::printf("\nright:\n");
    cerr << format("[%+8d]\n") % r;
    cerr << format("[% 8d]\n") % r;
    cerr << format("[%+08d]\n") % r;
    cerr << format("[% 08d] !!!\n") % r;

    std::printf("\ninternal:\n");
    cerr << format("[%_+8d]\n") % r;
    cerr << format("[%_ 8d]\n") % r;
    cerr << format("[%_+08d]\n") % r;
    cerr << format("[%_ 08d] !!!\n") % r;

    std::printf("\ncenter:\n");
    cerr << format("[%=+8d]\n") % r;
    cerr << format("[%= 8d]\n") % r;
    cerr << format("[%=+08d]\n") % r;
    cerr << format("[%= 08d] !!!\n") % r;

    std::printf("\nleft:\n");
    cerr << format("[%-+8d]\n") % r;
    cerr << format("[%- 8d]\n") % r;
    cerr << format("[%-+08d]\n") % r;
    cerr << format("[%- 08d]\n") % r;
}

output (boost.format on the left, std::printf on the right, the differences is marked with !!!):

[  format][  printf]

right:
[     +12][     +12]
[      12][      12]
[+0000012][+0000012]
[00000012][ 0000012] !!!

internal:
[+     12]
[      12]
[+0000012]
[00000012] !!!

center:
[   +12  ]
[    12  ]
[+0000012]
[00000012] !!!

left:
[+12     ][+12     ]
[ 12     ][ 12     ]
[+12     ][+12     ]
[ 12     ][ 12     ]

right:
[   +16/9]
[    16/9]
[000+16/9]
[000016/9] !!!

internal:
[   +16/9]
[    16/9]
[000+16/9]
[000016/9] !!!

center:
[  +16/9 ]
[   16/9 ]
[000+16/9]
[000016/9] !!!

left:
[+16/9   ]
[ 16/9   ]
[+16/9   ]
[ 16/9   ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant