Coverage Report

Created: 2026-11-21 06:12

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
211k
SourcePosition SourceContentView::size() const {
48
211k
  return static_cast<SourcePosition>(absl::visit(
49
211k
      absl::Overload(
50
211k
          [](absl::Span<const char> view) { return view.size(); },
51
211k
          [](absl::Span<const uint8_t> view) { return view.size(); },
52
211k
          [](absl::Span<const char16_t> view) { return view.size(); },
53
211k
          [](absl::Span<const char32_t> view) { return view.size(); }),
54
211k
      view_));
55
211k
}
56
57
215k
bool SourceContentView::empty() const {
58
215k
  return absl::visit(
59
215k
      absl::Overload(
60
215k
          [](absl::Span<const char> view) { return view.empty(); },
61
215k
          [](absl::Span<const uint8_t> view) { return view.empty(); },
62
215k
          [](absl::Span<const char16_t> view) { return view.empty(); },
63
215k
          [](absl::Span<const char32_t> view) { return view.empty(); }),
64
215k
      view_);
65
215k
}
66
67
168M
char32_t SourceContentView::at(SourcePosition position) const {
68
168M
  ABSL_DCHECK_GE(position, 0);
69
168M
  ABSL_DCHECK_LT(position, size());
70
168M
  return absl::visit(
71
168M
      absl::Overload(
72
168M
          [position =
73
168M
               static_cast<size_t>(position)](absl::Span<const char> view) {
74
99.3M
            return static_cast<char32_t>(static_cast<uint8_t>(view[position]));
75
99.3M
          },
76
168M
          [position =
77
168M
               static_cast<size_t>(position)](absl::Span<const uint8_t> view) {
78
9.15M
            return static_cast<char32_t>(view[position]);
79
9.15M
          },
80
168M
          [position =
81
168M
               static_cast<size_t>(position)](absl::Span<const char16_t> view) {
82
14.9M
            return static_cast<char32_t>(view[position]);
83
14.9M
          },
84
168M
          [position =
85
168M
               static_cast<size_t>(position)](absl::Span<const char32_t> view) {
86
45.1M
            return static_cast<char32_t>(view[position]);
87
45.1M
          }),
88
168M
      view_);
89
168M
}
90
91
std::string SourceContentView::ToString(SourcePosition begin,
92
13.2M
                                        SourcePosition end) const {
93
13.2M
  ABSL_DCHECK_GE(begin, 0);
94
13.2M
  ABSL_DCHECK_LE(end, size());
95
13.2M
  ABSL_DCHECK_LE(begin, end);
96
13.2M
  return absl::visit(
97
13.2M
      absl::Overload(
98
13.2M
          [begin = static_cast<size_t>(begin),
99
13.2M
           end = static_cast<size_t>(end)](absl::Span<const char> view) {
100
9.73M
            view = view.subspan(begin, end - begin);
101
9.73M
            return std::string(view.data(), view.size());
102
9.73M
          },
103
13.2M
          [begin = static_cast<size_t>(begin),
104
13.2M
           end = static_cast<size_t>(end)](absl::Span<const uint8_t> view) {
105
1.12M
            view = view.subspan(begin, end - begin);
106
1.12M
            std::string result;
107
1.12M
            result.reserve(view.size() * 2);
108
172M
            for (const auto& code_point : view) {
109
172M
              internal::Utf8Encode(result, code_point);
110
172M
            }
111
1.12M
            result.shrink_to_fit();
112
1.12M
            return result;
113
1.12M
          },
114
13.2M
          [begin = static_cast<size_t>(begin),
115
13.2M
           end = static_cast<size_t>(end)](absl::Span<const char16_t> view) {
116
1.09M
            view = view.subspan(begin, end - begin);
117
1.09M
            std::string result;
118
1.09M
            result.reserve(view.size() * 3);
119
242M
            for (const auto& code_point : view) {
120
242M
              internal::Utf8Encode(result, code_point);
121
242M
            }
122
1.09M
            result.shrink_to_fit();
123
1.09M
            return result;
124
1.09M
          },
125
13.2M
          [begin = static_cast<size_t>(begin),
126
13.2M
           end = static_cast<size_t>(end)](absl::Span<const char32_t> view) {
127
1.28M
            view = view.subspan(begin, end - begin);
128
1.28M
            std::string result;
129
1.28M
            result.reserve(view.size() * 4);
130
311M
            for (const auto& code_point : view) {
131
311M
              internal::Utf8Encode(result, code_point);
132
311M
            }
133
1.28M
            result.shrink_to_fit();
134
1.28M
            return result;
135
1.28M
          }),
136
13.2M
      view_);
137
13.2M
}
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
32.7k
      : description_(std::move(description)),
175
32.7k
        line_offsets_(std::move(line_offsets)),
176
32.7k
        byte_offsets_(std::move(byte_offsets)) {}
177
178
274k
  absl::string_view description() const final { return description_; }
179
180
791k
  absl::Span<const SourcePosition> line_offsets() const final {
181
791k
    return absl::MakeConstSpan(line_offsets_);
182
791k
  }
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
29.6k
      : SourceImpl(std::move(description), std::move(line_offsets)),
238
29.6k
        text_(std::move(text)) {}
239
240
181k
  ContentView content() const override {
241
181k
    return MakeContentView(absl::MakeConstSpan(text_));
242
181k
  }
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
519
      : SourceImpl(std::move(description), std::move(line_offsets),
254
519
                   std::move(byte_offsets)),
255
519
        text_(std::move(text)) {}
256
257
12.8k
  ContentView content() const override {
258
12.8k
    return MakeContentView(absl::MakeConstSpan(text_));
259
12.8k
  }
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.29k
      : SourceImpl(std::move(description), std::move(line_offsets),
272
1.29k
                   std::move(byte_offsets)),
273
1.29k
        text_(std::move(text)) {}
274
275
27.5k
  ContentView content() const override {
276
27.5k
    return MakeContentView(absl::MakeConstSpan(text_));
277
27.5k
  }
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.19k
      : SourceImpl(std::move(description), std::move(line_offsets),
290
1.19k
                   std::move(byte_offsets)),
291
1.19k
        text_(std::move(text)) {}
292
293
32.2k
  ContentView content() const override {
294
32.2k
    return MakeContentView(absl::MakeConstSpan(text_));
295
32.2k
  }
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
32.9k
  static iterator_type Begin(absl::string_view text) { return text; }
309
310
45.3M
  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
830
                       size_t n) {
314
830
    const auto* in = reinterpret_cast<const uint8_t*>(text.data());
315
830
    out.insert(out.end(), in, in + n);
316
830
  }
317
318
29.6k
  static std::vector<char> ToVector(absl::string_view in) {
319
29.6k
    std::vector<char> out;
320
29.6k
    out.reserve(in.size());
321
29.6k
    out.insert(out.end(), in.begin(), in.end());
322
29.6k
    return out;
323
29.6k
  }
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
33.0k
                                        const size_t max_codepoints) {
365
33.0k
  if (ABSL_PREDICT_FALSE(
366
33.0k
          text_size >
367
33.0k
          static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
368
0
    return absl::InvalidArgumentError("expression larger than 2GiB limit");
369
0
  }
370
33.0k
  if ((text_size >> 2) > max_codepoints) {
371
    // If byte size is 4 times the codepoint limit, then definitely exceeded.
372
25
    return absl::InvalidArgumentError(absl::StrCat(
373
25
        "expression is larger than codepoint limit ", max_codepoints));
374
25
  }
375
32.9k
  using Traits = SourceTextTraits<T>;
376
32.9k
  size_t index = 0;
377
32.9k
  typename Traits::iterator_type it = Traits::Begin(text);
378
32.9k
  SourcePosition offset = 0;
379
32.9k
  char32_t code_point;
380
32.9k
  size_t code_units;
381
32.9k
  std::vector<uint8_t> data8;
382
32.9k
  std::vector<char16_t> data16;
383
32.9k
  std::vector<char32_t> data32;
384
32.9k
  absl::InlinedVector<SourcePosition, 1> line_offsets;
385
32.9k
  std::vector<SourceImpl::OffsetEntry> byte_offsets;
386
31.6M
  while (index < text_size) {
387
31.6M
    if (offset >= max_codepoints) {
388
6
      return absl::InvalidArgumentError(absl::StrCat(
389
6
          "expression is larger than codepoint limit ", max_codepoints));
390
6
    }
391
31.6M
    std::tie(code_point, code_units) = cel::internal::Utf8Decode(it);
392
31.6M
    if (ABSL_PREDICT_FALSE(code_point ==
393
31.6M
                               cel::internal::kUnicodeReplacementCharacter &&
394
31.6M
                           code_units == 1)) {
395
      // Thats an invalid UTF-8 encoding.
396
72
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
397
72
    }
398
31.6M
    if (code_point == '\n') {
399
4.06M
      line_offsets.push_back(offset + 1);
400
4.06M
    }
401
31.6M
    if (code_point <= 0x7f) {
402
31.6M
      Traits::Advance(it, code_units);
403
31.6M
      index += code_units;
404
31.6M
      ++offset;
405
31.6M
      continue;
406
31.6M
    }
407
3.20k
    byte_offsets.push_back({static_cast<int32_t>(index), offset});
408
3.20k
    if (code_point <= 0xff) {
409
830
      data8.reserve(text_size);
410
830
      Traits::AppendTo(data8, text, index);
411
830
      data8.push_back(static_cast<uint8_t>(code_point));
412
830
      Traits::Advance(it, code_units);
413
830
      index += code_units;
414
830
      ++offset;
415
830
      goto latin1;
416
830
    }
417
2.37k
    if (code_point <= 0xffff) {
418
1.40k
      data16.reserve(text_size);
419
2.93M
      for (size_t offset = 0; offset < index; offset++) {
420
2.93M
        data16.push_back(static_cast<uint8_t>(text[offset]));
421
2.93M
      }
422
1.40k
      data16.push_back(static_cast<char16_t>(code_point));
423
1.40k
      Traits::Advance(it, code_units);
424
1.40k
      index += code_units;
425
1.40k
      ++offset;
426
1.40k
      goto basic;
427
1.40k
    }
428
968
    data32.reserve(text_size);
429
2.46M
    for (size_t offset = 0; offset < index; offset++) {
430
2.46M
      data32.push_back(static_cast<char32_t>(text[offset]));
431
2.46M
    }
432
968
    data32.push_back(code_point);
433
968
    Traits::Advance(it, code_units);
434
968
    index += code_units;
435
968
    ++offset;
436
968
    goto supplemental;
437
2.37k
  }
438
29.6k
  line_offsets.push_back(offset + 1);
439
29.6k
  return std::make_unique<AsciiSource>(
440
29.6k
      std::move(description), std::move(line_offsets), Traits::ToVector(text));
441
830
latin1:
442
3.30M
  while (index < text_size) {
443
3.30M
    if (offset >= max_codepoints) {
444
3
      return absl::InvalidArgumentError(absl::StrCat(
445
3
          "expression is larger than codepoint limit ", max_codepoints));
446
3
    }
447
3.30M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
448
3.30M
    if (ABSL_PREDICT_FALSE(code_point ==
449
3.30M
                               internal::kUnicodeReplacementCharacter &&
450
3.30M
                           code_units == 1)) {
451
      // Thats an invalid UTF-8 encoding.
452
48
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
453
48
    }
454
3.30M
    if (code_point == '\n') {
455
609k
      line_offsets.push_back(offset + 1);
456
609k
    }
457
3.30M
    if (code_units > 1) {
458
2.57k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
459
2.57k
    }
460
3.30M
    if (code_point <= 0xff) {
461
3.30M
      data8.push_back(static_cast<uint8_t>(code_point));
462
3.30M
      Traits::Advance(it, code_units);
463
3.30M
      index += code_units;
464
3.30M
      ++offset;
465
3.30M
      continue;
466
3.30M
    }
467
260
    if (code_point <= 0xffff) {
468
166
      data16.reserve(text_size);
469
724k
      for (const auto& value : data8) {
470
724k
        data16.push_back(value);
471
724k
      }
472
166
      std::vector<uint8_t>().swap(data8);
473
166
      data16.push_back(static_cast<char16_t>(code_point));
474
166
      Traits::Advance(it, code_units);
475
166
      index += code_units;
476
166
      ++offset;
477
166
      goto basic;
478
166
    }
479
94
    data32.reserve(text_size);
480
411k
    for (const auto& value : data8) {
481
411k
      data32.push_back(value);
482
411k
    }
483
94
    std::vector<uint8_t>().swap(data8);
484
94
    data32.push_back(code_point);
485
94
    Traits::Advance(it, code_units);
486
94
    index += code_units;
487
94
    ++offset;
488
94
    goto supplemental;
489
260
  }
490
519
  line_offsets.push_back(offset + 1);
491
519
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
492
519
  return std::make_unique<Latin1Source>(
493
519
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
494
519
      std::move(data8));
495
1.57k
basic:
496
5.75M
  while (index < text_size) {
497
5.75M
    if (offset >= max_codepoints) {
498
2
      return absl::InvalidArgumentError(absl::StrCat(
499
2
          "expression is larger than codepoint limit ", max_codepoints));
500
2
    }
501
5.75M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
502
5.75M
    if (ABSL_PREDICT_FALSE(code_point ==
503
5.75M
                               internal::kUnicodeReplacementCharacter &&
504
5.75M
                           code_units == 1)) {
505
      // Thats an invalid UTF-8 encoding.
506
72
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
507
72
    }
508
5.75M
    if (code_point == '\n') {
509
397k
      line_offsets.push_back(offset + 1);
510
397k
    }
511
5.75M
    if (code_units > 1) {
512
1.22M
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
513
1.22M
    }
514
5.75M
    if (code_point <= 0xffff) {
515
5.75M
      data16.push_back(static_cast<char16_t>(code_point));
516
5.75M
      Traits::Advance(it, code_units);
517
5.75M
      index += code_units;
518
5.75M
      ++offset;
519
5.75M
      continue;
520
5.75M
    }
521
207
    data32.reserve(text_size);
522
1.92M
    for (const auto& value : data16) {
523
1.92M
      data32.push_back(static_cast<char32_t>(value));
524
1.92M
    }
525
207
    std::vector<char16_t>().swap(data16);
526
207
    data32.push_back(code_point);
527
207
    Traits::Advance(it, code_units);
528
207
    index += code_units;
529
207
    ++offset;
530
207
    goto supplemental;
531
5.75M
  }
532
1.29k
  line_offsets.push_back(offset + 1);
533
1.29k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
534
1.29k
  return std::make_unique<BasicPlaneSource>(
535
1.29k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
536
1.29k
      std::move(data16));
537
1.26k
supplemental:
538
4.64M
  while (index < text_size) {
539
4.64M
    if (offset >= max_codepoints) {
540
3
      return absl::InvalidArgumentError(absl::StrCat(
541
3
          "expression is larger than codepoint limit ", max_codepoints));
542
3
    }
543
4.64M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
544
4.64M
    if (ABSL_PREDICT_FALSE(code_point ==
545
4.64M
                               internal::kUnicodeReplacementCharacter &&
546
4.64M
                           code_units == 1)) {
547
      // Thats an invalid UTF-8 encoding.
548
73
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
549
73
    }
550
4.64M
    if (code_point == '\n') {
551
1.58M
      line_offsets.push_back(offset + 1);
552
1.58M
    }
553
4.64M
    if (code_units > 1) {
554
355k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
555
355k
    }
556
4.64M
    data32.push_back(code_point);
557
4.64M
    Traits::Advance(it, code_units);
558
4.64M
    index += code_units;
559
4.64M
    ++offset;
560
4.64M
  }
561
1.19k
  line_offsets.push_back(offset + 1);
562
1.19k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
563
1.19k
  return std::make_unique<SupplementalPlaneSource>(
564
1.19k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
565
1.19k
      std::move(data32));
566
1.26k
}
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
33.0k
                                        const size_t max_codepoints) {
365
33.0k
  if (ABSL_PREDICT_FALSE(
366
33.0k
          text_size >
367
33.0k
          static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
368
0
    return absl::InvalidArgumentError("expression larger than 2GiB limit");
369
0
  }
370
33.0k
  if ((text_size >> 2) > max_codepoints) {
371
    // If byte size is 4 times the codepoint limit, then definitely exceeded.
372
25
    return absl::InvalidArgumentError(absl::StrCat(
373
25
        "expression is larger than codepoint limit ", max_codepoints));
374
25
  }
375
32.9k
  using Traits = SourceTextTraits<T>;
376
32.9k
  size_t index = 0;
377
32.9k
  typename Traits::iterator_type it = Traits::Begin(text);
378
32.9k
  SourcePosition offset = 0;
379
32.9k
  char32_t code_point;
380
32.9k
  size_t code_units;
381
32.9k
  std::vector<uint8_t> data8;
382
32.9k
  std::vector<char16_t> data16;
383
32.9k
  std::vector<char32_t> data32;
384
32.9k
  absl::InlinedVector<SourcePosition, 1> line_offsets;
385
32.9k
  std::vector<SourceImpl::OffsetEntry> byte_offsets;
386
31.6M
  while (index < text_size) {
387
31.6M
    if (offset >= max_codepoints) {
388
6
      return absl::InvalidArgumentError(absl::StrCat(
389
6
          "expression is larger than codepoint limit ", max_codepoints));
390
6
    }
391
31.6M
    std::tie(code_point, code_units) = cel::internal::Utf8Decode(it);
392
31.6M
    if (ABSL_PREDICT_FALSE(code_point ==
393
31.6M
                               cel::internal::kUnicodeReplacementCharacter &&
394
31.6M
                           code_units == 1)) {
395
      // Thats an invalid UTF-8 encoding.
396
72
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
397
72
    }
398
31.6M
    if (code_point == '\n') {
399
4.06M
      line_offsets.push_back(offset + 1);
400
4.06M
    }
401
31.6M
    if (code_point <= 0x7f) {
402
31.6M
      Traits::Advance(it, code_units);
403
31.6M
      index += code_units;
404
31.6M
      ++offset;
405
31.6M
      continue;
406
31.6M
    }
407
3.20k
    byte_offsets.push_back({static_cast<int32_t>(index), offset});
408
3.20k
    if (code_point <= 0xff) {
409
830
      data8.reserve(text_size);
410
830
      Traits::AppendTo(data8, text, index);
411
830
      data8.push_back(static_cast<uint8_t>(code_point));
412
830
      Traits::Advance(it, code_units);
413
830
      index += code_units;
414
830
      ++offset;
415
830
      goto latin1;
416
830
    }
417
2.37k
    if (code_point <= 0xffff) {
418
1.40k
      data16.reserve(text_size);
419
2.93M
      for (size_t offset = 0; offset < index; offset++) {
420
2.93M
        data16.push_back(static_cast<uint8_t>(text[offset]));
421
2.93M
      }
422
1.40k
      data16.push_back(static_cast<char16_t>(code_point));
423
1.40k
      Traits::Advance(it, code_units);
424
1.40k
      index += code_units;
425
1.40k
      ++offset;
426
1.40k
      goto basic;
427
1.40k
    }
428
968
    data32.reserve(text_size);
429
2.46M
    for (size_t offset = 0; offset < index; offset++) {
430
2.46M
      data32.push_back(static_cast<char32_t>(text[offset]));
431
2.46M
    }
432
968
    data32.push_back(code_point);
433
968
    Traits::Advance(it, code_units);
434
968
    index += code_units;
435
968
    ++offset;
436
968
    goto supplemental;
437
2.37k
  }
438
29.6k
  line_offsets.push_back(offset + 1);
439
29.6k
  return std::make_unique<AsciiSource>(
440
29.6k
      std::move(description), std::move(line_offsets), Traits::ToVector(text));
441
830
latin1:
442
3.30M
  while (index < text_size) {
443
3.30M
    if (offset >= max_codepoints) {
444
3
      return absl::InvalidArgumentError(absl::StrCat(
445
3
          "expression is larger than codepoint limit ", max_codepoints));
446
3
    }
447
3.30M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
448
3.30M
    if (ABSL_PREDICT_FALSE(code_point ==
449
3.30M
                               internal::kUnicodeReplacementCharacter &&
450
3.30M
                           code_units == 1)) {
451
      // Thats an invalid UTF-8 encoding.
452
48
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
453
48
    }
454
3.30M
    if (code_point == '\n') {
455
609k
      line_offsets.push_back(offset + 1);
456
609k
    }
457
3.30M
    if (code_units > 1) {
458
2.57k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
459
2.57k
    }
460
3.30M
    if (code_point <= 0xff) {
461
3.30M
      data8.push_back(static_cast<uint8_t>(code_point));
462
3.30M
      Traits::Advance(it, code_units);
463
3.30M
      index += code_units;
464
3.30M
      ++offset;
465
3.30M
      continue;
466
3.30M
    }
467
260
    if (code_point <= 0xffff) {
468
166
      data16.reserve(text_size);
469
724k
      for (const auto& value : data8) {
470
724k
        data16.push_back(value);
471
724k
      }
472
166
      std::vector<uint8_t>().swap(data8);
473
166
      data16.push_back(static_cast<char16_t>(code_point));
474
166
      Traits::Advance(it, code_units);
475
166
      index += code_units;
476
166
      ++offset;
477
166
      goto basic;
478
166
    }
479
94
    data32.reserve(text_size);
480
411k
    for (const auto& value : data8) {
481
411k
      data32.push_back(value);
482
411k
    }
483
94
    std::vector<uint8_t>().swap(data8);
484
94
    data32.push_back(code_point);
485
94
    Traits::Advance(it, code_units);
486
94
    index += code_units;
487
94
    ++offset;
488
94
    goto supplemental;
489
260
  }
490
519
  line_offsets.push_back(offset + 1);
491
519
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
492
519
  return std::make_unique<Latin1Source>(
493
519
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
494
519
      std::move(data8));
495
1.57k
basic:
496
5.75M
  while (index < text_size) {
497
5.75M
    if (offset >= max_codepoints) {
498
2
      return absl::InvalidArgumentError(absl::StrCat(
499
2
          "expression is larger than codepoint limit ", max_codepoints));
500
2
    }
501
5.75M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
502
5.75M
    if (ABSL_PREDICT_FALSE(code_point ==
503
5.75M
                               internal::kUnicodeReplacementCharacter &&
504
5.75M
                           code_units == 1)) {
505
      // Thats an invalid UTF-8 encoding.
506
72
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
507
72
    }
508
5.75M
    if (code_point == '\n') {
509
397k
      line_offsets.push_back(offset + 1);
510
397k
    }
511
5.75M
    if (code_units > 1) {
512
1.22M
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
513
1.22M
    }
514
5.75M
    if (code_point <= 0xffff) {
515
5.75M
      data16.push_back(static_cast<char16_t>(code_point));
516
5.75M
      Traits::Advance(it, code_units);
517
5.75M
      index += code_units;
518
5.75M
      ++offset;
519
5.75M
      continue;
520
5.75M
    }
521
207
    data32.reserve(text_size);
522
1.92M
    for (const auto& value : data16) {
523
1.92M
      data32.push_back(static_cast<char32_t>(value));
524
1.92M
    }
525
207
    std::vector<char16_t>().swap(data16);
526
207
    data32.push_back(code_point);
527
207
    Traits::Advance(it, code_units);
528
207
    index += code_units;
529
207
    ++offset;
530
207
    goto supplemental;
531
5.75M
  }
532
1.29k
  line_offsets.push_back(offset + 1);
533
1.29k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
534
1.29k
  return std::make_unique<BasicPlaneSource>(
535
1.29k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
536
1.29k
      std::move(data16));
537
1.26k
supplemental:
538
4.64M
  while (index < text_size) {
539
4.64M
    if (offset >= max_codepoints) {
540
3
      return absl::InvalidArgumentError(absl::StrCat(
541
3
          "expression is larger than codepoint limit ", max_codepoints));
542
3
    }
543
4.64M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
544
4.64M
    if (ABSL_PREDICT_FALSE(code_point ==
545
4.64M
                               internal::kUnicodeReplacementCharacter &&
546
4.64M
                           code_units == 1)) {
547
      // Thats an invalid UTF-8 encoding.
548
73
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
549
73
    }
550
4.64M
    if (code_point == '\n') {
551
1.58M
      line_offsets.push_back(offset + 1);
552
1.58M
    }
553
4.64M
    if (code_units > 1) {
554
355k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
555
355k
    }
556
4.64M
    data32.push_back(code_point);
557
4.64M
    Traits::Advance(it, code_units);
558
4.64M
    index += code_units;
559
4.64M
    ++offset;
560
4.64M
  }
561
1.19k
  line_offsets.push_back(offset + 1);
562
1.19k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
563
1.19k
  return std::make_unique<SupplementalPlaneSource>(
564
1.19k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
565
1.19k
      std::move(data32));
566
1.26k
}
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
220k
    SourcePosition position) const {
574
220k
  if (auto line_and_offset = FindLine(position);
575
220k
      ABSL_PREDICT_TRUE(line_and_offset.has_value())) {
576
215k
    return SourceLocation{line_and_offset->first,
577
215k
                          position - line_and_offset->second};
578
215k
  }
579
5.22k
  return std::nullopt;
580
220k
}
581
582
absl::optional<SourcePosition> Source::GetPosition(
583
2.48M
    const SourceLocation& location) const {
584
2.48M
  if (ABSL_PREDICT_FALSE(location.line < 1 || location.column < 0)) {
585
0
    return std::nullopt;
586
0
  }
587
2.48M
  if (auto position = FindLinePosition(location.line);
588
2.48M
      ABSL_PREDICT_TRUE(position.has_value())) {
589
2.48M
    return *position + location.column;
590
2.48M
  }
591
0
  return std::nullopt;
592
2.48M
}
593
594
221k
absl::optional<std::string> Source::Snippet(int32_t line) const {
595
221k
  auto content = this->content();
596
221k
  auto start = FindLinePosition(line);
597
221k
  if (ABSL_PREDICT_FALSE(!start.has_value() || content.empty())) {
598
6.12k
    return std::nullopt;
599
6.12k
  }
600
215k
  auto end = FindLinePosition(line + 1);
601
215k
  if (end.has_value()) {
602
37.1k
    return content.ToString(*start, *end - 1);
603
37.1k
  }
604
178k
  return content.ToString(*start);
605
215k
}
606
607
221k
std::string Source::DisplayErrorLocation(SourceLocation location) const {
608
221k
  constexpr char32_t kDot = '.';
609
221k
  constexpr char32_t kHat = '^';
610
611
221k
  constexpr char32_t kWideDot = 0xff0e;
612
221k
  constexpr char32_t kWideHat = 0xff3e;
613
221k
  absl::optional<std::string> snippet = Snippet(location.line);
614
221k
  if (!snippet || snippet->empty()) {
615
6.84k
    return "";
616
6.84k
  }
617
618
214k
  *snippet = absl::StrReplaceAll(*snippet, {{"\t", " "}});
619
214k
  absl::string_view snippet_view(*snippet);
620
214k
  std::string result;
621
214k
  absl::StrAppend(&result, "\n | ", *snippet);
622
214k
  absl::StrAppend(&result, "\n | ");
623
624
214k
  std::string index_line;
625
364M
  for (int32_t i = 0; i < location.column && !snippet_view.empty(); ++i) {
626
364M
    size_t count;
627
364M
    std::tie(std::ignore, count) = internal::Utf8Decode(snippet_view);
628
364M
    snippet_view.remove_prefix(count);
629
364M
    if (count > 1) {
630
18.7M
      internal::Utf8Encode(index_line, kWideDot);
631
345M
    } else {
632
345M
      internal::Utf8Encode(index_line, kDot);
633
345M
    }
634
364M
  }
635
214k
  size_t count = 0;
636
214k
  if (!snippet_view.empty()) {
637
206k
    std::tie(std::ignore, count) = internal::Utf8Decode(snippet_view);
638
206k
  }
639
214k
  if (count > 1) {
640
5.85k
    internal::Utf8Encode(index_line, kWideHat);
641
208k
  } else {
642
208k
    internal::Utf8Encode(index_line, kHat);
643
208k
  }
644
214k
  absl::StrAppend(&result, index_line);
645
214k
  return result;
646
221k
}
647
648
2.92M
absl::optional<SourcePosition> Source::FindLinePosition(int32_t line) const {
649
2.92M
  if (ABSL_PREDICT_FALSE(line < 1)) {
650
6.12k
    return std::nullopt;
651
6.12k
  }
652
2.91M
  if (line == 1) {
653
2.38M
    return SourcePosition{0};
654
2.38M
  }
655
535k
  const auto line_offsets = this->line_offsets();
656
535k
  if (ABSL_PREDICT_TRUE(line <= static_cast<int32_t>(line_offsets.size()))) {
657
356k
    return line_offsets[static_cast<size_t>(line - 2)];
658
356k
  }
659
178k
  return std::nullopt;
660
535k
}
661
662
absl::optional<std::pair<int32_t, SourcePosition>> Source::FindLine(
663
220k
    SourcePosition position) const {
664
220k
  if (ABSL_PREDICT_FALSE(position < 0)) {
665
5.22k
    return std::nullopt;
666
5.22k
  }
667
215k
  int32_t line = 1;
668
215k
  const auto line_offsets = this->line_offsets();
669
157M
  for (const auto& line_offset : line_offsets) {
670
157M
    if (line_offset > position) {
671
215k
      break;
672
215k
    }
673
157M
    ++line;
674
157M
  }
675
215k
  if (line == 1) {
676
175k
    return std::make_pair(line, SourcePosition{0});
677
175k
  }
678
39.8k
  return std::make_pair(line, line_offsets[static_cast<size_t>(line) - 2]);
679
215k
}
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
33.0k
static size_t ClampLimit(int value) {
727
33.0k
  if (value < 0) {
728
0
    return std::numeric_limits<size_t>::max();
729
0
  }
730
33.0k
  return static_cast<size_t>(value);
731
33.0k
}
732
733
absl::StatusOr<absl_nonnull SourcePtr> NewSource(absl::string_view content,
734
                                                 std::string description,
735
33.0k
                                                 const SourceOptions& options) {
736
33.0k
  return common_internal::NewSourceImpl(std::move(description), content,
737
33.0k
                                        content.size(),
738
33.0k
                                        ClampLimit(options.max_codepoint_size));
739
33.0k
}
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