MCPcopy Create free account
hub / github.com/andreasfertig/programming-with-cpp20 / itoa

Function itoa

12.19-constevalParmCheck1/main.cpp:95–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

93#endif
94
95constexpr string itoa(int num, int base = 10)
96{
97 if(num == 0) { return {'0'}; }
98
99 string value{};
100
101 while(num > 0) {
102 const char sub = static_cast<char>(num % base);
103 num /= base;
104
105 // lack of std::string receiving a char
106 const char v = sub + '0';
107 value.append(&v, 1);
108 }
109
110 // reverse
111 string ret{};
112 std::string_view rv{value};
113
114 for(auto i{rv.size()}; i > 0; --i) {
115 ret.append(rv.substr(i - 1, 1));
116 }
117
118 return ret;
119}
120
121template<typename T, class... Args>
122constexpr void Formatter(string& buffer,

Callers 1

FormatterFunction · 0.70

Calls 1

sizeMethod · 0.80

Tested by

no test coverage detected