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

hexdump .to_string / ascdump .to_string #290

Open
po0p opened this issue Jun 24, 2024 · 3 comments
Open

hexdump .to_string / ascdump .to_string #290

po0p opened this issue Jun 24, 2024 · 3 comments

Comments

@po0p
Copy link

po0p commented Jun 24, 2024

Hey, great library!
I am coming from learning C-style code first, so I very much love printf-type formatting
(And still will love probably, writing %llx%d,<size_t><int> takes less inputs than std::hex << std::dec)
That being said, hexdump and ascdump come very handy in a lot of cases, but they only support << operator.

Propose adding a .str/.to_str method, that would either return a char* or std::string/wstring (whichever you prefer), for the reason of being usable with printf-type formatting.

@po0p
Copy link
Author

po0p commented Jun 24, 2024

Probably something like this (if this is bad, please explain to me why, i am not a great coder by any means)

util::nstring to_string() const //member of HexDump
{
    util::nostringstream new_stream;//content is identical to << operator, aside from the fact that new stream is created
    new_stream << std::hex << std::setfill(PLOG_NSTR('0'));
    for (size_t i = 0; i < m_size;)
    {
        new_stream << std::setw(2) << static_cast<int>(m_ptr[i]);

        if (++i < m_size)
        {
            if (m_group > 0 && i % m_group == 0)
            {
                new_stream << m_groupSeparator;
            }
            else
            {
                new_stream << m_digitSeparator;
            }
        }
    }
    return new_stream.str();
}

notes:
1.util::nstring may be unexpected, it is wstring for me and i wondered why i was getting no output when testing it first time :)
2.have to deal with c_str when using printf.
3.new_stream in theory is destroyed after leaving to_string scope.
4..str() on nostringstream (which afaik just uses correct wideness basic_ostringstream) creates a copy, so final string lives after leaving scope.

@SergiusTheBest
Copy link
Owner

Hey @po0p ! Yes, it's possible. Unfortunately it will look a little bit ugly as it needs an additional call to c_str():

PLOGI.printf("test format: %s", plog::hexdump(arr).str().c_str());

The implementation is simpler: it's basically a one line of code. I'll push it in a while.

@po0p
Copy link
Author

po0p commented Jun 28, 2024

Hey @po0p ! Yes, it's possible. Unfortunately it will look a little bit ugly as it needs an additional call to c_str():

Hey, thanks!
Its also quite useful if you are using std::format (which wont need .c_str() call but still i believe dont support << operator)
Or even more ugliness like
PLOGI<<"asd"<<std::format("...",v1,v2,v3) << plog::hexdump << "dfgh";
Actual usecase example right now (i implemented to_string already)

PLOGW << std::format("call. len:{} input: {}(@{:#x}) result: {} st:\n{}",
                     length,
                     plog::hexdump(input, min_l).separator("", "").to_string(),
                     (uint64_t)input,//input is char* so needs casting to display address
                     plog::hexdump(out, 32).separator("", "").to_string(),
                     stacktrace);

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

2 participants