1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:17:34 +00:00

Reduce dependence on STL.

This commit is contained in:
Andreas Kling 2018-10-16 12:10:01 +02:00
parent 0c1a4e8de3
commit fd708a4cb1
8 changed files with 48 additions and 34 deletions

View file

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

View file

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

View file

@ -24,6 +24,11 @@ static inline T ceilDiv(T a, T b)
return result; 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); 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) if (!length)
return theEmptyStringImpl(); return theEmptyStringImpl();
@ -89,7 +89,7 @@ RetainPtr<StringImpl> StringImpl::toLowercase() const
if (!m_length) if (!m_length)
return const_cast<StringImpl*>(this); 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])) if (!isASCIILowercase(m_characters[i]))
goto slowPath; goto slowPath;
} }
@ -98,9 +98,8 @@ RetainPtr<StringImpl> StringImpl::toLowercase() const
slowPath: slowPath:
char* buffer; char* buffer;
auto lowercased = createUninitialized(m_length, 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]); buffer[i] = toASCIILowercase(m_characters[i]);
}
return lowercased; return lowercased;
} }
@ -110,7 +109,7 @@ RetainPtr<StringImpl> StringImpl::toUppercase() const
if (!m_length) if (!m_length)
return const_cast<StringImpl*>(this); 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])) if (!isASCIIUppercase(m_characters[i]))
goto slowPath; goto slowPath;
} }
@ -119,9 +118,8 @@ RetainPtr<StringImpl> StringImpl::toUppercase() const
slowPath: slowPath:
char* buffer; char* buffer;
auto uppercased = createUninitialized(m_length, 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]); buffer[i] = toASCIIUppercase(m_characters[i]);
}
return uppercased; return uppercased;
} }
@ -132,7 +130,7 @@ void StringImpl::computeHash() const
m_hash = 0; m_hash = 0;
} else { } else {
unsigned hash = 0; 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 += m_characters[i];
hash += (hash << 10); hash += (hash << 10);
hash ^= (hash >> 6); hash ^= (hash >> 6);

View file

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

View file

@ -1,6 +1,10 @@
#pragma once #pragma once
#ifdef SERENITY_KERNEL
#else
#include <stdint.h> #include <stdint.h>
#include <sys/types.h>
#endif
typedef uint8_t byte; typedef uint8_t byte;
typedef uint16_t word; typedef uint16_t word;
@ -15,3 +19,8 @@ typedef int64_t signed_qword;
constexpr unsigned KB = 1024; constexpr unsigned KB = 1024;
constexpr unsigned MB = KB * KB; constexpr unsigned MB = KB * KB;
constexpr unsigned GB = KB * 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 "OwnPtr.h"
#include "kmalloc.h" #include "kmalloc.h"
#include <new> #include <new>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
namespace AK { namespace AK {
@ -34,7 +31,7 @@ public:
ASSERT(index < m_size); ASSERT(index < m_size);
at(index).~T(); at(index).~T();
for (unsigned i = index + 1; i < m_size; ++i) { 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(); at(i).~T();
} }
@ -63,14 +60,14 @@ public:
~Vector() { clear(); } ~Vector() { clear(); }
Vector(Vector&& other) Vector(Vector&& other)
: m_impl(std::move(other.m_impl)) : m_impl(move(other.m_impl))
{ {
} }
Vector& operator=(Vector&& other) Vector& operator=(Vector&& other)
{ {
if (this != &other) if (this != &other)
m_impl = std::move(other.m_impl); m_impl = move(other.m_impl);
return *this; return *this;
} }
@ -101,7 +98,7 @@ public:
T takeLast() T takeLast()
{ {
ASSERT(!isEmpty()); ASSERT(!isEmpty());
T value = std::move(last()); T value = move(last());
last().~T(); last().~T();
--m_impl->m_size; --m_impl->m_size;
return value; return value;
@ -115,7 +112,7 @@ public:
void append(T&& value) void append(T&& value)
{ {
ensureCapacity(size() + 1); 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; ++m_impl->m_size;
} }
@ -135,11 +132,11 @@ public:
if (m_impl) { if (m_impl) {
newImpl->m_size = m_impl->m_size; newImpl->m_size = m_impl->m_size;
for (unsigned i = 0; i < size(); ++i) { 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->at(i).~T();
} }
} }
m_impl = std::move(newImpl); m_impl = move(newImpl);
} }
class Iterator { class Iterator {
@ -175,7 +172,7 @@ public:
private: private:
static unsigned paddedCapacity(unsigned capacity) 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; OwnPtr<VectorImpl<T>> m_impl;

View file

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