Compare commits
2 Commits
944d32de36
...
cf8d3c6ff9
Author | SHA1 | Date | |
---|---|---|---|
cf8d3c6ff9 | |||
12d2039f68 |
@ -7,12 +7,16 @@ extern "C"
|
|||||||
void* memset(void* buf, int c, usize n);
|
void* memset(void* buf, int c, usize n);
|
||||||
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);
|
||||||
|
usize strnlen(const char* str, usize max);
|
||||||
|
|
||||||
int strcmp(const char* a, const char* b);
|
int strcmp(const char* a, const char* b);
|
||||||
|
|
||||||
usize wcslen(const wchar_t* str);
|
usize wcslen(const wchar_t* str);
|
||||||
|
|
||||||
char* strdup(const char* str);
|
char* strdup(const char* str);
|
||||||
|
char* strndup(const char* str, usize max);
|
||||||
|
|
||||||
// Copies len bytes from src into dest and adds a null terminator.
|
// Copies len bytes from src into dest and adds a null terminator.
|
||||||
// FIXME: Replace this invented function with strlcpy().
|
// FIXME: Replace this invented function with strlcpy().
|
||||||
|
@ -5,6 +5,7 @@ class OwnedStringView
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OwnedStringView(char* c_str);
|
OwnedStringView(char* c_str);
|
||||||
|
OwnedStringView(char* c_str, usize length);
|
||||||
OwnedStringView();
|
OwnedStringView();
|
||||||
OwnedStringView(OwnedStringView&&);
|
OwnedStringView(OwnedStringView&&);
|
||||||
OwnedStringView(const OwnedStringView&) = delete;
|
OwnedStringView(const OwnedStringView&) = delete;
|
||||||
@ -12,6 +13,8 @@ class OwnedStringView
|
|||||||
|
|
||||||
Result<OwnedStringView> clone() const;
|
Result<OwnedStringView> clone() const;
|
||||||
|
|
||||||
|
Result<OwnedStringView> substring(usize begin, usize size) const;
|
||||||
|
|
||||||
static Result<OwnedStringView> from_string_literal(const char* str);
|
static Result<OwnedStringView> from_string_literal(const char* str);
|
||||||
|
|
||||||
const char* chars() const
|
const char* chars() const
|
||||||
|
@ -46,6 +46,14 @@ extern "C"
|
|||||||
return (usize)(i - str);
|
return (usize)(i - str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
usize strnlen(const char* str, usize max)
|
||||||
|
{
|
||||||
|
const char* i = str;
|
||||||
|
for (; max, *i; ++i, --max)
|
||||||
|
;
|
||||||
|
return (usize)(i - str);
|
||||||
|
}
|
||||||
|
|
||||||
int strcmp(const char* a, const char* b)
|
int strcmp(const char* a, const char* b)
|
||||||
{
|
{
|
||||||
while (*a && (*a == *b))
|
while (*a && (*a == *b))
|
||||||
@ -68,10 +76,22 @@ extern "C"
|
|||||||
{
|
{
|
||||||
const usize len = strlen(str);
|
const usize len = strlen(str);
|
||||||
|
|
||||||
char* dest = (char*)malloc_impl(len + 1).value_or(nullptr);
|
char* dest = (char*)calloc_impl(len + 1, 1).value_or(nullptr);
|
||||||
if (!dest) return nullptr;
|
if (!dest) return nullptr;
|
||||||
|
|
||||||
memcpy(dest, str, len + 1);
|
memcpy(dest, str, len);
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* strndup(const char* str, usize max)
|
||||||
|
{
|
||||||
|
const usize len = strnlen(str, max);
|
||||||
|
|
||||||
|
char* dest = (char*)calloc_impl(len + 1, 1).value_or(nullptr);
|
||||||
|
if (!dest) return nullptr;
|
||||||
|
|
||||||
|
memcpy(dest, str, len);
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,12 @@ OwnedStringView::OwnedStringView(char* c_str)
|
|||||||
if (m_string) { m_length = strlen(m_string); }
|
if (m_string) { m_length = strlen(m_string); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
OwnedStringView::OwnedStringView(char* c_str, usize length)
|
||||||
|
{
|
||||||
|
m_string = c_str;
|
||||||
|
m_length = length;
|
||||||
|
}
|
||||||
|
|
||||||
OwnedStringView::~OwnedStringView()
|
OwnedStringView::~OwnedStringView()
|
||||||
{
|
{
|
||||||
if (m_string) free_impl(m_string);
|
if (m_string) free_impl(m_string);
|
||||||
@ -31,6 +37,14 @@ Result<OwnedStringView> OwnedStringView::clone() const
|
|||||||
return from_string_literal(m_string);
|
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
|
const char& OwnedStringView::operator[](usize index) const
|
||||||
{
|
{
|
||||||
expect(index < m_length, "index out of range");
|
expect(index < m_length, "index out of range");
|
||||||
|
Loading…
Reference in New Issue
Block a user