模板
setInt()
inline char* setInt(register char buffer[], register int src, const char suffix) { static char tmp[13]; register char* st = tmp + 12; register unsigned int div; *st = 0; if (src < 0) { src = ~src + 1; *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; while (src = div) * (--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; *(--st) = '-'; } else { do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); } while (*buffer++ = *st++) ; *buffer++ = suffix; return buffer; }
|
setUInt()
inline char* setUInt(register char buffer[], register unsigned int src, const char suffix) { static char tmp[11]; register char* st = tmp + 10; register unsigned int div; *st = 0; do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); while (*buffer++ = *st++) ; *buffer++ = suffix; return buffer; }
|
setLongLong()
inline char* setLongLong(register char buffer[], register long long src, const char suffix) { static char tmp[22]; register char * st = tmp + 21; register unsigned long long div; *st = 0; if (src < 0) { src = ~src + 1; *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; while (src = div) * (--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; *(--st) = '-'; } else { do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); } while (*buffer++ = *st++) ; *buffer++ = suffix; return buffer; }
|
setULongLong()
inline char* setULongLong(register char buffer[], register unsigned long long src, const char suffix) { static char tmp[22]; register char *st = tmp + 21; register unsigned long long div; *st = 0; do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); while (*buffer++ = *st++) ; *buffer++ = suffix; return buffer; }
|
概述
本組模板為整數轉字串模板,使用此模板取代 sprintf() 可以大幅將整數打印到字串的時間,此外本模板內建指針偏移,可以節省使用 strlen() 偏移指針的開銷。
參數
buffer
本字串用來接收字串化後的數值。
src
本參數為要輸出的整數
suffix
本參數為串接在整數後面的一個後綴字元,若無後墜請輸入 0 (或 '\0')
返回值
該函數返回最後一個格式化數字的下一位元指標,如果含有後綴則在後綴的下一位。
實例
#include <stdio.h>
inline char* setUInt(register char buffer[], register unsigned int src, const char suffix) { static char tmp[11]; register char* st = tmp + 10; register unsigned int div; *st = 0; do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); while (*buffer++ = *st++) ; *buffer++ = suffix; return buffer; }
inline char* setInt(register char buffer[], register int src, const char suffix) { static char tmp[13]; register char* st = tmp + 12; register unsigned int div; *st = 0; if (src < 0) { src = ~src + 1; *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; while (src = div) * (--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; *(--st) = '-'; } else { do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); } while (*buffer++ = *st++) ; *buffer++ = suffix; return buffer; }
char input[10000], * st = input;
int main() { unsigned long long v = 1LL << 31; st = setInt(st, 2147483647, ' '); st = setInt(st, v, '\n'); st = setInt(st, -2147483647, ' '); puts(input); return 0; }
|
編譯運行結果
2147483647 -2147483648
-2147483647
注意事項
- 在 32 位元架構下的編譯器,由於不需要做右移處理所以 int 的速度一般會比 short 來的更快 (或至少一樣快),所以在記憶體空間足夠的請況下,保持使用 int 是比較好的選擇。
更新紀錄
2019-9-6
- 將轉正移至單獨一行避免 ZJ 編譯器出錯
- 整數轉字元用 '|' 取代 '+' 提升效率
- buffer 改成靜態減少宣告時間