Also added a pretty much untested and totally incomplete STL string type.
This commit is contained in:
parent
ff3214fa5a
commit
219a48c616
@ -8,7 +8,7 @@ env['CXX'] = 'i686-elf-g++'
|
||||
env['LD'] = 'i686-elf-g++'
|
||||
env.Append(CXXFLAGS = ['-ffreestanding', '-fno-exceptions', '-fno-rtti', '-std=c++20'])
|
||||
env.Append(LINKFLAGS = ['-T', 'linker.ld', '-ffreestanding', '-nostdlib'])
|
||||
env.Append(CPPPATH = ['#include', '#stdlib/include'])
|
||||
env.Append(CPPPATH = ['#include', '#bastl/include'])
|
||||
env.Append(CCFLAGS = ['-g', '-O0'])
|
||||
|
||||
def get_crt_object(name: str) -> str:
|
||||
@ -36,8 +36,8 @@ os_sources = Split('''
|
||||
src/cstdlib/stdlib.cpp
|
||||
src/cstdlib/string.cpp
|
||||
|
||||
stdlib/src/exception.cpp
|
||||
stdlib/src/new.cpp
|
||||
bastl/src/exception.cpp
|
||||
bastl/src/new.cpp
|
||||
''')
|
||||
env['LINKCOM'] = env['LINKCOM'].replace('$_LIBFLAGS', f'{crti_o[0]} {crtbegin_o} $_LIBFLAGS {crtend_o} {crtn_o[0]}')
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
namespace std
|
||||
{
|
||||
using ::nullptr_t;
|
||||
using ::ptrdiff_t;
|
||||
using ::size_t;
|
||||
}
|
125
bastl/include/string
Normal file
125
bastl/include/string
Normal file
@ -0,0 +1,125 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(BAD_APPLE_OS_STRING_INCLUDED)
|
||||
#define BAD_APPLE_OS_STRING_INCLUDED
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace std
|
||||
{
|
||||
template<typename CharT>
|
||||
class char_traits {};
|
||||
|
||||
template<>
|
||||
class char_traits<char>
|
||||
{
|
||||
public:
|
||||
using char_type = char;
|
||||
using int_type = int;
|
||||
using off_type = std::ptrdiff_t; // TODO: should be std::streamoff?
|
||||
// TODO: ??
|
||||
};
|
||||
|
||||
template<typename CharT, typename Traits = char_traits<CharT>> // TODO: Allocator
|
||||
class basic_string
|
||||
{
|
||||
public:
|
||||
using traits_type = Traits;
|
||||
using value_type = CharT;
|
||||
using size_type = size_t;
|
||||
using difference_type = ptrdiff_t;
|
||||
using reference = value_type&;
|
||||
using const_reference = const value_type&;
|
||||
using pointer = value_type*;
|
||||
using const_pointer = const value_type*;
|
||||
using iterator = pointer;
|
||||
using const_iterator = const_pointer;
|
||||
private:
|
||||
vector<value_type> _data;
|
||||
public:
|
||||
constexpr basic_string() noexcept : basic_string("") {}
|
||||
constexpr basic_string(const basic_string& other) = default;
|
||||
constexpr basic_string(basic_string&& other) = default;
|
||||
constexpr basic_string(size_type count, value_type chr) : _data(count, chr) {}
|
||||
constexpr basic_string(const basic_string& other, size_type pos, size_type count = npos)
|
||||
: basic_string(other.substr(pos, count)) {}
|
||||
constexpr basic_string(const value_type* str, size_type count)
|
||||
{
|
||||
_data.resize(count + 1);
|
||||
for (size_type idx = 0; idx < count; ++idx) {
|
||||
_data[idx] = str[idx];
|
||||
}
|
||||
_data[count] = value_type(0);
|
||||
}
|
||||
constexpr basic_string(const value_type* str) : basic_string(str, strlen(str)) {} // NOLINT
|
||||
// TODO: all the other constructors
|
||||
constexpr basic_string& operator=(const basic_string& other) = default;
|
||||
constexpr basic_string& operator=(basic_string&& other) = default;
|
||||
constexpr basic_string& operator=(const value_type* str)
|
||||
{
|
||||
const size_t count = strlen(str);
|
||||
_data.resize(count + 1);
|
||||
for (size_type idx = 0; idx < count; ++idx) {
|
||||
_data[idx] = str[idx];
|
||||
}
|
||||
_data[count] = value_type(0);
|
||||
return *this;
|
||||
}
|
||||
constexpr basic_string& operator=(value_type chr)
|
||||
{
|
||||
_data.resize(2);
|
||||
_data[0] = chr;
|
||||
_data[1] = value_type(0);
|
||||
return *this;
|
||||
}
|
||||
constexpr basic_string& operator=(std::nullptr_t) = delete;
|
||||
|
||||
[[nodiscard]] constexpr reference operator[](size_type pos) noexcept { return _data[pos]; }
|
||||
[[nodiscard]] constexpr const_reference operator[](size_type pos) const noexcept { return _data[pos]; }
|
||||
|
||||
// TODO: the range check should actually also fail for the \0 in the end
|
||||
[[nodiscard]] constexpr reference at(size_type pos) { return _data.at(pos); }
|
||||
[[nodiscard]] constexpr const_reference at(size_type pos) const { return _data.at(pos); }
|
||||
|
||||
[[nodiscard]] constexpr reference front() noexcept { return _data.front(); }
|
||||
[[nodiscard]] constexpr const_reference front() const noexcept { return _data.front(); }
|
||||
|
||||
[[nodiscard]] constexpr reference back() noexcept { return _data.back(); }
|
||||
[[nodiscard]] constexpr const_reference back() const noexcept { return _data.back(); }
|
||||
|
||||
[[nodiscard]] constexpr pointer data() noexcept { return _data.data(); }
|
||||
[[nodiscard]] constexpr const_pointer data() const noexcept { return _data.data(); }
|
||||
|
||||
[[nodiscard]] constexpr const_pointer c_str() const noexcept { return data(); }
|
||||
|
||||
[[nodiscard]] constexpr iterator begin() noexcept { return _data.begin(); }
|
||||
[[nodiscard]] constexpr const_iterator begin() const noexcept { return _data.begin(); }
|
||||
[[nodiscard]] constexpr const_iterator cbegin() const noexcept { return _data.begin(); }
|
||||
[[nodiscard]] constexpr iterator end() noexcept { return _data.end() - 1; }
|
||||
[[nodiscard]] constexpr const_iterator end() const noexcept { return _data.end() - 1; }
|
||||
[[nodiscard]] constexpr const_iterator cend() const noexcept { return _data.end() - 1; }
|
||||
|
||||
[[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
|
||||
[[nodiscard]] constexpr size_type size() const noexcept { return _data.size() - 1; }
|
||||
[[nodiscard]] constexpr size_type max_size() const noexcept { return _data.max_size() - 1; }
|
||||
constexpr void reserve(size_type newCapacity) noexcept { _data.reserve(newCapacity + 1); }
|
||||
[[nodiscard]] constexpr size_type capacity() const noexcept { return _data.capacity() - 1; }
|
||||
constexpr void shrink_to_fit() noexcept { _data.shrink_to_fit(); }
|
||||
|
||||
constexpr void clear() { *this = ""; }
|
||||
constexpr void resize(size_type count, value_type chr = value_type())
|
||||
{
|
||||
_data.resize(count + 1, chr);
|
||||
_data.back() = value_type(0);
|
||||
}
|
||||
|
||||
static inline size_type npos = size_type(-1);
|
||||
};
|
||||
using string = basic_string<char>;
|
||||
}
|
||||
|
||||
#endif // !defined(BAD_APPLE_OS_STRING_INCLUDED)
|
@ -44,6 +44,10 @@ public:
|
||||
_capacity(exchange(other._capacity, 0))
|
||||
{
|
||||
}
|
||||
constexpr explicit vector(size_type count, const value_type& value = value_type())
|
||||
{
|
||||
resize(count, value);
|
||||
}
|
||||
constexpr ~vector() noexcept
|
||||
{
|
||||
delete _elements;
|
||||
@ -72,12 +76,12 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr reference operator[](size_type pos)
|
||||
[[nodiscard]] constexpr reference operator[](size_type pos) noexcept
|
||||
{
|
||||
return _elements[pos];
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr const_reference operator[](size_type pos) const
|
||||
[[nodiscard]] constexpr const_reference operator[](size_type pos) const noexcept
|
||||
{
|
||||
return _elements[pos];
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
#if !defined(BAD_APPLE_OS_STDIO_H_INCLUDED)
|
||||
#define BAD_APPLE_OS_STDIO_H_INCLUDED
|
||||
|
||||
#include "../stdlib/include/cstdarg"
|
||||
#include <stdarg.h>
|
||||
#include "./detail/common.h"
|
||||
|
||||
BA_EXTERN_C_BEGIN
|
||||
@ -12,7 +12,7 @@ BA_EXTERN_C_BEGIN
|
||||
int putchar(int chr) BA_CXX_NOEXCEPT;
|
||||
int puts(const char* str) BA_CXX_NOEXCEPT;
|
||||
int printf(const char* format, ...) BA_CXX_NOEXCEPT;
|
||||
int vprintf(const char* format, std::va_list vlist) BA_CXX_NOEXCEPT;
|
||||
int vprintf(const char* format, va_list vlist) BA_CXX_NOEXCEPT;
|
||||
|
||||
BA_EXTERN_C_END
|
||||
|
||||
|
@ -1,8 +1,4 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
extern "C" void main()
|
||||
{
|
||||
std::vector<int> someInts;
|
||||
someInts.resize(100);
|
||||
}
|
||||
|
@ -150,7 +150,7 @@ int printf(const char* format, ...) noexcept
|
||||
return result;
|
||||
}
|
||||
|
||||
int vprintf(const char* format, std::va_list vlist) BA_CXX_NOEXCEPT
|
||||
int vprintf(const char* format, va_list vlist) BA_CXX_NOEXCEPT
|
||||
{
|
||||
int result = 0;
|
||||
const char* pos = format;
|
||||
|
Loading…
x
Reference in New Issue
Block a user