Skip to content

Commit 1e116d2

Browse files
committed
Code cleanup.
1 parent 0e74118 commit 1e116d2

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

ext/json/ext/fbuffer/fbuffer.h

+11-20
Original file line numberDiff line numberDiff line change
@@ -175,41 +175,32 @@ static inline void fbuffer_append_char(FBuffer *fb, char newchr)
175175
fb->len++;
176176
}
177177

178-
static inline long fultoa(unsigned long number, char *buf)
179-
{
180-
static const char digits[] = "0123456789";
181-
char* tmp = buf;
182-
183-
do *tmp-- = digits[number % 10]; while (number /= 10);
184-
return buf - tmp;
185-
}
186-
187-
#define MAX_CHARS_FOR_LONG 20
188-
189178
/*
190179
* Appends the decimal string representation of \a number into the buffer.
191180
*/
192181
static void fbuffer_append_long(FBuffer *fb, long number)
193182
{
194183
/*
195-
* The to_text_from_ulong implementation produces digits left-to-right,
196-
* allowing us to write directly into the buffer. However, we don't know
197-
* how many characters we'll need exactly.
184+
* The to_text_from_ulong() function produces digits left-to-right,
185+
* allowing us to write directly into the buffer, but we don't know
186+
* the number of resulting characters.
198187
*
199-
* However, the number argument is always in the range 0xc000000000000000
200-
* to 0x3fffffffffffffff, or, in decimal, -4611686018427387904 to
201-
* 4611686018427387903. We therefore need at most 20 chars in the target
202-
* buffer.
188+
* We do know, however, that the `number` argument is always in the
189+
* range 0xc000000000000000 to 0x3fffffffffffffff, or, in decimal,
190+
* -4611686018427387904 to 4611686018427387903. The max number of chars
191+
* generated is therefore 20 (including a potential sign character).
203192
*/
204193

194+
static const int MAX_CHARS_FOR_LONG = 20;
195+
205196
fbuffer_inc_capa(fb, MAX_CHARS_FOR_LONG);
206197

207198
if (number < 0) {
208199
fbuffer_append_reserved_char(fb, '-');
209200

210201
/*
211-
* Since LONG_MIN is not in the valid range, `number = -number` always turns
212-
* a negative number into the positive.
202+
* Since number is always > LONG_MIN, `-number` will not overflow
203+
* and is always the positive abs() value.
213204
*/
214205
number = -number;
215206
}

0 commit comments

Comments
 (0)