AK: Massage it into building on my host system without breaking Serenity.

This commit is contained in:
Andreas Kling 2019-06-14 06:43:56 +02:00
parent b7cca76ca2
commit 255c7562ba
Notes: sideshowbarker 2024-07-19 13:37:12 +09:00
8 changed files with 54 additions and 42 deletions

View File

@ -59,7 +59,7 @@ public:
{ {
} }
String(const char* cstring, ssize_t length, ShouldChomp shouldChomp = NoChomp) String(const char* cstring, int length, ShouldChomp shouldChomp = NoChomp)
: m_impl(StringImpl::create(cstring, length, shouldChomp)) : m_impl(StringImpl::create(cstring, length, shouldChomp))
{ {
} }
@ -118,9 +118,9 @@ public:
bool is_null() const { return !m_impl; } bool is_null() const { return !m_impl; }
bool is_empty() const { return length() == 0; } bool is_empty() const { return length() == 0; }
ssize_t length() const { return m_impl ? m_impl->length() : 0; } int length() const { return m_impl ? m_impl->length() : 0; }
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; } const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
char operator[](ssize_t i) const char operator[](int i) const
{ {
ASSERT(m_impl); ASSERT(m_impl);
return (*m_impl)[i]; return (*m_impl)[i];

View File

@ -100,12 +100,12 @@ public:
return *this; return *this;
} }
static ByteBuffer create_uninitialized(ssize_t size) { return ByteBuffer(ByteBufferImpl::create_uninitialized(size)); } static ByteBuffer create_uninitialized(int size) { return ByteBuffer(ByteBufferImpl::create_uninitialized(size)); }
static ByteBuffer create_zeroed(ssize_t size) { return ByteBuffer(ByteBufferImpl::create_zeroed(size)); } static ByteBuffer create_zeroed(int size) { return ByteBuffer(ByteBufferImpl::create_zeroed(size)); }
static ByteBuffer copy(const void* data, ssize_t size) { return ByteBuffer(ByteBufferImpl::copy(data, size)); } static ByteBuffer copy(const void* data, int size) { return ByteBuffer(ByteBufferImpl::copy(data, size)); }
static ByteBuffer wrap(const void* data, ssize_t size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); } static ByteBuffer wrap(const void* data, int size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); }
static ByteBuffer wrap(void* data, ssize_t size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); } static ByteBuffer wrap(void* data, int size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); }
static ByteBuffer adopt(void* data, ssize_t size) { return ByteBuffer(ByteBufferImpl::adopt(data, size)); } static ByteBuffer adopt(void* data, int size) { return ByteBuffer(ByteBufferImpl::adopt(data, size)); }
~ByteBuffer() { clear(); } ~ByteBuffer() { clear(); }
void clear() { m_impl = nullptr; } void clear() { m_impl = nullptr; }
@ -114,18 +114,18 @@ public:
bool operator!() const { return is_null(); } bool operator!() const { return is_null(); }
bool is_null() const { return m_impl == nullptr; } bool is_null() const { return m_impl == nullptr; }
byte& operator[](ssize_t i) byte& operator[](int i)
{ {
ASSERT(m_impl); ASSERT(m_impl);
return (*m_impl)[i]; return (*m_impl)[i];
} }
byte operator[](ssize_t i) const byte operator[](int i) const
{ {
ASSERT(m_impl); ASSERT(m_impl);
return (*m_impl)[i]; return (*m_impl)[i];
} }
bool is_empty() const { return !m_impl || m_impl->is_empty(); } bool is_empty() const { return !m_impl || m_impl->is_empty(); }
ssize_t size() const { return m_impl ? m_impl->size() : 0; } int size() const { return m_impl ? m_impl->size() : 0; }
byte* data() { return pointer(); } byte* data() { return pointer(); }
const byte* data() const { return pointer(); } const byte* data() const { return pointer(); }
@ -133,8 +133,8 @@ public:
byte* pointer() { return m_impl ? m_impl->pointer() : nullptr; } byte* pointer() { return m_impl ? m_impl->pointer() : nullptr; }
const byte* pointer() const { return m_impl ? m_impl->pointer() : nullptr; } const byte* pointer() const { return m_impl ? m_impl->pointer() : nullptr; }
byte* offset_pointer(ssize_t offset) { return m_impl ? m_impl->offset_pointer(offset) : nullptr; } byte* offset_pointer(int offset) { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
const byte* offset_pointer(ssize_t offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; } const byte* offset_pointer(int offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
void* end_pointer() { return m_impl ? m_impl->end_pointer() : nullptr; } void* end_pointer() { return m_impl ? m_impl->end_pointer() : nullptr; }
const void* end_pointer() const { return m_impl ? m_impl->end_pointer() : nullptr; } const void* end_pointer() const { return m_impl ? m_impl->end_pointer() : nullptr; }
@ -147,13 +147,13 @@ public:
} }
// NOTE: trim() does not reallocate. // NOTE: trim() does not reallocate.
void trim(ssize_t size) void trim(int size)
{ {
if (m_impl) if (m_impl)
m_impl->trim(size); m_impl->trim(size);
} }
ByteBuffer slice(ssize_t offset, ssize_t size) const ByteBuffer slice(int offset, int size) const
{ {
if (is_null()) if (is_null())
return {}; return {};
@ -164,7 +164,7 @@ public:
return copy(offset_pointer(offset), size); return copy(offset_pointer(offset), size);
} }
void grow(ssize_t size) void grow(int size)
{ {
if (!m_impl) if (!m_impl)
m_impl = ByteBufferImpl::create_uninitialized(size); m_impl = ByteBufferImpl::create_uninitialized(size);
@ -204,7 +204,7 @@ inline ByteBufferImpl::ByteBufferImpl(const void* data, int size, ConstructionMo
m_owned = true; m_owned = true;
} }
inline ByteBufferImpl::ByteBufferImpl(void* data, ssize_t size, ConstructionMode mode) inline ByteBufferImpl::ByteBufferImpl(void* data, int size, ConstructionMode mode)
: m_data(static_cast<byte*>(data)) : m_data(static_cast<byte*>(data))
, m_size(size) , m_size(size)
{ {
@ -215,7 +215,7 @@ inline ByteBufferImpl::ByteBufferImpl(void* data, ssize_t size, ConstructionMode
} }
} }
inline void ByteBufferImpl::grow(ssize_t size) inline void ByteBufferImpl::grow(int size)
{ {
ASSERT(size > m_size); ASSERT(size > m_size);
ASSERT(m_owned); ASSERT(m_owned);

View File

@ -1,11 +1,15 @@
#pragma once #pragma once
#include <AK/Types.h> #include <AK/Types.h>
#include <LibC/stdarg.h> #include <stdarg.h>
static constexpr const char* printf_hex_digits = "0123456789abcdef"; static constexpr const char* printf_hex_digits = "0123456789abcdef";
#ifdef __serenity__
extern "C" size_t strlen(const char*); extern "C" size_t strlen(const char*);
#else
#include <string.h>
#endif
template<typename PutChFunc, typename T> template<typename PutChFunc, typename T>
[[gnu::always_inline]] inline int print_hex(PutChFunc putch, char*& bufptr, T number, byte fields) [[gnu::always_inline]] inline int print_hex(PutChFunc putch, char*& bufptr, T number, byte fields)
@ -174,7 +178,7 @@ template<typename PutChFunc>
} }
template<typename PutChFunc> template<typename PutChFunc>
[[gnu::always_inline]] inline int printf_internal(PutChFunc putch, char* buffer, const char*& fmt, char*& ap) [[gnu::always_inline]] inline int printf_internal(PutChFunc putch, char* buffer, const char*& fmt, va_list ap)
{ {
const char* p; const char* p;

View File

@ -1,7 +1,7 @@
#include "AKString.h" #include "AKString.h"
#include "StdLibExtras.h" #include "StdLibExtras.h"
#include "StringBuilder.h" #include "StringBuilder.h"
#include <LibC/stdarg.h> #include <stdarg.h>
namespace AK { namespace AK {
@ -78,17 +78,17 @@ Vector<String> String::split_limit(const char separator, int limit) const
return {}; return {};
Vector<String> v; Vector<String> v;
ssize_t substart = 0; int substart = 0;
for (ssize_t i = 0; i < length() && (v.size() + 1) != limit; ++i) { for (int i = 0; i < length() && (v.size() + 1) != limit; ++i) {
char ch = characters()[i]; char ch = characters()[i];
if (ch == separator) { if (ch == separator) {
ssize_t sublen = i - substart; int sublen = i - substart;
if (sublen != 0) if (sublen != 0)
v.append(substring(substart, sublen)); v.append(substring(substart, sublen));
substart = i + 1; substart = i + 1;
} }
} }
ssize_t taillen = length() - substart; int taillen = length() - substart;
if (taillen != 0) if (taillen != 0)
v.append(substring(substart, taillen)); v.append(substring(substart, taillen));
if (characters()[length() - 1] == separator) if (characters()[length() - 1] == separator)
@ -102,17 +102,17 @@ Vector<StringView> String::split_view(const char separator) const
return {}; return {};
Vector<StringView> v; Vector<StringView> v;
ssize_t substart = 0; int substart = 0;
for (ssize_t i = 0; i < length(); ++i) { for (int i = 0; i < length(); ++i) {
char ch = characters()[i]; char ch = characters()[i];
if (ch == separator) { if (ch == separator) {
ssize_t sublen = i - substart; int sublen = i - substart;
if (sublen != 0) if (sublen != 0)
v.append(substring_view(substart, sublen)); v.append(substring_view(substart, sublen));
substart = i + 1; substart = i + 1;
} }
} }
ssize_t taillen = length() - substart; int taillen = length() - substart;
if (taillen != 0) if (taillen != 0)
v.append(substring_view(substart, taillen)); v.append(substring_view(substart, taillen));
if (characters()[length() - 1] == separator) if (characters()[length() - 1] == separator)
@ -131,7 +131,7 @@ int String::to_int(bool& ok) const
{ {
bool negative = false; bool negative = false;
int value = 0; int value = 0;
ssize_t i = 0; int i = 0;
if (is_null()) { if (is_null()) {
ok = false; ok = false;
@ -158,7 +158,7 @@ int String::to_int(bool& ok) const
unsigned String::to_uint(bool& ok) const unsigned String::to_uint(bool& ok) const
{ {
unsigned value = 0; unsigned value = 0;
for (ssize_t i = 0; i < length(); ++i) { for (int i = 0; i < length(); ++i) {
if (characters()[i] < '0' || characters()[i] > '9') { if (characters()[i] < '0' || characters()[i] > '9') {
ok = false; ok = false;
return 0; return 0;

View File

@ -1,17 +1,17 @@
#include <AK/PrintfImplementation.h> #include <AK/PrintfImplementation.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibC/stdarg.h> #include <stdarg.h>
namespace AK { namespace AK {
inline void StringBuilder::will_append(ssize_t size) inline void StringBuilder::will_append(int size)
{ {
if ((m_length + size) > m_buffer.size()) if ((m_length + size) > m_buffer.size())
m_buffer.grow(max((ssize_t)16, m_buffer.size() * 2 + size)); m_buffer.grow(max((int)16, m_buffer.size() * 2 + size));
} }
StringBuilder::StringBuilder(ssize_t initial_capacity) StringBuilder::StringBuilder(int initial_capacity)
{ {
m_buffer.grow(initial_capacity); m_buffer.grow(initial_capacity);
} }
@ -25,7 +25,7 @@ void StringBuilder::append(const StringView& str)
m_length += str.length(); m_length += str.length();
} }
void StringBuilder::append(const char* characters, ssize_t length) void StringBuilder::append(const char* characters, int length)
{ {
if (!length) if (!length)
return; return;

View File

@ -2,18 +2,18 @@
#include "AKString.h" #include "AKString.h"
#include "Vector.h" #include "Vector.h"
#include <LibC/stdarg.h> #include <stdarg.h>
namespace AK { namespace AK {
class StringBuilder { class StringBuilder {
public: public:
explicit StringBuilder(ssize_t initial_capacity = 16); explicit StringBuilder(int initial_capacity = 16);
~StringBuilder() {} ~StringBuilder() {}
void append(const StringView&); void append(const StringView&);
void append(char); void append(char);
void append(const char*, ssize_t); void append(const char*, int);
void appendf(const char*, ...); void appendf(const char*, ...);
void appendvf(const char*, va_list); void appendvf(const char*, va_list);
@ -21,10 +21,10 @@ public:
ByteBuffer to_byte_buffer(); ByteBuffer to_byte_buffer();
private: private:
void will_append(ssize_t); void will_append(int);
ByteBuffer m_buffer; ByteBuffer m_buffer;
ssize_t m_length { 0 }; int m_length { 0 };
}; };
} }

View File

@ -3,6 +3,10 @@
#include "StdLibExtras.h" #include "StdLibExtras.h"
#include "kmalloc.h" #include "kmalloc.h"
#ifndef __serenity__
#include <new>
#endif
//#define DEBUG_STRINGIMPL //#define DEBUG_STRINGIMPL
#ifdef DEBUG_STRINGIMPL #ifdef DEBUG_STRINGIMPL

View File

@ -4,6 +4,10 @@
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <AK/kmalloc.h> #include <AK/kmalloc.h>
#ifndef __serenity__
#include <new>
#endif
namespace AK { namespace AK {
template<typename T, int inline_capacity = 0> template<typename T, int inline_capacity = 0>