Coverage Report

Created: 2026-03-31 06:24

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 <string>
23
#include <tuple>
24
#include <utility>
25
#include <vector>
26
27
#include "absl/base/nullability.h"
28
#include "absl/base/optimization.h"
29
#include "absl/container/inlined_vector.h"
30
#include "absl/functional/overload.h"
31
#include "absl/log/absl_check.h"
32
#include "absl/status/status.h"
33
#include "absl/status/statusor.h"
34
#include "absl/strings/cord.h"
35
#include "absl/strings/str_cat.h"
36
#include "absl/strings/str_replace.h"
37
#include "absl/strings/string_view.h"
38
#include "absl/types/optional.h"
39
#include "absl/types/span.h"
40
#include "absl/types/variant.h"
41
#include "internal/unicode.h"
42
#include "internal/utf8.h"
43
44
namespace cel {
45
46
97.7k
SourcePosition SourceContentView::size() const {
47
97.7k
  return static_cast<SourcePosition>(absl::visit(
48
97.7k
      absl::Overload(
49
97.7k
          [](absl::Span<const char> view) { return view.size(); },
50
97.7k
          [](absl::Span<const uint8_t> view) { return view.size(); },
51
97.7k
          [](absl::Span<const char16_t> view) { return view.size(); },
52
97.7k
          [](absl::Span<const char32_t> view) { return view.size(); }),
53
97.7k
      view_));
54
97.7k
}
55
56
122k
bool SourceContentView::empty() const {
57
122k
  return absl::visit(
58
122k
      absl::Overload(
59
122k
          [](absl::Span<const char> view) { return view.empty(); },
60
122k
          [](absl::Span<const uint8_t> view) { return view.empty(); },
61
122k
          [](absl::Span<const char16_t> view) { return view.empty(); },
62
122k
          [](absl::Span<const char32_t> view) { return view.empty(); }),
63
122k
      view_);
64
122k
}
65
66
429M
char32_t SourceContentView::at(SourcePosition position) const {
67
429M
  ABSL_DCHECK_GE(position, 0);
68
429M
  ABSL_DCHECK_LT(position, size());
69
429M
  return absl::visit(
70
429M
      absl::Overload(
71
429M
          [position =
72
429M
               static_cast<size_t>(position)](absl::Span<const char> view) {
73
221M
            return static_cast<char32_t>(static_cast<uint8_t>(view[position]));
74
221M
          },
75
429M
          [position =
76
429M
               static_cast<size_t>(position)](absl::Span<const uint8_t> view) {
77
13.6M
            return static_cast<char32_t>(view[position]);
78
13.6M
          },
79
429M
          [position =
80
429M
               static_cast<size_t>(position)](absl::Span<const char16_t> view) {
81
73.3M
            return static_cast<char32_t>(view[position]);
82
73.3M
          },
83
429M
          [position =
84
429M
               static_cast<size_t>(position)](absl::Span<const char32_t> view) {
85
120M
            return static_cast<char32_t>(view[position]);
86
120M
          }),
87
429M
      view_);
88
429M
}
89
90
std::string SourceContentView::ToString(SourcePosition begin,
91
16.1M
                                        SourcePosition end) const {
92
16.1M
  ABSL_DCHECK_GE(begin, 0);
93
16.1M
  ABSL_DCHECK_LE(end, size());
94
16.1M
  ABSL_DCHECK_LE(begin, end);
95
16.1M
  return absl::visit(
96
16.1M
      absl::Overload(
97
16.1M
          [begin = static_cast<size_t>(begin),
98
16.1M
           end = static_cast<size_t>(end)](absl::Span<const char> view) {
99
10.9M
            view = view.subspan(begin, end - begin);
100
10.9M
            return std::string(view.data(), view.size());
101
10.9M
          },
102
16.1M
          [begin = static_cast<size_t>(begin),
103
16.1M
           end = static_cast<size_t>(end)](absl::Span<const uint8_t> view) {
104
305k
            view = view.subspan(begin, end - begin);
105
305k
            std::string result;
106
305k
            result.reserve(view.size() * 2);
107
72.2M
            for (const auto& code_point : view) {
108
72.2M
              internal::Utf8Encode(result, code_point);
109
72.2M
            }
110
305k
            result.shrink_to_fit();
111
305k
            return result;
112
305k
          },
113
16.1M
          [begin = static_cast<size_t>(begin),
114
16.1M
           end = static_cast<size_t>(end)](absl::Span<const char16_t> view) {
115
3.03M
            view = view.subspan(begin, end - begin);
116
3.03M
            std::string result;
117
3.03M
            result.reserve(view.size() * 3);
118
529M
            for (const auto& code_point : view) {
119
529M
              internal::Utf8Encode(result, code_point);
120
529M
            }
121
3.03M
            result.shrink_to_fit();
122
3.03M
            return result;
123
3.03M
          },
124
16.1M
          [begin = static_cast<size_t>(begin),
125
16.1M
           end = static_cast<size_t>(end)](absl::Span<const char32_t> view) {
126
1.82M
            view = view.subspan(begin, end - begin);
127
1.82M
            std::string result;
128
1.82M
            result.reserve(view.size() * 4);
129
467M
            for (const auto& code_point : view) {
130
467M
              internal::Utf8Encode(result, code_point);
131
467M
            }
132
1.82M
            result.shrink_to_fit();
133
1.82M
            return result;
134
1.82M
          }),
135
16.1M
      view_);
136
16.1M
}
137
138
0
void SourceContentView::AppendToString(std::string& dest) const {
139
0
  absl::visit(absl::Overload(
140
0
                  [&dest](absl::Span<const char> view) {
141
0
                    dest.append(view.data(), view.size());
142
0
                  },
143
0
                  [&dest](absl::Span<const uint8_t> view) {
144
0
                    for (const auto& code_point : view) {
145
0
                      internal::Utf8Encode(dest, code_point);
146
0
                    }
147
0
                  },
148
0
                  [&dest](absl::Span<const char16_t> view) {
149
0
                    for (const auto& code_point : view) {
150
0
                      internal::Utf8Encode(dest, code_point);
151
0
                    }
152
0
                  },
153
0
                  [&dest](absl::Span<const char32_t> view) {
154
0
                    for (const auto& code_point : view) {
155
0
                      internal::Utf8Encode(dest, code_point);
156
0
                    }
157
0
                  }),
158
0
              view_);
159
0
}
160
161
namespace common_internal {
162
163
class SourceImpl : public Source {
164
 public:
165
  SourceImpl(std::string description,
166
             absl::InlinedVector<SourcePosition, 1> line_offsets)
167
7.00k
      : description_(std::move(description)),
168
7.00k
        line_offsets_(std::move(line_offsets)) {}
169
170
133k
  absl::string_view description() const final { return description_; }
171
172
1.60M
  absl::Span<const SourcePosition> line_offsets() const final {
173
1.60M
    return absl::MakeConstSpan(line_offsets_);
174
1.60M
  }
175
176
 private:
177
  const std::string description_;
178
  const absl::InlinedVector<SourcePosition, 1> line_offsets_;
179
};
180
181
namespace {
182
183
class AsciiSource final : public SourceImpl {
184
 public:
185
  AsciiSource(std::string description,
186
              absl::InlinedVector<SourcePosition, 1> line_offsets,
187
              std::vector<char> text)
188
5.65k
      : SourceImpl(std::move(description), std::move(line_offsets)),
189
5.65k
        text_(std::move(text)) {}
190
191
85.2k
  ContentView content() const override {
192
85.2k
    return MakeContentView(absl::MakeConstSpan(text_));
193
85.2k
  }
194
195
 private:
196
  const std::vector<char> text_;
197
};
198
199
class Latin1Source final : public SourceImpl {
200
 public:
201
  Latin1Source(std::string description,
202
               absl::InlinedVector<SourcePosition, 1> line_offsets,
203
               std::vector<uint8_t> text)
204
222
      : SourceImpl(std::move(description), std::move(line_offsets)),
205
222
        text_(std::move(text)) {}
206
207
6.48k
  ContentView content() const override {
208
6.48k
    return MakeContentView(absl::MakeConstSpan(text_));
209
6.48k
  }
210
211
 private:
212
  const std::vector<uint8_t> text_;
213
};
214
215
class BasicPlaneSource final : public SourceImpl {
216
 public:
217
  BasicPlaneSource(std::string description,
218
                   absl::InlinedVector<SourcePosition, 1> line_offsets,
219
                   std::vector<char16_t> text)
220
576
      : SourceImpl(std::move(description), std::move(line_offsets)),
221
576
        text_(std::move(text)) {}
222
223
21.1k
  ContentView content() const override {
224
21.1k
    return MakeContentView(absl::MakeConstSpan(text_));
225
21.1k
  }
226
227
 private:
228
  const std::vector<char16_t> text_;
229
};
230
231
class SupplementalPlaneSource final : public SourceImpl {
232
 public:
233
  SupplementalPlaneSource(std::string description,
234
                          absl::InlinedVector<SourcePosition, 1> line_offsets,
235
                          std::vector<char32_t> text)
236
553
      : SourceImpl(std::move(description), std::move(line_offsets)),
237
553
        text_(std::move(text)) {}
238
239
19.0k
  ContentView content() const override {
240
19.0k
    return MakeContentView(absl::MakeConstSpan(text_));
241
19.0k
  }
242
243
 private:
244
  const std::vector<char32_t> text_;
245
};
246
247
template <typename T>
248
struct SourceTextTraits;
249
250
template <>
251
struct SourceTextTraits<absl::string_view> {
252
  using iterator_type = absl::string_view;
253
254
7.07k
  static iterator_type Begin(absl::string_view text) { return text; }
255
256
165M
  static void Advance(iterator_type& it, size_t n) { it.remove_prefix(n); }
257
258
  static void AppendTo(std::vector<uint8_t>& out, absl::string_view text,
259
379
                       size_t n) {
260
379
    const auto* in = reinterpret_cast<const uint8_t*>(text.data());
261
379
    out.insert(out.end(), in, in + n);
262
379
  }
263
264
5.65k
  static std::vector<char> ToVector(absl::string_view in) {
265
5.65k
    std::vector<char> out;
266
5.65k
    out.reserve(in.size());
267
5.65k
    out.insert(out.end(), in.begin(), in.end());
268
5.65k
    return out;
269
5.65k
  }
270
};
271
272
template <>
273
struct SourceTextTraits<absl::Cord> {
274
  using iterator_type = absl::Cord::CharIterator;
275
276
0
  static iterator_type Begin(const absl::Cord& text) {
277
0
    return text.char_begin();
278
0
  }
279
280
0
  static void Advance(iterator_type& it, size_t n) {
281
0
    absl::Cord::Advance(&it, n);
282
0
  }
283
284
  static void AppendTo(std::vector<uint8_t>& out, const absl::Cord& text,
285
0
                       size_t n) {
286
0
    auto it = text.char_begin();
287
0
    while (n > 0) {
288
0
      auto str = absl::Cord::ChunkRemaining(it);
289
0
      size_t to_append = std::min(n, str.size());
290
0
      const auto* in = reinterpret_cast<const uint8_t*>(str.data());
291
0
      out.insert(out.end(), in, in + to_append);
292
0
      n -= to_append;
293
0
      absl::Cord::Advance(&it, to_append);
294
0
    }
295
0
  }
296
297
0
  static std::vector<char> ToVector(const absl::Cord& in) {
298
0
    std::vector<char> out;
299
0
    out.reserve(in.size());
300
0
    for (const auto& chunk : in.Chunks()) {
301
0
      out.insert(out.end(), chunk.begin(), chunk.end());
302
0
    }
303
0
    return out;
304
0
  }
305
};
306
307
template <typename T>
308
absl::StatusOr<SourcePtr> NewSourceImpl(std::string description, const T& text,
309
7.07k
                                        const size_t text_size) {
310
7.07k
  if (ABSL_PREDICT_FALSE(
311
7.07k
          text_size >
312
7.07k
          static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
313
0
    return absl::InvalidArgumentError("expression larger than 2GiB limit");
314
0
  }
315
7.07k
  using Traits = SourceTextTraits<T>;
316
7.07k
  size_t index = 0;
317
7.07k
  typename Traits::iterator_type it = Traits::Begin(text);
318
7.07k
  SourcePosition offset = 0;
319
7.07k
  char32_t code_point;
320
7.07k
  size_t code_units;
321
7.07k
  std::vector<uint8_t> data8;
322
7.07k
  std::vector<char16_t> data16;
323
7.07k
  std::vector<char32_t> data32;
324
7.07k
  absl::InlinedVector<SourcePosition, 1> line_offsets;
325
106M
  while (index < text_size) {
326
106M
    std::tie(code_point, code_units) = cel::internal::Utf8Decode(it);
327
106M
    if (ABSL_PREDICT_FALSE(code_point ==
328
106M
                               cel::internal::kUnicodeReplacementCharacter &&
329
106M
                           code_units == 1)) {
330
      // Thats an invalid UTF-8 encoding.
331
21
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
332
21
    }
333
106M
    if (code_point == '\n') {
334
34.5M
      line_offsets.push_back(offset + 1);
335
34.5M
    }
336
106M
    if (code_point <= 0x7f) {
337
106M
      Traits::Advance(it, code_units);
338
106M
      index += code_units;
339
106M
      ++offset;
340
106M
      continue;
341
106M
    }
342
1.39k
    if (code_point <= 0xff) {
343
379
      data8.reserve(text_size);
344
379
      Traits::AppendTo(data8, text, index);
345
379
      data8.push_back(static_cast<uint8_t>(code_point));
346
379
      Traits::Advance(it, code_units);
347
379
      index += code_units;
348
379
      ++offset;
349
379
      goto latin1;
350
379
    }
351
1.01k
    if (code_point <= 0xffff) {
352
629
      data16.reserve(text_size);
353
15.0M
      for (size_t offset = 0; offset < index; offset++) {
354
15.0M
        data16.push_back(static_cast<uint8_t>(text[offset]));
355
15.0M
      }
356
629
      data16.push_back(static_cast<char16_t>(code_point));
357
629
      Traits::Advance(it, code_units);
358
629
      index += code_units;
359
629
      ++offset;
360
629
      goto basic;
361
629
    }
362
390
    data32.reserve(text_size);
363
5.82M
    for (size_t offset = 0; offset < index; offset++) {
364
5.82M
      data32.push_back(static_cast<char32_t>(text[offset]));
365
5.82M
    }
366
390
    data32.push_back(code_point);
367
390
    Traits::Advance(it, code_units);
368
390
    index += code_units;
369
390
    ++offset;
370
390
    goto supplemental;
371
1.01k
  }
372
5.65k
  line_offsets.push_back(offset + 1);
373
5.65k
  return std::make_unique<AsciiSource>(
374
5.65k
      std::move(description), std::move(line_offsets), Traits::ToVector(text));
375
379
latin1:
376
7.50M
  while (index < text_size) {
377
7.50M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
378
7.50M
    if (ABSL_PREDICT_FALSE(code_point ==
379
7.50M
                               internal::kUnicodeReplacementCharacter &&
380
7.50M
                           code_units == 1)) {
381
      // Thats an invalid UTF-8 encoding.
382
12
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
383
12
    }
384
7.50M
    if (code_point == '\n') {
385
4.09M
      line_offsets.push_back(offset + 1);
386
4.09M
    }
387
7.50M
    if (code_point <= 0xff) {
388
7.50M
      data8.push_back(static_cast<uint8_t>(code_point));
389
7.50M
      Traits::Advance(it, code_units);
390
7.50M
      index += code_units;
391
7.50M
      ++offset;
392
7.50M
      continue;
393
7.50M
    }
394
145
    if (code_point <= 0xffff) {
395
61
      data16.reserve(text_size);
396
2.26M
      for (const auto& value : data8) {
397
2.26M
        data16.push_back(value);
398
2.26M
      }
399
61
      std::vector<uint8_t>().swap(data8);
400
61
      data16.push_back(static_cast<char16_t>(code_point));
401
61
      Traits::Advance(it, code_units);
402
61
      index += code_units;
403
61
      ++offset;
404
61
      goto basic;
405
61
    }
406
84
    data32.reserve(text_size);
407
1.64M
    for (const auto& value : data8) {
408
1.64M
      data32.push_back(value);
409
1.64M
    }
410
84
    std::vector<uint8_t>().swap(data8);
411
84
    data32.push_back(code_point);
412
84
    Traits::Advance(it, code_units);
413
84
    index += code_units;
414
84
    ++offset;
415
84
    goto supplemental;
416
145
  }
417
222
  line_offsets.push_back(offset + 1);
418
222
  return std::make_unique<Latin1Source>(
419
222
      std::move(description), std::move(line_offsets), std::move(data8));
420
690
basic:
421
18.0M
  while (index < text_size) {
422
18.0M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
423
18.0M
    if (ABSL_PREDICT_FALSE(code_point ==
424
18.0M
                               internal::kUnicodeReplacementCharacter &&
425
18.0M
                           code_units == 1)) {
426
      // Thats an invalid UTF-8 encoding.
427
16
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
428
16
    }
429
18.0M
    if (code_point == '\n') {
430
9.15M
      line_offsets.push_back(offset + 1);
431
9.15M
    }
432
18.0M
    if (code_point <= 0xffff) {
433
18.0M
      data16.push_back(static_cast<char16_t>(code_point));
434
18.0M
      Traits::Advance(it, code_units);
435
18.0M
      index += code_units;
436
18.0M
      ++offset;
437
18.0M
      continue;
438
18.0M
    }
439
98
    data32.reserve(text_size);
440
2.14M
    for (const auto& value : data16) {
441
2.14M
      data32.push_back(static_cast<char32_t>(value));
442
2.14M
    }
443
98
    std::vector<char16_t>().swap(data16);
444
98
    data32.push_back(code_point);
445
98
    Traits::Advance(it, code_units);
446
98
    index += code_units;
447
98
    ++offset;
448
98
    goto supplemental;
449
18.0M
  }
450
576
  line_offsets.push_back(offset + 1);
451
576
  return std::make_unique<BasicPlaneSource>(
452
576
      std::move(description), std::move(line_offsets), std::move(data16));
453
572
supplemental:
454
33.6M
  while (index < text_size) {
455
33.6M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
456
33.6M
    if (ABSL_PREDICT_FALSE(code_point ==
457
33.6M
                               internal::kUnicodeReplacementCharacter &&
458
33.6M
                           code_units == 1)) {
459
      // Thats an invalid UTF-8 encoding.
460
19
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
461
19
    }
462
33.6M
    if (code_point == '\n') {
463
12.8M
      line_offsets.push_back(offset + 1);
464
12.8M
    }
465
33.6M
    data32.push_back(code_point);
466
33.6M
    Traits::Advance(it, code_units);
467
33.6M
    index += code_units;
468
33.6M
    ++offset;
469
33.6M
  }
470
553
  line_offsets.push_back(offset + 1);
471
553
  return std::make_unique<SupplementalPlaneSource>(
472
553
      std::move(description), std::move(line_offsets), std::move(data32));
473
572
}
source.cc:absl::lts_20260107::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)
Line
Count
Source
309
7.07k
                                        const size_t text_size) {
310
7.07k
  if (ABSL_PREDICT_FALSE(
311
7.07k
          text_size >
312
7.07k
          static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
313
0
    return absl::InvalidArgumentError("expression larger than 2GiB limit");
314
0
  }
315
7.07k
  using Traits = SourceTextTraits<T>;
316
7.07k
  size_t index = 0;
317
7.07k
  typename Traits::iterator_type it = Traits::Begin(text);
318
7.07k
  SourcePosition offset = 0;
319
7.07k
  char32_t code_point;
320
7.07k
  size_t code_units;
321
7.07k
  std::vector<uint8_t> data8;
322
7.07k
  std::vector<char16_t> data16;
323
7.07k
  std::vector<char32_t> data32;
324
7.07k
  absl::InlinedVector<SourcePosition, 1> line_offsets;
325
106M
  while (index < text_size) {
326
106M
    std::tie(code_point, code_units) = cel::internal::Utf8Decode(it);
327
106M
    if (ABSL_PREDICT_FALSE(code_point ==
328
106M
                               cel::internal::kUnicodeReplacementCharacter &&
329
106M
                           code_units == 1)) {
330
      // Thats an invalid UTF-8 encoding.
331
21
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
332
21
    }
333
106M
    if (code_point == '\n') {
334
34.5M
      line_offsets.push_back(offset + 1);
335
34.5M
    }
336
106M
    if (code_point <= 0x7f) {
337
106M
      Traits::Advance(it, code_units);
338
106M
      index += code_units;
339
106M
      ++offset;
340
106M
      continue;
341
106M
    }
342
1.39k
    if (code_point <= 0xff) {
343
379
      data8.reserve(text_size);
344
379
      Traits::AppendTo(data8, text, index);
345
379
      data8.push_back(static_cast<uint8_t>(code_point));
346
379
      Traits::Advance(it, code_units);
347
379
      index += code_units;
348
379
      ++offset;
349
379
      goto latin1;
350
379
    }
351
1.01k
    if (code_point <= 0xffff) {
352
629
      data16.reserve(text_size);
353
15.0M
      for (size_t offset = 0; offset < index; offset++) {
354
15.0M
        data16.push_back(static_cast<uint8_t>(text[offset]));
355
15.0M
      }
356
629
      data16.push_back(static_cast<char16_t>(code_point));
357
629
      Traits::Advance(it, code_units);
358
629
      index += code_units;
359
629
      ++offset;
360
629
      goto basic;
361
629
    }
362
390
    data32.reserve(text_size);
363
5.82M
    for (size_t offset = 0; offset < index; offset++) {
364
5.82M
      data32.push_back(static_cast<char32_t>(text[offset]));
365
5.82M
    }
366
390
    data32.push_back(code_point);
367
390
    Traits::Advance(it, code_units);
368
390
    index += code_units;
369
390
    ++offset;
370
390
    goto supplemental;
371
1.01k
  }
372
5.65k
  line_offsets.push_back(offset + 1);
373
5.65k
  return std::make_unique<AsciiSource>(
374
5.65k
      std::move(description), std::move(line_offsets), Traits::ToVector(text));
375
379
latin1:
376
7.50M
  while (index < text_size) {
377
7.50M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
378
7.50M
    if (ABSL_PREDICT_FALSE(code_point ==
379
7.50M
                               internal::kUnicodeReplacementCharacter &&
380
7.50M
                           code_units == 1)) {
381
      // Thats an invalid UTF-8 encoding.
382
12
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
383
12
    }
384
7.50M
    if (code_point == '\n') {
385
4.09M
      line_offsets.push_back(offset + 1);
386
4.09M
    }
387
7.50M
    if (code_point <= 0xff) {
388
7.50M
      data8.push_back(static_cast<uint8_t>(code_point));
389
7.50M
      Traits::Advance(it, code_units);
390
7.50M
      index += code_units;
391
7.50M
      ++offset;
392
7.50M
      continue;
393
7.50M
    }
394
145
    if (code_point <= 0xffff) {
395
61
      data16.reserve(text_size);
396
2.26M
      for (const auto& value : data8) {
397
2.26M
        data16.push_back(value);
398
2.26M
      }
399
61
      std::vector<uint8_t>().swap(data8);
400
61
      data16.push_back(static_cast<char16_t>(code_point));
401
61
      Traits::Advance(it, code_units);
402
61
      index += code_units;
403
61
      ++offset;
404
61
      goto basic;
405
61
    }
406
84
    data32.reserve(text_size);
407
1.64M
    for (const auto& value : data8) {
408
1.64M
      data32.push_back(value);
409
1.64M
    }
410
84
    std::vector<uint8_t>().swap(data8);
411
84
    data32.push_back(code_point);
412
84
    Traits::Advance(it, code_units);
413
84
    index += code_units;
414
84
    ++offset;
415
84
    goto supplemental;
416
145
  }
417
222
  line_offsets.push_back(offset + 1);
418
222
  return std::make_unique<Latin1Source>(
419
222
      std::move(description), std::move(line_offsets), std::move(data8));
420
690
basic:
421
18.0M
  while (index < text_size) {
422
18.0M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
423
18.0M
    if (ABSL_PREDICT_FALSE(code_point ==
424
18.0M
                               internal::kUnicodeReplacementCharacter &&
425
18.0M
                           code_units == 1)) {
426
      // Thats an invalid UTF-8 encoding.
427
16
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
428
16
    }
429
18.0M
    if (code_point == '\n') {
430
9.15M
      line_offsets.push_back(offset + 1);
431
9.15M
    }
432
18.0M
    if (code_point <= 0xffff) {
433
18.0M
      data16.push_back(static_cast<char16_t>(code_point));
434
18.0M
      Traits::Advance(it, code_units);
435
18.0M
      index += code_units;
436
18.0M
      ++offset;
437
18.0M
      continue;
438
18.0M
    }
439
98
    data32.reserve(text_size);
440
2.14M
    for (const auto& value : data16) {
441
2.14M
      data32.push_back(static_cast<char32_t>(value));
442
2.14M
    }
443
98
    std::vector<char16_t>().swap(data16);
444
98
    data32.push_back(code_point);
445
98
    Traits::Advance(it, code_units);
446
98
    index += code_units;
447
98
    ++offset;
448
98
    goto supplemental;
449
18.0M
  }
450
576
  line_offsets.push_back(offset + 1);
451
576
  return std::make_unique<BasicPlaneSource>(
452
576
      std::move(description), std::move(line_offsets), std::move(data16));
453
572
supplemental:
454
33.6M
  while (index < text_size) {
455
33.6M
    std::tie(code_point, code_units) = internal::Utf8Decode(it);
456
33.6M
    if (ABSL_PREDICT_FALSE(code_point ==
457
33.6M
                               internal::kUnicodeReplacementCharacter &&
458
33.6M
                           code_units == 1)) {
459
      // Thats an invalid UTF-8 encoding.
460
19
      return absl::InvalidArgumentError("cannot parse malformed UTF-8 input");
461
19
    }
462
33.6M
    if (code_point == '\n') {
463
12.8M
      line_offsets.push_back(offset + 1);
464
12.8M
    }
465
33.6M
    data32.push_back(code_point);
466
33.6M
    Traits::Advance(it, code_units);
467
33.6M
    index += code_units;
468
33.6M
    ++offset;
469
33.6M
  }
470
553
  line_offsets.push_back(offset + 1);
471
553
  return std::make_unique<SupplementalPlaneSource>(
472
553
      std::move(description), std::move(line_offsets), std::move(data32));
473
572
}
Unexecuted instantiation: source.cc:absl::lts_20260107::StatusOr<std::__1::unique_ptr<cel::Source, std::__1::default_delete<cel::Source> > > cel::common_internal::(anonymous namespace)::NewSourceImpl<absl::lts_20260107::Cord>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, absl::lts_20260107::Cord const&, unsigned long)
474
475
}  // namespace
476
477
}  // namespace common_internal
478
479
absl::optional<SourceLocation> Source::GetLocation(
480
124k
    SourcePosition position) const {
481
124k
  if (auto line_and_offset = FindLine(position);
482
124k
      ABSL_PREDICT_TRUE(line_and_offset.has_value())) {
483
122k
    return SourceLocation{line_and_offset->first,
484
122k
                          position - line_and_offset->second};
485
122k
  }
486
2.33k
  return absl::nullopt;
487
124k
}
488
489
absl::optional<SourcePosition> Source::GetPosition(
490
8.27M
    const SourceLocation& location) const {
491
8.27M
  if (ABSL_PREDICT_FALSE(location.line < 1 || location.column < 0)) {
492
0
    return absl::nullopt;
493
0
  }
494
8.27M
  if (auto position = FindLinePosition(location.line);
495
8.27M
      ABSL_PREDICT_TRUE(position.has_value())) {
496
8.27M
    return *position + location.column;
497
8.27M
  }
498
0
  return absl::nullopt;
499
8.27M
}
500
501
124k
absl::optional<std::string> Source::Snippet(int32_t line) const {
502
124k
  auto content = this->content();
503
124k
  auto start = FindLinePosition(line);
504
124k
  if (ABSL_PREDICT_FALSE(!start.has_value() || content.empty())) {
505
2.33k
    return absl::nullopt;
506
2.33k
  }
507
122k
  auto end = FindLinePosition(line + 1);
508
122k
  if (end.has_value()) {
509
31.8k
    return content.ToString(*start, *end - 1);
510
31.8k
  }
511
90.7k
  return content.ToString(*start);
512
122k
}
513
514
124k
std::string Source::DisplayErrorLocation(SourceLocation location) const {
515
124k
  constexpr char32_t kDot = '.';
516
124k
  constexpr char32_t kHat = '^';
517
518
124k
  constexpr char32_t kWideDot = 0xff0e;
519
124k
  constexpr char32_t kWideHat = 0xff3e;
520
124k
  absl::optional<std::string> snippet = Snippet(location.line);
521
124k
  if (!snippet || snippet->empty()) {
522
2.73k
    return "";
523
2.73k
  }
524
525
122k
  *snippet = absl::StrReplaceAll(*snippet, {{"\t", " "}});
526
122k
  absl::string_view snippet_view(*snippet);
527
122k
  std::string result;
528
122k
  absl::StrAppend(&result, "\n | ", *snippet);
529
122k
  absl::StrAppend(&result, "\n | ");
530
531
122k
  std::string index_line;
532
512M
  for (int32_t i = 0; i < location.column && !snippet_view.empty(); ++i) {
533
512M
    size_t count;
534
512M
    std::tie(std::ignore, count) = internal::Utf8Decode(snippet_view);
535
512M
    snippet_view.remove_prefix(count);
536
512M
    if (count > 1) {
537
125k
      internal::Utf8Encode(index_line, kWideDot);
538
512M
    } else {
539
512M
      internal::Utf8Encode(index_line, kDot);
540
512M
    }
541
512M
  }
542
122k
  size_t count = 0;
543
122k
  if (!snippet_view.empty()) {
544
118k
    std::tie(std::ignore, count) = internal::Utf8Decode(snippet_view);
545
118k
  }
546
122k
  if (count > 1) {
547
2.55k
    internal::Utf8Encode(index_line, kWideHat);
548
119k
  } else {
549
119k
    internal::Utf8Encode(index_line, kHat);
550
119k
  }
551
122k
  absl::StrAppend(&result, index_line);
552
122k
  return result;
553
124k
}
554
555
8.52M
absl::optional<SourcePosition> Source::FindLinePosition(int32_t line) const {
556
8.52M
  if (ABSL_PREDICT_FALSE(line < 1)) {
557
2.33k
    return absl::nullopt;
558
2.33k
  }
559
8.52M
  if (line == 1) {
560
7.05M
    return SourcePosition{0};
561
7.05M
  }
562
1.47M
  const auto line_offsets = this->line_offsets();
563
1.47M
  if (ABSL_PREDICT_TRUE(line <= static_cast<int32_t>(line_offsets.size()))) {
564
1.38M
    return line_offsets[static_cast<size_t>(line - 2)];
565
1.38M
  }
566
90.7k
  return absl::nullopt;
567
1.47M
}
568
569
absl::optional<std::pair<int32_t, SourcePosition>> Source::FindLine(
570
124k
    SourcePosition position) const {
571
124k
  if (ABSL_PREDICT_FALSE(position < 0)) {
572
2.33k
    return absl::nullopt;
573
2.33k
  }
574
122k
  int32_t line = 1;
575
122k
  const auto line_offsets = this->line_offsets();
576
1.61G
  for (const auto& line_offset : line_offsets) {
577
1.61G
    if (line_offset > position) {
578
122k
      break;
579
122k
    }
580
1.61G
    ++line;
581
1.61G
  }
582
122k
  if (line == 1) {
583
88.6k
    return std::make_pair(line, SourcePosition{0});
584
88.6k
  }
585
33.9k
  return std::make_pair(line, line_offsets[static_cast<size_t>(line) - 2]);
586
122k
}
587
588
absl::StatusOr<absl_nonnull SourcePtr> NewSource(absl::string_view content,
589
7.07k
                                                 std::string description) {
590
7.07k
  return common_internal::NewSourceImpl(std::move(description), content,
591
7.07k
                                        content.size());
592
7.07k
}
593
594
absl::StatusOr<absl_nonnull SourcePtr> NewSource(const absl::Cord& content,
595
0
                                                 std::string description) {
596
0
  return common_internal::NewSourceImpl(std::move(description), content,
597
0
                                        content.size());
598
0
}
599
600
}  // namespace cel