this repo has no description
1/* Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) */
2#pragma once
3
4#include "asserts.h"
5#include "globals.h"
6#include "objects.h"
7
8namespace py {
9
10extern const byte kLowerCaseHexDigitArray[16];
11extern const byte kUpperCaseHexDigitArray[16];
12
13// Converts an uword to ascii decimal digits. The digits can only be efficiently
14// produced from least to most significant without knowing the exact number of
15// digits upfront. Because of this the function takes a `buffer_end` argument
16// and writes the digit before it. Returns a pointer to the last byte written.
17inline byte* uwordToDecimal(uword num, byte* buffer_end) {
18 byte* start = buffer_end;
19 do {
20 *--start = '0' + num % 10;
21 num /= 10;
22 } while (num > 0);
23 return start;
24}
25
26inline byte lowerCaseHexDigit(uword value) {
27 return kLowerCaseHexDigitArray[value];
28}
29
30inline void uwordToHexadecimal(byte* buffer, word num_digits, uword value) {
31 DCHECK(num_digits > 0, "num_digits must be positive");
32 for (;;) {
33 byte b = kLowerCaseHexDigitArray[value & ((1 << kBitsPerHexDigit) - 1)];
34 num_digits--;
35 buffer[num_digits] = b;
36 if (num_digits == 0) break;
37 value >>= kBitsPerHexDigit;
38 }
39}
40
41inline void uwordToHexadecimalWithMutableBytes(RawMutableBytes dest, word index,
42 word num_digits, uword value) {
43 DCHECK(num_digits > 0, "num_digits must be positive");
44 for (;;) {
45 byte b = kLowerCaseHexDigitArray[value & ((1 << kBitsPerHexDigit) - 1)];
46 num_digits--;
47 dest.byteAtPut(index + num_digits, b);
48 if (num_digits == 0) break;
49 value >>= kBitsPerHexDigit;
50 }
51}
52
53} // namespace py