Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Alibaba MNN RapidJSON Itoa

From Leeroopedia


Knowledge Sources
Domains JSON_Parsing, Number_Conversion
Last Updated 2026-02-10 12:00 GMT

Overview

3rd_party/rapidjson/internal/itoa.h (308 lines) provides fast integer-to-string conversion routines used by the vendored RapidJSON library within Alibaba MNN. These functions are used during JSON serialization (by Writer) to convert integer values into their decimal string representations with minimal overhead.

Usage note: Vendored dependency used internally by MNN for JSON configuration parsing (model configs, LLM configs). Not directly imported by end users.

Key Functions

GetDigitsLut

Returns a precomputed 200-byte lookup table that maps pairs of digits (00-99) to their ASCII characters. This avoids expensive division/modulo operations during conversion.

inline const char* GetDigitsLut() {
    static const char cDigitsLut[200] = {
        '0','0','0','1','0','2','0','3','0','4', /* ... */
        '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9'
    };
    return cDigitsLut;
}

u32toa

Converts an unsigned 32-bit integer to a null-terminated string:

inline char* u32toa(uint32_t value, char* buffer);

The function uses three code paths based on the magnitude of the value:

  • value < 10000 -- Processes two pairs of digits via lookup table
  • value < 100000000 -- Splits into two 4-digit groups
  • value >= 100000000 -- Splits into three groups (leading digits + two 4-digit groups)

i32toa

Converts a signed 32-bit integer to string, handling the negative sign before delegating to u32toa:

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

u64toa

Converts an unsigned 64-bit integer to string, using similar lookup-table optimization extended for larger magnitudes:

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

i64toa

Converts a signed 64-bit integer to string:

inline char* i64toa(int64_t value, char* buffer);

Algorithm

The conversion algorithm avoids per-digit division by processing two digits at a time using a 200-element lookup table (the "digits LUT"). Each pair of digits (0-99) maps to two consecutive characters in the table. For a 4-digit group ABCD, the algorithm computes:

  1. d1 = (value / 100) << 1 -- index into LUT for first two digits
  2. d2 = (value % 100) << 1 -- index into LUT for last two digits
  3. Writes cDigitsLut[d1], cDigitsLut[d1+1], cDigitsLut[d2], cDigitsLut[d2+1]

Leading zeros are suppressed by checking magnitude thresholds.

Namespace

All functions reside in RAPIDJSON_NAMESPACE::internal (typically rapidjson::internal).

License

MIT License. Copyright (C) 2015 THL A29 Limited (Tencent) and Milo Yip.

See Also

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment