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