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

Open
#81 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
tooling

Research direction

Start by building and running the supplied C++ reproducer for boost::format, comparing its output with std::printf for space signs, zero padding, and each alignment. Trace the formatting path used by boost::format and its handling of sign and padding flags. Done means sign=space is preserved with zero padding for right, internal, and center alignment, including the Rational example.

Written by the indexing model from the issue text.

Description

#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   ]
Dominant language
C++
Stars
31
Forks
53
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/format

All issues in boostorg/format

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.