Skip to content
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 cel-c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -727,14 +727,14 @@ cc_library(
],
visibility = ["//visibility:public"],
deps = [
":alloc",
":assert",
":config",
":error_space",
":status_code",
":string_view",
"//cel-c/internal:array",
"//cel-c/internal:string",
"//cel-c/internal:trivial_relocation",
],
)

Expand Down
5 changes: 4 additions & 1 deletion cel-c/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ cc_library(
deps = [
":config",
":string",
"//cel-c:alloc",
"//cel-c:assert",
"//cel-c:config",
"//cel-c:string_view",
],
)
Expand Down Expand Up @@ -1190,7 +1190,9 @@ cc_library(
deps = [
":config",
":generic_string",
":trivial_relocation",
"//cel-c:alloc",
"//cel-c:assert",
"//cel-c:cstring_view",
"//cel-c:string_view",
],
Expand All @@ -1203,6 +1205,7 @@ cc_test(
":config",
":generic_string",
":string",
":trivial_relocation",
"//cel-c:alloc",
"//cel-c:cstring_view",
"//cel-c:string_view",
Expand Down
192 changes: 108 additions & 84 deletions cel-c/internal/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,124 +15,148 @@
#ifndef THIRD_PARTY_CEL_C_INTERNAL_CONTAINER_H_
#define THIRD_PARTY_CEL_C_INTERNAL_CONTAINER_H_

#include <stddef.h>
#include <cstddef>
#include <utility>

#include "cel-c/alloc.h"
#include "cel-c/assert.h"
#include "cel-c/config.h"
#include "cel-c/internal/config.h"
#include "cel-c/internal/string.h"
#include "cel-c/string_view.h"

CEL_BEGIN_DECLS
struct _cel_ContainerIterator {
cel_StringView name = cel_StringView{.data = "", .size = 0};
size_t size = 0;

typedef struct {
_cel_String name;
size_t count;
} _cel_Container;
[[nodiscard]]
bool HasNext() const {
return size > 0;
}

typedef struct {
cel_StringView name;
size_t size;
} _cel_ContainerIterator;
[[nodiscard]]
cel_StringView Next() {
CEL_ASSERT_GT(size, 0);

cel_StringView name = this->name;
--size;
const char* pos =
cel_StringView_FindLast(this->name, cel_StringView_FromString("."));
if (pos == cel_nullptr) {
CEL_ASSERT_EQ(size, 0);
pos = cel_StringView_Data(this->name);
}
cel_StringView_RemoveSuffix(&this->name, (cel_StringView_Data(this->name) +
cel_StringView_Size(this->name)) -
pos);
return name;
}
};

static CEL_INLINE void _cel_Container_Construct(
_cel_Container* cel_nonnull container) {
CEL_ASSERT_NOT_NULL(container);
class _cel_Container {
public:
_cel_Container() = default;
_cel_Container(const _cel_Container&) = delete;
_cel_Container& operator=(const _cel_Container&) = delete;

_cel_String_Construct(&container->name);
container->count = 0;
}
CEL_INLINE _cel_Container(_cel_Container&& other) noexcept
: name_(std::move(other.name_)), count_(std::exchange(other.count_, 0)) {}

static CEL_INLINE void _cel_Container_Destruct(
_cel_Container* cel_nonnull container, cel_Allocator* cel_nonnull alloc) {
CEL_ASSERT_NOT_NULL(container);
CEL_ASSERT_NOT_NULL(alloc);
CEL_INLINE _cel_Container& operator=(_cel_Container&& other) noexcept {
if (CEL_LIKELY(this != &other)) {
name_ = std::move(other.name_);
count_ = std::exchange(other.count_, 0);
}
return *this;
}

_cel_String_Destruct(&container->name, alloc);
}
[[nodiscard]]
CEL_INLINE bool empty() const {
return count() == 0;
}

[[nodiscard]]
CEL_INLINE size_t count() const {
return count_;
}

[[nodiscard]]
CEL_INLINE cel_StringView name() const CEL_ATTRIBUTE_LIFETIME_BOUND {
return _cel_String_ToStringView(&name_);
}

[[nodiscard]]
CEL_INLINE bool Update(cel_StringView name) {
_cel_String_Clear(&name_);
count_ = 0;
_cel_String_Reserve(&name_, cel_StringView_Size(name));

cel_StringViewTokenizer tokenizer =
cel_StringView_Tokenize(name, cel_StringView_FromString("."));
cel_StringView token;
while (cel_StringViewTokenizer_Next(&tokenizer, &token)) {
if (cel_StringView_Empty(token)) {
continue;
}
++count_;
if (!_cel_String_Empty(&name_)) {
if (!_cel_String_PushBack(&name_, '.')) {
return false;
}
}
if (!_cel_String_Append(&name_, token)) {
return false;
}
}
return true;
}

[[nodiscard]]
CEL_INLINE _cel_ContainerIterator Iterate() const {
_cel_ContainerIterator iter;
iter.name = _cel_String_ToStringView(&name_);
iter.size = count_;
return iter;
}

private:
_cel_String name_;
size_t count_ = 0;
};

CEL_ATTRIBUTE_NODISCARD
static CEL_INLINE bool _cel_Container_Empty(
CEL_DEPRECATE_AND_INLINE CEL_INLINE bool _cel_Container_Empty(
const _cel_Container* cel_nonnull container) {
return container->count == 0;
return container->empty();
}

CEL_ATTRIBUTE_NODISCARD
static CEL_INLINE size_t
CEL_DEPRECATE_AND_INLINE CEL_INLINE size_t
_cel_Container_Count(const _cel_Container* cel_nonnull container) {
return container->count;
return container->count();
}

CEL_ATTRIBUTE_NODISCARD
static CEL_INLINE bool _cel_Container_Update(
_cel_Container* cel_nonnull container, cel_StringView name,
cel_Allocator* cel_nonnull alloc) {
CEL_ASSERT_NOT_NULL(container);
CEL_ASSERT_NOT_NULL(alloc);

_cel_String_Clear(&container->name);
container->count = 0;
_cel_String_Reserve(&container->name, alloc, cel_StringView_Size(name));

cel_StringViewTokenizer tokenizer =
cel_StringView_Tokenize(name, cel_StringView_FromString("."));
cel_StringView token;
while (cel_StringViewTokenizer_Next(&tokenizer, &token)) {
if (cel_StringView_Empty(token)) {
continue;
}
++container->count;
if (!_cel_String_Empty(&container->name)) {
if (!_cel_String_PushBack(&container->name, alloc, '.')) {
return false;
}
}
if (!_cel_String_Append(&container->name, alloc, token)) {
return false;
}
}
return true;
CEL_DEPRECATE_AND_INLINE CEL_INLINE bool _cel_Container_Update(
_cel_Container* cel_nonnull container, cel_StringView name) {
return container->Update(name);
}

CEL_ATTRIBUTE_NODISCARD
static CEL_INLINE _cel_ContainerIterator
CEL_DEPRECATE_AND_INLINE CEL_INLINE _cel_ContainerIterator
_cel_Container_Iterate(const _cel_Container* cel_nonnull container) {
CEL_ASSERT_NOT_NULL(container);

_cel_ContainerIterator iter;
iter.name = _cel_String_ToStringView(&container->name);
iter.size = container->count;
return iter;
return container->Iterate();
}

CEL_ATTRIBUTE_NODISCARD
static CEL_INLINE bool _cel_ContainerIterator_HasNext(
CEL_DEPRECATE_AND_INLINE CEL_INLINE bool _cel_ContainerIterator_HasNext(
const _cel_ContainerIterator* cel_nonnull iter) {
CEL_ASSERT_NOT_NULL(iter);

return iter->size > 0;
return iter->HasNext();
}

CEL_ATTRIBUTE_NODISCARD
static CEL_INLINE cel_StringView
CEL_DEPRECATE_AND_INLINE CEL_INLINE cel_StringView
_cel_ContainerIterator_Next(_cel_ContainerIterator* cel_nonnull iter) {
CEL_ASSERT_NOT_NULL(iter);
CEL_ASSERT_GT(iter->size, 0);

cel_StringView name = iter->name;
--iter->size;
const char* pos =
cel_StringView_FindLast(iter->name, cel_StringView_FromString("."));
if (pos == cel_nullptr) {
CEL_ASSERT_EQ(iter->size, 0);
pos = cel_StringView_Data(iter->name);
}
cel_StringView_RemoveSuffix(&iter->name, (cel_StringView_Data(iter->name) +
cel_StringView_Size(iter->name)) -
pos);
return name;
return iter->Next();
}

CEL_END_DECLS

#endif // THIRD_PARTY_CEL_C_INTERNAL_CONTAINER_H_
10 changes: 2 additions & 8 deletions cel-c/internal/container_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ namespace {

class ContainerTest : public ::testing::Test {
public:
void SetUp() override { _cel_Container_Construct(&cont_); }

void TearDown() override { _cel_Container_Destruct(cont(), alloc()); }

cel_Allocator* alloc() { return cel_DefaultAllocator; }

_cel_Container* cont() { return &cont_; }
Expand All @@ -45,8 +41,7 @@ TEST_F(ContainerTest, Empty) {
_cel_ContainerIterator cont_it = _cel_Container_Iterate(cont());
EXPECT_FALSE(_cel_ContainerIterator_HasNext(&cont_it));

ASSERT_TRUE(
_cel_Container_Update(cont(), cel_StringView_FromString(""), alloc()));
ASSERT_TRUE(_cel_Container_Update(cont(), cel_StringView_FromString("")));

EXPECT_TRUE(_cel_Container_Empty(cont()));
EXPECT_EQ(_cel_Container_Count(cont()), 0);
Expand All @@ -66,8 +61,7 @@ class ContainerTestWithParam
TEST_P(ContainerTestWithParam, Match) {
const auto& param = GetParam();
ASSERT_TRUE(_cel_Container_Update(
cont(), cel_StringView_FromAbsl(absl::string_view(param.subject)),
alloc()));
cont(), cel_StringView_FromAbsl(absl::string_view(param.subject))));
EXPECT_EQ(_cel_Container_Count(cont()), param.results.size());
_cel_ContainerIterator cont_it = _cel_Container_Iterate(cont());
for (const auto& part : param.results) {
Expand Down
Loading
Loading