Coverage Report

Created: 2026-11-12 06:42

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 <optional>
23
#include <string>
24
#include <tuple>
25
#include <utility>
26
#include <vector>
27
28
#include "absl/base/nullability.h"
29
#include "absl/base/optimization.h"
30
#include "absl/container/inlined_vector.h"
31
#include "absl/functional/overload.h"
32
#include "absl/log/absl_check.h"
33
#include "absl/status/status.h"
34
#include "absl/status/statusor.h"
35
#include "absl/strings/cord.h"
36
#include "absl/strings/str_cat.h"
37
#include "absl/strings/str_replace.h"
38
#include "absl/strings/string_view.h"
39
#include "absl/types/optional.h"
40
#include "absl/types/span.h"
41
#include "absl/types/variant.h"
42
#include "internal/unicode.h"
43
#include "internal/utf8.h"
44
45
namespace cel {
46
47
224k
SourcePosition SourceContentView::size() const {
48
224k
  return static_cast<SourcePosition>(absl::visit(
49
224k
      absl::Overload(
50
224k
          [](absl::Span<const char> view) { return view.size(); },
51
224k
          [](absl::Span<const uint8_t> view) { return view.size(); },
52
224k
          [](absl::Span<const char16_t> view) { return view.size(); },
53
224k
          [](absl::Span<const char32_t> view) { return view.size(); }),
54
224k
      view_));
55
224k
}
56
57
228k
bool SourceContentView::empty() const {
58
228k
  return absl::visit(
59
228k
      absl::Overload(
60
228k
          [](absl::Span<const char> view) { return view.empty(); },
61
228k
          [](absl::Span<const uint8_t> view) { return view.empty(); },
62
228k
          [](absl::Span<const char16_t> view) { return view.empty(); },
63
228k
          [](absl::Span<const char32_t> view) { return view.empty(); }),
64
228k
      view_);
65
228k
}
66
67
167M
char32_t SourceContentView::at(SourcePosition position) const {
68
167M
  ABSL_DCHECK_GE(position, 0);
69
167M
  ABSL_DCHECK_LT(position, size());
70
167M
  return absl::visit(
71
167M
      absl::Overload(
72
167M
          [position =
73
167M
               static_cast<size_t>(position)](absl::Span<const char> view) {
74
94.5M
            return static_cast<char32_t>(static_cast<uint8_t>(view[position]));
75
94.5M
          },
76
167M
          [position =
77
167M
               static_cast<size_t>(position)](absl::Span<const uint8_t> view) {
78
8.11M
            return static_cast<char32_t>(view[position]);
79
8.11M
          },
80
167M
          [position =
81
167M
               static_cast<size_t>(position)](absl::Span<const char16_t> view) {
82
16.0M
            return static_cast<char32_t>(view[position]);
83
16.0M
          },
84
167M
          [position =
85
167M
               static_cast<size_t>(position)](absl::Span<const char32_t> view) {
86
48.7M
            return static_cast<char32_t>(view[position]);
87
48.7M
          }),
88
167M
      view_);
89
167M
}
90
91
std::string SourceContentView::ToString(SourcePosition begin,
92
12.0M
                                        SourcePosition end) const {
93
12.0M
  ABSL_DCHECK_GE(begin, 0);
94
12.0M
  ABSL_DCHECK_LE(end, size());
95
12.0M
  ABSL_DCHECK_LE(begin, end);
96
12.0M
  return absl::visit(
97
12.0M
      absl::Overload(
98
12.0M
          [begin = static_cast<size_t>(begin),
99
12.0M
           end = static_cast<size_t>(end)](absl::Span<const char> view) {
100
8.05M
            view = view.subspan(begin, end - begin);
101
8.05M
            return std::string(view.data(), view.size());
102
8.05M
          },
103
12.0M
          [begin = static_cast<size_t>(begin),
104
12.0M
           end = static_cast<size_t>(end)](absl::Span<const uint8_t> view) {
105
928k
            view = view.subspan(begin, end - begin);
106
928k
            std::string result;
107
928k
            result.reserve(view.size() * 2);
108
111M
            for (const auto& code_point : view) {
109
111M
              internal::Utf8Encode(result, code_point);
110
111M
            }
111
928k
            result.shrink_to_fit();
112
928k
            return result;
113
928k
          },
114
12.0M
          [begin = static_cast<size_t>(begin),
115
12.0M
           end = static_cast<size_t>(end)](absl::Span<const char16_t> view) {
116
1.26M
            view = view.subspan(begin, end - begin);
117
1.26M
            std::string result;
118
1.26M
            result.reserve(view.size() * 3);
119
273M
            for (const auto& code_point : view) {
120
273M
              internal::Utf8Encode(result, code_point);
121
273M
            }
122
1.26M
            result.shrink_to_fit();
123
1.26M
            return result;
124
1.26M
          },
125
12.0M
          [begin = static_cast<size_t>(begin),
126
12.0M
           end = static_cast<size_t>(end)](absl::Span<const char32_t> view) {
127
1.75M
            view = view.subspan(begin, end - begin);
128
1.75M
            std::string result;
129
1.75M
            result.reserve(view.size() * 4);
130
362M
            for (const auto& code_point : view) {
131
362M
              internal::Utf8Encode(result, code_point);
132
362M
            }
133
1.75M
            result.shrink_to_fit();
134
1.75M
            return result;
135
1.75M
          }),
136
12.0M
      view_);
137
12.0M
}
138
139
0
void SourceContentView::AppendToString(std::string& dest) const {
140
0
  absl::visit(absl::Overload(
141
0
                  [&dest](absl::Span<const char> view) {
142
0
                    dest.append(view.data(), view.size());
143
0
                  },
144
0
                  [&dest](absl::Span<const uint8_t> view) {
145
0
                    for (const auto& code_point : view) {
146
0
                      internal::Utf8Encode(dest, code_point);
147
0
                    }
148
0
                  },
149
0
                  [&dest](absl::Span<const char16_t> view) {
150
0
                    for (const auto& code_point : view) {
151
0
                      internal::Utf8Encode(dest, code_point);
152
0
                    }
153
0
                  },
154
0
                  [&dest](absl::Span<const char32_t> view) {
155
0
                    for (const auto& code_point : view) {
156
0
                      internal::Utf8Encode(dest, code_point);
157
0
                    }
158
0
                  }),
159
0
              view_);
160
0
}
161
162
namespace common_internal {
163
164
class SourceImpl : public Source {
165
 public:
166
  struct OffsetEntry {
167
    int32_t byte_offset;
168
    SourcePosition codepoint;
169
  };
170
171
  SourceImpl(std::string description,
172
             absl::InlinedVector<SourcePosition, 1> line_offsets,
173
             std::vector<OffsetEntry> byte_offsets = {})
174
36.4k
      : description_(std::move(description)),
175
36.4k
        line_offsets_(std::move(line_offsets)),
176
36.4k
        byte_offsets_(std::move(byte_offsets)) {}
177
178
294k
  absl::string_view description() const final { return description_; }
179
180
823k
  absl::Span<const SourcePosition> line_offsets() const final {
181
823k
    return absl::MakeConstSpan(line_offsets_);
182
823k
  }
183
184
  absl::optional<SourcePosition> GetPositionFromByteOffset(
185
0
      SourcePosition byte_offset) const final {
186
    // handle out of bounds and all ascii.
187
0
    if (byte_offset < 0) {
188
0
      return absl::nullopt;
189
0
    }
190
0
    if (byte_offsets_.empty()) {
191
0
      if (byte_offset > content().size()) {
192
0
        return absl::nullopt;
193
0
      }
194
0
      return byte_offset;
195
0
    }
196
0
    if (byte_offset > byte_offsets_.back().byte_offset) {
197
0
      return absl::nullopt;
198
0
    }
199
200
    // binary search for the next alignment shift after the given byte offset.
201
0
    auto it = std::upper_bound(
202
0
        byte_offsets_.begin(), byte_offsets_.end(), byte_offset,
203
0
        [](SourcePosition val, const OffsetEntry& entry) {
204
0
          return val < entry.byte_offset;
205
0
        });
206
0
    if (it == byte_offsets_.begin()) {
207
0
      return byte_offset;
208
0
    }
209
0
    const OffsetEntry& prev = *(it - 1);
210
0
    if (it == byte_offsets_.end()) {
211
      // matched the EOF entry.
212
0
      return prev.codepoint;
213
0
    }
214
215
    // the total shift at the next multi-byte character.
216
0
    int32_t next_delta = it->byte_offset - it->codepoint;
217
218
    // The offset is either in the previous multi-byte character or in the
219
    // run of ascii characters after it. if negative, then in, otherwise
220
    // shifted down.
221
0
    return std::max(prev.codepoint, byte_offset - next_delta);
222
0
  }
223
224
 private:
225
  const std::string description_;
226
  const absl::InlinedVector<SourcePosition, 1> line_offsets_;
227
  const std::vector<OffsetEntry> byte_offsets_;
228
};
229
230
namespace {
231
232
class AsciiSource final : public SourceImpl {
233
 public:
234
  AsciiSource(std::string description,
235
              absl::InlinedVector<SourcePosition, 1> line_offsets,
236
              std::vector<char> text)
237
33.1k
      : SourceImpl(std::move(description), std::move(line_offsets)),
238
33.1k
        text_(std::move(text)) {}
239
240
194k
  ContentView content() const override {
241
194k
    return MakeContentView(absl::MakeConstSpan(text_));
242
194k
  }
243
244
 private:
245
  const std::vector<char> text_;
246
};
247
248
class Latin1Source final : public SourceImpl {
249
 public:
250
  Latin1Source(std::string description,
251
               absl::InlinedVector<SourcePosition, 1> line_offsets,
252
               std::vector<OffsetEntry> byte_offsets, std::vector<uint8_t> text)
253
575
      : SourceImpl(std::move(description), std::move(line_offsets),
254
575
                   std::move(byte_offsets)),
255
575
        text_(std::move(text)) {}
256
257
14.3k
  ContentView content() const override {
258
14.3k
    return MakeContentView(absl::MakeConstSpan(text_));
259
14.3k
  }
260
261
 private:
262
  const std::vector<uint8_t> text_;
263
};
264
265
class BasicPlaneSource final : public SourceImpl {
266
 public:
267
  BasicPlaneSource(std::string description,
268
                   absl::InlinedVector<SourcePosition, 1> line_offsets,
269
                   std::vector<OffsetEntry> byte_offsets,
270
                   std::vector<char16_t> text)
271
1.40k
      : SourceImpl(std::move(description), std::move(line_offsets),
272
1.40k
                   std::move(byte_offsets)),
273
1.40k
        text_(std::move(text)) {}
274
275
27.3k
  ContentView content() const override {
276
27.3k
    return MakeContentView(absl::MakeConstSpan(text_));
277
27.3k
  }
278
279
 private:
280
  const std::vector<char16_t> text_;
281
};
282
283
class SupplementalPlaneSource final : public SourceImpl {
284
 public:
285
  SupplementalPlaneSource(std::string description,
286
                          absl::InlinedVector<SourcePosition, 1> line_offsets,
287
                          std::vector<OffsetEntry> byte_offsets,
288
                          std::vector<char32_t> text)
289
1.30k
      : SourceImpl(std::move(description), std::move(line_offsets),
290
1.30k
                   std::move(byte_offsets)),
291
1.30k
        text_(std::move(text)) {}
292
293
35.8k
  ContentView content() const override {
294
35.8k
    return MakeContentView(absl::MakeConstSpan(text_));
295
35.8k
  }
296
297
 private:
298
  const std::vector<char32_t> text_;
299
};
300
301
template <typename T>
302
struct SourceTextTraits;
303
304
template <>
305
struct SourceTextTraits<absl::string_view> {
306
  using iterator_type = absl::string_view;
307
308
36.6k
  static iterator_type Begin(absl::string_view text) { return text; }
309
310
45.6M
  static void Advance(iterator_type& it, size_t n) { it.remove_prefix(n); }
311
312
  static void AppendTo(std::vector<uint8_t>& out, absl::string_view text,
313
847
                       size_t n) {
314
847
    const auto* in = reinterpret_cast<const uint8_t*>(text.data());
315
847
    out.insert(out.end(), in, in + n);
316
847
  }
317
318
33.1k
  static std::vector<char> ToVector(absl::string_view in) {
319
33.1k
    std::vector<char> out;
320
33.1k
    out.reserve(in.size());
321
33.1k
    out.insert(out.end(), in.begin(), in.end());
322
33.1k
    return out;
323
33.1k
  }
324
};
325
326
template <>
327
struct SourceTextTraits<absl::Cord> {
328
  using iterator_type = absl::Cord::CharIterator;
329
330
0
  static iterator_type Begin(const absl::Cord& text) {
331
0
    return text.char_begin();
332
0
  }
333
334
0
  static void Advance(iterator_type& it, size_t n) {
335
0
    absl::Cord::Advance(&it, n);
336
0
  }
337
338
  static void AppendTo(std::vector<uint8_t>& out, const absl::Cord& text,
339
0
                       size_t n) {
340
0
    auto it = text.char_begin();
341
0
    while (n > 0) {
342
0
      auto str = absl::Cord::ChunkRemaining(it);
343
0
      size_t to_append = std::min(n, str.size());
344
0
      const auto* in = reinterpret_cast<const uint8_t*>(str.data());
345
0
      out.insert(out.end(), in, in + to_append);
346
0
      n -= to_append;
347
0
      absl::Cord::Advance(&it, to_append);
348
0
    }
349
0
  }
350
351
0
  static std::vector<char> ToVector(const absl::Cord& in) {
352
0
    std::vector<char> out;
353
0
    out.reserve(in.size());
354
0
    for (const auto& chunk : in.Chunks()) {
355
0
      out.insert(out.end(), chunk.begin(), chunk.end());
356
0
    }
357
0
    return out;
358
0
  }
359
};
360
361
template <typename T>
362
absl::StatusOr<SourcePtr> NewSourceImpl(std::string description, const T& text,
363
                                        const size_t text_size,
364
36.7k
                                        const size_t max_codepoints) {
365
36.7k
  if (ABSL_PREDICT_FALSE(
366
36.7k
          text_size >
367
36.7k
          static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
368
0
    return absl::InvalidArgumentError("expression larger than 2GiB limit");
369
0
  }
370
36.7k
  if ((text_size >> 2) > max_codepoints) {
371
    // If byte size is 4 times the codepoint limit, then definitely exceeded.
372
26
    return absl::InvalidArgumentError(absl::StrCat(
373
26
        "expression is larger than codepoint limit ", max_codepoints));
374
26
  }
375
36.6k
  using Traits = SourceTextTraits<T>;
376
36.6k
  size_t index = 0;
377
36.6k
  typename Traits::iterator_type it = Traits::Begin(text);
378
36.6k
  SourcePosition offset = 0;
379
36.6k
  char32_t code_point;
380
36.6k
  size_t code_units;
381
36.6k
  std::vector<uint8_t> data8;
382
36.6k
  std::vector<char16_t> data16;
383
36.6k
  std::vector<char32_t> data32;
384
36.6k
  absl::InlinedVector<SourcePosition, 1> line_offsets;
385
36.6k
  std::vector<SourceImpl::OffsetEntry> byte_offsets;
386
31.5M
  while (index < text_size) {
387
31.5M
    if (offset >= max_codepoints) {
388
4
      return absl::InvalidArgumentError(absl::StrCat(
389
4
          "expression is larger than codepoint limit ", max_codepoints));
390
4
    }
391
31.5M
    std::tie(code_point, code_units) = cel::internal::Utf8Decode(it);
392
31.5M
    if (ABSL_PREDICT_FALSE(code_point ==
393
31.5M
                               cel::internal::kUnicodeReplacementCharacter &&
394
31.5M
                           code_units == 1)) {
395
      // Thats an invalid UTF-8 encoding.
396
82
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
397
82
    }
398
31.5M
    if (code_point == '\n') {
399
5.53M
      line_offsets.push_back(offset + 1);
400
5.53M
    }
401
31.5M
    if (code_point <= 0x7f) {
402
31.5M
      Traits::Advance(it, code_units);
403
31.5M
      index += code_units;
404
31.5M
      ++offset;
405
31.5M
      continue;
406
31.5M
    }
407
3.44k
    byte_offsets.push_back({static_cast<int32_t>(index), offset});
408
3.44k
    if (code_point <= 0xff) {
409
847
      data8.reserve(text_size);
410
847
      Traits::AppendTo(data8, text, index);
411
847
      data8.push_back(static_cast<uint8_t>(code_point));
412
847
      Traits::Advance(it, code_units);
413
847
      index += code_units;
414
847
      ++offset;
415
847
      goto latin1;
416
847
    }
417
2.60k
    if (code_point <= 0xffff) {
418
1.53k
      data16.reserve(text_size);
419
3.34M
      for (size_t offset = 0; offset < index; offset++) {
420
3.34M
        data16.push_back(static_cast<uint8_t>(text[offset]));
421
3.34M
      }
422
1.53k
      data16.push_back(static_cast<char16_t>(code_point));
423
1.53k
      Traits::Advance(it, code_units);
424
1.53k
      index += code_units;
425
1.53k
      ++offset;
426
1.53k
      goto basic;
427
1.53k
    }
428
1.07k
    data32.reserve(text_size);
429
3.00M
    for (size_t offset = 0; offset < index; offset++) {
430
3.00M
      data32.push_back(static_cast<char32_t>(text[offset]));
431
3.00M
    }
432
1.07k
    data32.push_back(code_point);
433
1.07k
    Traits::Advance(it, code_units);
434
1.07k
    index += code_units;
435
1.07k
    ++offset;
436
1.07k
    goto supplemental;
437
2.60k
  }
438
33.1k
  line_offsets.push_back(offset + 1);
439
33.1k
  return std::make_unique<AsciiSource>(
440
33.1k
      std::move(description), std::move(line_offsets), Traits::ToVector(text));
441
847
latin1:
442
2.43M
  while (index < text_size) {
443
2.42M
    if (offset >= max_codepoints) {
444
3
      return absl::InvalidArgumentError(absl::StrCat(
445
3
          "expression is larger than codepoint limit ", max_codepoints));
446
3
    }
447
2.42M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
448
2.42M
    if (ABSL_PREDICT_FALSE(code_point ==
449
2.42M
                               internal::kUnicodeReplacementCharacter &&
450
2.42M
                           code_units == 1)) {
451
      // Thats an invalid UTF-8 encoding.
452
31
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
453
31
    }
454
2.42M
    if (code_point == '\n') {
455
472k
      line_offsets.push_back(offset + 1);
456
472k
    }
457
2.42M
    if (code_units > 1) {
458
1.63k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
459
1.63k
    }
460
2.42M
    if (code_point <= 0xff) {
461
2.42M
      data8.push_back(static_cast<uint8_t>(code_point));
462
2.42M
      Traits::Advance(it, code_units);
463
2.42M
      index += code_units;
464
2.42M
      ++offset;
465
2.42M
      continue;
466
2.42M
    }
467
238
    if (code_point <= 0xffff) {
468
167
      data16.reserve(text_size);
469
691k
      for (const auto& value : data8) {
470
691k
        data16.push_back(value);
471
691k
      }
472
167
      std::vector<uint8_t>().swap(data8);
473
167
      data16.push_back(static_cast<char16_t>(code_point));
474
167
      Traits::Advance(it, code_units);
475
167
      index += code_units;
476
167
      ++offset;
477
167
      goto basic;
478
167
    }
479
71
    data32.reserve(text_size);
480
218k
    for (const auto& value : data8) {
481
218k
      data32.push_back(value);
482
218k
    }
483
71
    std::vector<uint8_t>().swap(data8);
484
71
    data32.push_back(code_point);
485
71
    Traits::Advance(it, code_units);
486
71
    index += code_units;
487
71
    ++offset;
488
71
    goto supplemental;
489
238
  }
490
575
  line_offsets.push_back(offset + 1);
491
575
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
492
575
  return std::make_unique<Latin1Source>(
493
575
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
494
575
      std::move(data8));
495
1.69k
basic:
496
6.25M
  while (index < text_size) {
497
6.25M
    if (offset >= max_codepoints) {
498
2
      return absl::InvalidArgumentError(absl::StrCat(
499
2
          "expression is larger than codepoint limit ", max_codepoints));
500
2
    }
501
6.25M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
502
6.25M
    if (ABSL_PREDICT_FALSE(code_point ==
503
6.25M
                               internal::kUnicodeReplacementCharacter &&
504
6.25M
                           code_units == 1)) {
505
      // Thats an invalid UTF-8 encoding.
506
62
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
507
62
    }
508
6.25M
    if (code_point == '\n') {
509
599k
      line_offsets.push_back(offset + 1);
510
599k
    }
511
6.25M
    if (code_units > 1) {
512
1.73M
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
513
1.73M
    }
514
6.25M
    if (code_point <= 0xffff) {
515
6.25M
      data16.push_back(static_cast<char16_t>(code_point));
516
6.25M
      Traits::Advance(it, code_units);
517
6.25M
      index += code_units;
518
6.25M
      ++offset;
519
6.25M
      continue;
520
6.25M
    }
521
227
    data32.reserve(text_size);
522
2.30M
    for (const auto& value : data16) {
523
2.30M
      data32.push_back(static_cast<char32_t>(value));
524
2.30M
    }
525
227
    std::vector<char16_t>().swap(data16);
526
227
    data32.push_back(code_point);
527
227
    Traits::Advance(it, code_units);
528
227
    index += code_units;
529
227
    ++offset;
530
227
    goto supplemental;
531
6.25M
  }
532
1.40k
  line_offsets.push_back(offset + 1);
533
1.40k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
534
1.40k
  return std::make_unique<BasicPlaneSource>(
535
1.40k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
536
1.40k
      std::move(data16));
537
1.36k
supplemental:
538
5.44M
  while (index < text_size) {
539
5.44M
    if (offset >= max_codepoints) {
540
5
      return absl::InvalidArgumentError(absl::StrCat(
541
5
          "expression is larger than codepoint limit ", max_codepoints));
542
5
    }
543
5.44M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
544
5.44M
    if (ABSL_PREDICT_FALSE(code_point ==
545
5.44M
                               internal::kUnicodeReplacementCharacter &&
546
5.44M
                           code_units == 1)) {
547
      // Thats an invalid UTF-8 encoding.
548
54
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
549
54
    }
550
5.44M
    if (code_point == '\n') {
551
1.30M
      line_offsets.push_back(offset + 1);
552
1.30M
    }
553
5.44M
    if (code_units > 1) {
554
242k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
555
242k
    }
556
5.44M
    data32.push_back(code_point);
557
5.44M
    Traits::Advance(it, code_units);
558
5.44M
    index += code_units;
559
5.44M
    ++offset;
560
5.44M
  }
561
1.30k
  line_offsets.push_back(offset + 1);
562
1.30k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
563
1.30k
  return std::make_unique<SupplementalPlaneSource>(
564
1.30k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
565
1.30k
      std::move(data32));
566
1.36k
}
source.cc:absl::lts_20260526::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, unsigned long)
Line
Count
Source
364
36.7k
                                        const size_t max_codepoints) {
365
36.7k
  if (ABSL_PREDICT_FALSE(
366
36.7k
          text_size >
367
36.7k
          static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
368
0
    return absl::InvalidArgumentError("expression larger than 2GiB limit");
369
0
  }
370
36.7k
  if ((text_size >> 2) > max_codepoints) {
371
    // If byte size is 4 times the codepoint limit, then definitely exceeded.
372
26
    return absl::InvalidArgumentError(absl::StrCat(
373
26
        "expression is larger than codepoint limit ", max_codepoints));
374
26
  }
375
36.6k
  using Traits = SourceTextTraits<T>;
376
36.6k
  size_t index = 0;
377
36.6k
  typename Traits::iterator_type it = Traits::Begin(text);
378
36.6k
  SourcePosition offset = 0;
379
36.6k
  char32_t code_point;
380
36.6k
  size_t code_units;
381
36.6k
  std::vector<uint8_t> data8;
382
36.6k
  std::vector<char16_t> data16;
383
36.6k
  std::vector<char32_t> data32;
384
36.6k
  absl::InlinedVector<SourcePosition, 1> line_offsets;
385
36.6k
  std::vector<SourceImpl::OffsetEntry> byte_offsets;
386
31.5M
  while (index < text_size) {
387
31.5M
    if (offset >= max_codepoints) {
388
4
      return absl::InvalidArgumentError(absl::StrCat(
389
4
          "expression is larger than codepoint limit ", max_codepoints));
390
4
    }
391
31.5M
    std::tie(code_point, code_units) = cel::internal::Utf8Decode(it);
392
31.5M
    if (ABSL_PREDICT_FALSE(code_point ==
393
31.5M
                               cel::internal::kUnicodeReplacementCharacter &&
394
31.5M
                           code_units == 1)) {
395
      // Thats an invalid UTF-8 encoding.
396
82
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
397
82
    }
398
31.5M
    if (code_point == '\n') {
399
5.53M
      line_offsets.push_back(offset + 1);
400
5.53M
    }
401
31.5M
    if (code_point <= 0x7f) {
402
31.5M
      Traits::Advance(it, code_units);
403
31.5M
      index += code_units;
404
31.5M
      ++offset;
405
31.5M
      continue;
406
31.5M
    }
407
3.44k
    byte_offsets.push_back({static_cast<int32_t>(index), offset});
408
3.44k
    if (code_point <= 0xff) {
409
847
      data8.reserve(text_size);
410
847
      Traits::AppendTo(data8, text, index);
411
847
      data8.push_back(static_cast<uint8_t>(code_point));
412
847
      Traits::Advance(it, code_units);
413
847
      index += code_units;
414
847
      ++offset;
415
847
      goto latin1;
416
847
    }
417
2.60k
    if (code_point <= 0xffff) {
418
1.53k
      data16.reserve(text_size);
419
3.34M
      for (size_t offset = 0; offset < index; offset++) {
420
3.34M
        data16.push_back(static_cast<uint8_t>(text[offset]));
421
3.34M
      }
422
1.53k
      data16.push_back(static_cast<char16_t>(code_point));
423
1.53k
      Traits::Advance(it, code_units);
424
1.53k
      index += code_units;
425
1.53k
      ++offset;
426
1.53k
      goto basic;
427
1.53k
    }
428
1.07k
    data32.reserve(text_size);
429
3.00M
    for (size_t offset = 0; offset < index; offset++) {
430
3.00M
      data32.push_back(static_cast<char32_t>(text[offset]));
431
3.00M
    }
432
1.07k
    data32.push_back(code_point);
433
1.07k
    Traits::Advance(it, code_units);
434
1.07k
    index += code_units;
435
1.07k
    ++offset;
436
1.07k
    goto supplemental;
437
2.60k
  }
438
33.1k
  line_offsets.push_back(offset + 1);
439
33.1k
  return std::make_unique<AsciiSource>(
440
33.1k
      std::move(description), std::move(line_offsets), Traits::ToVector(text));
441
847
latin1:
442
2.43M
  while (index < text_size) {
443
2.42M
    if (offset >= max_codepoints) {
444
3
      return absl::InvalidArgumentError(absl::StrCat(
445
3
          "expression is larger than codepoint limit ", max_codepoints));
446
3
    }
447
2.42M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
448
2.42M
    if (ABSL_PREDICT_FALSE(code_point ==
449
2.42M
                               internal::kUnicodeReplacementCharacter &&
450
2.42M
                           code_units == 1)) {
451
      // Thats an invalid UTF-8 encoding.
452
31
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
453
31
    }
454
2.42M
    if (code_point == '\n') {
455
472k
      line_offsets.push_back(offset + 1);
456
472k
    }
457
2.42M
    if (code_units > 1) {
458
1.63k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
459
1.63k
    }
460
2.42M
    if (code_point <= 0xff) {
461
2.42M
      data8.push_back(static_cast<uint8_t>(code_point));
462
2.42M
      Traits::Advance(it, code_units);
463
2.42M
      index += code_units;
464
2.42M
      ++offset;
465
2.42M
      continue;
466
2.42M
    }
467
238
    if (code_point <= 0xffff) {
468
167
      data16.reserve(text_size);
469
691k
      for (const auto& value : data8) {
470
691k
        data16.push_back(value);
471
691k
      }
472
167
      std::vector<uint8_t>().swap(data8);
473
167
      data16.push_back(static_cast<char16_t>(code_point));
474
167
      Traits::Advance(it, code_units);
475
167
      index += code_units;
476
167
      ++offset;
477
167
      goto basic;
478
167
    }
479
71
    data32.reserve(text_size);
480
218k
    for (const auto& value : data8) {
481
218k
      data32.push_back(value);
482
218k
    }
483
71
    std::vector<uint8_t>().swap(data8);
484
71
    data32.push_back(code_point);
485
71
    Traits::Advance(it, code_units);
486
71
    index += code_units;
487
71
    ++offset;
488
71
    goto supplemental;
489
238
  }
490
575
  line_offsets.push_back(offset + 1);
491
575
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
492
575
  return std::make_unique<Latin1Source>(
493
575
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
494
575
      std::move(data8));
495
1.69k
basic:
496
6.25M
  while (index < text_size) {
497
6.25M
    if (offset >= max_codepoints) {
498
2
      return absl::InvalidArgumentError(absl::StrCat(
499
2
          "expression is larger than codepoint limit ", max_codepoints));
500
2
    }
501
6.25M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
502
6.25M
    if (ABSL_PREDICT_FALSE(code_point ==
503
6.25M
                               internal::kUnicodeReplacementCharacter &&
504
6.25M
                           code_units == 1)) {
505
      // Thats an invalid UTF-8 encoding.
506
62
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
507
62
    }
508
6.25M
    if (code_point == '\n') {
509
599k
      line_offsets.push_back(offset + 1);
510
599k
    }
511
6.25M
    if (code_units > 1) {
512
1.73M
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
513
1.73M
    }
514
6.25M
    if (code_point <= 0xffff) {
515
6.25M
      data16.push_back(static_cast<char16_t>(code_point));
516
6.25M
      Traits::Advance(it, code_units);
517
6.25M
      index += code_units;
518
6.25M
      ++offset;
519
6.25M
      continue;
520
6.25M
    }
521
227
    data32.reserve(text_size);
522
2.30M
    for (const auto& value : data16) {
523
2.30M
      data32.push_back(static_cast<char32_t>(value));
524
2.30M
    }
525
227
    std::vector<char16_t>().swap(data16);
526
227
    data32.push_back(code_point);
527
227
    Traits::Advance(it, code_units);
528
227
    index += code_units;
529
227
    ++offset;
530
227
    goto supplemental;
531
6.25M
  }
532
1.40k
  line_offsets.push_back(offset + 1);
533
1.40k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
534
1.40k
  return std::make_unique<BasicPlaneSource>(
535
1.40k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
536
1.40k
      std::move(data16));
537
1.36k
supplemental:
538
5.44M
  while (index < text_size) {
539
5.44M
    if (offset >= max_codepoints) {
540
5
      return absl::InvalidArgumentError(absl::StrCat(
541
5
          "expression is larger than codepoint limit ", max_codepoints));
542
5
    }
543
5.44M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
544
5.44M
    if (ABSL_PREDICT_FALSE(code_point ==
545
5.44M
                               internal::kUnicodeReplacementCharacter &&
546
5.44M
                           code_units == 1)) {
547
      // Thats an invalid UTF-8 encoding.
548
54
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
549
54
    }
550
5.44M
    if (code_point == '\n') {
551
1.30M
      line_offsets.push_back(offset + 1);
552
1.30M
    }
553
5.44M
    if (code_units > 1) {
554
242k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
555
242k
    }
556
5.44M
    data32.push_back(code_point);
557
5.44M
    Traits::Advance(it, code_units);
558
5.44M
    index += code_units;
559
5.44M
    ++offset;
560
5.44M
  }
561
1.30k
  line_offsets.push_back(offset + 1);
562
1.30k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
563
1.30k
  return std::make_unique<SupplementalPlaneSource>(
564
1.30k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
565
1.30k
      std::move(data32));
566
1.36k
}
Unexecuted instantiation: source.cc:absl::lts_20260526::StatusOr<std::__1::unique_ptr<cel::Source, std::__1::default_delete<cel::Source> > > cel::common_internal::(anonymous namespace)::NewSourceImpl<absl::lts_20260526::Cord>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, absl::lts_20260526::Cord const&, unsigned long, unsigned long)
567
568
}  // namespace
569
570
}  // namespace common_internal
571
572
absl::optional<SourceLocation> Source::GetLocation(
573
234k
    SourcePosition position) const {
574
234k
  if (auto line_and_offset = FindLine(position);
575
234k
      ABSL_PREDICT_TRUE(line_and_offset.has_value())) {
576
228k
    return SourceLocation{line_and_offset->first,
577
228k
                          position - line_and_offset->second};
578
228k
  }
579
5.67k
  return std::nullopt;
580
234k
}
581
582
absl::optional<SourcePosition> Source::GetPosition(
583
2.05M
    const SourceLocation& location) const {
584
2.05M
  if (ABSL_PREDICT_FALSE(location.line < 1 || location.column < 0)) {
585
0
    return std::nullopt;
586
0
  }
587
2.05M
  if (auto position = FindLinePosition(location.line);
588
2.05M
      ABSL_PREDICT_TRUE(position.has_value())) {
589
2.05M
    return *position + location.column;
590
2.05M
  }
591
0
  return std::nullopt;
592
2.05M
}
593
594
235k
absl::optional<std::string> Source::Snippet(int32_t line) const {
595
235k
  auto content = this->content();
596
235k
  auto start = FindLinePosition(line);
597
235k
  if (ABSL_PREDICT_FALSE(!start.has_value() || content.empty())) {
598
6.61k
    return std::nullopt;
599
6.61k
  }
600
228k
  auto end = FindLinePosition(line + 1);
601
228k
  if (end.has_value()) {
602
40.4k
    return content.ToString(*start, *end - 1);
603
40.4k
  }
604
188k
  return content.ToString(*start);
605
228k
}
606
607
235k
std::string Source::DisplayErrorLocation(SourceLocation location) const {
608
235k
  constexpr char32_t kDot = '.';
609
235k
  constexpr char32_t kHat = '^';
610
611
235k
  constexpr char32_t kWideDot = 0xff0e;
612
235k
  constexpr char32_t kWideHat = 0xff3e;
613
235k
  absl::optional<std::string> snippet = Snippet(location.line);
614
235k
  if (!snippet || snippet->empty()) {
615
7.41k
    return "";
616
7.41k
  }
617
618
228k
  *snippet = absl::StrReplaceAll(*snippet, {{"\t", " "}});
619
228k
  absl::string_view snippet_view(*snippet);
620
228k
  std::string result;
621
228k
  absl::StrAppend(&result, "\n | ", *snippet);
622
228k
  absl::StrAppend(&result, "\n | ");
623
624
228k
  std::string index_line;
625
358M
  for (int32_t i = 0; i < location.column && !snippet_view.empty(); ++i) {
626
357M
    size_t count;
627
357M
    std::tie(std::ignore, count) = internal::Utf8Decode(snippet_view);
628
357M
    snippet_view.remove_prefix(count);
629
357M
    if (count > 1) {
630
13.1M
      internal::Utf8Encode(index_line, kWideDot);
631
344M
    } else {
632
344M
      internal::Utf8Encode(index_line, kDot);
633
344M
    }
634
357M
  }
635
228k
  size_t count = 0;
636
228k
  if (!snippet_view.empty()) {
637
217k
    std::tie(std::ignore, count) = internal::Utf8Decode(snippet_view);
638
217k
  }
639
228k
  if (count > 1) {
640
7.68k
    internal::Utf8Encode(index_line, kWideHat);
641
220k
  } else {
642
220k
    internal::Utf8Encode(index_line, kHat);
643
220k
  }
644
228k
  absl::StrAppend(&result, index_line);
645
228k
  return result;
646
235k
}
647
648
2.51M
absl::optional<SourcePosition> Source::FindLinePosition(int32_t line) const {
649
2.51M
  if (ABSL_PREDICT_FALSE(line < 1)) {
650
6.61k
    return std::nullopt;
651
6.61k
  }
652
2.51M
  if (line == 1) {
653
1.96M
    return SourcePosition{0};
654
1.96M
  }
655
548k
  const auto line_offsets = this->line_offsets();
656
548k
  if (ABSL_PREDICT_TRUE(line <= static_cast<int32_t>(line_offsets.size()))) {
657
360k
    return line_offsets[static_cast<size_t>(line - 2)];
658
360k
  }
659
188k
  return std::nullopt;
660
548k
}
661
662
absl::optional<std::pair<int32_t, SourcePosition>> Source::FindLine(
663
234k
    SourcePosition position) const {
664
234k
  if (ABSL_PREDICT_FALSE(position < 0)) {
665
5.67k
    return std::nullopt;
666
5.67k
  }
667
228k
  int32_t line = 1;
668
228k
  const auto line_offsets = this->line_offsets();
669
199M
  for (const auto& line_offset : line_offsets) {
670
199M
    if (line_offset > position) {
671
228k
      break;
672
228k
    }
673
199M
    ++line;
674
199M
  }
675
228k
  if (line == 1) {
676
184k
    return std::make_pair(line, SourcePosition{0});
677
184k
  }
678
43.8k
  return std::make_pair(line, line_offsets[static_cast<size_t>(line) - 2]);
679
228k
}
680
681
SourceSubrange::SourceSubrange(const Source& source, SourceRange range)
682
0
    : source_(source), range_(range) {
683
0
  SourcePosition size = source_.content().size();
684
0
  ABSL_DCHECK(range_.begin >= 0);
685
0
  ABSL_DCHECK(range_.begin <= size);
686
0
  ABSL_DCHECK(range_.end >= range_.begin);
687
0
  ABSL_DCHECK(range_.end <= size);
688
0
  if (range_.begin < 0) {
689
0
    range_.begin = 0;
690
0
  }
691
0
  if (range_.begin > size) {
692
0
    range_.begin = size;
693
0
  }
694
0
  if (range_.end < range_.begin) {
695
0
    range_.end = range_.begin;
696
0
  }
697
0
  if (range_.end > size) {
698
0
    range_.end = size;
699
0
  }
700
0
  for (const auto& line_offset : source_.line_offsets()) {
701
0
    if (line_offset > range_.begin && line_offset <= range_.end) {
702
0
      line_offsets_.push_back(line_offset - range_.begin);
703
0
    }
704
0
  }
705
0
  line_offsets_.push_back(range_.end - range_.begin + 1);
706
0
}
707
708
0
SourceContentView SourceSubrange::content() const {
709
0
  auto parent_content = source_.content();
710
0
  if (parent_content.empty() || range_.begin >= range_.end) {
711
0
    return EmptyContentView();
712
0
  }
713
0
  return absl::visit(
714
0
      [this](auto view) {
715
0
        return SourceContentView(
716
0
            view.subspan(static_cast<size_t>(range_.begin),
717
0
                         static_cast<size_t>(range_.end - range_.begin)));
718
0
      },
Unexecuted instantiation: source.cc:auto cel::SourceSubrange::content() const::$_0::operator()<absl::lts_20260526::Span<char const> >(absl::lts_20260526::Span<char const>) const
Unexecuted instantiation: source.cc:auto cel::SourceSubrange::content() const::$_0::operator()<absl::lts_20260526::Span<unsigned char const> >(absl::lts_20260526::Span<unsigned char const>) const
Unexecuted instantiation: source.cc:auto cel::SourceSubrange::content() const::$_0::operator()<absl::lts_20260526::Span<char16_t const> >(absl::lts_20260526::Span<char16_t const>) const
Unexecuted instantiation: source.cc:auto cel::SourceSubrange::content() const::$_0::operator()<absl::lts_20260526::Span<char32_t const> >(absl::lts_20260526::Span<char32_t const>) const
719
0
      parent_content.view_);
720
0
}
721
722
0
absl::Span<const SourcePosition> SourceSubrange::line_offsets() const {
723
0
  return absl::MakeConstSpan(line_offsets_);
724
0
}
725
726
36.7k
static size_t ClampLimit(int value) {
727
36.7k
  if (value < 0) {
728
0
    return std::numeric_limits<size_t>::max();
729
0
  }
730
36.7k
  return static_cast<size_t>(value);
731
36.7k
}
732
733
absl::StatusOr<absl_nonnull SourcePtr> NewSource(absl::string_view content,
734
                                                 std::string description,
735
36.7k
                                                 const SourceOptions& options) {
736
36.7k
  return common_internal::NewSourceImpl(std::move(description), content,
737
36.7k
                                        content.size(),
738
36.7k
                                        ClampLimit(options.max_codepoint_size));
739
36.7k
}
740
741
absl::StatusOr<absl_nonnull SourcePtr> NewSource(const absl::Cord& content,
742
                                                 std::string description,
743
0
                                                 const SourceOptions& options) {
744
0
  return common_internal::NewSourceImpl(std::move(description), content,
745
0
                                        content.size(),
746
0
                                        ClampLimit(options.max_codepoint_size));
747
0
}
748
749
}  // namespace cel