Reduce dependence on STL.

This commit is contained in:
Andreas Kling 2018-10-16 12:10:01 +02:00
parent 0c1a4e8de3
commit fd708a4cb1
Notes: sideshowbarker 2024-07-19 18:47:32 +09:00
8 changed files with 48 additions and 34 deletions

View file

@ -2,9 +2,7 @@
#include "Buffer.h"
#include "Types.h"
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include "StdLib.h"
namespace AK {
@ -17,13 +15,13 @@ public:
{
}
ByteBuffer(ByteBuffer&& other)
: m_impl(std::move(other.m_impl))
: m_impl(move(other.m_impl))
{
}
ByteBuffer& operator=(ByteBuffer&& other)
{
if (this != &other)
m_impl = std::move(other.m_impl);
m_impl = move(other.m_impl);
return *this;
}
@ -73,7 +71,7 @@ public:
private:
explicit ByteBuffer(RetainPtr<Buffer<byte>>&& impl)
: m_impl(std::move(impl))
: m_impl(move(impl))
{
}

View file

@ -1,7 +1,8 @@
#pragma once
#include <cstddef>
#include <utility>
namespace std {
typedef decltype(nullptr) nullptr_t;
}
namespace AK {

View file

@ -24,6 +24,11 @@ static inline T ceilDiv(T a, T b)
return result;
}
template <typename T>
T&& move(T& arg)
{
return static_cast<T&&>(arg);
}
}

View file

@ -17,12 +17,12 @@ StringImpl::~StringImpl()
{
}
static inline size_t allocationSizeForStringImpl(unsigned length)
static inline size_t allocationSizeForStringImpl(size_t length)
{
return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char);
}
RetainPtr<StringImpl> StringImpl::createUninitialized(unsigned length, char*& buffer)
RetainPtr<StringImpl> StringImpl::createUninitialized(size_t length, char*& buffer)
{
if (!length)
return theEmptyStringImpl();
@ -89,7 +89,7 @@ RetainPtr<StringImpl> StringImpl::toLowercase() const
if (!m_length)
return const_cast<StringImpl*>(this);
for (unsigned i = 0; i < m_length; ++i) {
for (size_t i = 0; i < m_length; ++i) {
if (!isASCIILowercase(m_characters[i]))
goto slowPath;
}
@ -98,9 +98,8 @@ RetainPtr<StringImpl> StringImpl::toLowercase() const
slowPath:
char* buffer;
auto lowercased = createUninitialized(m_length, buffer);
for (unsigned i = 0; i < m_length; ++i) {
for (size_t i = 0; i < m_length; ++i)
buffer[i] = toASCIILowercase(m_characters[i]);
}
return lowercased;
}
@ -110,7 +109,7 @@ RetainPtr<StringImpl> StringImpl::toUppercase() const
if (!m_length)
return const_cast<StringImpl*>(this);
for (unsigned i = 0; i < m_length; ++i) {
for (size_t i = 0; i < m_length; ++i) {
if (!isASCIIUppercase(m_characters[i]))
goto slowPath;
}
@ -119,9 +118,8 @@ RetainPtr<StringImpl> StringImpl::toUppercase() const
slowPath:
char* buffer;
auto uppercased = createUninitialized(m_length, buffer);
for (unsigned i = 0; i < m_length; ++i) {
for (size_t i = 0; i < m_length; ++i)
buffer[i] = toASCIIUppercase(m_characters[i]);
}
return uppercased;
}
@ -132,7 +130,7 @@ void StringImpl::computeHash() const
m_hash = 0;
} else {
unsigned hash = 0;
for (unsigned i = 0; i < m_length; ++i) {
for (size_t i = 0; i < m_length; ++i) {
hash += m_characters[i];
hash += (hash << 10);
hash ^= (hash >> 6);

View file

@ -2,12 +2,13 @@
#include "Retainable.h"
#include "RetainPtr.h"
#include "Types.h"
namespace AK {
class StringImpl : public Retainable<StringImpl> {
public:
static RetainPtr<StringImpl> createUninitialized(unsigned length, char*& buffer);
static RetainPtr<StringImpl> createUninitialized(size_t length, char*& buffer);
static RetainPtr<StringImpl> create(const char* cstring);
static RetainPtr<StringImpl> create(const char* cstring, size_t length);
RetainPtr<StringImpl> toLowercase() const;
@ -17,9 +18,9 @@ public:
~StringImpl();
unsigned length() const { return m_length; }
size_t length() const { return m_length; }
const char* characters() const { return m_characters; }
char operator[](unsigned i) const { ASSERT(i < m_length); return m_characters[i]; }
char operator[](size_t i) const { ASSERT(i < m_length); return m_characters[i]; }
unsigned hash() const
{
@ -33,11 +34,11 @@ private:
explicit StringImpl(ConstructTheEmptyStringImplTag) : m_characters("") { }
enum ConstructWithInlineBufferTag { ConstructWithInlineBuffer };
explicit StringImpl(ConstructWithInlineBufferTag, unsigned length) : m_length(length), m_characters(m_inlineBuffer) { }
explicit StringImpl(ConstructWithInlineBufferTag, size_t length) : m_length(length), m_characters(m_inlineBuffer) { }
void computeHash() const;
unsigned m_length { 0 };
size_t m_length { 0 };
bool m_ownsBuffer { true };
mutable bool m_hasHash { false };
const char* m_characters { nullptr };

View file

@ -1,6 +1,10 @@
#pragma once
#ifdef SERENITY_KERNEL
#else
#include <stdint.h>
#include <sys/types.h>
#endif
typedef uint8_t byte;
typedef uint16_t word;
@ -15,3 +19,8 @@ typedef int64_t signed_qword;
constexpr unsigned KB = 1024;
constexpr unsigned MB = KB * KB;
constexpr unsigned GB = KB * KB * KB;
namespace std {
typedef decltype(nullptr) nullptr_t;
}

View file

@ -4,9 +4,6 @@
#include "OwnPtr.h"
#include "kmalloc.h"
#include <new>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
namespace AK {
@ -34,7 +31,7 @@ public:
ASSERT(index < m_size);
at(index).~T();
for (unsigned i = index + 1; i < m_size; ++i) {
new (slot(i - 1)) T(std::move(at(i)));
new (slot(i - 1)) T(move(at(i)));
at(i).~T();
}
@ -63,14 +60,14 @@ public:
~Vector() { clear(); }
Vector(Vector&& other)
: m_impl(std::move(other.m_impl))
: m_impl(move(other.m_impl))
{
}
Vector& operator=(Vector&& other)
{
if (this != &other)
m_impl = std::move(other.m_impl);
m_impl = move(other.m_impl);
return *this;
}
@ -101,7 +98,7 @@ public:
T takeLast()
{
ASSERT(!isEmpty());
T value = std::move(last());
T value = move(last());
last().~T();
--m_impl->m_size;
return value;
@ -115,7 +112,7 @@ public:
void append(T&& value)
{
ensureCapacity(size() + 1);
new (m_impl->slot(m_impl->m_size)) T(std::move(value));
new (m_impl->slot(m_impl->m_size)) T(move(value));
++m_impl->m_size;
}
@ -135,11 +132,11 @@ public:
if (m_impl) {
newImpl->m_size = m_impl->m_size;
for (unsigned i = 0; i < size(); ++i) {
new (newImpl->slot(i)) T(std::move(m_impl->at(i)));
new (newImpl->slot(i)) T(move(m_impl->at(i)));
m_impl->at(i).~T();
}
}
m_impl = std::move(newImpl);
m_impl = move(newImpl);
}
class Iterator {
@ -175,7 +172,7 @@ public:
private:
static unsigned paddedCapacity(unsigned capacity)
{
return std::max(4u, capacity + (capacity / 4) + 4);
return max(4u, capacity + (capacity / 4) + 4);
}
OwnPtr<VectorImpl<T>> m_impl;

View file

@ -20,7 +20,12 @@ int main(int, char**)
for (auto& part : parts)
printf("<%s>\n", part.characters());
}
{
String cmd = "cd";
auto parts = cmd.split(' ');
for (auto& part : parts)
printf("<%s>\n", part.characters());
}
String empty = "";