itoa的实现_static const char digits[200] = { ‘0‘, ‘0‘, ‘0‘, ‘

复制代码
    const uint32_t a = value / 100000000; // 1 to 42
    value %= 100000000;

    if (a >= 10) {
        const unsigned i = a << 1;
        *buffer++ = cDigitsLut[i];
        *buffer++ = cDigitsLut[i + 1];
    }
    else
        *buffer++ = static_cast<char>('0' + static_cast<char>(a));

    const uint32_t b = value / 10000; // 0 to 9999
    const uint32_t c = value % 10000; // 0 to 9999

    const uint32_t d1 = (b / 100) << 1;
    const uint32_t d2 = (b % 100) << 1;

    const uint32_t d3 = (c / 100) << 1;
    const uint32_t d4 = (c % 100) << 1;

    *buffer++ = cDigitsLut[d1];
    *buffer++ = cDigitsLut[d1 + 1];
    *buffer++ = cDigitsLut[d2];
    *buffer++ = cDigitsLut[d2 + 1];
    *buffer++ = cDigitsLut[d3];
    *buffer++ = cDigitsLut[d3 + 1];
    *buffer++ = cDigitsLut[d4];
    *buffer++ = cDigitsLut[d4 + 1];
}
return buffer;

}

inline char* i32toa(int32_t value, char* buffer) {

uint32_t u = static_cast<uint32_t>(value);

if (value < 0) {

*buffer++ = '-';

u = ~u + 1;

}

复制代码
return u32toa(u, buffer);

}

inline char* u64toa(uint64_t value, char* buffer) {

const char* cDigitsLut = GetDigitsLut();

const uint64_t kTen8 = 100000000;

const uint64_t kTen9 = kTen8 * 10;

const uint64_t kTen10 = kTen8 * 100;

const uint64_t kTen11 = kTen8 * 1000;

const uint64_t kTen12 = kTen8 * 10000;

const uint64_t kTen13 = kTen8 * 100000;

const uint64_t kTen14 = kTen8 * 1000000;

const uint64_t kTen15 = kTen8 * 10000000;

const uint64_t kTen16 = kTen8 * kTen8;

复制代码
if (value < kTen8) {
    uint32_t v = static_cast<uint32_t>(value);
    if (v < 10000) {
        const uint32_t d1 = (v / 100) << 1;
        const uint32_t d2 = (v % 100) << 1;

        if (v >= 1000)
            *buffer++ = cDigitsLut[d1];
        if (v >= 100)
            *buffer++ = cDigitsLut[d1 + 1];
        if (v >= 10)
            *buffer++ = cDigitsLut[d2];
        *buffer++ = cDigitsLut[d2 + 1];
    }
    else {
        // value = bbbbcccc
        const uint32_t b = v / 10000;
        const uint32_t c = v % 10000;

        const uint32_t d1 = (b / 100) << 1;
        const uint32_t d2 = (b % 100) << 1;

        const uint32_t d3 = (c / 100) << 1;
        const uint32_t d4 = (c % 100) << 1;

        if (value >= 10000000)
            *buffer++ = cDigitsLut[d1];
        if (value >= 1000000)
            *buffer++ = cDigitsLut[d1 + 1];
        if (value >= 100000)
            *buffer++ = cDigitsLut[d2];
        *buffer++ = cDigitsLut[d2 + 1];

        *buffer++ = cDigitsLut[d3];
        *buffer++ = cDigitsLut[d3 + 1];
        *buffer++ = cDigitsLut[d4];
        *buffer++ = cDigitsLut[d4 + 1];
    }
}
else if (value < kTen16) {
    const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
    const uint32_t v1 = static_cast<uint32_t>(value % kTen8);

    const uint32_t b0 = v0 / 10000;
    const uint32_t c0 = v0 % 10000;

    const uint32_t d1 = (b0 / 100) << 1;
    const uint32_t d2 = (b0 % 100) << 1;

    const uint32_t d3 = (c0 / 100) << 1;
    const uint32_t d4 = (c0 % 100) << 1;

    const uint32_t b1 = v1 / 10000;
    const uint32_t c1 = v1 % 10000;

    const uint32_t d5 = (b1 / 100) << 1;
    const uint32_t d6 = (b1 % 100) << 1;

    const uint32_t d7 = (c1 / 100) << 1;
    const uint32_t d8 = (c1 % 100) << 1;

    if (value >= kTen15)
        *buffer++ = cDigitsLut[d1];
    if (value >= kTen14)
        *buffer++ = cDigitsLut[d1 + 1];
    if (value >= kTen13)
        *buffer++ = cDigitsLut[d2];
    if (value >= kTen12)
        *buffer++ = cDigitsLut[d2 + 1];
    if (value >= kTen11)
        *buffer++ = cDigitsLut[d3];
    if (value >= kTen10)
        *buffer++ = cDigitsLut[d3 + 1];
    if (value >= kTen9)
        *buffer++ = cDigitsLut[d4];
    if (value >= kTen8)
        *buffer++ = cDigitsLut[d4 + 1];

    *buffer++ = cDigitsLut[d5];
    *buffer++ = cDigitsLut[d5 + 1];
    *buffer++ = cDigitsLut[d6];
    *buffer++ = cDigitsLut[d6 + 1];
    *buffer++ = cDigitsLut[d7];
    *buffer++ = cDigitsLut[d7 + 1];
    *buffer++ = cDigitsLut[d8];
    *buffer++ = cDigitsLut[d8 + 1];
}
else {
    const uint32_t a = static_cast<uint32_t>(value / kTen16); // 1 to 1844
    value %= kTen16;

    if (a < 10)
        *buffer++ = static_cast<char>('0' + static_cast<char>(a));
    else if (a < 100) {
        const uint32_t i = a << 1;
        *buffer++ = cDigitsLut[i];
        *buffer++ = cDigitsLut[i + 1];
    }
    else if (a < 1000) {
        *buffer++ = static_cast<char>('0' + static_cast<char>(a / 100));

        const uint32_t i = (a % 100) << 1;
        *buffer++ = cDigitsLut[i];
        *buffer++ = cDigitsLut[i + 1];
    }
    else {
        const uint32_t i = (a / 100) << 1;
        const uint32_t j = (a % 100) << 1;
        *buffer++ = cDigitsLut[i];
        *buffer++ = cDigitsLut[i + 1];
        *buffer++ = cDigitsLut[j];
        *buffer++ = cDigitsLut[j + 1];
    }

    const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
    const uint32_t v1 = static_cast<uint32_t>(value % kTen8);

    const uint32_t b0 = v0 / 10000;
    const uint32_t c0 = v0 % 10000;

    const uint32_t d1 = (b0 / 100) << 1;
    const uint32_t d2 = (b0 % 100) << 1;

    const uint32_t d3 = (c0 / 100) << 1;
    const uint32_t d4 = (c0 % 100) << 1;

    const uint32_t b1 = v1 / 10000;
    const uint32_t c1 = v1 % 10000;

    const uint32_t d5 = (b1 / 100) << 1;
    const uint32_t d6 = (b1 % 100) << 1;

    const uint32_t d7 = (c1 / 100) << 1;
    const uint32_t d8 = (c1 % 100) << 1;

    *buffer++ = cDigitsLut[d1];
    *buffer++ = cDigitsLut[d1 + 1];
    *buffer++ = cDigitsLut[d2];
    *buffer++ = cDigitsLut[d2 + 1];
    *buffer++ = cDigitsLut[d3];
    *buffer++ = cDigitsLut[d3 + 1];
    *buffer++ = cDigitsLut[d4];
    *buffer++ = cDigitsLut[d4 + 1];
    *buffer++ = cDigitsLut[d5];
    *buffer++ = cDigitsLut[d5 + 1];
    *buffer++ = cDigitsLut[d6];
    *buffer++ = cDigitsLut[d6 + 1];
    *buffer++ = cDigitsLut[d7];
    *buffer++ = cDigitsLut[d7 + 1];
    *buffer++ = cDigitsLut[d8];
    *buffer++ = cDigitsLut[d8 + 1];
}

return buffer;
相关推荐
melck1 分钟前
liunx日志查询常用命令总结
java·服务器·网络
守护者1706 分钟前
JAVA学习-练习试用Java实现“实现一个Hadoop程序,使用Hive进行复杂查询和数据筛查”
java·学习
喝拿铁写前端8 分钟前
智能系统的冰山结构
前端
程序员 小柴12 分钟前
docker的与使用
java·docker·eureka
ゞ 正在缓冲99%…17 分钟前
leetcode76.最小覆盖子串
java·算法·leetcode·字符串·双指针·滑动窗口
Seven9731 分钟前
【Guava】并发编程ListenableFuture&Service
java
WannaRunning31 分钟前
浅谈Tomcat数据源连接池
java·oracle·tomcat
forestsea39 分钟前
使用 Spring Boot 和 GraalVM 的原生镜像
java·spring boot·spring native·原生映像
逸狼42 分钟前
【JavaEE进阶】Spring AOP入门
java·java-ee
色空大师1 小时前
【idea】实用插件
java·ide·intellij-idea