Introduce Cosmopolitan Templates Library (CTL)

This commit is contained in:
Justine Tunney 2024-06-03 09:09:33 -07:00
parent b003888696
commit 4937843f70
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
16 changed files with 7054 additions and 12 deletions

View file

@ -174,6 +174,7 @@ all: o
o: o/$(MODE)
o/$(MODE): \
o/$(MODE)/ape \
o/$(MODE)/ctl \
o/$(MODE)/dsp \
o/$(MODE)/net \
o/$(MODE)/libc \
@ -255,6 +256,7 @@ include third_party/nsync/mem/BUILD.mk # │ You can now use stdio
include libc/proc/BUILD.mk # │ You can now use threads
include libc/dlopen/BUILD.mk # │ You can now use processes
include libc/thread/BUILD.mk # │ You can finally call malloc()
include ctl/BUILD.mk # │
include third_party/zlib/BUILD.mk # │
include libc/stdio/BUILD.mk # │
include tool/hello/BUILD.mk # │
@ -299,6 +301,7 @@ include tool/viz/lib/BUILD.mk
include tool/args/BUILD.mk
include test/math/BUILD.mk
include test/posix/BUILD.mk
include test/ctl/BUILD.mk
include test/libcxx/BUILD.mk
include test/tool/args/BUILD.mk
include third_party/linenoise/BUILD.mk
@ -451,6 +454,7 @@ COSMOPOLITAN_OBJECTS = \
LIBC_THREAD \
LIBC_PROC \
THIRD_PARTY_NSYNC_MEM \
CTL \
LIBC_MEM \
THIRD_PARTY_DLMALLOC \
LIBC_DLOPEN \
@ -519,6 +523,7 @@ COSMOPOLITAN_H_PKGS = \
COSMOCC_PKGS = \
$(COSMOPOLITAN_H_PKGS) \
CTL \
THIRD_PARTY_AARCH64 \
THIRD_PARTY_LIBCXX \
THIRD_PARTY_LIBCXXABI \

12
ctl/.clang-format Normal file
View file

@ -0,0 +1,12 @@
---
BasedOnStyle: Mozilla
IndentWidth: 4
ColumnLimit: 80
---
Language: Cpp
AllowShortFunctionsOnASingleLine: false
AlignTrailingComments: false
AlignEscapedNewlines: DontAlign
AlwaysBreakTemplateDeclarations: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
---

48
ctl/BUILD.mk Normal file
View file

@ -0,0 +1,48 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘
PKGS += CTL
CTL_ARTIFACTS += CTL_A
CTL = $(CTL_A_DEPS) $(CTL_A)
CTL_A = o/$(MODE)/ctl/ctl.a
CTL_A_FILES := $(wildcard ctl/*)
CTL_A_HDRS = $(filter %.h,$(CTL_A_FILES))
CTL_A_SRCS = $(filter %.cc,$(CTL_A_FILES))
CTL_A_OBJS = $(CTL_A_SRCS:%.cc=o/$(MODE)/%.o)
CTL_A_CHECKS = \
$(CTL_A).pkg \
$(CTL_A_HDRS:%=o/$(MODE)/%.okk) \
CTL_A_DIRECTDEPS = \
LIBC_INTRIN \
LIBC_MEM \
LIBC_STR \
CTL_A_DEPS := $(call uniq,$(foreach x,$(CTL_A_DIRECTDEPS),$($(x))))
$(CTL_A): ctl/ \
$(CTL_A).pkg \
$(CTL_A_OBJS)
$(CTL_A).pkg: \
$(CTL_A_OBJS) \
$(foreach x,$(CTL_A_DIRECTDEPS),$($(x)_A).pkg)
$(CTL_A_OBJS): private \
OVERRIDE_CXXFLAGS += \
-Wframe-larger-than=4096 \
-Walloca-larger-than=4096 \
-ffunction-sections \
-fdata-sections \
CTL_LIBS = $(foreach x,$(CTL_ARTIFACTS),$($(x)))
CTL_SRCS = $(foreach x,$(CTL_ARTIFACTS),$($(x)_SRCS))
CTL_HDRS = $(foreach x,$(CTL_ARTIFACTS),$($(x)_HDRS))
CTL_CHECKS = $(foreach x,$(CTL_ARTIFACTS),$($(x)_CHECKS))
CTL_OBJS = $(foreach x,$(CTL_ARTIFACTS),$($(x)_OBJS))
$(CTL_OBJS): $(BUILD_FILES) ctl/BUILD.mk
.PHONY: o/$(MODE)/ctl
o/$(MODE)/ctl: $(CTL_CHECKS)

4967
ctl/README.md Normal file

File diff suppressed because it is too large Load diff

125
ctl/optional.h Normal file
View file

@ -0,0 +1,125 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef COSMOPOLITAN_CTL_OPTIONAL_H_
#define COSMOPOLITAN_CTL_OPTIONAL_H_
#include <__utility/forward.h>
#include <__utility/move.h>
#include <__utility/swap.h>
template<typename T>
class Optional
{
public:
using value_type = T;
~Optional() = default;
Optional() noexcept : present_(false)
{
}
Optional(const T& value) : present_(true), value_(value)
{
}
Optional(T&& value) : present_(true), value_(std::move(value))
{
}
Optional(const Optional& other) : present_(other.present_)
{
if (present_)
new (&value_) T(other.value_);
}
Optional(Optional&& other) noexcept : present_(other.present_)
{
if (present_)
value_ = std::move(other.value_);
}
Optional& operator=(const Optional& other)
{
if (this != &other) {
present_ = other.present_;
if (present_)
value_ = other.value_;
}
return *this;
}
Optional& operator=(Optional&& other) noexcept
{
if (this != &other) {
present_ = other.present_;
if (present_)
value_ = std::move(other.value_);
}
return *this;
}
T& value() &
{
if (!present_)
__builtin_trap();
return value_;
}
const T& value() const&
{
if (!present_)
__builtin_trap();
return value_;
}
T&& value() &&
{
if (!present_)
__builtin_trap();
return std::move(value_);
}
explicit operator bool() const noexcept
{
return present_;
}
bool has_value() const noexcept
{
return present_;
}
void reset() noexcept
{
if (present_) {
value_.~T();
present_ = false;
}
}
template<typename... Args>
void emplace(Args&&... args)
{
present_ = true;
value_ = T(std::forward<Args>(args)...);
}
void swap(Optional& other) noexcept
{
if (present_ && other.present_) {
std::swap(value_, other.value_);
} else if (present_) {
other.emplace(std::move(value_));
reset();
} else if (other.present_) {
emplace(std::move(other.value_));
other.reset();
}
}
private:
bool present_;
T value_;
};
#endif // COSMOPOLITAN_CTL_OPTIONAL_H_

439
ctl/string.cc Normal file
View file

@ -0,0 +1,439 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8
//
// Copyright 2024 Justine Alexandra Roberts Tunney
//
// Permission to use, copy, modify, and/or distribute this software for
// any purpose with or without fee is hereby granted, provided that the
// above copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include "string.h"
#include <__atomic/fence.h>
#include <stdckdint.h>
String::~String() noexcept
{
if (n) {
if (n >= c)
__builtin_trap();
if (p[n])
__builtin_trap();
}
if (c && !p)
__builtin_trap();
free(p);
}
String::String(const char* s) noexcept
{
append(s, strlen(s));
}
String::String(const String& s) noexcept
{
append(s.p, s.n);
}
String::String(const StringView s) noexcept
{
append(s.p, s.n);
}
String::String(size_t size, char ch) noexcept
{
resize(size, ch);
}
String::String(const char* s, size_t size) noexcept
{
append(s, size);
}
const char*
String::c_str() const noexcept
{
if (!n)
return "";
if (n >= c)
__builtin_trap();
if (p[n])
__builtin_trap();
return p;
}
void
String::reserve(size_t c2) noexcept
{
char* p2;
if (c2 < n)
c2 = n;
if (ckd_add(&c2, c2, 15))
__builtin_trap();
c2 &= -16;
if (!(p2 = (char*)realloc(p, c2)))
__builtin_trap();
std::atomic_signal_fence(std::memory_order_seq_cst);
c = c2;
p = p2;
}
void
String::resize(size_t n2, char ch) noexcept
{
size_t c2;
if (ckd_add(&c2, n2, 1))
__builtin_trap();
reserve(c2);
if (n2 > n)
memset(p + n, ch, n2 - n);
p[n = n2] = 0;
}
void
String::append(char ch) noexcept
{
if (n + 2 > c) {
size_t c2 = c + 2;
c2 += c2 >> 1;
reserve(c2);
}
p[n++] = ch;
p[n] = 0;
}
void
String::grow(size_t size) noexcept
{
size_t need;
if (ckd_add(&need, n, size))
__builtin_trap();
if (ckd_add(&need, need, 1))
__builtin_trap();
if (need <= c)
return;
size_t c2 = c;
if (!c2) {
c2 = need;
} else {
while (c2 < need)
if (ckd_add(&c2, c2, c2 >> 1))
__builtin_trap();
}
reserve(c2);
}
void
String::append(char ch, size_t size) noexcept
{
grow(size);
if (size)
memset(p + n, ch, size);
p[n += size] = 0;
}
void
String::append(const void* data, size_t size) noexcept
{
grow(size);
if (size)
memcpy(p + n, data, size);
p[n += size] = 0;
}
void
String::pop_back() noexcept
{
if (!n)
__builtin_trap();
p[--n] = 0;
}
String&
String::operator=(String&& s) noexcept
{
if (p != s.p) {
free(p);
p = s.p;
n = s.n;
c = s.c;
s.p = nullptr;
s.n = 0;
s.c = 0;
}
return *this;
}
static String
StrCat(const StringView lhs, const StringView rhs) noexcept
{
String res;
size_t need;
if (ckd_add(&need, lhs.n, rhs.n))
__builtin_trap();
if (ckd_add(&need, need, 1))
__builtin_trap();
res.reserve(need);
if (lhs.n)
memcpy(res.p, lhs.p, lhs.n);
if (rhs.n)
memcpy(res.p + lhs.n, rhs.p, rhs.n);
res.p[res.n = lhs.n + rhs.n] = 0;
return res;
}
String
StringView::operator+(const StringView s) const noexcept
{
return StrCat(*this, s);
}
String
String::operator+(const StringView s) const noexcept
{
return StrCat(*this, s);
}
bool
String::operator==(const StringView s) const noexcept
{
if (n != s.n)
return false;
return !memcmp(p, s.p, n);
}
bool
String::operator!=(const StringView s) const noexcept
{
if (n != s.n)
return true;
return !!memcmp(p, s.p, n);
}
bool
String::contains(const StringView s) const noexcept
{
if (!s.n)
return true;
return !!memmem(p, n, s.p, s.n);
}
bool
String::ends_with(const StringView s) const noexcept
{
if (n < s.n)
return false;
return !memcmp(p + n - s.n, s.p, s.n);
}
bool
String::starts_with(const StringView s) const noexcept
{
if (n < s.n)
return false;
return !memcmp(p, s.p, s.n);
}
static int
StrCmp(const StringView lhs, const StringView rhs) noexcept
{
int r;
size_t m = lhs.n;
if ((m = rhs.n < m ? rhs.n : m))
if ((r = memcmp(lhs.p, rhs.p, m)))
return r;
if (lhs.n == rhs.n)
return 0;
if (m < lhs.n)
return +1;
return -1;
}
int
String::compare(const StringView s) const noexcept
{
return StrCmp(*this, s);
}
size_t
String::find(char ch, size_t pos) const noexcept
{
char* q;
if ((q = (char*)memchr(p, ch, n)))
return q - p;
return npos;
}
size_t
String::find(const StringView s, size_t pos) const noexcept
{
char* q;
if (pos > n)
__builtin_trap();
if ((q = (char*)memmem(p + pos, n - pos, s.p, s.n)))
return q - p;
return npos;
}
String
String::substr(size_t pos, size_t count) const noexcept
{
size_t last;
if (pos > n)
__builtin_trap();
if (count > n - pos)
count = n - pos;
if (ckd_add(&last, pos, count))
last = n;
if (last > n)
__builtin_trap();
return String(p + pos, count);
}
String&
String::replace(size_t pos, size_t count, const StringView& s) noexcept
{
size_t last;
if (ckd_add(&last, pos, count))
__builtin_trap();
if (last > n)
__builtin_trap();
size_t need;
if (ckd_add(&need, pos, s.n))
__builtin_trap();
size_t extra = n - last;
if (ckd_add(&need, need, extra))
__builtin_trap();
size_t c2;
if (ckd_add(&c2, need, 1))
__builtin_trap();
reserve(c2);
if (extra)
memmove(p + pos + s.n, p + last, extra);
memcpy(p + pos, s.p, s.n);
p[n = need] = 0;
return *this;
}
int
StringView::compare(const StringView s) const noexcept
{
return StrCmp(*this, s);
}
size_t
StringView::find(char ch, size_t pos) const noexcept
{
char* q;
if (n && (q = (char*)memchr(p, ch, n)))
return q - p;
return npos;
}
size_t
StringView::find(const StringView s, size_t pos) const noexcept
{
char* q;
if (pos > n)
__builtin_trap();
if ((q = (char*)memmem(p + pos, n - pos, s.p, s.n)))
return q - p;
return npos;
}
StringView
StringView::substr(size_t pos, size_t count) const noexcept
{
size_t last;
if (pos > n)
__builtin_trap();
if (count > n - pos)
count = n - pos;
if (ckd_add(&last, pos, count))
last = n;
if (last > n)
__builtin_trap();
return StringView(p + pos, count);
}
String&
String::insert(size_t i, const StringView s) noexcept
{
if (i > n)
__builtin_trap();
size_t extra = n - i;
size_t need;
if (ckd_add(&need, n, s.n))
__builtin_trap();
if (ckd_add(&need, need, 1))
__builtin_trap();
reserve(need);
if (extra)
memmove(p + i + s.n, p + i, extra);
memcpy(p + i, s.p, s.n);
p[n += s.n] = 0;
return *this;
}
String&
String::erase(size_t pos, size_t count) noexcept
{
if (pos > n)
__builtin_trap();
if (count > n - pos)
count = n - pos;
size_t extra = n - (pos + count);
if (extra)
memmove(p + pos, p + pos + count, extra);
p[n = pos + extra] = 0;
return *this;
}
bool
StringView::operator==(const StringView s) const noexcept
{
if (n == s.n)
return true;
return !memcmp(p, s.p, n);
}
bool
StringView::operator!=(const StringView s) const noexcept
{
if (n != s.n)
return true;
return !!memcmp(p, s.p, n);
}
bool
StringView::contains(const StringView s) const noexcept
{
if (!s.n)
return true;
return !!memmem(p, n, s.p, s.n);
}
bool
StringView::ends_with(const StringView s) const noexcept
{
if (n < s.n)
return false;
return !memcmp(p + n - s.n, s.p, s.n);
}
bool
StringView::starts_with(const StringView s) const noexcept
{
if (n < s.n)
return false;
return !memcmp(p, s.p, s.n);
}

320
ctl/string.h Normal file
View file

@ -0,0 +1,320 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef COSMOPOLITAN_CTL_STRING_H_
#define COSMOPOLITAN_CTL_STRING_H_
struct String;
struct StringView
{
const char* p;
size_t n;
static constexpr size_t npos = -1;
constexpr StringView(const char* s) noexcept
: p(s), n(s ? __builtin_strlen(s) : 0)
{
}
constexpr StringView(const char* s, size_t n) noexcept : p(s), n(n)
{
}
inline constexpr ~StringView() noexcept
{
}
int compare(const StringView) const noexcept;
bool operator==(const StringView) const noexcept;
bool operator!=(const StringView) const noexcept;
bool contains(const StringView) const noexcept;
String operator+(const StringView) const noexcept;
bool ends_with(const StringView) const noexcept;
bool starts_with(const StringView) const noexcept;
StringView substr(size_t = 0, size_t = npos) const noexcept;
size_t find(char, size_t = 0) const noexcept;
size_t find(const StringView, size_t = 0) const noexcept;
constexpr StringView& operator=(const StringView& s) noexcept
{
p = s.p;
n = s.n;
return *this;
}
constexpr bool empty() const noexcept
{
return !n;
}
constexpr const char* data() const noexcept
{
return p;
}
constexpr size_t size() const noexcept
{
return n;
}
constexpr size_t length() const noexcept
{
return n;
}
constexpr const char& operator[](size_t i) const noexcept
{
if (i >= n)
__builtin_trap();
return p[i];
}
constexpr void remove_prefix(size_t count)
{
if (count > n)
__builtin_trap();
p += count;
n -= count;
}
constexpr void remove_suffix(size_t count)
{
if (count > n)
__builtin_trap();
n -= count;
}
bool operator<(const StringView& s) const noexcept
{
return compare(s) < 0;
}
bool operator<=(const StringView& s) const noexcept
{
return compare(s) <= 0;
}
bool operator>(const StringView& s) const noexcept
{
return compare(s) > 0;
}
bool operator>=(const StringView& s) const noexcept
{
return compare(s) >= 0;
}
};
struct String
{
char* p = nullptr;
size_t n = 0;
size_t c = 0;
using iterator = char*;
using const_iterator = const char*;
static constexpr size_t npos = -1;
~String() noexcept;
String() = default;
String(const StringView) noexcept;
String(const char*) noexcept;
String(const String&) noexcept;
String(const char*, size_t) noexcept;
explicit String(size_t, char = 0) noexcept;
String& operator=(String&&) noexcept;
const char* c_str() const noexcept;
void pop_back() noexcept;
void grow(size_t) noexcept;
void reserve(size_t) noexcept;
void resize(size_t, char = 0) noexcept;
void append(char) noexcept;
void append(char, size_t) noexcept;
void append(unsigned long) noexcept;
void append(const void*, size_t) noexcept;
String& insert(size_t, const StringView) noexcept;
String& erase(size_t = 0, size_t = npos) noexcept;
String operator+(const StringView) const noexcept;
String substr(size_t = 0, size_t = npos) const noexcept;
String& replace(size_t, size_t, const StringView&) noexcept;
bool operator==(const StringView) const noexcept;
bool operator!=(const StringView) const noexcept;
bool contains(const StringView) const noexcept;
bool ends_with(const StringView) const noexcept;
bool starts_with(const StringView) const noexcept;
int compare(const StringView) const noexcept;
size_t find(char, size_t = 0) const noexcept;
size_t find(const StringView, size_t = 0) const noexcept;
String(String&& s) noexcept : p(s.p), n(s.n), c(s.c)
{
s.p = nullptr;
s.n = 0;
s.c = 0;
}
void clear() noexcept
{
n = 0;
}
bool empty() const noexcept
{
return !n;
}
char* data() const noexcept
{
return p;
}
size_t size() const noexcept
{
return n;
}
size_t length() const noexcept
{
return n;
}
size_t capacity() const noexcept
{
return c;
}
iterator begin() noexcept
{
return p;
}
iterator end() noexcept
{
return p + n;
}
const_iterator cbegin() const noexcept
{
return p;
}
const_iterator cend() const noexcept
{
return p + n;
}
char& front()
{
if (!n)
__builtin_trap();
return p[0];
}
const char& front() const
{
if (!n)
__builtin_trap();
return p[0];
}
char& back()
{
if (!n)
__builtin_trap();
return p[n - 1];
}
const char& back() const
{
if (!n)
__builtin_trap();
return p[n - 1];
}
char& operator[](size_t i) noexcept
{
if (i >= n)
__builtin_trap();
return p[i];
}
const char& operator[](size_t i) const noexcept
{
if (i >= n)
__builtin_trap();
return p[i];
}
void push_back(char ch) noexcept
{
append(ch);
}
void append(const StringView s) noexcept
{
append(s.p, s.n);
}
inline constexpr operator StringView() const noexcept
{
return StringView(p, n);
}
String& operator=(const char* s) noexcept
{
clear();
append(s);
return *this;
}
String& operator=(const StringView s) noexcept
{
clear();
append(s);
return *this;
}
String& operator+=(char x) noexcept
{
append(x);
return *this;
}
String& operator+=(const StringView s) noexcept
{
append(s);
return *this;
}
bool operator<(const StringView s) const noexcept
{
return compare(s) < 0;
}
bool operator<=(const StringView s) const noexcept
{
return compare(s) <= 0;
}
bool operator>(const StringView s) const noexcept
{
return compare(s) > 0;
}
bool operator>=(const StringView s) const noexcept
{
return compare(s) >= 0;
}
};
#pragma GCC diagnostic ignored "-Wliteral-suffix"
inline String
operator"" s(const char* s, size_t n)
{
return String(s, n);
}
#endif // COSMOPOLITAN_CTL_STRING_H_

242
ctl/vector.h Normal file
View file

@ -0,0 +1,242 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef COSMOPOLITAN_CTL_OPTIONAL_H_
#define COSMOPOLITAN_CTL_OPTIONAL_H_
#include <__utility/forward.h>
#include <__utility/move.h>
#include <__utility/swap.h>
template<typename T>
struct Vector
{
size_t n = 0;
size_t c = 0;
T* p = nullptr;
using iterator = T*;
using const_iterator = const T*;
Vector() = default;
~Vector()
{
delete[] p;
}
Vector(const Vector& other)
{
n = other.n;
c = other.c;
p = new T[c];
for (size_t i = 0; i < n; ++i)
new (&p[i]) T(other.p[i]);
}
Vector(Vector&& other) noexcept
{
n = other.n;
c = other.c;
p = other.p;
other.n = 0;
other.c = 0;
other.p = nullptr;
}
explicit Vector(size_t count, const T& value = T())
{
n = count;
c = count;
p = new T[c];
for (size_t i = 0; i < n; ++i)
new (&p[i]) T(value);
}
Vector& operator=(const Vector& other)
{
if (this != &other) {
T* newData = new T[other.c];
for (size_t i = 0; i < other.n; ++i) {
newData[i] = other.p[i];
}
delete[] p;
p = newData;
n = other.n;
c = other.c;
}
return *this;
}
Vector& operator=(Vector&& other) noexcept
{
if (this != &other) {
delete[] p;
p = other.p;
n = other.n;
c = other.c;
other.p = nullptr;
other.n = 0;
other.c = 0;
}
return *this;
}
bool empty() const
{
return !n;
}
size_t size() const
{
return n;
}
size_t capacity() const
{
return c;
}
T& operator[](size_t i)
{
if (i >= n)
__builtin_trap();
return p[i];
}
const T& operator[](size_t i) const
{
if (i >= n)
__builtin_trap();
return p[i];
}
iterator begin()
{
return p;
}
iterator end()
{
return p + n;
}
const_iterator cbegin() const
{
return p;
}
const_iterator cend() const
{
return p + n;
}
T& front()
{
if (!n)
__builtin_trap();
return p[0];
}
const T& front() const
{
if (!n)
__builtin_trap();
return p[0];
}
T& back()
{
if (!n)
__builtin_trap();
return p[n - 1];
}
const T& back() const
{
if (!n)
__builtin_trap();
return p[n - 1];
}
void clear()
{
for (size_t i = 0; i < n; ++i)
p[i].~T();
n = 0;
}
void reserve(size_t c2)
{
if (c2 <= c)
return;
T* newP = new T[c2];
for (size_t i = 0; i < n; ++i)
newP[i] = std::move(p[i]);
delete[] p;
p = newP;
c = c2;
}
void push_back(const T& e)
{
if (n == c) {
size_t c2 = c + 1;
c2 += c2 >> 1;
reserve(c2);
}
new (&p[n]) T(e);
++n;
}
void push_back(T&& e)
{
if (n == c) {
size_t c2 = c + 1;
c2 += c2 >> 1;
reserve(c2);
}
new (&p[n]) T(std::forward<T>(e));
++n;
}
template<typename... Args>
void emplace_back(Args&&... args)
{
if (n == c) {
size_t c2 = c + 1;
c2 += c2 >> 1;
reserve(c2);
}
new (&p[n]) T(std::forward<Args>(args)...);
++n;
}
void pop_back()
{
if (n > 0) {
--n;
p[n].~T();
}
}
void resize(size_t n2)
{
if (n2 > n) {
reserve(n2);
for (size_t i = n; i < n2; ++i)
new (&p[i]) T();
} else if (n2 < n) {
for (size_t i = n2; i < n; ++i)
p[i].~T();
}
n = n2;
}
void swap(Vector& other) noexcept
{
std::swap(n, other.n);
std::swap(c, other.c);
std::swap(p, other.p);
}
};
#endif // COSMOPOLITAN_CTL_OPTIONAL_H_

View file

@ -2,7 +2,8 @@
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘
.PHONY: o/$(MODE)/test
o/$(MODE)/test: o/$(MODE)/test/dsp \
o/$(MODE)/test: o/$(MODE)/test/ctl \
o/$(MODE)/test/dsp \
o/$(MODE)/test/libc \
o/$(MODE)/test/libcxx \
o/$(MODE)/test/math \

12
test/ctl/.clang-format Normal file
View file

@ -0,0 +1,12 @@
---
BasedOnStyle: Mozilla
IndentWidth: 4
ColumnLimit: 80
---
Language: Cpp
AllowShortFunctionsOnASingleLine: false
AlignTrailingComments: false
AlignEscapedNewlines: DontAlign
AlwaysBreakTemplateDeclarations: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
---

38
test/ctl/BUILD.mk Normal file
View file

@ -0,0 +1,38 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘
PKGS += TEST_CTL
TEST_CTL_FILES := $(wildcard test/ctl/*)
TEST_CTL_SRCS = $(filter %.cc,$(TEST_CTL_FILES))
TEST_CTL_OBJS = $(TEST_CTL_SRCS:%.cc=o/$(MODE)/%.o)
TEST_CTL_COMS = $(TEST_CTL_OBJS:%.o=%)
TEST_CTL_BINS = $(TEST_CTL_COMS) $(TEST_CTL_COMS:%=%.dbg)
TEST_CTL_CHECKS = $(TEST_CTL_COMS:%=%.runs)
TEST_CTL_TESTS = $(TEST_CTL_COMS:%=%.ok)
TEST_CTL_DIRECTDEPS = \
CTL \
LIBC_INTRIN \
LIBC_LOG \
THIRD_PARTY_LIBCXX \
TEST_CTL_DEPS := \
$(call uniq,$(foreach x,$(TEST_CTL_DIRECTDEPS),$($(x))))
o/$(MODE)/test/ctl/ctl.pkg: \
$(TEST_CTL_OBJS) \
$(foreach x,$(TEST_CTL_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/test/ctl/%.dbg: \
$(TEST_CTL_DEPS) \
o/$(MODE)/test/ctl/%.o \
o/$(MODE)/test/ctl/ctl.pkg \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
.PHONY: o/$(MODE)/test/ctl
o/$(MODE)/test/ctl: \
$(TEST_CTL_BINS) \
$(TEST_CTL_CHECKS)

114
test/ctl/optional_test.cc Normal file
View file

@ -0,0 +1,114 @@
// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 Mozilla Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ctl/optional.h"
#include <new>
#include "ctl/string.h"
int
main()
{
{
Optional<int> x;
if (x)
return 1;
if (x.has_value())
return 2;
}
{
Optional<int> x(42);
if (!x)
return 3;
if (!x.has_value())
return 4;
if (x.value() != 42)
return 5;
}
{
Optional<String> x("hello");
Optional<String> y(x);
if (!y)
return 6;
if (!y.has_value())
return 7;
if (y.value() != "hello")
return 8;
}
{
Optional<String> x("world");
Optional<String> y(std::move(x));
if (!y)
return 9;
if (!y.has_value())
return 10;
if (y.value() != "world")
return 11;
}
{
Optional<int> x(42);
Optional<int> y;
y = x;
if (!y)
return 13;
if (!y.has_value())
return 14;
if (y.value() != 42)
return 15;
}
{
Optional<String> x("hello");
Optional<String> y;
y = std::move(x);
if (!y)
return 16;
if (!y.has_value())
return 17;
if (y.value() != "hello")
return 18;
}
{
Optional<int> x(42);
x.reset();
if (x)
return 20;
if (x.has_value())
return 21;
}
{
Optional<String> x;
x.emplace("hello");
if (!x)
return 22;
if (!x.has_value())
return 23;
if (x.value() != "hello")
return 24;
}
CheckForMemoryLeaks();
return 0;
}

371
test/ctl/string_test.cc Normal file
View file

@ -0,0 +1,371 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8
//
// Copyright 2024 Justine Alexandra Roberts Tunney
//
// Permission to use, copy, modify, and/or distribute this software for
// any purpose with or without fee is hereby granted, provided that the
// above copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include "ctl/string.h"
#include <__utility/move.h>
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
// #include <string>
// #define String std::string
int
main(int argc, char* argv[])
{
{
String s;
s += 'h';
s += 'i';
if (s != "hi")
return 1;
}
{
String s;
if (!s.empty())
return 6;
s.reserve(32);
if (!s.empty())
return 7;
if (!s.starts_with(""))
return 8;
if (s.starts_with("a"))
return 9;
s += "abc";
if (!s.starts_with("a"))
return 10;
}
{
String s;
s += "hello world how are you";
s.reserve(3);
if (s != "hello world how are you")
return 11;
}
{
String s(4, 'x');
if (s != "xxxx")
return 12;
s.resize(3);
if (s != "xxx")
return 13;
s.resize(4, 'y');
if (s != "xxxy")
return 14;
}
{
String a = "a";
String b = "a";
if (a.compare(b) != 0)
return 17;
}
{
String a = "a";
String b = "b";
if (a.compare(b) >= 0)
return 18;
}
{
String a = "a";
String b = "ab";
if (a.compare(b) >= 0)
return 19;
}
{
String a = "ab";
String b = "a";
if (a.compare(b) <= 0)
return 20;
}
{
String a = "";
String b = "";
if (a.compare(b) != 0)
return 21;
}
{
String a = "fooBARbaz";
if (a.substr(3, 3) != "BAR")
return 22;
if (a.replace(3, 3, "MOO") != "fooMOObaz")
return 23;
}
{
String a = "fooBAR";
if (a.substr(3, 3) != "BAR")
return 24;
if (a.replace(3, 3, "MOO") != "fooMOO")
return 25;
}
{
String a = "fooBAR";
if (a.substr(1, 0) != "")
return 26;
if (a.replace(1, 0, "MOO") != "fMOOooBAR")
return 27;
if (!a.starts_with("fMOOooBAR"))
return 28;
if (!a.ends_with(""))
return 29;
if (!a.ends_with("BAR"))
return 30;
if (a.ends_with("bar"))
return 31;
}
{
String s1 = "hello";
String s2 = "world";
String s3 = s1 + " " + s2;
if (s3 != "hello world")
return 32;
}
{
String s = "hello";
if (s.size() != 5)
return 33;
if (s.length() != 5)
return 34;
if (s.capacity() < 5)
return 35;
}
{
String s = "hello";
if (s[0] != 'h' || s[1] != 'e' || s[2] != 'l' || s[3] != 'l' ||
s[4] != 'o')
return 36;
s[0] = 'H';
if (s != "Hello")
return 37;
}
{
String s = "hello";
if (s.find('e') != 1)
return 38;
if (s.find('l') != 2)
return 39;
if (s.find('x') != String::npos)
return 40;
}
{
String s = "hello";
if (!s.ends_with("lo"))
return 41;
if (s.ends_with("el"))
return 42;
}
{
String s = "hello";
String sub = s.substr(1, 3);
if (sub != "ell")
return 43;
sub = s.substr(2);
if (sub != "llo")
return 44;
}
{
String s = "hello";
String s2 = s;
if (s != s2)
return 45;
s2[0] = 'H';
if (s == s2)
return 46;
}
{
String s = "hello";
String s2 = std::move(s);
if (s2 != "hello")
return 47;
if (!s.empty())
return 48;
}
{
String s = "hello";
const char* cstr = s.c_str();
if (strcmp(cstr, "hello") != 0)
return 49;
}
// {
// String s = "hello";
// char buffer[10];
// s.copy(buffer, sizeof(buffer));
// if (strcmp(buffer, "hello") != 0)
// return 50;
// }
{
String s = "hello";
s.resize(3);
if (s != "hel")
return 51;
s.resize(10, 'x');
if (s != "helxxxxxxx")
return 52;
}
{
String s = "hello";
s.clear();
if (!s.empty())
return 53;
}
{
String s = "hello";
auto it = s.begin();
if (*it != 'h')
return 54;
++it;
if (*it != 'e')
return 55;
}
// {
// String s = "hello";
// String s2 = "world";
// s.swap(s2);
// if (s != "world" || s2 != "hello")
// return 56;
// }
{
String s = "hello";
if (s.front() != 'h' || s.back() != 'o')
return 57;
}
{
String s = "hello";
s.push_back('!');
if (s != "hello!")
return 58;
s.pop_back();
if (s != "hello")
return 59;
}
{
String s = "hello";
s.insert(2, "XYZ");
if (s != "heXYZllo")
return 60;
}
{
String s = "hello";
s.erase(1, 2);
if (s != "hlo")
return 61;
}
{
String s = "hello";
s.replace(1, 2, "XYZ");
if (s != "hXYZlo")
return 62;
}
{
String s = "hello";
s.append(" world");
if (s != "hello world")
return 63;
}
// {
// String s = "hello";
// s.assign("world");
// if (s != "world")
// return 64;
// }
{
String s = "hello";
if (s.compare("world") >= 0)
return 65;
if (s.compare("hello") != 0)
return 66;
if (s.compare("hallo") <= 0)
return 67;
}
{
String s = "hello";
if (s == "world")
return 68;
if (s != "hello")
return 69;
if (s < "hallo")
return 70;
if (s > "world")
return 71;
}
{
StringView s = "hello";
if (s.find('e') != 1)
return 72;
if (s.find('l') != 2)
return 73;
if (s.find('x') != String::npos)
return 74;
}
{
StringView s = "hello there";
s.remove_prefix(6);
if (s != "there")
return 75;
s.remove_suffix(1);
if (s != "ther")
return 76;
}
{
if ("hello"s != "hello")
return 77;
if ("hell"s + "o" != "hello")
return 78;
}
CheckForMemoryLeaks();
return 0;
}

320
test/ctl/vector_test.cc Normal file
View file

@ -0,0 +1,320 @@
// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 Mozilla Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ctl/vector.h"
#include <cosmo.h>
#include <new>
#include "ctl/string.h"
// #include <string>
// #include <vector>
// #define String std::string
// #define Vector std::vector
int
main(int argc, char* argv[])
{
{
int x = 3;
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(x);
if (A[0] != 1)
return 1;
if (A[1] != 2)
return 2;
if (A[2] != 3)
return 3;
if (A.size() != 3)
return 4;
}
{
String yo = "foo";
Vector<String> A;
A.push_back("fun");
A.push_back(std::move(yo));
if (yo != "")
return 5;
A.emplace_back("bar");
if (A[0] != "fun")
return 7;
if (A[1] != "foo")
return 8;
if (A[2] != "bar")
return 9;
if (A.size() != 3)
return 10;
}
{
Vector<int> A;
if (!A.empty())
return 11;
A.push_back(5);
if (A.empty())
return 12;
if (A.front() != 5)
return 13;
if (A.back() != 5)
return 14;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B(A);
if (B.size() != 3)
return 15;
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
return 16;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B(std::move(A));
if (A.size() != 0)
return 17;
if (B.size() != 3)
return 18;
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
return 19;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
B = A;
if (B.size() != 3)
return 20;
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
return 21;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
B = std::move(A);
if (A.size() != 0)
return 22;
if (B.size() != 3)
return 23;
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
return 24;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
A.pop_back();
if (A.size() != 2)
return 25;
if (A[0] != 1 || A[1] != 2)
return 26;
}
{
Vector<int> A;
A.resize(5);
if (A.size() != 5)
return 27;
A.resize(3);
if (A.size() != 3)
return 28;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
B.push_back(4);
B.push_back(5);
A.swap(B);
if (A.size() != 2)
return 29;
if (B.size() != 3)
return 30;
if (A[0] != 4 || A[1] != 5)
return 31;
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
return 32;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
A.clear();
if (A.size() != 0)
return 33;
if (!A.empty())
return 34;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int>::iterator it = A.begin();
if (*it != 1)
return 35;
++it;
if (*it != 2)
return 36;
++it;
if (*it != 3)
return 37;
++it;
if (it != A.end())
return 38;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int>::const_iterator cit = A.cbegin();
if (*cit != 1)
return 39;
++cit;
if (*cit != 2)
return 40;
++cit;
if (*cit != 3)
return 41;
++cit;
if (cit != A.cend())
return 42;
}
{
Vector<int> A;
for (int i = 0; i < 100; ++i) {
A.push_back(i);
}
if (A.size() != 100)
return 51;
for (int i = 0; i < 100; ++i) {
if (A[i] != i)
return 52;
}
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B(A);
if (B.size() != 3)
return 53;
B.push_back(4);
if (A.size() != 3)
return 54;
if (B.size() != 4)
return 55;
}
{
Vector<int> A;
A.reserve(100);
if (A.size() != 0)
return 56;
if (A.capacity() != 100)
return 57;
A.push_back(1);
if (A.size() != 1)
return 58;
if (A.capacity() != 100)
return 59;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
B = A;
if (B.size() != 3)
return 60;
B.push_back(4);
if (A.size() != 3)
return 61;
if (B.size() != 4)
return 62;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
B = std::move(A);
if (A.size() != 0)
return 63;
if (B.size() != 3)
return 64;
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
return 65;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
B.push_back(4);
B.push_back(5);
A.swap(B);
if (A.size() != 2)
return 66;
if (B.size() != 3)
return 67;
if (A[0] != 4 || A[1] != 5)
return 68;
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
return 69;
}
CheckForMemoryLeaks();
return 0;
}

View file

@ -211,7 +211,11 @@
(dots (file-relative-name root dir)) ;; e.g. "../"
(file (file-relative-name this root)) ;; e.g. "libc/crc32c.c"
(name (file-name-sans-extension file)) ;; e.g. "libc/crc32c"
(buddy (format "test/%s_test.c" name))
(buddy (let ((c-version (format "test/%s_test.c" name))
(cc-version (format "test/%s_test.cc" name)))
(if (file-exists-p cc-version)
cc-version
c-version)))
(runs (format "o/$m/%s%s V=5 TESTARGS=-b" name runsuffix))
(buns (format "o/$m/test/%s_test%s V=5 TESTARGS=-b" name runsuffix)))
(cond ((not (member ext '("c" "cc" "cpp" "s" "S" "rl" "f" "cu")))
@ -761,8 +765,14 @@
(concat dots notest ".hookabi.c")
(concat dots notest ".h"))))
(t
(format "%stest/%s_test.c"
dots (cosmo-file-name-sans-extensions name))))))
(let ((c-version (format "%stest/%s_test.c"
dots (cosmo-file-name-sans-extensions name)))
(cc-version (format "%stest/%s_test.cc"
dots (cosmo-file-name-sans-extensions name))))
(if (file-exists-p cc-version)
cc-version
c-version))
))))
(when buddy
(find-file buddy))))))

View file

@ -5,23 +5,41 @@ import re
import sys
def GetDeps(path):
def Dive(path, depth, visited):
visited = set()
def Dive(path, inside, depth, that_isnt=None):
sys.stdout.write('%s%s' % ('\t' * depth, path))
if path in visited:
sys.stdout.write(' cycle\n')
return
sys.stdout.write('\n')
if path in visited:
return
visited.add(path)
if not os.path.exists(path):
if inside:
samedir = os.path.join(os.path.dirname(inside), path)
if inside and os.path.exists(samedir) and samedir != that_isnt:
path = samedir
elif os.path.exists('third_party/libcxx/' + path) and 'third_party/libcxx/' + path != that_isnt:
path = 'third_party/libcxx/' + path
elif os.path.exists('libc/isystem/' + path):
path = 'libc/isystem/' + path
else:
# sys.stderr.write('not found: %s\n' % (path))
return
with open(path) as f:
code = f.read()
for dep in re.findall(r'[.#]include "([^"]+)"', code):
Dive(dep, depth + 1, visited + [path])
Dive(path, 0, [])
sys.stdout.write('\n')
for dep in re.findall(r'[.#]\s*include\s+[<"]([^">]+)[">]', code):
Dive(dep, path, depth + 1)
for dep in re.findall(r'[.#]\s*include_next\s+[<"]([^">]+)[">]', code):
Dive(dep, path, depth + 1, path)
Dive(path, None, 0)
once = False
for arg in sys.argv[1:]:
if os.path.isdir(arg):
for dirpath, dirs, files in os.walk(arg):
for filepath in files:
if not once:
sys.stdout.write('\n')
once = True
GetDeps(os.path.join(dirpath, filepath))
else:
GetDeps(arg)