Compare commits

..

No commits in common. "ef01f187c3f7f33ba9527df2dad960426990122a" and "7b0b3dabc4e1859fde1b171b777af5ec4411807a" have entirely different histories.

14 changed files with 113 additions and 235 deletions

View File

@ -433,7 +433,7 @@ namespace MemoryManager
}
// FIXME: Make this more efficient.
Result<String> strdup_from_user(u64 address)
Result<OwnedStringView> strdup_from_user(u64 address)
{
if (!validate_user_readable_page(address)) return err(EFAULT);
@ -451,7 +451,7 @@ namespace MemoryManager
TRY(result.try_append(0)); // null terminator
return String::from_cstring(result.data());
return OwnedStringView::from_string_literal(result.data());
}
bool validate_user_write(void* user, usize size)

View File

@ -1,6 +1,6 @@
#pragma once
#include <luna/OwnedStringView.h>
#include <luna/Result.h>
#include <luna/String.h>
#include <luna/Types.h>
namespace MemoryManager
@ -26,7 +26,7 @@ namespace MemoryManager
bool validate_user_writable_page(u64 address);
bool validate_userspace_string(u64 address);
Result<String> strdup_from_user(u64 address);
Result<OwnedStringView> strdup_from_user(u64 address);
bool validate_user_write(void* user, usize size);
bool validate_user_read(const void* user, usize size);

View File

@ -11,9 +11,9 @@
#include <luna/ScopeGuard.h>
#include <luna/Vector.h>
static Result<Vector<String>> copy_string_vector_from_userspace(u64 address)
static Result<Vector<OwnedStringView>> copy_string_vector_from_userspace(u64 address)
{
Vector<String> result;
Vector<OwnedStringView> result;
const u64* user_vector = (const u64*)address;
@ -31,7 +31,7 @@ static Result<Vector<String>> copy_string_vector_from_userspace(u64 address)
return result;
}
static Result<u64> copy_string_vector_to_userspace(const Vector<String>& vec, ThreadImage& image)
static Result<u64> copy_string_vector_to_userspace(const Vector<OwnedStringView>& vec, ThreadImage& image)
{
Vector<u64> user_vec;
for (const auto& item : vec)

View File

@ -13,8 +13,7 @@ set(FREESTANDING_SOURCES
src/Bitmap.cpp
src/Buffer.cpp
src/Stack.cpp
src/String.cpp
src/StringView.cpp
src/OwnedStringView.cpp
src/Utf8.cpp
src/TarStream.cpp
src/DebugLog.cpp

View File

@ -0,0 +1,35 @@
#pragma once
#include <luna/Result.h>
class OwnedStringView
{
public:
OwnedStringView(char* c_str);
OwnedStringView(char* c_str, usize length);
OwnedStringView();
OwnedStringView(OwnedStringView&&);
OwnedStringView(const OwnedStringView&) = delete;
~OwnedStringView();
Result<OwnedStringView> clone() const;
Result<OwnedStringView> substring(usize begin, usize size) const;
static Result<OwnedStringView> from_string_literal(const char* str);
const char* chars() const
{
return m_string;
}
usize length() const
{
return m_length;
}
const char& operator[](usize) const;
private:
char* m_string { nullptr };
usize m_length { 0 };
};

View File

@ -1,7 +1,7 @@
#pragma once
#include <luna/CString.h>
#include <luna/Heap.h>
#include <luna/String.h>
#include <luna/OwnedStringView.h>
class PathParser
{
@ -18,8 +18,8 @@ class PathParser
return *m_original == '/';
}
Result<String> basename();
Result<String> dirname();
Result<OwnedStringView> basename();
Result<OwnedStringView> dirname();
Option<const char*> next();

View File

@ -1,55 +0,0 @@
#pragma once
#include <luna/Result.h>
#include <luna/StringView.h>
class String
{
public:
String(char* c_str);
String(char* c_str, usize length);
String();
String(String&&);
String(const String&) = delete;
~String();
Result<String> clone() const;
Result<String> substring(usize begin, usize size) const;
static Result<String> from_cstring(const char* str);
const char* chars() const
{
return m_inline ? m_inline_storage : m_string;
}
usize length() const
{
return m_length;
}
bool is_empty() const
{
return m_length == 0;
}
StringView view() const
{
return { chars(), m_length };
}
const char& operator[](usize) const;
private:
union {
char* m_string { nullptr };
char m_inline_storage[sizeof(char*)];
};
bool m_inline { true };
usize m_length { 0 };
};

View File

@ -1,39 +0,0 @@
#pragma once
#include <luna/Result.h>
class String;
class StringView
{
public:
StringView(const char* c_str);
StringView(const char* c_str, usize length);
StringView();
StringView(const StringView&);
Result<String> to_string();
const char* chars() const
{
return m_string;
}
usize length() const
{
return m_length;
}
bool is_empty() const
{
return m_length == 0;
}
const char& operator[](usize) const;
private:
const char* m_string { nullptr };
usize m_length { 0 };
};

View File

@ -1,6 +1,6 @@
#pragma once
#include <luna/OwnedStringView.h>
#include <luna/Result.h>
#include <luna/String.h>
usize to_dynamic_unit_cstr(usize value, char* buffer, usize max);
Result<String> to_dynamic_unit(usize value);
Result<OwnedStringView> to_dynamic_unit(usize value);

View File

@ -0,0 +1,59 @@
#include <luna/Alloc.h>
#include <luna/CString.h>
#include <luna/OwnedStringView.h>
OwnedStringView::OwnedStringView()
{
}
OwnedStringView::OwnedStringView(OwnedStringView&& other)
{
m_string = other.m_string;
m_length = other.m_length;
other.m_string = nullptr;
}
OwnedStringView::OwnedStringView(char* c_str)
{
m_string = c_str;
if (m_string) { m_length = strlen(m_string); }
}
OwnedStringView::OwnedStringView(char* c_str, usize length)
{
m_string = c_str;
m_length = length;
}
OwnedStringView::~OwnedStringView()
{
if (m_string) free_impl(m_string);
}
Result<OwnedStringView> OwnedStringView::clone() const
{
return from_string_literal(m_string);
}
Result<OwnedStringView> OwnedStringView::substring(usize begin, usize size) const
{
if (begin + size >= size) return err(EINVAL);
char* const dup = strndup(m_string + begin, size);
if (!dup) return err(ENOMEM);
return OwnedStringView { dup, size };
}
const char& OwnedStringView::operator[](usize index) const
{
expect(index < m_length, "index out of range");
return m_string[index];
}
Result<OwnedStringView> OwnedStringView::from_string_literal(const char* str)
{
char* const dup = strdup(str);
if (!dup) return err(ENOMEM);
return OwnedStringView { dup };
}

View File

@ -34,7 +34,7 @@ Option<const char*> PathParser::next()
return result;
}
Result<String> PathParser::basename()
Result<OwnedStringView> PathParser::basename()
{
char* copy = strdup(m_original);
if (!copy) return err(ENOMEM);
@ -44,10 +44,10 @@ Result<String> PathParser::basename()
char* result = ::basename(copy);
// We must copy this as we cannot rely on the original string.
return String::from_cstring(result);
return OwnedStringView::from_string_literal(result);
}
Result<String> PathParser::dirname()
Result<OwnedStringView> PathParser::dirname()
{
char* copy = strdup(m_original);
if (!copy) return err(ENOMEM);
@ -57,5 +57,5 @@ Result<String> PathParser::dirname()
char* result = ::dirname(copy);
// We must copy this as we cannot rely on the original string.
return String::from_cstring(result);
return OwnedStringView::from_string_literal(result);
}

View File

@ -1,79 +0,0 @@
#include <luna/Alloc.h>
#include <luna/CString.h>
#include <luna/String.h>
String::String()
{
m_inline = true;
m_length = 0;
memset(m_inline_storage, 0, sizeof(m_inline_storage));
}
String::String(String&& other)
{
m_inline = other.m_inline;
m_string = other.m_string;
m_length = other.m_length;
if (m_inline) memcpy(m_inline_storage, other.m_inline_storage, sizeof(m_inline_storage));
other.m_string = nullptr;
}
String::String(char* c_str)
{
check(c_str);
m_string = c_str;
m_inline = false;
m_length = strlen(m_string);
}
String::String(char* c_str, usize length)
{
check(c_str);
m_string = c_str;
m_inline = false;
m_length = length;
}
String::~String()
{
if (!m_inline) free_impl(m_string);
}
Result<String> String::clone() const
{
return from_cstring(chars());
}
Result<String> String::substring(usize begin, usize size) const
{
if (begin + size >= size) return err(EINVAL);
char* const dup = strndup(chars() + begin, size);
if (!dup) return err(ENOMEM);
return String { dup, size };
}
const char& String::operator[](usize index) const
{
expect(index < m_length, "index out of range");
return chars()[index];
}
Result<String> String::from_cstring(const char* str)
{
usize len = strlen(str);
if (len < sizeof(m_inline_storage))
{
String result;
result.m_inline = true;
result.m_length = len;
strncpy(result.m_inline_storage, str, sizeof(m_inline_storage));
return result;
}
char* const dup = strdup(str);
if (!dup) return err(ENOMEM);
return String { dup };
}

View File

@ -1,42 +0,0 @@
#include <luna/CString.h>
#include <luna/String.h>
#include <luna/StringView.h>
static const char* empty = "";
StringView::StringView()
{
m_string = empty;
m_length = 0;
}
StringView::StringView(const StringView& other)
{
m_string = other.m_string;
m_length = other.m_length;
}
StringView::StringView(const char* c_str)
{
check(c_str);
m_string = c_str;
m_length = strlen(m_string);
}
StringView::StringView(const char* c_str, usize length)
{
check(c_str);
m_string = c_str;
m_length = length;
}
const char& StringView::operator[](usize index) const
{
expect(index < m_length, "index out of range");
return m_string[index];
}
Result<String> StringView::to_string()
{
return String::from_cstring(m_string);
}

View File

@ -18,11 +18,11 @@ usize to_dynamic_unit_cstr(usize value, char* buffer, usize max)
return string_format(buffer, max, "%zu.%zu %ciB", value / 1024, (value % 1024) / 103, *unit_prefixes);
}
Result<String> to_dynamic_unit(usize value)
Result<OwnedStringView> to_dynamic_unit(usize value)
{
char* const buf = TRY(make_array<char>(64));
to_dynamic_unit_cstr(value, buf, 64);
return String { buf };
return OwnedStringView { buf };
}