--- nono/lib/mystring.cpp 2026/04/29 17:05:08 1.1.1.6 +++ nono/lib/mystring.cpp 2026/04/29 17:05:15 1.1.1.7 @@ -86,6 +86,16 @@ string_tolower(const std::string& src) return dst; } +// 文字列 src 中の ASCII 小文字を大文字にした新しい文字列を返す。 +std::string +string_toupper(const std::string& src) +{ + std::string dst(src); + std::transform(dst.begin(), dst.end(), dst.begin(), + [](unsigned char c){ return std::toupper(c); }); + return dst; +} + // 文字列 lhs の先頭が rhs と大文字小文字の区別なしで一致すれば true を返す。 // ASCII 専用。 // C++20 の starts_with に似せておく。 @@ -182,6 +192,26 @@ format_number(uint64 val) return std::string(buf); } +// value を width 桁の16進数文字列にして返す。"%0{width}x" みたいな感じ。 +// strhex(0x12345678, 4) -> "5678" +// strhex(0x00000001, 3) -> "001" +std::string +strhex(uint32 value, int width) +{ + std::string s; + + for (width -= 1; width >= 0; width--) { + uint32 d = (value >> (width * 4)) & 0x0f; + if (__predict_true(d < 10)) { + s += '0' + d; + } else { + s += 'a' + d - 10; + } + } + return s; +} + + #if defined(SELFTEST) #include #include "stopwatch.h"