Update tc_common.h

Update tc_common.h notes, and add English version
This commit is contained in:
Douwanna 2020-05-23 02:14:40 +08:00 committed by GitHub
parent 089a725a4d
commit 344764f48f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,20 +47,26 @@ namespace tars
/**
* @file tc_common.h
* @brief ,, .
* @brief Helper Class. There're all static functions in this which provides some commonly used functions
*
*/
/////////////////////////////////////////////////
/**
* @brief 使.
* @brief Basic Utility Class. Some basic functions are provided.
*
* :
* These functions are provided as static functions.It includes the following functions:
*
* Trim类函数,,
*
* Trim class functions, case conversion functions, delimited string functions (directly delimited strings, numbers, etc.),
*
* ,,,,
*
* time-dependent functions, string conversion functions, binary string conversion functions,
*
* ,Ip匹配函数,
* replacement string functions, IP matching functions, determining whether a number is a prime number, etc.
*/
class UTIL_DLL_API TC_Common
{
@ -71,12 +77,14 @@ public:
/**
* @brief sleep
* @brief Cross Platform Sleep
*/
static void sleep(uint32_t sec);
static void msleep(uint32_t ms);
/**
* @brief ,double 6float默认6位精度
* @brief Floating Number Comparison, double defaults to be 6-bit precision, and float defaults to be 6-bit precision as well.
*/
static bool equal(double x, double y, double epsilon = _EPSILON_DOUBLE);
static bool equal(double x, double y, float epsilon );
@ -86,6 +94,7 @@ public:
/**
* @brief vector double
* @brief vector double, comparison functions for various scenarios
*/
static bool equal(const vector<double> & vx, const vector<double>& vy, double epsilon = _EPSILON_DOUBLE);
static bool equal(const vector<double>& vx, const vector<double>& vy, float epsilon );
@ -94,6 +103,7 @@ public:
/**
* @brief map中如果key或者value为double/float字段
* @brief In map, if the key or value is the double/float field, use this template function to compare.
*/
template<typename V, typename E>
static bool equal(const V& x, const V& y, E eps);
@ -102,6 +112,7 @@ public:
/**
* , ()
* Fixed width filled string for output alignment format (default right-padding)
* @param s
* @param c
* @param n
@ -120,232 +131,327 @@ public:
/**
* @brief .
* @brief Remove the head and the tail characters or strings
*
* @param sStr
* @param sStr input string
* @param s
* @param s the characters which need to be removed
* @param bChar true, s中每个字符; false, s字符串
* @param bChar bool : true, Remove each character in 's'; false, remove the s String
* @return string
* @return string Return the removed string
*/
static string trim(const string &sStr, const string &s = " \r\n\t", bool bChar = true);
/**
* @brief .
* @brief Remove left character or string
*
* @param sStr
* @param sStr input string
* @param s
* @param s the characters which need to be removed
* @param bChar true, s中每个字符; false, s字符串
* @param bChar bool : true, Remove each character in 's'; false, remove the s String
* @return string
* @return string Return the removed string
*/
static string trimleft(const string &sStr, const string &s = " \r\n\t", bool bChar = true);
/**
* @brief .
* @brief Remove right character or string
*
* @param sStr
* @param sStr input string
* @param s
* @param s the characters which need to be removed
* @param bChar true, s中每个字符; false, s字符串
* @param bChar bool : true, Remove each character in 's'; false, remove the s String
* @return string
* @return string Return the removed string
*/
static string trimright(const string &sStr, const string &s = " \r\n\t", bool bChar = true);
/**
* @brief .
* @brief Convert string to lowercase.
*
* @param sString
* @param sString String
* @return string
* @return string the converted string
*/
static string lower(const string &sString);
/**
* @brief .
* @brief Convert string to uppercase.
*
* @param sString
* @param sString string
* @return string
* @return string the converted string
*/
static string upper(const string &sString);
/**
* @brief .
* @brief Whether strings are all numbers or not.
*
* @param sString
* @param sString string
* @return bool
* @return bool whether number or not
*/
static bool isdigit(const string &sInput);
/**
* @brief .
* @brief Convert string to time structure.
*
* @param sString
* @param sString string Time Format
* @param sFormat
* @param sFormat format
* @param stTm
* @param stTm time structure
* @return 0: , -1:
* @return 0: success, -1: fail
*/
static int str2tm(const string &sString, const string &sFormat, struct tm &stTm);
/**
* @brief GMT格式的时间转化为时间结构
* @brief Conversion of time into time structure in GMT format
*
* eg.Sat, 06 Feb 2010 09:29:29 GMT, %a, %d %b %Y %H:%M:%S GMT
*
* mktime换成time_t, mktime(&stTm)
* You can replace time_t with mktime. Be careful about the time zones, and it can be used with mktime (&stTm)
*
* - timezone换成本地的秒(time(NULL)) timezone是系统的 ,
* - timezone changes costs to local seconds (time (NULL) values are the same). Timezones is systematic.
*
* extern long timezone;
* need extern long timezone;
*
* @param sString GMT格式的时间
* @param sString time in GMT format
* @param stTm
* @param stTm converted Time Structure
* @return 0: , -1:
* @return 0: success, -1: fail
*/
static int strgmt2tm(const string &sString, struct tm &stTm);
/**
* @brief .
* @brief Format time string to timestamp
*
* @param sString
* @param sString format time string
* @param sFormat
* @param sFormat format of formatted string time
* @return time_t
* @return time_t the converted timestamp
*/
static time_t str2time(const string &sString, const string &sFormat = "%Y%m%d%H%M%S");
/**
* @brief .
* @brief Convert time into string.
*
* @param stTm
* @param stTm time structure
* @param sFormat
* @param sFormat Target format to be converted, default to compact format
* @return string
* @return string converted time string
*/
static string tm2str(const struct tm &stTm, const string &sFormat = "%Y%m%d%H%M%S");
/**
* @brief .
* @brief Convert time into string
*
* @param t
* @param t time structure
* @param sFormat
* @param sFormat Target format to be converted, default to compact format
* @return string
* @return string converted time string
*/
static string tm2str(const time_t &t, const string &sFormat = "%Y%m%d%H%M%S");
/**
* @brief tm.
* @brief Convert time into tm.
*
* @param t
* @param t time structure
*/
static void tm2time(const time_t &t, struct tm &tt);
/**
* @brief time_t转换成tm(localtime_r, !!!)
* @brief Convert time_t to tm (Don't use system's localtime_r. The function will be slowed down.)
*
* @param t
* @param t time structure
* @param sFormat
* @param sFormat Target format to be converted, default to compact format
* @return string
* @return string converted time string
*/
static void tm2tm(const time_t &t, struct tm &stTm);
/**
* @brief
* @brief Get the current seconds and milliseconds
*
* @param t
* @param t time structure
*/
static int gettimeofday(struct timeval &tv);
/**
* @brief
* @brief Convert current time to compact string
* @param sFormat
* @param sFormat the format, default to compact format
* @return string
* @return string converted time string
*/
static string now2str(const string &sFormat = "%Y%m%d%H%M%S");
/**
* @brief GMT字符串GMT格式:Fri, 12 Jan 2001 18:18:18 GMT
* @brief Convert time into GMT string, GMT格式:Fri, 12 Jan 2001 18:18:18 GMT
* @param stTm
* @param stTm time structure
* @return string GMT格式的时间字符串
* @return string time string in GMT format
*/
static string tm2GMTstr(const struct tm &stTm);
/**
* @brief GMT字符串GMT格式:Fri, 12 Jan 2001 18:18:18 GMT
* @brief Convert time into GMT string, GMT格式:Fri, 12 Jan 2001 18:18:18 GMT
* @param stTm
* @param stTm time structure
* @return string GMT格式的时间字符串
* @return string time string in GMT format
*/
static string tm2GMTstr(const time_t &t);
/**
* @brief GMT字符串GMT格式:Fri, 12 Jan 2001 18:18:18 GMT
* @brief Convert current time into GMT string, GMT格式:Fri, 12 Jan 2001 18:18:18 GMT
* @return string GMT格式的时间字符串
* @return string time string in GMT format
*/
static string now2GMTstr();
/**
* @brief ()(%Y%m%d).
* @brief Get current date(yearmonthday) and convert it into string (%Y%m%d).
*
* @return string (%Y%m%d)
* @return string time string in (%Y%m%d) format
*/
static string nowdate2str();
/**
* @brief ()(%H%M%S).
* @brief Get current time(hourminutesecond) and convert it into string (%H%M%S).
*
* @return string (%H%M%S)
* @return string time string in (%H%M%S) format
*/
static string nowtime2str();
/**
* @brief .
* @brief Get the value of milliseconds of current time.
*
* @return int64_t
* @return int64_t current milliseconds of this time
*/
static int64_t now2ms();
/**
* @brief .
* @brief Take out microseconds of current time.
*
* @return int64_t
* @return int64_t Take out microseconds of current time.
*/
static int64_t now2us();
/**
* @brief T型T是数值类型, str为空,T为0.
* @brief Convert string to type T. if T is a numeric type and STR is empty, then T values 0.
*
* @param sStr
* @param sStr the string needs to be converted
* @return T T型类型
* @return T the type of type T
*/
template<typename T>
static T strto(const string &sStr);
/**
* @brief T型.
* @brief Convert string to type T
*
* @param sStr
* @param sStr the string needs to be converted
* @param sDefault
* @param sDefault default value
* @return T T类型
* @return T the converted type of type T
*/
template<typename T>
static T strto(const string &sStr, const string &sDefault);
/**
* @brief ,,vector里
*
* @brief Parse string, separate with separator, and save in vector
*
* : |a|b||c|
*
* Example: |a|b||c|
*
* withEmpty=true时, |:"","a", "b", "", "c", ""
* If 'withEmpty=true' then use '|' to separate it into "","a", "b", "", "c", "".
*
* withEmpty=false时, |:"a", "b", "c"
* If 'withEmpty=false' then use '|' to separate it into "a", "b", "c".
*
* T类型为int等数值类型, "", 0
* If the T type is a numeric type such as int, the delimited string is' ', then it is forced to 0.
*
* @param sStr
* @param sStr input string
* @param sSep ()
* @param sSep the separator string (each character counts as a separator)
* @param withEmpty true代表空的也算一个元素, false时空的过滤
* @param withEmpty bool: true, represented that empty is also an element ; false, filter empty ones.
* @return vector
* @return parsed character: vector
*/
template<typename T>
static vector<T> sepstr(const string &sStr, const string &sSep, bool withEmpty = false);
/**
* @brief T型转换成字符串T能够使用ostream对象用<<,
* @brief Convert T-type to string. As long as T can use ostream object with << to overload, it can be supported by this function.
*
* @param t
* @param t the data needs to be converted
* @return
* @return the converted string
*/
template<typename T>
inline static string tostr(const T &t)
@ -357,142 +463,203 @@ public:
/**
* @brief vector转换成string.
* @brief Convert vector to string.
*
* @param t vector型的数据
* @param t data which need to be convertes to vector type
* @return
* @return the converted string
*/
template<typename T>
static string tostr(const vector<T> &t);
/**
* @brief map输出为字符串.
* @brief export map as string
*
* @param map<K, V, D, A> map对象
* @param map<K, V, D, A> the map object needs to be converted
* @return string
* @return output string
*/
template<typename K, typename V, typename D, typename A>
static string tostr(const map<K, V, D, A> &t);
/**
* @brief map输出为字符串.
* @brief export map as string
*
* @param multimap<K, V, D, A> map对象
* @param multimap<K, V, D, A> the map object needs to be converted
* @return
* @return output string
*/
template<typename K, typename V, typename D, typename A>
static string tostr(const multimap<K, V, D, A> &t);
/**
* @brief map输出为字符串.
* @brief export map as string
*
* @param map<K, V, D, A> map对象
* @param map<K, V, D, A> the map object needs to be converted
* @return string
* @return output string
*/
template<typename K, typename V, typename D, typename P, typename A>
static string tostr(const unordered_map<K, V, D, P, A> &t);
/**
* @brief pair map等关系容器可以直接用tostr来输出
* @brief Convert pair to string, ensure that the relationship containers such as map can output directly with tostr.
* @param pair<F, S> pair对象
* @param pair<F, S> object pair
* @return
* @return output string
*/
template<typename F, typename S>
static string tostr(const pair<F, S> &itPair);
/**
* @brief container .
* @brief Convert container to string
*
* @param iFirst
* @param iFirst iterator
* @param iLast
* @param iLast iterator
* @param sSep
* @param sSep the separator between two elements
* @return
* @return the converted string
*/
template<typename InputIter>
static string tostr(InputIter iFirst, InputIter iLast, const string &sSep = "|");
/**
* @brief .
* @brief Convert binary data t0 string
*
* @param buf buffer
* @param buf binary buffer
* @param len buffer长度
* @param len buffer length
* @param sSep
* @param sSep separator
* @param lines , 0
* @param lines The max number of bytes for oneline.By default, 0 means no new line.
* @return
* @return the converted string
*/
static string bin2str(const void *buf, size_t len, const string &sSep = "", size_t lines = 0);
/**
* @brief .
* @brief Convert binary data t0 string
*
* @param sBinData
* @param sBinData binary data
* @param sSep
* @param sSep separator
* @param lines , 0
* @param lines The max number of bytes for oneline.By default, 0 means no new line.
* @return
* @return the converted string
*/
static string bin2str(const string &sBinData, const string &sSep = "", size_t lines = 0);
/**
* @brief .
* @brief Convert string to binary
*
* @param psAsciiData
* @param psAsciiData string
* @param sBinData
* @param sBinData binary data
* @param iBinSize
* @param iBinSize the length of the string which needs to be converted.
* @return 0
* @return Conversion length, less than or equal to 0 means failure
*/
static int str2bin(const char *psAsciiData, unsigned char *sBinData, int iBinSize);
/**
* @brief .
* @brief convert string to binary
*
* @param sBinData
* @param sBinData the string needs to be converted
* @param sSep
* @param sSep separator
* @param lines , 0
* @param lines The max number of bytes for oneline.By default, 0 means no new line.
* @return
* @return the converted binary data
*/
static string str2bin(const string &sBinData, const string &sSep = "", size_t lines = 0);
/**
* @brief .
* @brief replace string
*
* @param sString
* @param sString input string
* @param sSrc
* @param sSrc the original string
* @param sDest
* @param sDest the target string
* @return string
* @return string the converted string
*/
static string replace(const string &sString, const string &sSrc, const string &sDest);
/**
* @brief .
* @brief Batch replace string.
*
* @param sString
* @param sString input string
* @param mSrcDest map<,>
* @param mSrcDest map<original,target>
* @return string
* @return string the converted string
*/
static string replace(const string &sString, const map<string, string>& mSrcDest);
/**
* @brief .pat中*
* s为空, false pat为空, true
* @brief Match string separated by '.' And '*' in pat represents wildcard which represents any string that is not empty.
* If s is empty, return false. If pat is empty, return true.
* @param s
* @param s normal string
* @param pat *ip地址
* @param pat string matched with * to match IP address
* @return
* @return whether they matches or not
*/
static bool matchPeriod(const string& s, const string& pat);
/**
* @brief ..
* @brief Match strings separated by '.'
*
* @param s
* @param s normal string
* @param pat vector中的每个元素都是带*ip地址
* @param pat each elment in this vector means string matched with * to match IP address
* @return
* @return whether they matches or not
*/
static bool matchPeriod(const string& s, const vector<string>& pat);
/**
* @brief .
*
* @brief Determine whether a number is prime or not
* @param n
* @param n the data needs to be determined
* @return true代表是素数false表示非素数
* @return true for prime , false for non prime
*/
static bool isPrimeNumber(size_t n);
@ -505,13 +672,17 @@ public:
/**
* @brief
* @brief Ignore pipe exceptions
*/
static void ignorePipe();
/**
* @brief 16
* @brief Generating random strings based on hexadecimal characters.
* @param p
* @param p store random string
* @param len
* @param len string length
*/
static void getRandomHexChars(char* p, unsigned int len);
@ -519,23 +690,32 @@ public:
/**
* @brief string类型转成一个字节 .
* @brief Convert a string type to a byte .
*
* @param sWhat
* @param sWhat the string which needs to be converted
* @return char
* @return char the converted byte
*/
static char x2c(const string &sWhat);
/**
* @brief K, M, G两种 : 1K, 3M, 4G, 4.5M, 2.3G
* @brief The string can be changed into bytes. It supports two kinds of K, M and G, such as 1K, 3M, 4G, 4.5M and 2.3G
* @param s
* @param s the string which needs to be converted
* @param iDefaultSize ,
* @param iDefaultSize the default size in case of format error
* @return
* @return Bytes
*/
static size_t toSize(const string &s, size_t iDefaultSize);
/**
* @brief .
* @brief Get machine name
* @return string
* @return string machine name. if failed returns null
*/
static string getHostName();