http://synesis.com.au/publications.html
| 206 | |
| 207 | // http://synesis.com.au/publications.html |
| 208 | const char* MatthewWilsonConvert(int value, char buf[]) |
| 209 | { |
| 210 | static char digits[19] = |
| 211 | { '9', '8', '7', '6', '5', '4', '3', '2', '1', |
| 212 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; |
| 213 | static const char* zero = digits + 9; // zero 指向 '0' |
| 214 | // works for -2147483648 .. 2147483647 |
| 215 | int i = value; |
| 216 | char* p = buf; |
| 217 | do { |
| 218 | int lsd = i % 10; // lsd 可能小于 0 |
| 219 | i /= 10; // 是向下取整还是向零取整? |
| 220 | *p++ = zero[lsd]; // 下标可能为负 |
| 221 | } while (i != 0); |
| 222 | if (value < 0) { |
| 223 | *p++ = '-'; |
| 224 | } |
| 225 | *p = '\0'; |
| 226 | std::reverse(buf, p); |
| 227 | return p; // p - buf 即为整数长度 |
| 228 | } |
| 229 | |
| 230 | class StringNumberPerformanceTest : public testing::Test |
| 231 | { |