--- nono/lib/mystring.cpp 2026/04/29 17:05:47 1.1.1.8 +++ nono/lib/mystring.cpp 2026/04/29 17:05:56 1.1.1.10 @@ -9,6 +9,7 @@ // #include "mystring.h" +#include "ascii_ctype.h" #include std::string @@ -32,7 +33,7 @@ string_ltrim(const std::string& str) { auto it = str.begin(); for (; it != str.end(); it++) { - if (!isspace(*it)) + if (!is_ascii_space(*it)) break; } return std::string(it, str.end()); @@ -42,7 +43,7 @@ string_ltrim(const std::string& str) void string_rtrim(std::string& str) { - while (isspace(*str.rbegin())) { + while (is_ascii_space(*str.rbegin())) { str.pop_back(); } } @@ -52,7 +53,7 @@ void rtrim(char *str) { char *p = strchr(str, '\0'); - while (--p >= str && isspace((int)*p)) { + while (--p >= str && is_ascii_space(*p)) { *p = '\0'; } } @@ -65,11 +66,11 @@ string_trim(const std::string& str) int e = str.size(); for (; s < e; s++) { - if (!isspace(str[s])) + if (!is_ascii_space(str[s])) break; } for (e--; e >= s; e--) { - if (!isspace(str[e])) + if (!is_ascii_space(str[e])) break; } @@ -211,28 +212,13 @@ strhex(uint32 value, int width) return s; } -#define FORMAT_FULL 0 -#define FORMAT_SEC 1 +static constexpr bool FORMAT_TSEC = false; +static constexpr bool FORMAT_FULL = true; -// 経過時間 t を文字列にして返す。 -// 文字列長は、t の大きさ(とフォーマット指定)によって以下の通り。 -// -// 0 1 2 -// 01234567890123456789012345 -// "0.mmm'uuu'nnn" 10秒未満、または FORMAT_SEC なら13桁 -// "59.mmm'uuu'nnn" 1分未満なら14桁 -// " 9:59.mmm'uuu'nnn" 1時間未満なら17桁 -// " 9:59:59.mmm'uuu'nnn" 24時間未満なら20桁 -// "999d 23:59:59.mmm'uuu'nnn" 1日以上なら25桁。これが最大幅。 -// -// FORMAT_SEC は1桁秒以下の場合 (実際には1秒未満の場合) に用いる。 -// FORMAT_FULL は 1000日経過すると桁がずれるけど、それはもういいだろう。 -// -// 10秒未満の場合だけ %2u ではなく %u で1桁切り詰めているが、これは -// FORMAT_SEC との互換性のため。その必要のない10分未満と10時間未満は -// どちらも %2u で表記し桁数を維持することに努める。 +// TimeToStr() と SecToStr() の共通部分。 +template static const std::string -TimeToStrF(uint64 t, int format) +TimeToStrF(uint64 t) { char buf[32]; char *p; @@ -247,7 +233,7 @@ TimeToStrF(uint64 t, int format) t /= 1000; uint s, m, h, d; - if (format == FORMAT_FULL) { + if (full_format) { s = t % 60; t /= 60; m = t % 60; @@ -288,16 +274,37 @@ TimeToStrF(uint64 t, int format) return std::string(buf, p - buf); } +// t [nsec] を文字列に整形して返す。 +// ex) 1 2 +// 01234567890123456789012345 +// "1.mmm'uuu'nnn" 10秒未満なら13桁 +// "59.mmm'uuu'nnn" 1分未満なら14桁 +// " 9:59.mmm'uuu'nnn" 1時間未満なら17桁 +// " 9:59:59.mmm'uuu'nnn" 24時間未満なら20桁 +// "999d 23:59:59.mmm'uuu'nnn" 1000日未満なら25桁 +// +// 1000日以上になると桁がずれるけど、それはもういいだろう。 +// 10秒未満の場合だけ %2u ではなく %u で1桁切り詰めているが、これは +// FORMAT_TSEC との互換性のため。その必要のない10分未満と10時間未満は +// どちらも %2u で表記し桁数を維持することに努める。 const std::string TimeToStr(uint64 t) { - return TimeToStrF(t, FORMAT_FULL); + return TimeToStrF(t); } +// t [nsec] を文字列にして返す。 +// 秒以上はすべて %u だけで表す。 +// 1桁秒以内 (10秒未満) なことが分かっている場合は 13桁。 +// 2桁秒以上になると伸びていく。前に余白等なし。 +// ex) +// "1.000'000'000" (10秒未満なら13桁) +// "10.000'000'000" +// "100.000'000'000" const std::string SecToStr(uint64 t) { - return TimeToStrF(t, FORMAT_SEC); + return TimeToStrF(t); } @@ -315,8 +322,8 @@ int main() return 0; } sw.Stop(); - uint64 t = sw.Elapsed(); - printf("%ld.%03ld msec\n", t / 1000 / 1000, (t / 1000) % 1000); + uint64 t = sw.Elapsed_nsec(); + printf("%.3f msec\n", (double)t / 1e9); return 0; } #endif