Skip to content

Commit

Permalink
Changed more formatting usage to sfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ethouris committed May 26, 2024
1 parent e37d37d commit 6ce84fe
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 18 deletions.
4 changes: 2 additions & 2 deletions srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ string srt::CUDTUnited::CONID(SRTSOCKET sock)
if (sock == 0)
return "";

std::ostringstream os;
fmt::obufstream os;
os << "@" << sock << ":";
return os.str();
}
Expand Down Expand Up @@ -3240,7 +3240,7 @@ bool srt::CUDTUnited::updateListenerMux(CUDTSocket* s, const CUDTSocket* ls)
CMultiplexer& m = i->second;

#if ENABLE_HEAVY_LOGGING
ostringstream that_muxer;
fmt::obufstream that_muxer;
that_muxer << "id=" << m.m_iID << " port=" << m.m_iPort
<< " ip=" << (m.m_iIPversion == AF_INET ? "v4" : "v6");
#endif
Expand Down
4 changes: 2 additions & 2 deletions srtcore/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,8 @@ bool srt::CRendezvousQueue::qualifyToHandle(EReadStatus rst,
else
{
HLOGC(cnlog.Debug,
log << "RID: socket @" << i->m_iID << " still active (remaining " << std::fixed
<< (count_microseconds(i->m_tsTTL - tsNow) / 1000000.0) << "s of TTL)...");
log << "RID: socket @" << i->m_iID << " still active (remaining "
<< fmt::sfmt(count_microseconds(i->m_tsTTL - tsNow) / 1000000.0, "f") << "s of TTL)...");
}

const steady_clock::time_point tsLastReq = i->m_pUDT->m_tsLastReqTime;
Expand Down
53 changes: 53 additions & 0 deletions srtcore/sfmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// for FILE type from stdio. It has nothing to do with the rest of the {fmt}
// library, except that it reuses the namespace.

#ifndef INC_FMT_SFMT_H
#define INC_FMT_SFMT_H

#include <cstdio>
#include <cstring>
#include <string>
Expand Down Expand Up @@ -490,6 +493,19 @@ class obufstream
return *this;
}

// For unusual manipulation, usually to add NUL termination.
// NOTE: you must make sure that you won't use the extended
// buffers if the intention was to get a string.
void append(char c)
{
buffer.append(c);
}

const char* bufptr() const
{
return buffer.get_first();
}

template<size_t ANYSIZE>
obufstream& operator<<(const internal::form_memory_buffer<ANYSIZE>& b)
{
Expand Down Expand Up @@ -558,6 +574,41 @@ class obufstream
std::copy(data, data + i->size(), std::back_inserter(out));
}
}

template <class OutputContainer>
size_t copy_to(OutputContainer& out, size_t maxsize) const
{
using namespace internal;
size_t avail = maxsize;
if (avail < buffer.first_size())
{
std::copy(buffer.get_first(), buffer.get_first() + avail,
std::back_inserter(out));
return maxsize;
}

std::copy(buffer.get_first(), buffer.get_first() + buffer.first_size(),
std::back_inserter(out));

avail -= buffer.first_size();

for (form_memory_buffer<>::slices_t::const_iterator i = buffer.get_slices().begin();
i != buffer.get_slices().end(); ++i)
{
// Would be tempting to move the blocks, but C++03 doesn't feature moving.
const char* data = &(*i)[0];

if (avail < i->size())
{
std::copy(data, data + avail, std::back_inserter(out));
return maxsize;
}
std::copy(data, data + i->size(), std::back_inserter(out));
avail -= i->size();
}

return maxsize - avail;
}
};

namespace internal
Expand Down Expand Up @@ -662,3 +713,5 @@ inline ostdiostream& operator<<(ostdiostream& sout, const os_flush_manip&)


}

#endif
22 changes: 15 additions & 7 deletions srtcore/sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,21 @@ std::string FormatTime(const steady_clock::time_point& timestamp)
const uint64_t hours = total_sec / (60 * 60) - days * 24;
const uint64_t minutes = total_sec / 60 - (days * 24 * 60) - hours * 60;
const uint64_t seconds = total_sec - (days * 24 * 60 * 60) - hours * 60 * 60 - minutes * 60;
ostringstream out;

// Temporary solution. Need to find some better handling
// of dynamic width and precision.
fmt::obufstream dfmts;
dfmts << "0" << decimals;
dfmts.append('\0'); // form_memory_buffer doesn't use NUL-termination.
const char* decimal_fmt = dfmts.bufptr();

fmt::obufstream out;
if (days)
out << days << "D ";
out << setfill('0') << setw(2) << hours << ":"
<< setfill('0') << setw(2) << minutes << ":"
<< setfill('0') << setw(2) << seconds << "."
<< setfill('0') << setw(decimals) << (timestamp - seconds_from(total_sec)).time_since_epoch().count() << " [STDY]";
out << fmt::sfmt(hours, "02") << ":"
<< fmt::sfmt(minutes, "02") << ":"
<< fmt::sfmt(seconds, "02") << "."
<< fmt::sfmt((timestamp - seconds_from(total_sec)).time_since_epoch().count(), decimal_fmt) << " [STDY]";
return out.str();
}

Expand All @@ -72,8 +80,8 @@ std::string FormatTimeSys(const steady_clock::time_point& timestamp)
char tmp_buf[512];
strftime(tmp_buf, 512, "%X.", &tm);

ostringstream out;
out << tmp_buf << setfill('0') << setw(6) << (count_microseconds(timestamp.time_since_epoch()) % 1000000) << " [SYST]";
fmt::obufstream out;
out << tmp_buf << fmt::sfmt(count_microseconds(timestamp.time_since_epoch()) % 1000000, "06") << " [SYST]";
return out.str();
}

Expand Down
6 changes: 4 additions & 2 deletions srtcore/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#include "srt.h"
#include "utilities.h"
#include "srt_attr_defs.h"

#include "sfmt.h"

namespace srt
{
Expand Down Expand Up @@ -775,7 +775,9 @@ struct DurationUnitName<DUNIT_S>
template<eDurationUnit UNIT>
inline std::string FormatDuration(const steady_clock::duration& dur)
{
return Sprint(std::fixed, DurationUnitName<UNIT>::count(dur)) + DurationUnitName<UNIT>::name();
fmt::obufstream out;
out << fmt::sfmt(DurationUnitName<UNIT>::count(dur), "f") << DurationUnitName<UNIT>::name();
return out.str();
}

inline std::string FormatDuration(const steady_clock::duration& dur)
Expand Down
22 changes: 17 additions & 5 deletions srtcore/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ written by
#include <functional>
#include <memory>
#include <iomanip>
#include <sstream>
#include <utility>

#if HAVE_CXX11
Expand All @@ -46,6 +45,8 @@ written by
#include <cstring>
#include <stdexcept>

#include "sfmt.h"

// -------------- UTILITIES ------------------------

// --- ENDIAN ---
Expand Down Expand Up @@ -981,6 +982,19 @@ inline std::string FormatBinaryString(const uint8_t* bytes, size_t size)
if ( size == 0 )
return "";

using namespace fmt;

obufstream os;

os << sfmt<int>(bytes[0], "02X");
for (size_t i = 1; i < size; ++i)
{
os << sfmt<int>(bytes[i], "02X");
}
return os.str();

/* OLD VERSION
//char buf[256];
using namespace std;
Expand Down Expand Up @@ -1008,6 +1022,7 @@ inline std::string FormatBinaryString(const uint8_t* bytes, size_t size)
os << int(bytes[i]);
}
return os.str();
*/

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
}


Expand Down Expand Up @@ -1171,10 +1186,7 @@ inline std::string BufferStamp(const char* mem, size_t size)
}

// Convert to hex string
ostringstream os;
os << hex << uppercase << setfill('0') << setw(8) << sum;

return os.str();
return fmt::sfmts(sum, "08X");
}

template <class OutputIterator>
Expand Down

0 comments on commit 6ce84fe

Please sign in to comment.