1 // Copyright (c) 2024 Google LLC
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "source/to_string.h"
21 std::string
to_string(uint32_t n
) {
22 // This implementation avoids using standard library features that access
23 // the locale. Using the locale requires taking a mutex which causes
24 // annoying serialization.
26 constexpr int max_digits
= 10; // max uint has 10 digits
27 // Contains the resulting digits, with least significant digit in the last
30 int write_index
= max_digits
- 1;
32 buf
[write_index
] = '0';
36 buf
[write_index
--] = "0123456789"[units
];
41 assert(write_index
>= 0);
42 return std::string(buf
+ write_index
, max_digits
- write_index
);
44 } // namespace spvtools