Coverage Report

Created: 2026-11-12 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spirv-tools/source/to_string.cpp
Line
Count
Source
1
// Copyright (c) 2024 Google LLC
2
//
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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
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.
14
15
#include "source/to_string.h"
16
17
#include <cassert>
18
19
namespace spvtools {
20
21
43.7M
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.
25
26
43.7M
  constexpr int max_digits = 10;  // max uint has 10 digits
27
  // Contains the resulting digits, with least significant digit in the last
28
  // entry.
29
43.7M
  char buf[max_digits];
30
43.7M
  int write_index = max_digits - 1;
31
43.7M
  if (n == 0) {
32
407k
    buf[write_index] = '0';
33
43.3M
  } else {
34
311M
    while (n > 0) {
35
268M
      int units = n % 10;
36
268M
      buf[write_index--] = "0123456789"[units];
37
268M
      n = (n - units) / 10;
38
268M
    }
39
43.3M
    write_index++;
40
43.3M
  }
41
43.7M
  assert(write_index >= 0);
42
43.7M
  return std::string(buf + write_index, max_digits - write_index);
43
43.7M
}
44
}  // namespace spvtools