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

Function itoa

12.18-constevalParmCheck0/main.cpp:12–34  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10using std::string;
11
12string itoa(int num, int base = 10)
13{
14 if(num == 0) { return {'0'}; }
15
16 string value{};
17
18 while(num > 0) {
19 const char sub = static_cast<char>(num % base);
20 num /= base;
21
22 // lack of std::string receiving a char
23 const char v = sub + '0';
24 value.append(&v, 1);
25 }
26
27 // reverse
28 string ret{};
29 std::string_view rv{value};
30
31 for(auto i{rv.size()}; i > 0; --i) { ret.append(rv.substr(i - 1, 1)); }
32
33 return ret;
34}
35
36template<typename T, class... Args>
37void Formatter(string& buffer,

Callers 1

FormatterFunction · 0.70

Calls 1

sizeMethod · 0.80

Tested by

no test coverage detected