diff options
author | cathook <b01902109@csie.ntu.edu.tw> | 2014-06-19 07:25:48 +0800 |
---|---|---|
committer | cathook <b01902109@csie.ntu.edu.tw> | 2014-06-19 07:25:48 +0800 |
commit | fe926756145c5e5cf5f315af0acdbfd85ba27543 (patch) | |
tree | 4d75f94b87fd6d60262f2377d92f5896faf1be7d /meowpp/utility.h | |
parent | b2b55d8c642524274d8115d5b1863e1a40715887 (diff) | |
download | meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar.gz meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar.bz2 meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar.lz meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar.xz meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar.zst meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.zip |
x
Diffstat (limited to 'meowpp/utility.h')
-rw-r--r-- | meowpp/utility.h | 72 |
1 files changed, 47 insertions, 25 deletions
diff --git a/meowpp/utility.h b/meowpp/utility.h index b430dd8..83c7554 100644 --- a/meowpp/utility.h +++ b/meowpp/utility.h @@ -11,13 +11,35 @@ namespace meow { /*! + * @brief 有.from.first, .from.second, .to.first, .to.second + * + * @author cathook + */ +template<class F1, class F2 = F1, class T1 = F1, class T2 = T1> +struct PairToPair { + std::pair<F1, F2> from; + std::pair<T1, T2> to; + + PairToPair() { + } + PairToPair(PairToPair const& pp): from(pp.from), to(pp.to) { + } + PairToPair(F1 const& f1, F2 const& f2, T1 const& t1, T2 const& t2): + from(f1, f2), to(t1, t2) { + } + bool operator==(PairToPair const& p) const { + return (from == p.from && to == p.to); + } +}; + +/*! * @brief 類似C的printf, 不過是將格式化的字串丟到 \c std::string 裡回傳 - * + * * @param [in] fmt,... 同printf * @return 一個 \c std::string * @warning 目前格式化字串最長只支援8191個字元 */ -inline std::string stringPrintf(char const * fmt, ...){ +inline std::string stringPrintf(char const * fmt, ...) { char str[8192]; va_list args; va_start(args, fmt); @@ -28,7 +50,7 @@ inline std::string stringPrintf(char const * fmt, ...){ /*! * @brief 將輸入字串中的某個pattern取代成另一個pattern - * + * * @param [in] str 輸入的字串 * @param [in] from 要被取代的pattern * @param [in] to 將要取代的pattern @@ -37,10 +59,10 @@ inline std::string stringPrintf(char const * fmt, ...){ */ inline std::string stringReplace(std::string str, std::string const& from, - std::string const& to){ + std::string const& to) { std::string out = str; int len = from.length(); - for(size_t pos; (pos = out.find(from)) != std::string::npos; ){ + for (size_t pos; (pos = out.find(from)) != std::string::npos; ) { out.replace(pos, len, to); } return out; @@ -56,14 +78,14 @@ inline std::string stringReplace(std::string str, * @note 參數中所有的字串都是用\c cstring 來傳遞, * 也就是 \c char \c const* 型態 */ -inline bool cstringEndWith(char const* str, int n, ...){ +inline bool cstringEndWith(char const* str, int n, ...) { int len = strlen(str); va_list args; va_start(args, n); - for(int i = 0; i < n; i++){ + for (int i = 0; i < n; i++) { char const* arg = va_arg(args, char const*); int arglen = strlen(arg); - if(arglen <= len && strcmp(str + len - arglen, arg) == 0){ + if (arglen <= len && strcmp(str + len - arglen, arg) == 0) { return true; } } @@ -87,7 +109,7 @@ debugPrintf_(\ inline void debugPrintf_(char const* file, char const* func, size_t line, - char const* msg){ + char const* msg) { #ifdef DEBUG fprintf(stderr, "%s[%d] %s >> %s", file, line, func, msg); #endif // DEBUG @@ -96,7 +118,7 @@ inline void debugPrintf_(char const* file, /*! * @brief 階層式輸出 * - * 像是printf, 但多了一個 \b 巢狀 的概念, 例如: + * 像是printf, 但多了一個 \b 巢狀 的概念, 例如: * * @code * message1(level = 0) @@ -120,7 +142,7 @@ inline void debugPrintf_(char const* file, * @param [in] fmt, ... 跟printf一樣 * @return 無 */ -inline void messagePrintf(int level_change, char const* fmt, ...){ +inline void messagePrintf(int level_change, char const* fmt, ...) { static int level = 0; static int last_level = -5; char str[8192]; @@ -128,14 +150,15 @@ inline void messagePrintf(int level_change, char const* fmt, ...){ va_start(args, fmt); vsnprintf(str, 8192, fmt, args); va_end(args); - if(last_level == 1 && level_change == -1){ + if (last_level == 1 && level_change == -1) { printf(" ...%s\n", str); - }else{ - if(last_level == 1) printf("\n"); + } + else { + if (last_level == 1) printf("\n"); int level2 = level + (level_change == -1 ? -1 : 0); - for(int i = 0; i < level2; i++) printf("| "); + for (int i = 0; i < level2; i++) printf("| "); printf("%s%s", (level_change == -1 ? "..." : ""), str); - if(level_change != 1) printf("\n"); + if (level_change != 1) printf("\n"); } level += level_change; last_level = level_change; @@ -145,32 +168,31 @@ inline void messagePrintf(int level_change, char const* fmt, ...){ /*! * @brief 將兩個字串用人類習慣的檔名排序方式排序 * - * 例如 a1 \< a2 \< a3 \< a10 \< a12 \< a20, + * 例如 a1 \< a2 \< a3 \< a10 \< a12 \< a20, * 而不是 a1 \< a10 \< a12 \< a2 \< a20 \< a3 * * @param [in] f1 第一個字串 * @param [in] f2 第二個字串 * @return \c true/false 表 \b f1是否該排在f2前面 */ -inline bool filenameCompare(std::string const& f1, std::string const& f2){ +inline bool filenameCompare(std::string const& f1, std::string const& f2) { char const* s1 = f1.c_str(); char const* s2 = f2.c_str(); int l1 = f1.length(); int l2 = f2.length(); int i1, i2; - for(i1 = i2 = 0; i1 < l1 || i2 < l2; i1++, i2++){ - if(isdigit(s1[i1]) && isdigit(s2[i2])){ + for (i1 = i2 = 0; i1 < l1 || i2 < l2; i1++, i2++) { + if (isdigit(s1[i1]) && isdigit(s2[i2])) { int n1 = atoi(s1 + i1); int n2 = atoi(s2 + i2); - if(n1 != n2){ + if (n1 != n2) return (n1 < n2); - } while(i1 + 1 < l1 && isdigit(s1[i1 + 1])) i1++; while(i2 + 1 < l2 && isdigit(s2[i2 + 1])) i2++; - }else{ - if(s1[i1] != s2[i2]){ + } + else { + if(s1[i1] != s2[i2]) return s1[i1] < s2[i2]; - } } } return false; |