Skip to content

Commit

Permalink
This repo isn't C, more gitattributes. Also update Arlib and tweak ge…
Browse files Browse the repository at this point in the history
…nerator a little
  • Loading branch information
Alcaro committed Feb 24, 2020
1 parent f2f46ab commit b9fc026
Show file tree
Hide file tree
Showing 69 changed files with 1,287 additions and 596 deletions.
1 change: 1 addition & 0 deletions arlib.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#if defined(ARLIB_OPENGL) && defined(ARLIB_OPT)
#define AROPENGL_SLIM
#endif
Expand Down
3 changes: 3 additions & 0 deletions arlib/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
deps/** linguist-vendored
opengl/glsym-all.h linguist-generated
sandbox/bpf.inc linguist-generated
8 changes: 7 additions & 1 deletion arlib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ define LF

endef

ifeq ($(OS),Windows_NT)
OS = windows
endif
ifeq ($(OS),)
dumpmachine := $(shell $(CXX) -dumpmachine)
ifneq ($(findstring mingw,$(dumpmachine)),)
Expand Down Expand Up @@ -102,6 +105,8 @@ else
endif

CCXXFLAGS = -fvisibility=hidden -Wall -Wmissing-declarations -Wimplicit-fallthrough -Wvla -pipe
#to silence for (size_t n : range(42))
CCXXFLAGS += -Wno-unused-variable
#doesn't seem to do anything, and CFLAGS is used for c++ too which throws warnings all over
#CFLAGS += -Wmissing-prototypes
ifneq ($(EXCEPTIONS),1)
Expand Down Expand Up @@ -426,7 +431,7 @@ TRUE_CFLAGS := -std=c99 $(CCXXFLAGS) $(CONF_CFLAGS) $(CFLAGS)
# C++2a void foo(auto arg) (shorthand for template<typename T> void foo(T arg) - simplifies function::bind_member)
# C++17 <charconv> in libc (not libstdc++) - will speed up tostring(float), and probably simplify it too
# [[trivially_relocatable]] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1144r3.html
# Lambdas without braces/return; function<int(int)> foo = [](int i) => i*2; (useful for LINQ)
# Lambdas without braces/return; function<int(int)> mul2 = [](int i) => i*2; (useful for LINQ)
# Sufficient constexpr improvements to rewrite the regex parser into normal code, that template mess is terrible
# Zero-overhead deterministic exceptions http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf
# Compile-time reflection (that SERIALIZE macro is a little too limited, and the error messages are worse than from the regexes)
Expand All @@ -436,6 +441,7 @@ TRUE_CFLAGS := -std=c99 $(CCXXFLAGS) $(CONF_CFLAGS) $(CFLAGS)
# std::embed http://open-std.org/JTC1/SC22/WG21/docs/papers/2018/p1040r0.html, to get rid of rescompile.py's assembly shenanigans
# may allow some other optimizations/simplifications too
# Allow int x(int y=0); int x(int y=0) { return y*2; } (only if default value is identical on both ends, of course)
# Ranges TS - not because I want it as is, but because it'll get rid of the unused variable warning from for (size_t n : range(42)).
# The following may or may not be usable, depending on the exact details:
# C++2a coroutines, for anything involving the runloop
# C++2a modules (it's very easy to produce a bad module system, I don't know if this one avoids the pitfalls)
Expand Down
2 changes: 1 addition & 1 deletion arlib/arlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// - And, most importantly, every feature I implement is a feature I fully understand, so I can
// debug it, debug other instances of the same protocol or format, know which edge cases are
// likely to cause bugs (for example to write a test suites, or research potential security
// vulnerabilities), and appreciate the hidden complexity of something that seems simple.
// vulnerabilities), and appreciate the true complexity of something that seems simple.

//if anyone whines about antivirus, https://arstechnica.com/information-technology/2017/01/antivirus-is-bad/
// and linked:
Expand Down
2 changes: 1 addition & 1 deletion arlib/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ test("array", "", "array")
{
//passes if it does not leak memory
class glutton {
array<byte> food;
array<uint8_t> food;
public:
glutton() { food.resize(1000); }
};
Expand Down
22 changes: 19 additions & 3 deletions arlib/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,16 @@ inline array<T2> arrayview<T>::cast() const
template<typename T, size_t N> class sarray {
T storage[N];
public:
template<typename... Ts> sarray(Ts... args) : storage{ (T)args... }
{
static_assert(sizeof...(Ts) == N);
}
T& operator[](size_t n) { return storage[n]; }
//TODO: implement and use this
const T& operator[](size_t n) const { return storage[n]; }
operator arrayvieww<T>() { return storage; }
operator arrayview<T>() const { return storage; }
size_t size() const { return N; }
//TODO: implement more missing features
};


Expand Down Expand Up @@ -744,13 +752,16 @@ template<> class array<bool> {
template<typename T> class refarray {
array<autoptr<T>> items;
public:
explicit operator bool() const { return (bool)items; }

T& operator[](size_t n)
{
return *items[n];
}
T& append()
template<typename... Ts>
T& append(Ts... args)
{
T* ret = new T();
T* ret = new T(std::move(args)...);
items.append(ret);
return *ret;
}
Expand Down Expand Up @@ -785,3 +796,8 @@ class cstring;
extern template class array<cstring>;
class string;
extern template class array<string>;

// TODO: find proper names - I can't use bytes, lots of objects use that as a function name.
using bytesr = arrayview<uint8_t>;
using bytesw = arrayvieww<uint8_t>;
using bytearray = array<uint8_t>;
10 changes: 5 additions & 5 deletions arlib/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static bool l_isspace(char ch)
return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n');
}

size_t base64_dec_raw(arrayvieww<byte> out, size_t* outend, cstring text, size_t* textend)
size_t base64_dec_raw(arrayvieww<uint8_t> out, size_t* outend, cstring text, size_t* textend)
{
size_t outat = 0;
const char * ptr = (char*)text.bytes().ptr();
Expand Down Expand Up @@ -116,17 +116,17 @@ size_t base64_dec_raw(arrayvieww<byte> out, size_t* outend, cstring text, size_t
return 0;
}

array<byte> base64_dec(cstring text)
array<uint8_t> base64_dec(cstring text)
{
array<byte> ret;
array<uint8_t> ret;
ret.resize(base64_dec_len(text.length()));
size_t actual = base64_dec_raw(ret, NULL, text, NULL);
if (!actual) return NULL;
ret.resize(actual);
return ret;
}

void base64_enc_raw(arrayvieww<byte> out, arrayview<byte> bytes)
void base64_enc_raw(arrayvieww<uint8_t> out, arrayview<uint8_t> bytes)
{
uint8_t* outp = out.ptr();
const uint8_t* inp = bytes.ptr();
Expand Down Expand Up @@ -164,7 +164,7 @@ void base64_enc_raw(arrayvieww<byte> out, arrayview<byte> bytes)
}
}

string base64_enc(arrayview<byte> bytes)
string base64_enc(arrayview<uint8_t> bytes)
{
string ret;
base64_enc_raw(ret.construct(base64_enc_len(bytes.size())), bytes);
Expand Down
8 changes: 4 additions & 4 deletions arlib/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ inline size_t base64_enc_len(size_t len) { return (len+2)/3*4; }
// (or past end of text, if the entire thing was parsed)
//'out' must be at least base64_dec_len(text.length()) bytes; otherwise, undefined behavior
//'out' may overlap 'text' only if 'out' starts at least 8 bytes before 'text'
size_t base64_dec_raw(arrayvieww<byte> out, size_t* outend, cstring text, size_t* textend);
size_t base64_dec_raw(arrayvieww<uint8_t> out, size_t* outend, cstring text, size_t* textend);
//returns blank array if not fully valid
array<byte> base64_dec(cstring text);
array<uint8_t> base64_dec(cstring text);

//'out' must be at least base64_enc_len(text.length()) bytes; otherwise, undefined behavior
//'out' may not overlap 'text'
//can never fail
void base64_enc_raw(arrayvieww<byte> out, arrayview<byte> bytes);
string base64_enc(arrayview<byte> bytes);
void base64_enc_raw(arrayvieww<uint8_t> out, arrayview<uint8_t> bytes);
string base64_enc(arrayview<uint8_t> bytes);
6 changes: 3 additions & 3 deletions arlib/bmlwrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
inline string bmlwriter::indent()
{
string ret;
arrayvieww<byte> bytes = ret.construct(m_indent*2);
arrayvieww<uint8_t> bytes = ret.construct(m_indent*2);
memset(bytes.ptr(), ' ', m_indent*2);
return ret;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ string bmlwriter::escape(cstring val)
{
string esc = "-";
bool needescape = (val.startswith("-"));
for (byte c : val.bytes())
for (uint8_t c : val.bytes())
{
if (isalnum(c) || c=='.') esc+=c;
else if (c=='-') esc+="--";
Expand All @@ -108,7 +108,7 @@ string bmlwriter::unescape(cstring val)
{
if (val[i]=='-')
{
byte tmp;
uint8_t tmp;
if (val[i+1]=='-')
{
i++;
Expand Down
20 changes: 10 additions & 10 deletions arlib/bytepipe-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

static void push(bytepipe& p, size_t& t_written, size_t request, size_t push)
{
arrayvieww<byte> tmp = p.push_buf(request);
arrayvieww<uint8_t> tmp = p.push_buf(request);
assert(tmp.ptr() != NULL);
assert(tmp.size() >= request);
for (size_t i=0;i<push;i++)
Expand All @@ -15,7 +15,7 @@ static void push(bytepipe& p, size_t& t_written, size_t request, size_t push)

static void pull(bytepipe& p, size_t& t_read, size_t& t_written, bool all, size_t expect, size_t use)
{
arrayview<byte> tmp = (all ? p.pull_buf_full() : p.pull_buf());
arrayview<uint8_t> tmp = (all ? p.pull_buf_full() : p.pull_buf());
assert_eq(p.remaining(), t_written-t_read);
assert_eq(tmp.size(), expect);
for (size_t i=0;i<use;i++)
Expand Down Expand Up @@ -83,9 +83,9 @@ test("bytepipe", "array", "bytepipe")
{
for (int i=0;text[i];i++)
{
byte tmp[1] = { (byte)text[i] };
p.push(arrayview<byte>(tmp));
arrayview<byte> line = p.pull_line();
uint8_t tmp[1] = { (uint8_t)text[i] };
p.push(arrayview<uint8_t>(tmp));
arrayview<uint8_t> line = p.pull_line();
if (text[i]!='\n') assert(!line);
else
{
Expand All @@ -97,27 +97,27 @@ test("bytepipe", "array", "bytepipe")

for (int pass_inner=0;pass_inner<3;pass_inner++)
{
p.push(arrayview<byte>((byte*)text, strlen(text)));
arrayview<byte> line = p.pull_line();
p.push(arrayview<uint8_t>((uint8_t*)text, strlen(text)));
arrayview<uint8_t> line = p.pull_line();
assert_eq(string(line), text);
p.pull_done(line);
}

for (int pass_inner=0;pass_inner<3;pass_inner++)
{
p.push(arrayview<byte>((byte*)text, strlen(text)));
p.push(arrayview<uint8_t>((uint8_t*)text, strlen(text)));
}

for (int pass_inner=0;pass_inner<3;pass_inner++)
{
arrayview<byte> line = p.pull_line();
arrayview<uint8_t> line = p.pull_line();
assert_eq(string(line), text);
p.pull_done(line);
}

for (int pass_inner=0;pass_inner<3;pass_inner++)
{
arrayview<byte> line = p.pull_line();
arrayview<uint8_t> line = p.pull_line();
assert(!line);
}
}
Expand Down
34 changes: 17 additions & 17 deletions arlib/bytepipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
//A bytepipe accepts an infinite amount of bytes and returns them, first one first.
//Guaranteed amortized O(n) no matter how many bytes are pushed at the time, except if pull_line() is used and there is no line.
class bytepipe {
array<byte> buf1;
array<uint8_t> buf1;
size_t buf1st;
size_t buf1end;

array<byte> buf2;
array<uint8_t> buf2;
size_t buf2end;

void try_swap()
Expand All @@ -24,9 +24,9 @@ class bytepipe {
}
}

void push_one(arrayview<byte> bytes)
void push_one(arrayview<uint8_t> bytes)
{
arrayvieww<byte> tmp = push_buf(bytes.size());
arrayvieww<uint8_t> tmp = push_buf(bytes.size());
memcpy(tmp.ptr(), bytes.ptr(), bytes.size());
push_done(bytes.size());
}
Expand All @@ -43,7 +43,7 @@ class bytepipe {
}

//Will return a buffer of at least 'bytes' bytes. Can be bigger. Use push_done afterwards.
arrayvieww<byte> push_buf(size_t bytes = 512)
arrayvieww<uint8_t> push_buf(size_t bytes = 512)
{
if (buf2end + bytes > buf2.size())
{
Expand All @@ -69,26 +69,26 @@ class bytepipe {
}

//Can return less than remaining().
arrayview<byte> pull_buf()
arrayview<uint8_t> pull_buf()
{
try_swap();
return buf1.slice(buf1st, buf1end-buf1st);
}
//Returns whatever was pushed that pull_buf didn't return. Can't be acknowledged and discarded, use pull_buf.
arrayview<byte> pull_next()
arrayview<uint8_t> pull_next()
{
return buf2.slice(0, buf2end);
}
void pull_done(size_t bytes)
{
buf1st += bytes;
}
void pull_done(arrayview<byte> bytes)
void pull_done(arrayview<uint8_t> bytes)
{
pull_done(bytes.size());
}
//Returns the entire thing.
arrayview<byte> pull_buf_full()
arrayview<uint8_t> pull_buf_full()
{
if (buf1end+buf2end > buf1.size())
{
Expand All @@ -112,7 +112,7 @@ class bytepipe {
return buf1.slice(buf1st, buf1end-buf1st);
}
//Returns the entire thing, and immediately acknowledges it. Other than the return value, it's equivalent to reset().
array<byte> pull_buf_full_drain()
array<uint8_t> pull_buf_full_drain()
{
if (buf1st != 0)
{
Expand All @@ -131,25 +131,25 @@ class bytepipe {
buf1end += buf2end;
}

array<byte> ret = std::move(buf1);
array<uint8_t> ret = std::move(buf1);
ret.resize(buf1end);

reset();
return ret;
}

//Returns data until and including the next \n. Doesn't acknowledge it. If there is no \n, returns an empty array.
arrayview<byte> pull_line()
arrayview<uint8_t> pull_line()
{
byte* start = buf1.ptr()+buf1st;
uint8_t* start = buf1.ptr()+buf1st;
size_t len = buf1end-buf1st;
byte* nl = (byte*)memchr(start, '\n', len);
uint8_t* nl = (uint8_t*)memchr(start, '\n', len);
if (nl)
{
return arrayview<byte>(start, nl+1-start);
return arrayview<uint8_t>(start, nl+1-start);
}

nl = (byte*)memchr(buf2.ptr(), '\n', buf2end);
nl = (uint8_t*)memchr(buf2.ptr(), '\n', buf2end);
if (nl)
{
size_t pos = buf1end-buf1st + nl+1-buf2.ptr();
Expand All @@ -160,7 +160,7 @@ class bytepipe {
}
//Returns 'line' minus a trailing \r\n or \n. The \n must exist.
//Usable together with the above, though you must acknowledge the \n too.
static arrayview<byte> trim_line(arrayview<byte> line)
static arrayview<uint8_t> trim_line(arrayview<uint8_t> line)
{
if (line.size()==1) return NULL;
if (line[line.size()-2]=='\r') return line.slice(0, line.size()-2);
Expand Down
10 changes: 10 additions & 0 deletions arlib/bytestream-test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "bytestream.h"
#include "test.h"

test("bytestream", "array", "bytestream")
{
uint8_t foo[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
bytestream b(foo);
assert(!b.signature(1, 2, 3, 5));
assert(b.signature(1, 2, 3, 4));
}
Loading

0 comments on commit b9fc026

Please sign in to comment.