Coverage Report

Created: 2026-11-24 06:40

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
228k
SourcePosition SourceContentView::size() const {
48
228k
  return static_cast<SourcePosition>(absl::visit(
49
228k
      absl::Overload(
50
228k
          [](absl::Span<const char> view) { return view.size(); },
51
228k
          [](absl::Span<const uint8_t> view) { return view.size(); },
52
228k
          [](absl::Span<const char16_t> view) { return view.size(); },
53
228k
          [](absl::Span<const char32_t> view) { return view.size(); }),
54
228k
      view_));
55
228k
}
56
57
229k
bool SourceContentView::empty() const {
58
229k
  return absl::visit(
59
229k
      absl::Overload(
60
229k
          [](absl::Span<const char> view) { return view.empty(); },
61
229k
          [](absl::Span<const uint8_t> view) { return view.empty(); },
62
229k
          [](absl::Span<const char16_t> view) { return view.empty(); },
63
229k
          [](absl::Span<const char32_t> view) { return view.empty(); }),
64
229k
      view_);
65
229k
}
66
67
171M
char32_t SourceContentView::at(SourcePosition position) const {
68
171M
  ABSL_DCHECK_GE(position, 0);
69
171M
  ABSL_DCHECK_LT(position, size());
70
171M
  return absl::visit(
71
171M
      absl::Overload(
72
171M
          [position =
73
171M
               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
171M
          [position =
77
171M
               static_cast<size_t>(position)](absl::Span<const uint8_t> view) {
78
8.93M
            return static_cast<char32_t>(view[position]);
79
8.93M
          },
80
171M
          [position =
81
171M
               static_cast<size_t>(position)](absl::Span<const char16_t> view) {
82
14.4M
            return static_cast<char32_t>(view[position]);
83
14.4M
          },
84
171M
          [position =
85
171M
               static_cast<size_t>(position)](absl::Span<const char32_t> view) {
86
48.6M
            return static_cast<char32_t>(view[position]);
87
48.6M
          }),
88
171M
      view_);
89
171M
}
90
91
std::string SourceContentView::ToString(SourcePosition begin,
92
12.7M
                                        SourcePosition end) const {
93
12.7M
  ABSL_DCHECK_GE(begin, 0);
94
12.7M
  ABSL_DCHECK_LE(end, size());
95
12.7M
  ABSL_DCHECK_LE(begin, end);
96
12.7M
  return absl::visit(
97
12.7M
      absl::Overload(
98
12.7M
          [begin = static_cast<size_t>(begin),
99
12.7M
           end = static_cast<size_t>(end)](absl::Span<const char> view) {
100
9.38M
            view = view.subspan(begin, end - begin);
101
9.38M
            return std::string(view.data(), view.size());
102
9.38M
          },
103
12.7M
          [begin = static_cast<size_t>(begin),
104
12.7M
           end = static_cast<size_t>(end)](absl::Span<const uint8_t> view) {
105
977k
            view = view.subspan(begin, end - begin);
106
977k
            std::string result;
107
977k
            result.reserve(view.size() * 2);
108
146M
            for (const auto& code_point : view) {
109
146M
              internal::Utf8Encode(result, code_point);
110
146M
            }
111
977k
            result.shrink_to_fit();
112
977k
            return result;
113
977k
          },
114
12.7M
          [begin = static_cast<size_t>(begin),
115
12.7M
           end = static_cast<size_t>(end)](absl::Span<const char16_t> view) {
116
1.01M
            view = view.subspan(begin, end - begin);
117
1.01M
            std::string result;
118
1.01M
            result.reserve(view.size() * 3);
119
240M
            for (const auto& code_point : view) {
120
240M
              internal::Utf8Encode(result, code_point);
121
240M
            }
122
1.01M
            result.shrink_to_fit();
123
1.01M
            return result;
124
1.01M
          },
125
12.7M
          [begin = static_cast<size_t>(begin),
126
12.7M
           end = static_cast<size_t>(end)](absl::Span<const char32_t> view) {
127
1.42M
            view = view.subspan(begin, end - begin);
128
1.42M
            std::string result;
129
1.42M
            result.reserve(view.size() * 4);
130
337M
            for (const auto& code_point : view) {
131
337M
              internal::Utf8Encode(result, code_point);
132
337M
            }
133
1.42M
            result.shrink_to_fit();
134
1.42M
            return result;
135
1.42M
          }),
136
12.7M
      view_);
137
12.7M
}
138
139
0
void SourceContentView::AppendToString(std::string& dest) const {
140
0
  absl::visit(absl::Overload(
141
0
                  [&dest](absl::Span<const char> view) {
142
0
                    dest.append(view.data(), view.size());
143
0
                  },
144
0
                  [&dest](absl::Span<const uint8_t> view) {
145
0
                    for (const auto& code_point : view) {
146
0
                      internal::Utf8Encode(dest, code_point);
147
0
                    }
148
0
                  },
149
0
                  [&dest](absl::Span<const char16_t> view) {
150
0
                    for (const auto& code_point : view) {
151
0
                      internal::Utf8Encode(dest, code_point);
152
0
                    }
153
0
                  },
154
0
                  [&dest](absl::Span<const char32_t> view) {
155
0
                    for (const auto& code_point : view) {
156
0
                      internal::Utf8Encode(dest, code_point);
157
0
                    }
158
0
                  }),
159
0
              view_);
160
0
}
161
162
namespace common_internal {
163
164
class SourceImpl : public Source {
165
 public:
166
  struct OffsetEntry {
167
    int32_t byte_offset;
168
    SourcePosition codepoint;
169
  };
170
171
  SourceImpl(std::string description,
172
             absl::InlinedVector<SourcePosition, 1> line_offsets,
173
             std::vector<OffsetEntry> byte_offsets = {})
174
36.9k
      : description_(std::move(description)),
175
36.9k
        line_offsets_(std::move(line_offsets)),
176
36.9k
        byte_offsets_(std::move(byte_offsets)) {}
177
178
296k
  absl::string_view description() const final { return description_; }
179
180
839k
  absl::Span<const SourcePosition> line_offsets() const final {
181
839k
    return absl::MakeConstSpan(line_offsets_);
182
839k
  }
183
184
  absl::optional<SourcePosition> GetPositionFromByteOffset(
185
0
      SourcePosition byte_offset) const final {
186
    // handle out of bounds and all ascii.
187
0
    if (byte_offset < 0) {
188
0
      return absl::nullopt;
189
0
    }
190
0
    if (byte_offsets_.empty()) {
191
0
      if (byte_offset > content().size()) {
192
0
        return absl::nullopt;
193
0
      }
194
0
      return byte_offset;
195
0
    }
196
0
    if (byte_offset > byte_offsets_.back().byte_offset) {
197
0
      return absl::nullopt;
198
0
    }
199
200
    // binary search for the next alignment shift after the given byte offset.
201
0
    auto it = std::upper_bound(
202
0
        byte_offsets_.begin(), byte_offsets_.end(), byte_offset,
203
0
        [](SourcePosition val, const OffsetEntry& entry) {
204
0
          return val < entry.byte_offset;
205
0
        });
206
0
    if (it == byte_offsets_.begin()) {
207
0
      return byte_offset;
208
0
    }
209
0
    const OffsetEntry& prev = *(it - 1);
210
0
    if (it == byte_offsets_.end()) {
211
      // matched the EOF entry.
212
0
      return prev.codepoint;
213
0
    }
214
215
    // the total shift at the next multi-byte character.
216
0
    int32_t next_delta = it->byte_offset - it->codepoint;
217
218
    // The offset is either in the previous multi-byte character or in the
219
    // run of ascii characters after it. if negative, then in, otherwise
220
    // shifted down.
221
0
    return std::max(prev.codepoint, byte_offset - next_delta);
222
0
  }
223
224
 private:
225
  const std::string description_;
226
  const absl::InlinedVector<SourcePosition, 1> line_offsets_;
227
  const std::vector<OffsetEntry> byte_offsets_;
228
};
229
230
namespace {
231
232
class AsciiSource final : public SourceImpl {
233
 public:
234
  AsciiSource(std::string description,
235
              absl::InlinedVector<SourcePosition, 1> line_offsets,
236
              std::vector<char> text)
237
33.7k
      : SourceImpl(std::move(description), std::move(line_offsets)),
238
33.7k
        text_(std::move(text)) {}
239
240
198k
  ContentView content() const override {
241
198k
    return MakeContentView(absl::MakeConstSpan(text_));
242
198k
  }
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
562
      : SourceImpl(std::move(description), std::move(line_offsets),
254
562
                   std::move(byte_offsets)),
255
562
        text_(std::move(text)) {}
256
257
13.5k
  ContentView content() const override {
258
13.5k
    return MakeContentView(absl::MakeConstSpan(text_));
259
13.5k
  }
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.38k
      : SourceImpl(std::move(description), std::move(line_offsets),
272
1.38k
                   std::move(byte_offsets)),
273
1.38k
        text_(std::move(text)) {}
274
275
28.8k
  ContentView content() const override {
276
28.8k
    return MakeContentView(absl::MakeConstSpan(text_));
277
28.8k
  }
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.21k
      : SourceImpl(std::move(description), std::move(line_offsets),
290
1.21k
                   std::move(byte_offsets)),
291
1.21k
        text_(std::move(text)) {}
292
293
32.1k
  ContentView content() const override {
294
32.1k
    return MakeContentView(absl::MakeConstSpan(text_));
295
32.1k
  }
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
37.1k
  static iterator_type Begin(absl::string_view text) { return text; }
309
310
46.9M
  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
838
                       size_t n) {
314
838
    const auto* in = reinterpret_cast<const uint8_t*>(text.data());
315
838
    out.insert(out.end(), in, in + n);
316
838
  }
317
318
33.7k
  static std::vector<char> ToVector(absl::string_view in) {
319
33.7k
    std::vector<char> out;
320
33.7k
    out.reserve(in.size());
321
33.7k
    out.insert(out.end(), in.begin(), in.end());
322
33.7k
    return out;
323
33.7k
  }
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
37.1k
                                        const size_t max_codepoints) {
365
37.1k
  if (ABSL_PREDICT_FALSE(
366
37.1k
          text_size >
367
37.1k
          static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
368
0
    return absl::InvalidArgumentError("expression larger than 2GiB limit");
369
0
  }
370
37.1k
  if ((text_size >> 2) > max_codepoints) {
371
    // If byte size is 4 times the codepoint limit, then definitely exceeded.
372
27
    return absl::InvalidArgumentError(absl::StrCat(
373
27
        "expression is larger than codepoint limit ", max_codepoints));
374
27
  }
375
37.1k
  using Traits = SourceTextTraits<T>;
376
37.1k
  size_t index = 0;
377
37.1k
  typename Traits::iterator_type it = Traits::Begin(text);
378
37.1k
  SourcePosition offset = 0;
379
37.1k
  char32_t code_point;
380
37.1k
  size_t code_units;
381
37.1k
  std::vector<uint8_t> data8;
382
37.1k
  std::vector<char16_t> data16;
383
37.1k
  std::vector<char32_t> data32;
384
37.1k
  absl::InlinedVector<SourcePosition, 1> line_offsets;
385
37.1k
  std::vector<SourceImpl::OffsetEntry> byte_offsets;
386
32.6M
  while (index < text_size) {
387
32.6M
    if (offset >= max_codepoints) {
388
5
      return absl::InvalidArgumentError(absl::StrCat(
389
5
          "expression is larger than codepoint limit ", max_codepoints));
390
5
    }
391
32.6M
    std::tie(code_point, code_units) = cel::internal::Utf8Decode(it);
392
32.6M
    if (ABSL_PREDICT_FALSE(code_point ==
393
32.6M
                               cel::internal::kUnicodeReplacementCharacter &&
394
32.6M
                           code_units == 1)) {
395
      // Thats an invalid UTF-8 encoding.
396
85
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
397
85
    }
398
32.6M
    if (code_point == '\n') {
399
5.16M
      line_offsets.push_back(offset + 1);
400
5.16M
    }
401
32.6M
    if (code_point <= 0x7f) {
402
32.6M
      Traits::Advance(it, code_units);
403
32.6M
      index += code_units;
404
32.6M
      ++offset;
405
32.6M
      continue;
406
32.6M
    }
407
3.31k
    byte_offsets.push_back({static_cast<int32_t>(index), offset});
408
3.31k
    if (code_point <= 0xff) {
409
838
      data8.reserve(text_size);
410
838
      Traits::AppendTo(data8, text, index);
411
838
      data8.push_back(static_cast<uint8_t>(code_point));
412
838
      Traits::Advance(it, code_units);
413
838
      index += code_units;
414
838
      ++offset;
415
838
      goto latin1;
416
838
    }
417
2.47k
    if (code_point <= 0xffff) {
418
1.48k
      data16.reserve(text_size);
419
3.12M
      for (size_t offset = 0; offset < index; offset++) {
420
3.12M
        data16.push_back(static_cast<uint8_t>(text[offset]));
421
3.12M
      }
422
1.48k
      data16.push_back(static_cast<char16_t>(code_point));
423
1.48k
      Traits::Advance(it, code_units);
424
1.48k
      index += code_units;
425
1.48k
      ++offset;
426
1.48k
      goto basic;
427
1.48k
    }
428
990
    data32.reserve(text_size);
429
2.75M
    for (size_t offset = 0; offset < index; offset++) {
430
2.75M
      data32.push_back(static_cast<char32_t>(text[offset]));
431
2.75M
    }
432
990
    data32.push_back(code_point);
433
990
    Traits::Advance(it, code_units);
434
990
    index += code_units;
435
990
    ++offset;
436
990
    goto supplemental;
437
2.47k
  }
438
33.7k
  line_offsets.push_back(offset + 1);
439
33.7k
  return std::make_unique<AsciiSource>(
440
33.7k
      std::move(description), std::move(line_offsets), Traits::ToVector(text));
441
838
latin1:
442
2.75M
  while (index < text_size) {
443
2.75M
    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.75M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
448
2.75M
    if (ABSL_PREDICT_FALSE(code_point ==
449
2.75M
                               internal::kUnicodeReplacementCharacter &&
450
2.75M
                           code_units == 1)) {
451
      // Thats an invalid UTF-8 encoding.
452
36
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
453
36
    }
454
2.75M
    if (code_point == '\n') {
455
376k
      line_offsets.push_back(offset + 1);
456
376k
    }
457
2.75M
    if (code_units > 1) {
458
3.32k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
459
3.32k
    }
460
2.75M
    if (code_point <= 0xff) {
461
2.75M
      data8.push_back(static_cast<uint8_t>(code_point));
462
2.75M
      Traits::Advance(it, code_units);
463
2.75M
      index += code_units;
464
2.75M
      ++offset;
465
2.75M
      continue;
466
2.75M
    }
467
237
    if (code_point <= 0xffff) {
468
155
      data16.reserve(text_size);
469
803k
      for (const auto& value : data8) {
470
803k
        data16.push_back(value);
471
803k
      }
472
155
      std::vector<uint8_t>().swap(data8);
473
155
      data16.push_back(static_cast<char16_t>(code_point));
474
155
      Traits::Advance(it, code_units);
475
155
      index += code_units;
476
155
      ++offset;
477
155
      goto basic;
478
155
    }
479
82
    data32.reserve(text_size);
480
498k
    for (const auto& value : data8) {
481
498k
      data32.push_back(value);
482
498k
    }
483
82
    std::vector<uint8_t>().swap(data8);
484
82
    data32.push_back(code_point);
485
82
    Traits::Advance(it, code_units);
486
82
    index += code_units;
487
82
    ++offset;
488
82
    goto supplemental;
489
237
  }
490
562
  line_offsets.push_back(offset + 1);
491
562
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
492
562
  return std::make_unique<Latin1Source>(
493
562
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
494
562
      std::move(data8));
495
1.63k
basic:
496
5.68M
  while (index < text_size) {
497
5.68M
    if (offset >= max_codepoints) {
498
4
      return absl::InvalidArgumentError(absl::StrCat(
499
4
          "expression is larger than codepoint limit ", max_codepoints));
500
4
    }
501
5.68M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
502
5.68M
    if (ABSL_PREDICT_FALSE(code_point ==
503
5.68M
                               internal::kUnicodeReplacementCharacter &&
504
5.68M
                           code_units == 1)) {
505
      // Thats an invalid UTF-8 encoding.
506
50
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
507
50
    }
508
5.68M
    if (code_point == '\n') {
509
681k
      line_offsets.push_back(offset + 1);
510
681k
    }
511
5.68M
    if (code_units > 1) {
512
1.41M
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
513
1.41M
    }
514
5.68M
    if (code_point <= 0xffff) {
515
5.68M
      data16.push_back(static_cast<char16_t>(code_point));
516
5.68M
      Traits::Advance(it, code_units);
517
5.68M
      index += code_units;
518
5.68M
      ++offset;
519
5.68M
      continue;
520
5.68M
    }
521
199
    data32.reserve(text_size);
522
1.99M
    for (const auto& value : data16) {
523
1.99M
      data32.push_back(static_cast<char32_t>(value));
524
1.99M
    }
525
199
    std::vector<char16_t>().swap(data16);
526
199
    data32.push_back(code_point);
527
199
    Traits::Advance(it, code_units);
528
199
    index += code_units;
529
199
    ++offset;
530
199
    goto supplemental;
531
5.68M
  }
532
1.38k
  line_offsets.push_back(offset + 1);
533
1.38k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
534
1.38k
  return std::make_unique<BasicPlaneSource>(
535
1.38k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
536
1.38k
      std::move(data16));
537
1.27k
supplemental:
538
5.93M
  while (index < text_size) {
539
5.93M
    if (offset >= max_codepoints) {
540
6
      return absl::InvalidArgumentError(absl::StrCat(
541
6
          "expression is larger than codepoint limit ", max_codepoints));
542
6
    }
543
5.93M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
544
5.93M
    if (ABSL_PREDICT_FALSE(code_point ==
545
5.93M
                               internal::kUnicodeReplacementCharacter &&
546
5.93M
                           code_units == 1)) {
547
      // Thats an invalid UTF-8 encoding.
548
51
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
549
51
    }
550
5.93M
    if (code_point == '\n') {
551
1.91M
      line_offsets.push_back(offset + 1);
552
1.91M
    }
553
5.93M
    if (code_units > 1) {
554
466k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
555
466k
    }
556
5.93M
    data32.push_back(code_point);
557
5.93M
    Traits::Advance(it, code_units);
558
5.93M
    index += code_units;
559
5.93M
    ++offset;
560
5.93M
  }
561
1.21k
  line_offsets.push_back(offset + 1);
562
1.21k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
563
1.21k
  return std::make_unique<SupplementalPlaneSource>(
564
1.21k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
565
1.21k
      std::move(data32));
566
1.27k
}
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
37.1k
                                        const size_t max_codepoints) {
365
37.1k
  if (ABSL_PREDICT_FALSE(
366
37.1k
          text_size >
367
37.1k
          static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
368
0
    return absl::InvalidArgumentError("expression larger than 2GiB limit");
369
0
  }
370
37.1k
  if ((text_size >> 2) > max_codepoints) {
371
    // If byte size is 4 times the codepoint limit, then definitely exceeded.
372
27
    return absl::InvalidArgumentError(absl::StrCat(
373
27
        "expression is larger than codepoint limit ", max_codepoints));
374
27
  }
375
37.1k
  using Traits = SourceTextTraits<T>;
376
37.1k
  size_t index = 0;
377
37.1k
  typename Traits::iterator_type it = Traits::Begin(text);
378
37.1k
  SourcePosition offset = 0;
379
37.1k
  char32_t code_point;
380
37.1k
  size_t code_units;
381
37.1k
  std::vector<uint8_t> data8;
382
37.1k
  std::vector<char16_t> data16;
383
37.1k
  std::vector<char32_t> data32;
384
37.1k
  absl::InlinedVector<SourcePosition, 1> line_offsets;
385
37.1k
  std::vector<SourceImpl::OffsetEntry> byte_offsets;
386
32.6M
  while (index < text_size) {
387
32.6M
    if (offset >= max_codepoints) {
388
5
      return absl::InvalidArgumentError(absl::StrCat(
389
5
          "expression is larger than codepoint limit ", max_codepoints));
390
5
    }
391
32.6M
    std::tie(code_point, code_units) = cel::internal::Utf8Decode(it);
392
32.6M
    if (ABSL_PREDICT_FALSE(code_point ==
393
32.6M
                               cel::internal::kUnicodeReplacementCharacter &&
394
32.6M
                           code_units == 1)) {
395
      // Thats an invalid UTF-8 encoding.
396
85
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
397
85
    }
398
32.6M
    if (code_point == '\n') {
399
5.16M
      line_offsets.push_back(offset + 1);
400
5.16M
    }
401
32.6M
    if (code_point <= 0x7f) {
402
32.6M
      Traits::Advance(it, code_units);
403
32.6M
      index += code_units;
404
32.6M
      ++offset;
405
32.6M
      continue;
406
32.6M
    }
407
3.31k
    byte_offsets.push_back({static_cast<int32_t>(index), offset});
408
3.31k
    if (code_point <= 0xff) {
409
838
      data8.reserve(text_size);
410
838
      Traits::AppendTo(data8, text, index);
411
838
      data8.push_back(static_cast<uint8_t>(code_point));
412
838
      Traits::Advance(it, code_units);
413
838
      index += code_units;
414
838
      ++offset;
415
838
      goto latin1;
416
838
    }
417
2.47k
    if (code_point <= 0xffff) {
418
1.48k
      data16.reserve(text_size);
419
3.12M
      for (size_t offset = 0; offset < index; offset++) {
420
3.12M
        data16.push_back(static_cast<uint8_t>(text[offset]));
421
3.12M
      }
422
1.48k
      data16.push_back(static_cast<char16_t>(code_point));
423
1.48k
      Traits::Advance(it, code_units);
424
1.48k
      index += code_units;
425
1.48k
      ++offset;
426
1.48k
      goto basic;
427
1.48k
    }
428
990
    data32.reserve(text_size);
429
2.75M
    for (size_t offset = 0; offset < index; offset++) {
430
2.75M
      data32.push_back(static_cast<char32_t>(text[offset]));
431
2.75M
    }
432
990
    data32.push_back(code_point);
433
990
    Traits::Advance(it, code_units);
434
990
    index += code_units;
435
990
    ++offset;
436
990
    goto supplemental;
437
2.47k
  }
438
33.7k
  line_offsets.push_back(offset + 1);
439
33.7k
  return std::make_unique<AsciiSource>(
440
33.7k
      std::move(description), std::move(line_offsets), Traits::ToVector(text));
441
838
latin1:
442
2.75M
  while (index < text_size) {
443
2.75M
    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.75M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
448
2.75M
    if (ABSL_PREDICT_FALSE(code_point ==
449
2.75M
                               internal::kUnicodeReplacementCharacter &&
450
2.75M
                           code_units == 1)) {
451
      // Thats an invalid UTF-8 encoding.
452
36
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
453
36
    }
454
2.75M
    if (code_point == '\n') {
455
376k
      line_offsets.push_back(offset + 1);
456
376k
    }
457
2.75M
    if (code_units > 1) {
458
3.32k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
459
3.32k
    }
460
2.75M
    if (code_point <= 0xff) {
461
2.75M
      data8.push_back(static_cast<uint8_t>(code_point));
462
2.75M
      Traits::Advance(it, code_units);
463
2.75M
      index += code_units;
464
2.75M
      ++offset;
465
2.75M
      continue;
466
2.75M
    }
467
237
    if (code_point <= 0xffff) {
468
155
      data16.reserve(text_size);
469
803k
      for (const auto& value : data8) {
470
803k
        data16.push_back(value);
471
803k
      }
472
155
      std::vector<uint8_t>().swap(data8);
473
155
      data16.push_back(static_cast<char16_t>(code_point));
474
155
      Traits::Advance(it, code_units);
475
155
      index += code_units;
476
155
      ++offset;
477
155
      goto basic;
478
155
    }
479
82
    data32.reserve(text_size);
480
498k
    for (const auto& value : data8) {
481
498k
      data32.push_back(value);
482
498k
    }
483
82
    std::vector<uint8_t>().swap(data8);
484
82
    data32.push_back(code_point);
485
82
    Traits::Advance(it, code_units);
486
82
    index += code_units;
487
82
    ++offset;
488
82
    goto supplemental;
489
237
  }
490
562
  line_offsets.push_back(offset + 1);
491
562
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
492
562
  return std::make_unique<Latin1Source>(
493
562
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
494
562
      std::move(data8));
495
1.63k
basic:
496
5.68M
  while (index < text_size) {
497
5.68M
    if (offset >= max_codepoints) {
498
4
      return absl::InvalidArgumentError(absl::StrCat(
499
4
          "expression is larger than codepoint limit ", max_codepoints));
500
4
    }
501
5.68M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
502
5.68M
    if (ABSL_PREDICT_FALSE(code_point ==
503
5.68M
                               internal::kUnicodeReplacementCharacter &&
504
5.68M
                           code_units == 1)) {
505
      // Thats an invalid UTF-8 encoding.
506
50
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
507
50
    }
508
5.68M
    if (code_point == '\n') {
509
681k
      line_offsets.push_back(offset + 1);
510
681k
    }
511
5.68M
    if (code_units > 1) {
512
1.41M
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
513
1.41M
    }
514
5.68M
    if (code_point <= 0xffff) {
515
5.68M
      data16.push_back(static_cast<char16_t>(code_point));
516
5.68M
      Traits::Advance(it, code_units);
517
5.68M
      index += code_units;
518
5.68M
      ++offset;
519
5.68M
      continue;
520
5.68M
    }
521
199
    data32.reserve(text_size);
522
1.99M
    for (const auto& value : data16) {
523
1.99M
      data32.push_back(static_cast<char32_t>(value));
524
1.99M
    }
525
199
    std::vector<char16_t>().swap(data16);
526
199
    data32.push_back(code_point);
527
199
    Traits::Advance(it, code_units);
528
199
    index += code_units;
529
199
    ++offset;
530
199
    goto supplemental;
531
5.68M
  }
532
1.38k
  line_offsets.push_back(offset + 1);
533
1.38k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
534
1.38k
  return std::make_unique<BasicPlaneSource>(
535
1.38k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
536
1.38k
      std::move(data16));
537
1.27k
supplemental:
538
5.93M
  while (index < text_size) {
539
5.93M
    if (offset >= max_codepoints) {
540
6
      return absl::InvalidArgumentError(absl::StrCat(
541
6
          "expression is larger than codepoint limit ", max_codepoints));
542
6
    }
543
5.93M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
544
5.93M
    if (ABSL_PREDICT_FALSE(code_point ==
545
5.93M
                               internal::kUnicodeReplacementCharacter &&
546
5.93M
                           code_units == 1)) {
547
      // Thats an invalid UTF-8 encoding.
548
51
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
549
51
    }
550
5.93M
    if (code_point == '\n') {
551
1.91M
      line_offsets.push_back(offset + 1);
552
1.91M
    }
553
5.93M
    if (code_units > 1) {
554
466k
      byte_offsets.push_back({static_cast<int32_t>(index), offset});
555
466k
    }
556
5.93M
    data32.push_back(code_point);
557
5.93M
    Traits::Advance(it, code_units);
558
5.93M
    index += code_units;
559
5.93M
    ++offset;
560
5.93M
  }
561
1.21k
  line_offsets.push_back(offset + 1);
562
1.21k
  byte_offsets.push_back({static_cast<int32_t>(text_size), offset});
563
1.21k
  return std::make_unique<SupplementalPlaneSource>(
564
1.21k
      std::move(description), std::move(line_offsets), std::move(byte_offsets),
565
1.21k
      std::move(data32));
566
1.27k
}
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
235k
    SourcePosition position) const {
574
235k
  if (auto line_and_offset = FindLine(position);
575
235k
      ABSL_PREDICT_TRUE(line_and_offset.has_value())) {
576
229k
    return SourceLocation{line_and_offset->first,
577
229k
                          position - line_and_offset->second};
578
229k
  }
579
6.31k
  return std::nullopt;
580
235k
}
581
582
absl::optional<SourcePosition> Source::GetPosition(
583
2.38M
    const SourceLocation& location) const {
584
2.38M
  if (ABSL_PREDICT_FALSE(location.line < 1 || location.column < 0)) {
585
0
    return std::nullopt;
586
0
  }
587
2.38M
  if (auto position = FindLinePosition(location.line);
588
2.38M
      ABSL_PREDICT_TRUE(position.has_value())) {
589
2.38M
    return *position + location.column;
590
2.38M
  }
591
0
  return std::nullopt;
592
2.38M
}
593
594
236k
absl::optional<std::string> Source::Snippet(int32_t line) const {
595
236k
  auto content = this->content();
596
236k
  auto start = FindLinePosition(line);
597
236k
  if (ABSL_PREDICT_FALSE(!start.has_value() || content.empty())) {
598
7.28k
    return std::nullopt;
599
7.28k
  }
600
229k
  auto end = FindLinePosition(line + 1);
601
229k
  if (end.has_value()) {
602
37.5k
    return content.ToString(*start, *end - 1);
603
37.5k
  }
604
191k
  return content.ToString(*start);
605
229k
}
606
607
236k
std::string Source::DisplayErrorLocation(SourceLocation location) const {
608
236k
  constexpr char32_t kDot = '.';
609
236k
  constexpr char32_t kHat = '^';
610
611
236k
  constexpr char32_t kWideDot = 0xff0e;
612
236k
  constexpr char32_t kWideHat = 0xff3e;
613
236k
  absl::optional<std::string> snippet = Snippet(location.line);
614
236k
  if (!snippet || snippet->empty()) {
615
8.09k
    return "";
616
8.09k
  }
617
618
228k
  *snippet = absl::StrReplaceAll(*snippet, {{"\t", " "}});
619
228k
  absl::string_view snippet_view(*snippet);
620
228k
  std::string result;
621
228k
  absl::StrAppend(&result, "\n | ", *snippet);
622
228k
  absl::StrAppend(&result, "\n | ");
623
624
228k
  std::string index_line;
625
353M
  for (int32_t i = 0; i < location.column && !snippet_view.empty(); ++i) {
626
353M
    size_t count;
627
353M
    std::tie(std::ignore, count) = internal::Utf8Decode(snippet_view);
628
353M
    snippet_view.remove_prefix(count);
629
353M
    if (count > 1) {
630
13.6M
      internal::Utf8Encode(index_line, kWideDot);
631
340M
    } else {
632
340M
      internal::Utf8Encode(index_line, kDot);
633
340M
    }
634
353M
  }
635
228k
  size_t count = 0;
636
228k
  if (!snippet_view.empty()) {
637
218k
    std::tie(std::ignore, count) = internal::Utf8Decode(snippet_view);
638
218k
  }
639
228k
  if (count > 1) {
640
6.88k
    internal::Utf8Encode(index_line, kWideHat);
641
221k
  } else {
642
221k
    internal::Utf8Encode(index_line, kHat);
643
221k
  }
644
228k
  absl::StrAppend(&result, index_line);
645
228k
  return result;
646
236k
}
647
648
2.85M
absl::optional<SourcePosition> Source::FindLinePosition(int32_t line) const {
649
2.85M
  if (ABSL_PREDICT_FALSE(line < 1)) {
650
7.28k
    return std::nullopt;
651
7.28k
  }
652
2.84M
  if (line == 1) {
653
2.28M
    return SourcePosition{0};
654
2.28M
  }
655
564k
  const auto line_offsets = this->line_offsets();
656
564k
  if (ABSL_PREDICT_TRUE(line <= static_cast<int32_t>(line_offsets.size()))) {
657
372k
    return line_offsets[static_cast<size_t>(line - 2)];
658
372k
  }
659
191k
  return std::nullopt;
660
564k
}
661
662
absl::optional<std::pair<int32_t, SourcePosition>> Source::FindLine(
663
235k
    SourcePosition position) const {
664
235k
  if (ABSL_PREDICT_FALSE(position < 0)) {
665
6.31k
    return std::nullopt;
666
6.31k
  }
667
229k
  int32_t line = 1;
668
229k
  const auto line_offsets = this->line_offsets();
669
181M
  for (const auto& line_offset : line_offsets) {
670
181M
    if (line_offset > position) {
671
229k
      break;
672
229k
    }
673
181M
    ++line;
674
181M
  }
675
229k
  if (line == 1) {
676
187k
    return std::make_pair(line, SourcePosition{0});
677
187k
  }
678
41.3k
  return std::make_pair(line, line_offsets[static_cast<size_t>(line) - 2]);
679
229k
}
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
37.1k
static size_t ClampLimit(int value) {
727
37.1k
  if (value < 0) {
728
0
    return std::numeric_limits<size_t>::max();
729
0
  }
730
37.1k
  return static_cast<size_t>(value);
731
37.1k
}
732
733
absl::StatusOr<absl_nonnull SourcePtr> NewSource(absl::string_view content,
734
                                                 std::string description,
735
37.1k
                                                 const SourceOptions& options) {
736
37.1k
  return common_internal::NewSourceImpl(std::move(description), content,
737
37.1k
                                        content.size(),
738
37.1k
                                        ClampLimit(options.max_codepoint_size));
739
37.1k
}
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