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