Compare commits

..

3 Commits

Author SHA1 Message Date
59765aa334
Rename String.h -> CString.h
All checks were successful
continuous-integration/drone/push Build is passing
Let's not confuse String.h with a managed string class, it's in fact the equivalent of the C stdlib's <string.h>
2022-12-16 20:40:04 +01:00
42a2c2af49
Add strdup() 2022-12-16 20:37:57 +01:00
e56075fb46
Use nothrow in raw_malloc 2022-12-16 20:36:43 +01:00
14 changed files with 37 additions and 16 deletions

View File

@ -6,9 +6,9 @@
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include "thread/Scheduler.h" #include "thread/Scheduler.h"
#include <cpuid.h> #include <cpuid.h>
#include <luna/CString.h>
#include <luna/Check.h> #include <luna/Check.h>
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/String.h>
#include <luna/SystemError.h> #include <luna/SystemError.h>
#include <luna/Types.h> #include <luna/Types.h>

View File

@ -1,7 +1,7 @@
#include "arch/MMU.h" #include "arch/MMU.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include <luna/CString.h>
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/String.h>
#include <luna/SystemError.h> #include <luna/SystemError.h>
#pragma GCC push_options #pragma GCC push_options

View File

@ -1,5 +1,5 @@
#include "thread/Thread.h" #include "thread/Thread.h"
#include <luna/String.h> #include <luna/CString.h>
bool is_in_kernel(Registers* regs) bool is_in_kernel(Registers* regs)
{ {

View File

@ -4,8 +4,8 @@
#include "boot/bootboot.h" #include "boot/bootboot.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include "video/Framebuffer.h" #include "video/Framebuffer.h"
#include <luna/CString.h>
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/String.h>
extern const BOOTBOOT bootboot; extern const BOOTBOOT bootboot;

View File

@ -6,10 +6,10 @@
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include <luna/Alignment.h> #include <luna/Alignment.h>
#include <luna/Alloc.h> #include <luna/Alloc.h>
#include <luna/CString.h>
#include <luna/LinkedList.h> #include <luna/LinkedList.h>
#include <luna/SafeArithmetic.h> #include <luna/SafeArithmetic.h>
#include <luna/ScopeGuard.h> #include <luna/ScopeGuard.h>
#include <luna/String.h>
#include <luna/SystemError.h> #include <luna/SystemError.h>
namespace std namespace std

View File

@ -6,8 +6,8 @@
#include "memory/MemoryMap.h" #include "memory/MemoryMap.h"
#include <luna/Alignment.h> #include <luna/Alignment.h>
#include <luna/Bitmap.h> #include <luna/Bitmap.h>
#include <luna/CString.h>
#include <luna/ScopeGuard.h> #include <luna/ScopeGuard.h>
#include <luna/String.h>
#include <luna/SystemError.h> #include <luna/SystemError.h>
#include <luna/Types.h> #include <luna/Types.h>

View File

@ -1,9 +1,9 @@
#include "video/TextConsole.h" #include "video/TextConsole.h"
#include "boot/bootboot.h" #include "boot/bootboot.h"
#include "video/Framebuffer.h" #include "video/Framebuffer.h"
#include <luna/CString.h>
#include <luna/Format.h> #include <luna/Format.h>
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/String.h>
#include <stdarg.h> #include <stdarg.h>
extern const BOOTBOOT bootboot; extern const BOOTBOOT bootboot;

View File

@ -3,7 +3,7 @@
set(FREESTANDING_SOURCES set(FREESTANDING_SOURCES
src/Format.cpp src/Format.cpp
src/NumberParsing.cpp src/NumberParsing.cpp
src/String.cpp src/CString.cpp
src/Units.cpp src/Units.cpp
src/SystemError.cpp src/SystemError.cpp
src/Bitmap.cpp src/Bitmap.cpp

View File

@ -8,4 +8,6 @@ extern "C"
int memcmp(const void* a, const void* b, usize n); int memcmp(const void* a, const void* b, usize n);
void* memmove(void* dest, const void* src, usize n); void* memmove(void* dest, const void* src, usize n);
usize strlen(const char* str); usize strlen(const char* str);
char* strdup(const char* str);
} }

View File

@ -22,6 +22,12 @@ class OwnedStringView
return m_length; return m_length;
} }
const char& operator[](usize index) const
{
expect(index < m_length, "OwnedStringView: index out of range");
return m_string[index];
}
private: private:
char* m_string{nullptr}; char* m_string{nullptr};
usize m_length{0}; usize m_length{0};

View File

@ -7,7 +7,7 @@
[[nodiscard]] void* raw_malloc(usize size) [[nodiscard]] void* raw_malloc(usize size)
{ {
#ifdef USE_FREESTANDING #ifdef USE_FREESTANDING
char* const rc = new char[size]; char* const rc = new (std::nothrow) char[size];
return (void*)rc; return (void*)rc;
#else #else
return malloc(size); return malloc(size);

View File

@ -1,6 +1,6 @@
#include <luna/Bitmap.h> #include <luna/Bitmap.h>
#include <luna/CString.h>
#include <luna/Check.h> #include <luna/Check.h>
#include <luna/String.h>
Bitmap::Bitmap() Bitmap::Bitmap()
{ {

View File

@ -1,4 +1,5 @@
#include <luna/String.h> #include <luna/Alloc.h>
#include <luna/CString.h>
extern "C" extern "C"
{ {
@ -44,4 +45,16 @@ extern "C"
; ;
return (usize)(i - str); return (usize)(i - str);
} }
char* strdup(const char* str)
{
const usize len = strlen(str);
char* dest = (char*)raw_malloc(len + 1);
if (!dest) return nullptr;
memcpy(dest, str, len + 1);
return dest;
}
} }

View File

@ -1,6 +1,6 @@
#include <luna/Alloc.h> #include <luna/Alloc.h>
#include <luna/CString.h>
#include <luna/OwnedStringView.h> #include <luna/OwnedStringView.h>
#include <luna/String.h>
OwnedStringView::OwnedStringView() OwnedStringView::OwnedStringView()
{ {
@ -23,14 +23,14 @@ OwnedStringView::OwnedStringView(char* c_str)
OwnedStringView::~OwnedStringView() OwnedStringView::~OwnedStringView()
{ {
if (m_string) destroy_array(m_string); if (m_string) raw_free(m_string);
} }
Result<OwnedStringView> OwnedStringView::clone() const Result<OwnedStringView> OwnedStringView::clone() const
{ {
char* buf = TRY(make_array<char>(m_length + 1)); char* const c_str = strdup(m_string);
memcpy(buf, m_string, m_length + 1); if (!c_str) return err(ENOMEM);
return OwnedStringView{buf}; return OwnedStringView{c_str};
} }