Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Minor modifications to common/source.h #674

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ cc_library(
deps = [
"//internal:unicode",
"//internal:utf8",
"@com_google_absl//absl/base",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/functional:overload",
"@com_google_absl//absl/log:absl_check",
Expand Down
18 changes: 11 additions & 7 deletions common/source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@
#include "common/source.h"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include "absl/base/casts.h"
#include "absl/base/nullability.h"
#include "absl/base/optimization.h"
#include "absl/container/inlined_vector.h"
#include "absl/functional/overload.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
#include "absl/types/variant.h"
#include "internal/unicode.h"
Expand Down Expand Up @@ -251,7 +255,7 @@ struct SourceTextTraits<absl::string_view> {

static void AppendTo(std::vector<uint8_t>& out, absl::string_view text,
size_t n) {
const auto* in = absl::bit_cast<const uint8_t*>(text.data());
const auto* in = reinterpret_cast<const uint8_t*>(text.data());
out.insert(out.end(), in, in + n);
}

Expand Down Expand Up @@ -281,7 +285,7 @@ struct SourceTextTraits<absl::Cord> {
while (n > 0) {
auto str = absl::Cord::ChunkRemaining(it);
size_t to_append = std::min(n, str.size());
const auto* in = absl::bit_cast<const uint8_t*>(str.data());
const auto* in = reinterpret_cast<const uint8_t*>(str.data());
out.insert(out.end(), in, in + to_append);
n -= to_append;
absl::Cord::Advance(&it, to_append);
Expand Down Expand Up @@ -538,14 +542,14 @@ absl::optional<std::pair<int32_t, SourcePosition>> Source::FindLine(
return std::make_pair(line, line_offsets[static_cast<size_t>(line) - 2]);
}

absl::StatusOr<SourcePtr> NewSource(absl::string_view content,
std::string description) {
absl::StatusOr<absl::Nonnull<SourcePtr>> NewSource(absl::string_view content,
std::string description) {
return common_internal::NewSourceImpl(std::move(description), content,
content.size());
}

absl::StatusOr<SourcePtr> NewSource(const absl::Cord& content,
std::string description) {
absl::StatusOr<absl::Nonnull<SourcePtr>> NewSource(const absl::Cord& content,
std::string description) {
return common_internal::NewSourceImpl(std::move(description), content,
content.size());
}
Expand Down
25 changes: 17 additions & 8 deletions common/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <utility>

#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
Expand All @@ -42,14 +43,22 @@ using SourcePosition = int32_t;
// SourceRange represents a range of positions, where `begin` is inclusive and
// `end` is exclusive.
struct SourceRange final {
SourcePosition begin;
SourcePosition end;
SourcePosition begin = -1;
SourcePosition end = -1;
};

inline bool operator==(const SourceRange& lhs, const SourceRange& rhs) {
return lhs.begin == rhs.begin && lhs.end == rhs.end;
}

inline bool operator!=(const SourceRange& lhs, const SourceRange& rhs) {
return !operator==(lhs, rhs);
}

// `SourceLocation` is a representation of a line and column in source text.
struct SourceLocation final {
int32_t line; // 1-based line number.
int32_t column; // 0-based column number.
int32_t line = -1; // 1-based line number.
int32_t column = -1; // 0-based column number.
};

inline bool operator==(const SourceLocation& lhs, const SourceLocation& rhs) {
Expand Down Expand Up @@ -172,11 +181,11 @@ class Source {

using SourcePtr = std::unique_ptr<Source>;

absl::StatusOr<SourcePtr> NewSource(absl::string_view content,
std::string description = {});
absl::StatusOr<absl::Nonnull<SourcePtr>> NewSource(
absl::string_view content, std::string description = "<input>");

absl::StatusOr<SourcePtr> NewSource(const absl::Cord& content,
std::string description = {});
absl::StatusOr<absl::Nonnull<SourcePtr>> NewSource(
const absl::Cord& content, std::string description = "<input>");

} // namespace cel

Expand Down
23 changes: 23 additions & 0 deletions common/source_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,31 @@ namespace {

using testing::ElementsAre;
using testing::Eq;
using testing::Ne;
using testing::Optional;

TEST(SourceRange, Default) {
SourceRange range;
EXPECT_EQ(range.begin, -1);
EXPECT_EQ(range.end, -1);
}

TEST(SourceRange, Equality) {
EXPECT_THAT((SourceRange{}), (Eq(SourceRange{})));
EXPECT_THAT((SourceRange{0, 1}), (Ne(SourceRange{0, 0})));
}

TEST(SourceLocation, Default) {
SourceLocation location;
EXPECT_EQ(location.line, -1);
EXPECT_EQ(location.column, -1);
}

TEST(SourceLocation, Equality) {
EXPECT_THAT((SourceLocation{}), (Eq(SourceLocation{})));
EXPECT_THAT((SourceLocation{1, 1}), (Ne(SourceLocation{1, 0})));
}

TEST(StringSource, Description) {
ASSERT_OK_AND_ASSIGN(
auto source,
Expand Down