common_util
Loading...
Searching...
No Matches
Functions
strfmt.h File Reference

String format functions. this file defines a series of string-formatted functions. More...

#include <string>
#include <cstdint>
#include <sstream>
#include <iomanip>
#include "timeutil.h"

Go to the source code of this file.

Functions

std::string cutl::fmt_uint (uint64_t val, uint8_t width=0, char fill='0')
 Format uint64 value to a string with a given width and fill character.
 
std::string cutl::fmt_double (double val, int precision=2)
 Format double value to a string with a given precision.
 
std::string cutl::fmt_filesize (uint64_t size, bool simplify=true, int precision=1)
 Format a file size to a human-readable string with a given precision.
 
std::string cutl::fmt_timeduration_s (uint64_t seconds)
 Format a time duration to a human-readable string.
 
std::string cutl::fmt_timeduration_ms (uint64_t microseconds)
 Format a time duration to a human-readable string.
 
std::string cutl::fmt_timeduration_us (uint64_t nanoseconds)
 Format a time duration to a human-readable string.
 
std::string cutl::fmt_timestamp (uint64_t second, bool local, const std::string &fmt)
 Format a timestamp to a human-readable string with a given format.
 
std::string cutl::fmt_timestamp_s (uint64_t second, bool local=true)
 Format a timestamp to a human-readable string.
 
std::string cutl::fmt_timestamp_ms (uint64_t ms, bool local=true)
 Format a timestamp to a human-readable string.
 
std::string cutl::fmt_timestamp_us (uint64_t us, bool local=true)
 Format a timestamp to a human-readable string.
 
std::string cutl::to_hex (const uint8_t *data, size_t len, bool upper=true, char separator=' ')
 Format data to a hex string.
 
std::string cutl::to_hex (uint8_t value, bool upper=true, const std::string &prefix="")
 Format a uint8_t value to a hex string.
 
std::string cutl::to_hex (uint16_t value, bool upper=true, const std::string &prefix="")
 Format a uint16_t value to a hex string.
 
std::string cutl::to_hex (uint32_t value, bool upper=true, const std::string &prefix="")
 Format a uint32_t value to a hex string.
 
std::string cutl::to_hex (uint64_t value, bool upper=true, const std::string &prefix="")
 Format a uint64_t value to a hex string.
 
std::string cutl::to_bin (uint8_t value, char separator=',')
 Format uint8_t value to a binary string.
 
std::string cutl::to_bin (uint16_t value, char separator=' ')
 Format uint16_t value to a binary string.
 
std::string cutl::to_bin (uint32_t value, char separator=' ')
 Format uint32_t value to a binary string.
 
std::string cutl::to_bin (uint64_t value, char separator=' ')
 Format uint64_t value to a binary string.
 

Detailed Description

String format functions. this file defines a series of string-formatted functions.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations.

Author
spencer.luo
Date
2024-05-13

Function Documentation

◆ fmt_double()

std::string cutl::fmt_double ( double val,
int precision = 2 )

Format double value to a string with a given precision.

Parameters
valthe value to be formatted.
precisionthe precision of the formatted string, default is 2.
Returns
std::string the formatted string.

◆ fmt_filesize()

std::string cutl::fmt_filesize ( uint64_t size,
bool simplify = true,
int precision = 1 )

Format a file size to a human-readable string with a given precision.

Parameters
sizethe size to be formatted.
simplifywhether to use a simplify unit.
precisionthe precision of the formatted string, default is 1.
Returns
std::string the formatted string.

◆ fmt_timeduration_ms()

std::string cutl::fmt_timeduration_ms ( uint64_t microseconds)

Format a time duration to a human-readable string.

Parameters
microsecondsthe duration in microseconds.
Returns
std::string the formatted string.

◆ fmt_timeduration_s()

std::string cutl::fmt_timeduration_s ( uint64_t seconds)

Format a time duration to a human-readable string.

Parameters
secondsthe duration in seconds.
Returns
std::string the formatted string.

◆ fmt_timeduration_us()

std::string cutl::fmt_timeduration_us ( uint64_t nanoseconds)

Format a time duration to a human-readable string.

Parameters
nanosecondsthe duration in nanoseconds.
Returns
std::string the formatted string.

◆ fmt_timestamp()

std::string cutl::fmt_timestamp ( uint64_t second,
bool local,
const std::string & fmt )

Format a timestamp to a human-readable string with a given format.

Parameters
secondthe timestamp in seconds.
localwhether to use local time or UTC time, default is local time. If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
fmtthe format of the formatted string. useage like std::put_time, see https://en.cppreference.com/w/cpp/io/manip/put_time
Returns
std::string the formatted string.

◆ fmt_timestamp_ms()

std::string cutl::fmt_timestamp_ms ( uint64_t ms,
bool local = true )

Format a timestamp to a human-readable string.

Parameters
msthe timestamp in milliseconds.
localwhether to use local time or UTC time, default is local time. If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
Returns
std::string the formatted string.

◆ fmt_timestamp_s()

std::string cutl::fmt_timestamp_s ( uint64_t second,
bool local = true )

Format a timestamp to a human-readable string.

Parameters
secondthe timestamp in seconds.
localwhether to use local time or UTC time, default is local time. If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
Returns
std::string the formatted string.

◆ fmt_timestamp_us()

std::string cutl::fmt_timestamp_us ( uint64_t us,
bool local = true )

Format a timestamp to a human-readable string.

Parameters
usthe timestamp in microseconds.
localwhether to use local time or UTC time, default is local time. If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
Returns
std::string the formatted string.

◆ fmt_uint()

std::string cutl::fmt_uint ( uint64_t val,
uint8_t width = 0,
char fill = '0' )

Format uint64 value to a string with a given width and fill character.

Parameters
valthe value to be formatted.
widththe width of the formatted string.
fillthe fill character of the formatted string, default is '0'.
Returns
std::string the formatted string.

◆ to_bin() [1/4]

std::string cutl::to_bin ( uint16_t value,
char separator = ' ' )

Format uint16_t value to a binary string.

Parameters
valuethe value to be formatted.
separatorthe separator between each pair of binary characters, default is space.
Returns
std::string the formatted string.

◆ to_bin() [2/4]

std::string cutl::to_bin ( uint32_t value,
char separator = ' ' )

Format uint32_t value to a binary string.

Parameters
valuethe value to be formatted.
separatorthe separator between each pair of binary characters, default is space.
Returns
std::string the formatted string.

◆ to_bin() [3/4]

std::string cutl::to_bin ( uint64_t value,
char separator = ' ' )

Format uint64_t value to a binary string.

Parameters
valuethe value to be formatted.
separatorthe separator between each pair of binary characters, default is space.
Returns
std::string the formatted string.

◆ to_bin() [4/4]

std::string cutl::to_bin ( uint8_t value,
char separator = ',' )

Format uint8_t value to a binary string.

Parameters
valuethe value to be formatted.
separatorthe separator between each pair of binary characters, default is comma.
Returns
std::string the formatted string.

◆ to_hex() [1/5]

std::string cutl::to_hex ( const uint8_t * data,
size_t len,
bool upper = true,
char separator = ' ' )

Format data to a hex string.

Parameters
datathe data to be formatted.
lenthe length of the data.
upperwhether to use upper case or lower case for hex characters, default is upper case.
separatorthe separator between each pair of hex characters, default is space.
Returns
std::string the formatted string.

◆ to_hex() [2/5]

std::string cutl::to_hex ( uint16_t value,
bool upper = true,
const std::string & prefix = "" )

Format a uint16_t value to a hex string.

Parameters
valuethe value to be formatted.
upperwhether to use upper case or lower case for hex characters, default is upper case.
prefixthe prefix of the formatted string, default is empty.
Returns
std::string the formatted string.

◆ to_hex() [3/5]

std::string cutl::to_hex ( uint32_t value,
bool upper = true,
const std::string & prefix = "" )

Format a uint32_t value to a hex string.

Parameters
valuethe value to be formatted.
upperwhether to use upper case or lower case for hex characters, default is upper case.
prefixthe prefix of the formatted string, default is empty.
Returns
std::string the formatted string.

◆ to_hex() [4/5]

std::string cutl::to_hex ( uint64_t value,
bool upper = true,
const std::string & prefix = "" )

Format a uint64_t value to a hex string.

Parameters
valuethe value to be formatted.
upperwhether to use upper case or lower case for hex characters, default is upper case.
prefixthe prefix of the formatted string, default is empty.
Returns
std::string the formatted string.

◆ to_hex() [5/5]

std::string cutl::to_hex ( uint8_t value,
bool upper = true,
const std::string & prefix = "" )

Format a uint8_t value to a hex string.

Parameters
valuethe value to be formatted.
upperwhether to use upper case or lower case for hex characters, default is upper case.
prefixthe prefix of the formatted string, default is empty.
Returns
std::string the formatted string.