Coverage Report

Created: 2026-05-27 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/common/source.cc
Line
Count
Source
1
// Copyright 2023 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
//     https://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 "common/source.h"
16
17
#include <algorithm>
18
#include <cstddef>
19
#include <cstdint>
20
#include <limits>
21
#include <memory>
22
#include <string>
23
#include <tuple>
24
#include <utility>
25
#include <vector>
26
27
#include "absl/base/nullability.h"
28
#include "absl/base/optimization.h"
29
#include "absl/container/inlined_vector.h"
30
#include "absl/functional/overload.h"
31
#include "absl/log/absl_check.h"
32
#include "absl/status/status.h"
33
#include "absl/status/statusor.h"
34
#include "absl/strings/cord.h"
35
#include "absl/strings/str_cat.h"
36
#include "absl/strings/str_replace.h"
37
#include "absl/strings/string_view.h"
38
#include "absl/types/optional.h"
39
#include "absl/types/span.h"
40
#include "absl/types/variant.h"
41
#include "internal/unicode.h"
42
#include "internal/utf8.h"
43
44
namespace cel {
45
46
171k
SourcePosition SourceContentView::size() const {
47
171k
  return static_cast<SourcePosition>(absl::visit(
48
171k
      absl::Overload(
49
171k
          [](absl::Span<const char> view) { return view.size(); },
50
171k
          [](absl::Span<const uint8_t> view) { return view.size(); },
51
171k
          [](absl::Span<const char16_t> view) { return view.size(); },
52
171k
          [](absl::Span<const char32_t> view) { return view.size(); }),
53
171k
      view_));
54
171k
}
55
56
188k
bool SourceContentView::empty() const {
57
188k
  return absl::visit(
58
188k
      absl::Overload(
59
188k
          [](absl::Span<const char> view) { return view.empty(); },
60
188k
          [](absl::Span<const uint8_t> view) { return view.empty(); },
61
188k
          [](absl::Span<const char16_t> view) { return view.empty(); },
62
188k
          [](absl::Span<const char32_t> view) { return view.empty(); }),
63
188k
      view_);
64
188k
}
65
66
434M
char32_t SourceContentView::at(SourcePosition position) const {
67
434M
  ABSL_DCHECK_GE(position, 0);
68
434M
  ABSL_DCHECK_LT(position, size());
69
434M
  return absl::visit(
70
434M
      absl::Overload(
71
434M
          [position =
72
434M
               static_cast<size_t>(position)](absl::Span<const char> view) {
73
226M
            return static_cast<char32_t>(static_cast<uint8_t>(view[position]));
74
226M
          },
75
434M
          [position =
76
434M
               static_cast<size_t>(position)](absl::Span<const uint8_t> view) {
77
13.6M
            return static_cast<char32_t>(view[position]);
78
13.6M
          },
79
434M
          [position =
80
434M
               static_cast<size_t>(position)](absl::Span<const char16_t> view) {
81
73.5M
            return static_cast<char32_t>(view[position]);
82
73.5M
          },
83
434M
          [position =
84
434M
               static_cast<size_t>(position)](absl::Span<const char32_t> view) {
85
120M
            return static_cast<char32_t>(view[position]);
86
120M
          }),
87
434M
      view_);
88
434M
}
89
90
std::string SourceContentView::ToString(SourcePosition begin,
91
16.8M
                                        SourcePosition end) const {
92
16.8M
  ABSL_DCHECK_GE(begin, 0);
93
16.8M
  ABSL_DCHECK_LE(end, size());
94
16.8M
  ABSL_DCHECK_LE(begin, end);
95
16.8M
  return absl::visit(
96
16.8M
      absl::Overload(
97
16.8M
          [begin = static_cast<size_t>(begin),
98
16.8M
           end = static_cast<size_t>(end)](absl::Span<const char> view) {
99
11.6M
            view = view.subspan(begin, end - begin);
100
11.6M
            return std::string(view.data(), view.size());
101
11.6M
          },
102
16.8M
          [begin = static_cast<size_t>(begin),
103
16.8M
           end = static_cast<size_t>(end)](absl::Span<const uint8_t> view) {
104
309k
            view = view.subspan(begin, end - begin);
105
309k
            std::string result;
106
309k
            result.reserve(view.size() * 2);
107
72.4M
            for (const auto& code_point : view) {
108
72.4M
              internal::Utf8Encode(result, code_point);
109
72.4M
            }
110
309k
            result.shrink_to_fit();
111
309k
            return result;
112
309k
          },
113
16.8M
          [begin = static_cast<size_t>(begin),
114
16.8M
           end = static_cast<size_t>(end)](absl::Span<const char16_t> view) {
115
3.05M
            view = view.subspan(begin, end - begin);
116
3.05M
            std::string result;
117
3.05M
            result.reserve(view.size() * 3);
118
531M
            for (const auto& code_point : view) {
119
531M
              internal::Utf8Encode(result, code_point);
120
531M
            }
121
3.05M
            result.shrink_to_fit();
122
3.05M
            return result;
123
3.05M
          },
124
16.8M
          [begin = static_cast<size_t>(begin),
125
16.8M
           end = static_cast<size_t>(end)](absl::Span<const char32_t> view) {
126
1.84M
            view = view.subspan(begin, end - begin);
127
1.84M
            std::string result;
128
1.84M
            result.reserve(view.size() * 4);
129
469M
            for (const auto& code_point : view) {
130
469M
              internal::Utf8Encode(result, code_point);
131
469M
            }
132
1.84M
            result.shrink_to_fit();
133
1.84M
            return result;
134
1.84M
          }),
135
16.8M
      view_);
136
16.8M
}
137
138
0
void SourceContentView::AppendToString(std::string& dest) const {
139
0
  absl::visit(absl::Overload(
140
0
                  [&dest](absl::Span<const char> view) {
141
0
                    dest.append(view.data(), view.size());
142
0
                  },
143
0
                  [&dest](absl::Span<const uint8_t> view) {
144
0
                    for (const auto& code_point : view) {
145
0
                      internal::Utf8Encode(dest, code_point);
146
0
                    }
147
0
                  },
148
0
                  [&dest](absl::Span<const char16_t> view) {
149
0
                    for (const auto& code_point : view) {
150
0
                      internal::Utf8Encode(dest, code_point);
151
0
                    }
152
0
                  },
153
0
                  [&dest](absl::Span<const char32_t> view) {
154
0
                    for (const auto& code_point : view) {
155
0
                      internal::Utf8Encode(dest, code_point);
156
0
                    }
157
0
                  }),
158
0
              view_);
159
0
}
160
161
namespace common_internal {
162
163
class SourceImpl : public Source {
164
 public:
165
  SourceImpl(std::string description,
166
             absl::InlinedVector<SourcePosition, 1> line_offsets)
167
21.4k
      : description_(std::move(description)),
168
21.4k
        line_offsets_(std::move(line_offsets)) {}
169
170
226k
  absl::string_view description() const final { return description_; }
171
172
1.76M
  absl::Span<const SourcePosition> line_offsets() const final {
173
1.76M
    return absl::MakeConstSpan(line_offsets_);
174
1.76M
  }
175
176
 private:
177
  const std::string description_;
178
  const absl::InlinedVector<SourcePosition, 1> line_offsets_;
179
};
180
181
namespace {
182
183
class AsciiSource final : public SourceImpl {
184
 public:
185
  AsciiSource(std::string description,
186
              absl::InlinedVector<SourcePosition, 1> line_offsets,
187
              std::vector<char> text)
188
19.2k
      : SourceImpl(std::move(description), std::move(line_offsets)),
189
19.2k
        text_(std::move(text)) {}
190
191
153k
  ContentView content() const override {
192
153k
    return MakeContentView(absl::MakeConstSpan(text_));
193
153k
  }
194
195
 private:
196
  const std::vector<char> text_;
197
};
198
199
class Latin1Source final : public SourceImpl {
200
 public:
201
  Latin1Source(std::string description,
202
               absl::InlinedVector<SourcePosition, 1> line_offsets,
203
               std::vector<uint8_t> text)
204
324
      : SourceImpl(std::move(description), std::move(line_offsets)),
205
324
        text_(std::move(text)) {}
206
207
8.20k
  ContentView content() const override {
208
8.20k
    return MakeContentView(absl::MakeConstSpan(text_));
209
8.20k
  }
210
211
 private:
212
  const std::vector<uint8_t> text_;
213
};
214
215
class BasicPlaneSource final : public SourceImpl {
216
 public:
217
  BasicPlaneSource(std::string description,
218
                   absl::InlinedVector<SourcePosition, 1> line_offsets,
219
                   std::vector<char16_t> text)
220
983
      : SourceImpl(std::move(description), std::move(line_offsets)),
221
983
        text_(std::move(text)) {}
222
223
26.8k
  ContentView content() const override {
224
26.8k
    return MakeContentView(absl::MakeConstSpan(text_));
225
26.8k
  }
226
227
 private:
228
  const std::vector<char16_t> text_;
229
};
230
231
class SupplementalPlaneSource final : public SourceImpl {
232
 public:
233
  SupplementalPlaneSource(std::string description,
234
                          absl::InlinedVector<SourcePosition, 1> line_offsets,
235
                          std::vector<char32_t> text)
236
935
      : SourceImpl(std::move(description), std::move(line_offsets)),
237
935
        text_(std::move(text)) {}
238
239
25.7k
  ContentView content() const override {
240
25.7k
    return MakeContentView(absl::MakeConstSpan(text_));
241
25.7k
  }
242
243
 private:
244
  const std::vector<char32_t> text_;
245
};
246
247
template <typename T>
248
struct SourceTextTraits;
249
250
template <>
251
struct SourceTextTraits<absl::string_view> {
252
  using iterator_type = absl::string_view;
253
254
21.5k
  static iterator_type Begin(absl::string_view text) { return text; }
255
256
167M
  static void Advance(iterator_type& it, size_t n) { it.remove_prefix(n); }
257
258
  static void AppendTo(std::vector<uint8_t>& out, absl::string_view text,
259
543
                       size_t n) {
260
543
    const auto* in = reinterpret_cast<const uint8_t*>(text.data());
261
543
    out.insert(out.end(), in, in + n);
262
543
  }
263
264
19.2k
  static std::vector<char> ToVector(absl::string_view in) {
265
19.2k
    std::vector<char> out;
266
19.2k
    out.reserve(in.size());
267
19.2k
    out.insert(out.end(), in.begin(), in.end());
268
19.2k
    return out;
269
19.2k
  }
270
};
271
272
template <>
273
struct SourceTextTraits<absl::Cord> {
274
  using iterator_type = absl::Cord::CharIterator;
275
276
0
  static iterator_type Begin(const absl::Cord& text) {
277
0
    return text.char_begin();
278
0
  }
279
280
0
  static void Advance(iterator_type& it, size_t n) {
281
0
    absl::Cord::Advance(&it, n);
282
0
  }
283
284
  static void AppendTo(std::vector<uint8_t>& out, const absl::Cord& text,
285
0
                       size_t n) {
286
0
    auto it = text.char_begin();
287
0
    while (n > 0) {
288
0
      auto str = absl::Cord::ChunkRemaining(it);
289
0
      size_t to_append = std::min(n, str.size());
290
0
      const auto* in = reinterpret_cast<const uint8_t*>(str.data());
291
0
      out.insert(out.end(), in, in + to_append);
292
0
      n -= to_append;
293
0
      absl::Cord::Advance(&it, to_append);
294
0
    }
295
0
  }
296
297
0
  static std::vector<char> ToVector(const absl::Cord& in) {
298
0
    std::vector<char> out;
299
0
    out.reserve(in.size());
300
0
    for (const auto& chunk : in.Chunks()) {
301
0
      out.insert(out.end(), chunk.begin(), chunk.end());
302
0
    }
303
0
    return out;
304
0
  }
305
};
306
307
template <typename T>
308
absl::StatusOr<SourcePtr> NewSourceImpl(std::string description, const T& text,
309
21.5k
                                        const size_t text_size) {
310
21.5k
  if (ABSL_PREDICT_FALSE(
311
21.5k
          text_size >
312
21.5k
          static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
313
0
    return absl::InvalidArgumentError("expression larger than 2GiB limit");
314
0
  }
315
21.5k
  using Traits = SourceTextTraits<T>;
316
21.5k
  size_t index = 0;
317
21.5k
  typename Traits::iterator_type it = Traits::Begin(text);
318
21.5k
  SourcePosition offset = 0;
319
21.5k
  char32_t code_point;
320
21.5k
  size_t code_units;
321
21.5k
  std::vector<uint8_t> data8;
322
21.5k
  std::vector<char16_t> data16;
323
21.5k
  std::vector<char32_t> data32;
324
21.5k
  absl::InlinedVector<SourcePosition, 1> line_offsets;
325
108M
  while (index < text_size) {
326
108M
    std::tie(code_point, code_units) = cel::internal::Utf8Decode(it);
327
108M
    if (ABSL_PREDICT_FALSE(code_point ==
328
108M
                               cel::internal::kUnicodeReplacementCharacter &&
329
108M
                           code_units == 1)) {
330
      // Thats an invalid UTF-8 encoding.
331
49
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
332
49
    }
333
108M
    if (code_point == '\n') {
334
34.5M
      line_offsets.push_back(offset + 1);
335
34.5M
    }
336
108M
    if (code_point <= 0x7f) {
337
108M
      Traits::Advance(it, code_units);
338
108M
      index += code_units;
339
108M
      ++offset;
340
108M
      continue;
341
108M
    }
342
2.32k
    if (code_point <= 0xff) {
343
543
      data8.reserve(text_size);
344
543
      Traits::AppendTo(data8, text, index);
345
543
      data8.push_back(static_cast<uint8_t>(code_point));
346
543
      Traits::Advance(it, code_units);
347
543
      index += code_units;
348
543
      ++offset;
349
543
      goto latin1;
350
543
    }
351
1.78k
    if (code_point <= 0xffff) {
352
1.08k
      data16.reserve(text_size);
353
15.0M
      for (size_t offset = 0; offset < index; offset++) {
354
15.0M
        data16.push_back(static_cast<uint8_t>(text[offset]));
355
15.0M
      }
356
1.08k
      data16.push_back(static_cast<char16_t>(code_point));
357
1.08k
      Traits::Advance(it, code_units);
358
1.08k
      index += code_units;
359
1.08k
      ++offset;
360
1.08k
      goto basic;
361
1.08k
    }
362
695
    data32.reserve(text_size);
363
5.84M
    for (size_t offset = 0; offset < index; offset++) {
364
5.84M
      data32.push_back(static_cast<char32_t>(text[offset]));
365
5.84M
    }
366
695
    data32.push_back(code_point);
367
695
    Traits::Advance(it, code_units);
368
695
    index += code_units;
369
695
    ++offset;
370
695
    goto supplemental;
371
1.78k
  }
372
19.2k
  line_offsets.push_back(offset + 1);
373
19.2k
  return std::make_unique<AsciiSource>(
374
19.2k
      std::move(description), std::move(line_offsets), Traits::ToVector(text));
375
543
latin1:
376
7.51M
  while (index < text_size) {
377
7.50M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
378
7.50M
    if (ABSL_PREDICT_FALSE(code_point ==
379
7.50M
                               internal::kUnicodeReplacementCharacter &&
380
7.50M
                           code_units == 1)) {
381
      // Thats an invalid UTF-8 encoding.
382
17
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
383
17
    }
384
7.50M
    if (code_point == '\n') {
385
4.09M
      line_offsets.push_back(offset + 1);
386
4.09M
    }
387
7.50M
    if (code_point <= 0xff) {
388
7.50M
      data8.push_back(static_cast<uint8_t>(code_point));
389
7.50M
      Traits::Advance(it, code_units);
390
7.50M
      index += code_units;
391
7.50M
      ++offset;
392
7.50M
      continue;
393
7.50M
    }
394
202
    if (code_point <= 0xffff) {
395
97
      data16.reserve(text_size);
396
2.26M
      for (const auto& value : data8) {
397
2.26M
        data16.push_back(value);
398
2.26M
      }
399
97
      std::vector<uint8_t>().swap(data8);
400
97
      data16.push_back(static_cast<char16_t>(code_point));
401
97
      Traits::Advance(it, code_units);
402
97
      index += code_units;
403
97
      ++offset;
404
97
      goto basic;
405
97
    }
406
105
    data32.reserve(text_size);
407
1.64M
    for (const auto& value : data8) {
408
1.64M
      data32.push_back(value);
409
1.64M
    }
410
105
    std::vector<uint8_t>().swap(data8);
411
105
    data32.push_back(code_point);
412
105
    Traits::Advance(it, code_units);
413
105
    index += code_units;
414
105
    ++offset;
415
105
    goto supplemental;
416
202
  }
417
324
  line_offsets.push_back(offset + 1);
418
324
  return std::make_unique<Latin1Source>(
419
324
      std::move(description), std::move(line_offsets), std::move(data8));
420
1.18k
basic:
421
18.0M
  while (index < text_size) {
422
18.0M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
423
18.0M
    if (ABSL_PREDICT_FALSE(code_point ==
424
18.0M
                               internal::kUnicodeReplacementCharacter &&
425
18.0M
                           code_units == 1)) {
426
      // Thats an invalid UTF-8 encoding.
427
34
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
428
34
    }
429
18.0M
    if (code_point == '\n') {
430
9.15M
      line_offsets.push_back(offset + 1);
431
9.15M
    }
432
18.0M
    if (code_point <= 0xffff) {
433
18.0M
      data16.push_back(static_cast<char16_t>(code_point));
434
18.0M
      Traits::Advance(it, code_units);
435
18.0M
      index += code_units;
436
18.0M
      ++offset;
437
18.0M
      continue;
438
18.0M
    }
439
167
    data32.reserve(text_size);
440
2.15M
    for (const auto& value : data16) {
441
2.15M
      data32.push_back(static_cast<char32_t>(value));
442
2.15M
    }
443
167
    std::vector<char16_t>().swap(data16);
444
167
    data32.push_back(code_point);
445
167
    Traits::Advance(it, code_units);
446
167
    index += code_units;
447
167
    ++offset;
448
167
    goto supplemental;
449
18.0M
  }
450
983
  line_offsets.push_back(offset + 1);
451
983
  return std::make_unique<BasicPlaneSource>(
452
983
      std::move(description), std::move(line_offsets), std::move(data16));
453
967
supplemental:
454
33.6M
  while (index < text_size) {
455
33.6M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
456
33.6M
    if (ABSL_PREDICT_FALSE(code_point ==
457
33.6M
                               internal::kUnicodeReplacementCharacter &&
458
33.6M
                           code_units == 1)) {
459
      // Thats an invalid UTF-8 encoding.
460
32
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
461
32
    }
462
33.6M
    if (code_point == '\n') {
463
12.8M
      line_offsets.push_back(offset + 1);
464
12.8M
    }
465
33.6M
    data32.push_back(code_point);
466
33.6M
    Traits::Advance(it, code_units);
467
33.6M
    index += code_units;
468
33.6M
    ++offset;
469
33.6M
  }
470
935
  line_offsets.push_back(offset + 1);
471
935
  return std::make_unique<SupplementalPlaneSource>(
472
935
      std::move(description), std::move(line_offsets), std::move(data32));
473
967
}
source.cc:absl::lts_20260107::StatusOr<std::__1::unique_ptr<cel::Source, std::__1::default_delete<cel::Source> > > cel::common_internal::(anonymous namespace)::NewSourceImpl<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, unsigned long)
Line
Count
Source
309
21.5k
                                        const size_t text_size) {
310
21.5k
  if (ABSL_PREDICT_FALSE(
311
21.5k
          text_size >
312
21.5k
          static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
313
0
    return absl::InvalidArgumentError("expression larger than 2GiB limit");
314
0
  }
315
21.5k
  using Traits = SourceTextTraits<T>;
316
21.5k
  size_t index = 0;
317
21.5k
  typename Traits::iterator_type it = Traits::Begin(text);
318
21.5k
  SourcePosition offset = 0;
319
21.5k
  char32_t code_point;
320
21.5k
  size_t code_units;
321
21.5k
  std::vector<uint8_t> data8;
322
21.5k
  std::vector<char16_t> data16;
323
21.5k
  std::vector<char32_t> data32;
324
21.5k
  absl::InlinedVector<SourcePosition, 1> line_offsets;
325
108M
  while (index < text_size) {
326
108M
    std::tie(code_point, code_units) = cel::internal::Utf8Decode(it);
327
108M
    if (ABSL_PREDICT_FALSE(code_point ==
328
108M
                               cel::internal::kUnicodeReplacementCharacter &&
329
108M
                           code_units == 1)) {
330
      // Thats an invalid UTF-8 encoding.
331
49
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
332
49
    }
333
108M
    if (code_point == '\n') {
334
34.5M
      line_offsets.push_back(offset + 1);
335
34.5M
    }
336
108M
    if (code_point <= 0x7f) {
337
108M
      Traits::Advance(it, code_units);
338
108M
      index += code_units;
339
108M
      ++offset;
340
108M
      continue;
341
108M
    }
342
2.32k
    if (code_point <= 0xff) {
343
543
      data8.reserve(text_size);
344
543
      Traits::AppendTo(data8, text, index);
345
543
      data8.push_back(static_cast<uint8_t>(code_point));
346
543
      Traits::Advance(it, code_units);
347
543
      index += code_units;
348
543
      ++offset;
349
543
      goto latin1;
350
543
    }
351
1.78k
    if (code_point <= 0xffff) {
352
1.08k
      data16.reserve(text_size);
353
15.0M
      for (size_t offset = 0; offset < index; offset++) {
354
15.0M
        data16.push_back(static_cast<uint8_t>(text[offset]));
355
15.0M
      }
356
1.08k
      data16.push_back(static_cast<char16_t>(code_point));
357
1.08k
      Traits::Advance(it, code_units);
358
1.08k
      index += code_units;
359
1.08k
      ++offset;
360
1.08k
      goto basic;
361
1.08k
    }
362
695
    data32.reserve(text_size);
363
5.84M
    for (size_t offset = 0; offset < index; offset++) {
364
5.84M
      data32.push_back(static_cast<char32_t>(text[offset]));
365
5.84M
    }
366
695
    data32.push_back(code_point);
367
695
    Traits::Advance(it, code_units);
368
695
    index += code_units;
369
695
    ++offset;
370
695
    goto supplemental;
371
1.78k
  }
372
19.2k
  line_offsets.push_back(offset + 1);
373
19.2k
  return std::make_unique<AsciiSource>(
374
19.2k
      std::move(description), std::move(line_offsets), Traits::ToVector(text));
375
543
latin1:
376
7.51M
  while (index < text_size) {
377
7.50M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
378
7.50M
    if (ABSL_PREDICT_FALSE(code_point ==
379
7.50M
                               internal::kUnicodeReplacementCharacter &&
380
7.50M
                           code_units == 1)) {
381
      // Thats an invalid UTF-8 encoding.
382
17
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
383
17
    }
384
7.50M
    if (code_point == '\n') {
385
4.09M
      line_offsets.push_back(offset + 1);
386
4.09M
    }
387
7.50M
    if (code_point <= 0xff) {
388
7.50M
      data8.push_back(static_cast<uint8_t>(code_point));
389
7.50M
      Traits::Advance(it, code_units);
390
7.50M
      index += code_units;
391
7.50M
      ++offset;
392
7.50M
      continue;
393
7.50M
    }
394
202
    if (code_point <= 0xffff) {
395
97
      data16.reserve(text_size);
396
2.26M
      for (const auto& value : data8) {
397
2.26M
        data16.push_back(value);
398
2.26M
      }
399
97
      std::vector<uint8_t>().swap(data8);
400
97
      data16.push_back(static_cast<char16_t>(code_point));
401
97
      Traits::Advance(it, code_units);
402
97
      index += code_units;
403
97
      ++offset;
404
97
      goto basic;
405
97
    }
406
105
    data32.reserve(text_size);
407
1.64M
    for (const auto& value : data8) {
408
1.64M
      data32.push_back(value);
409
1.64M
    }
410
105
    std::vector<uint8_t>().swap(data8);
411
105
    data32.push_back(code_point);
412
105
    Traits::Advance(it, code_units);
413
105
    index += code_units;
414
105
    ++offset;
415
105
    goto supplemental;
416
202
  }
417
324
  line_offsets.push_back(offset + 1);
418
324
  return std::make_unique<Latin1Source>(
419
324
      std::move(description), std::move(line_offsets), std::move(data8));
420
1.18k
basic:
421
18.0M
  while (index < text_size) {
422
18.0M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
423
18.0M
    if (ABSL_PREDICT_FALSE(code_point ==
424
18.0M
                               internal::kUnicodeReplacementCharacter &&
425
18.0M
                           code_units == 1)) {
426
      // Thats an invalid UTF-8 encoding.
427
34
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
428
34
    }
429
18.0M
    if (code_point == '\n') {
430
9.15M
      line_offsets.push_back(offset + 1);
431
9.15M
    }
432
18.0M
    if (code_point <= 0xffff) {
433
18.0M
      data16.push_back(static_cast<char16_t>(code_point));
434
18.0M
      Traits::Advance(it, code_units);
435
18.0M
      index += code_units;
436
18.0M
      ++offset;
437
18.0M
      continue;
438
18.0M
    }
439
167
    data32.reserve(text_size);
440
2.15M
    for (const auto& value : data16) {
441
2.15M
      data32.push_back(static_cast<char32_t>(value));
442
2.15M
    }
443
167
    std::vector<char16_t>().swap(data16);
444
167
    data32.push_back(code_point);
445
167
    Traits::Advance(it, code_units);
446
167
    index += code_units;
447
167
    ++offset;
448
167
    goto supplemental;
449
18.0M
  }
450
983
  line_offsets.push_back(offset + 1);
451
983
  return std::make_unique<BasicPlaneSource>(
452
983
      std::move(description), std::move(line_offsets), std::move(data16));
453
967
supplemental:
454
33.6M
  while (index < text_size) {
455
33.6M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
456
33.6M
    if (ABSL_PREDICT_FALSE(code_point ==
457
33.6M
                               internal::kUnicodeReplacementCharacter &&
458
33.6M
                           code_units == 1)) {
459
      // Thats an invalid UTF-8 encoding.
460
32
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
461
32
    }
462
33.6M
    if (code_point == '\n') {
463
12.8M
      line_offsets.push_back(offset + 1);
464
12.8M
    }
465
33.6M
    data32.push_back(code_point);
466
33.6M
    Traits::Advance(it, code_units);
467
33.6M
    index += code_units;
468
33.6M
    ++offset;
469
33.6M
  }
470
935
  line_offsets.push_back(offset + 1);
471
935
  return std::make_unique<SupplementalPlaneSource>(
472
935
      std::move(description), std::move(line_offsets), std::move(data32));
473
967
}
Unexecuted instantiation: source.cc:absl::lts_20260107::StatusOr<std::__1::unique_ptr<cel::Source, std::__1::default_delete<cel::Source> > > cel::common_internal::(anonymous namespace)::NewSourceImpl<absl::lts_20260107::Cord>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, absl::lts_20260107::Cord const&, unsigned long)
474
475
}  // namespace
476
477
}  // namespace common_internal
478
479
absl::optional<SourceLocation> Source::GetLocation(
480
191k
    SourcePosition position) const {
481
191k
  if (auto line_and_offset = FindLine(position);
482
191k
      ABSL_PREDICT_TRUE(line_and_offset.has_value())) {
483
188k
    return SourceLocation{line_and_offset->first,
484
188k
                          position - line_and_offset->second};
485
188k
  }
486
3.22k
  return absl::nullopt;
487
191k
}
488
489
absl::optional<SourcePosition> Source::GetPosition(
490
8.36M
    const SourceLocation& location) const {
491
8.36M
  if (ABSL_PREDICT_FALSE(location.line < 1 || location.column < 0)) {
492
0
    return absl::nullopt;
493
0
  }
494
8.36M
  if (auto position = FindLinePosition(location.line);
495
8.36M
      ABSL_PREDICT_TRUE(position.has_value())) {
496
8.36M
    return *position + location.column;
497
8.36M
  }
498
0
  return absl::nullopt;
499
8.36M
}
500
501
192k
absl::optional<std::string> Source::Snippet(int32_t line) const {
502
192k
  auto content = this->content();
503
192k
  auto start = FindLinePosition(line);
504
192k
  if (ABSL_PREDICT_FALSE(!start.has_value() || content.empty())) {
505
4.01k
    return absl::nullopt;
506
4.01k
  }
507
188k
  auto end = FindLinePosition(line + 1);
508
188k
  if (end.has_value()) {
509
38.4k
    return content.ToString(*start, *end - 1);
510
38.4k
  }
511
150k
  return content.ToString(*start);
512
188k
}
513
514
192k
std::string Source::DisplayErrorLocation(SourceLocation location) const {
515
192k
  constexpr char32_t kDot = '.';
516
192k
  constexpr char32_t kHat = '^';
517
518
192k
  constexpr char32_t kWideDot = 0xff0e;
519
192k
  constexpr char32_t kWideHat = 0xff3e;
520
192k
  absl::optional<std::string> snippet = Snippet(location.line);
521
192k
  if (!snippet || snippet->empty()) {
522
4.55k
    return "";
523
4.55k
  }
524
525
187k
  *snippet = absl::StrReplaceAll(*snippet, {{"\t", " "}});
526
187k
  absl::string_view snippet_view(*snippet);
527
187k
  std::string result;
528
187k
  absl::StrAppend(&result, "\n | ", *snippet);
529
187k
  absl::StrAppend(&result, "\n | ");
530
531
187k
  std::string index_line;
532
519M
  for (int32_t i = 0; i < location.column && !snippet_view.empty(); ++i) {
533
518M
    size_t count;
534
518M
    std::tie(std::ignore, count) = internal::Utf8Decode(snippet_view);
535
518M
    snippet_view.remove_prefix(count);
536
518M
    if (count > 1) {
537
162k
      internal::Utf8Encode(index_line, kWideDot);
538
518M
    } else {
539
518M
      internal::Utf8Encode(index_line, kDot);
540
518M
    }
541
518M
  }
542
187k
  size_t count = 0;
543
187k
  if (!snippet_view.empty()) {
544
181k
    std::tie(std::ignore, count) = internal::Utf8Decode(snippet_view);
545
181k
  }
546
187k
  if (count > 1) {
547
3.81k
    internal::Utf8Encode(index_line, kWideHat);
548
184k
  } else {
549
184k
    internal::Utf8Encode(index_line, kHat);
550
184k
  }
551
187k
  absl::StrAppend(&result, index_line);
552
187k
  return result;
553
192k
}
554
555
8.74M
absl::optional<SourcePosition> Source::FindLinePosition(int32_t line) const {
556
8.74M
  if (ABSL_PREDICT_FALSE(line < 1)) {
557
4.01k
    return absl::nullopt;
558
4.01k
  }
559
8.74M
  if (line == 1) {
560
7.18M
    return SourcePosition{0};
561
7.18M
  }
562
1.55M
  const auto line_offsets = this->line_offsets();
563
1.55M
  if (ABSL_PREDICT_TRUE(line <= static_cast<int32_t>(line_offsets.size()))) {
564
1.40M
    return line_offsets[static_cast<size_t>(line - 2)];
565
1.40M
  }
566
150k
  return absl::nullopt;
567
1.55M
}
568
569
absl::optional<std::pair<int32_t, SourcePosition>> Source::FindLine(
570
191k
    SourcePosition position) const {
571
191k
  if (ABSL_PREDICT_FALSE(position < 0)) {
572
3.22k
    return absl::nullopt;
573
3.22k
  }
574
188k
  int32_t line = 1;
575
188k
  const auto line_offsets = this->line_offsets();
576
1.61G
  for (const auto& line_offset : line_offsets) {
577
1.61G
    if (line_offset > position) {
578
188k
      break;
579
188k
    }
580
1.61G
    ++line;
581
1.61G
  }
582
188k
  if (line == 1) {
583
147k
    return std::make_pair(line, SourcePosition{0});
584
147k
  }
585
40.7k
  return std::make_pair(line, line_offsets[static_cast<size_t>(line) - 2]);
586
188k
}
587
588
absl::StatusOr<absl_nonnull SourcePtr> NewSource(absl::string_view content,
589
21.5k
                                                 std::string description) {
590
21.5k
  return common_internal::NewSourceImpl(std::move(description), content,
591
21.5k
                                        content.size());
592
21.5k
}
593
594
absl::StatusOr<absl_nonnull SourcePtr> NewSource(const absl::Cord& content,
595
0
                                                 std::string description) {
596
0
  return common_internal::NewSourceImpl(std::move(description), content,
597
0
                                        content.size());
598
0
}
599
600
}  // namespace cel