implement basic dynamic string
This commit is contained in:
37
c_os/user/lib/String.cc
Normal file
37
c_os/user/lib/String.cc
Normal file
@ -0,0 +1,37 @@
|
||||
#include "user/lib/String.h"
|
||||
#include "user/lib/Math.h"
|
||||
#include "user/lib/Memory.h"
|
||||
|
||||
unsigned int bse::strlen(const char* str) {
|
||||
const char* current = str;
|
||||
while (*current != '\0') { ++current; }
|
||||
return current - str;
|
||||
}
|
||||
|
||||
void bse::strncpy(char* destination, unsigned int n, const char* source) {
|
||||
memcpy<char>(destination, source, n);
|
||||
}
|
||||
|
||||
// Only compares equal length strings
|
||||
int bse::strcmp(const char* a, const char* b) {
|
||||
const unsigned int a_len = strlen(a);
|
||||
const unsigned int b_len = strlen(b);
|
||||
|
||||
if (a_len < b_len) {
|
||||
return -1;
|
||||
}
|
||||
if (b_len < a_len) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < a_len; ++i) {
|
||||
if (a[i] < b[i]) {
|
||||
return -1;
|
||||
}
|
||||
if (b[i] < a[i]) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
109
c_os/user/lib/String.h
Normal file
109
c_os/user/lib/String.h
Normal file
@ -0,0 +1,109 @@
|
||||
#ifndef __String_Include_H_
|
||||
#define __String_Include_H_
|
||||
|
||||
#include "user/lib/Array.h"
|
||||
#include <cstddef>
|
||||
#include <user/lib/Iterator.h>
|
||||
|
||||
namespace bse {
|
||||
|
||||
unsigned int strlen(const char* str);
|
||||
void strncpy(char* destination, unsigned int n, const char* source);
|
||||
int strcmp(const char* a, const char* b);
|
||||
|
||||
class string {
|
||||
private:
|
||||
unsigned int len = 0;
|
||||
char* buf = nullptr;
|
||||
|
||||
public:
|
||||
using iterator = ContinuousIterator<char>;
|
||||
|
||||
string() = default;
|
||||
|
||||
string(const char* str) : len(strlen(str)), buf(new char[len + 1]) {
|
||||
strncpy(buf, len + 1, str);
|
||||
}
|
||||
|
||||
template<unsigned int N>
|
||||
string(const array<char, N>& arr) : len(N), buf(new char[len + 1]) {
|
||||
for (unsigned int i = 0; i < N; ++i) {
|
||||
buf[i] = arr[i];
|
||||
}
|
||||
}
|
||||
|
||||
string(const string& copy) : len(copy.len), buf(new char[len + 1]) {
|
||||
strncpy(buf, len + 1, copy.buf);
|
||||
}
|
||||
|
||||
string& operator=(const string& copy) {
|
||||
if (© != this) {
|
||||
len = copy.len;
|
||||
buf = new char[len + 1];
|
||||
strncpy(buf, len + 1, copy.buf);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
string(string&& move) noexcept : len(move.len), buf(move.buf) {
|
||||
delete[] move.buf;
|
||||
move.len = 0;
|
||||
move.buf = nullptr;
|
||||
}
|
||||
|
||||
string& operator=(string&& move) noexcept {
|
||||
if (&move != this) {
|
||||
len = move.len;
|
||||
buf = move.buf;
|
||||
|
||||
delete[] move.buf;
|
||||
move.len = 0;
|
||||
move.buf = nullptr;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
~string() {
|
||||
delete[] buf;
|
||||
}
|
||||
|
||||
iterator begin() { return iterator(&buf[0]); }
|
||||
iterator begin() const { return iterator(&buf[0]); }
|
||||
iterator end() { return iterator(&buf[len]); }
|
||||
iterator end() const { return iterator(&buf[len]); }
|
||||
|
||||
explicit operator char*() { return buf; }
|
||||
explicit operator char*() const { return buf; }
|
||||
|
||||
char operator[](std::size_t pos) { return buf[pos]; }
|
||||
char operator[](std::size_t pos) const { return buf[pos]; }
|
||||
|
||||
string operator+(const string& other) {
|
||||
string new_str;
|
||||
new_str.len = len + other.len;
|
||||
new_str.buf = new char[new_str.len + 1];
|
||||
|
||||
strncpy(new_str.buf, len, buf); // Copy this content
|
||||
strncpy(&new_str.buf[len], other.len + 1, other.buf); // Copy other content
|
||||
|
||||
return new_str;
|
||||
}
|
||||
|
||||
bool operator==(const string& other) {
|
||||
return strcmp(buf, other.buf) == 0;
|
||||
}
|
||||
|
||||
bool operator!=(const string& other) {
|
||||
return strcmp(buf, other.buf) != 0;
|
||||
}
|
||||
|
||||
unsigned int size() const {
|
||||
return len;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bse
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user