Files

186 lines
4.5 KiB
C++
Raw Permalink Normal View History

#ifndef _RSTL_STRING
#define _RSTL_STRING
#include "types.h"
2022-08-09 19:03:51 -04:00
#include "rstl/rmemory_allocator.hpp"
2022-10-14 13:01:20 +03:00
class CInputStream;
2023-01-11 18:43:43 -08:00
class COutputStream;
2022-10-14 13:01:20 +03:00
namespace rstl {
template < typename _CharTp >
struct char_traits {};
2022-09-18 02:05:46 -04:00
template < typename _CharTp, typename Traits = char_traits< _CharTp >,
typename Alloc = rmemory_allocator >
class basic_string {
struct COWData {
2022-10-09 01:37:23 -04:00
uint x0_capacity;
uint x4_refCount;
_CharTp* x8_data;
};
const _CharTp* x0_ptr;
COWData* x4_cow;
2022-10-09 01:37:23 -04:00
uint x8_size;
uint _pad; // Alloc?
2023-01-12 14:46:08 -08:00
void internal_prepare_to_write(int len, bool);
2023-01-06 14:30:08 +02:00
void internal_allocate(int size);
// {
// x4_cow = reinterpret_cast<COWData*>(rs_new uchar[size * sizeof(_CharTp) +
2022-10-09 01:37:23 -04:00
// 8]); x0_ptr = x4_cow->x8_data; x4_cow->x0_capacity = uint(size);
// x4_cow->x4_refCount = 1;
// }
void internal_dereference();
// {
// if (x4_cow && --x4_cow->x4_refCount == 0)
// delete[] x4_cow;
// }
2022-10-16 19:35:43 +03:00
static const _CharTp mNull;
public:
struct literal_t {};
2022-10-16 19:35:43 +03:00
basic_string() : x0_ptr(&mNull), x4_cow(nullptr), x8_size(0) {}
2024-09-30 00:02:23 -06:00
basic_string(literal_t, const _CharTp* data) {
x0_ptr = data;
x4_cow = nullptr;
2024-09-30 00:02:23 -06:00
const _CharTp* it = data;
while (*it)
++it;
2024-09-30 00:02:23 -06:00
x8_size = static_cast< uint >(it - data);
}
basic_string(const basic_string& str);
// {
// x0_ptr = str.x0_ptr;
// x4_cow = str.x4_cow;
// x8_size = str.x8_size;
// if (x4_cow)
// ++x4_cow->x4_refCount;
// }
2022-10-14 13:01:20 +03:00
basic_string(CInputStream& in, const Alloc& = rmemory_allocator());
2022-10-08 01:46:42 -07:00
basic_string(const _CharTp* data, int size = -1, const Alloc& = rmemory_allocator());
// {
// if (size <= 0 && !data)
// {
2022-10-16 19:35:43 +03:00
// x0_ptr = &mNull;
// x4_cow = nullptr;
// x8_size = 0;
// return;
// }
// const _CharTp* it = data;
2022-10-09 01:37:23 -04:00
// uint len = 0;
// while (*it)
// {
// if (size != -1 && len >= size)
// break;
// ++it;
// ++len;
// }
// internal_allocate(len + 1);
// x8_size = len;
// for (int i = 0; i < len; ++i)
// x4_cow->x8_data[i] = data[i];
// x4_cow->x8_data[len] = 0;
// }
~basic_string() { internal_dereference(); }
size_t size() const { return x8_size; }
2023-01-12 14:46:08 -08:00
void reserve(int len) { internal_prepare_to_write(len, true); }
2022-10-16 19:35:43 +03:00
void assign(const basic_string&);
void assign(const _CharTp*, int);
2022-10-30 15:47:50 -04:00
basic_string& operator=(const basic_string& other) {
assign(other);
return *this;
}
basic_string operator+(const _CharTp*);
2023-01-06 14:30:08 +02:00
void append(const basic_string& other);
void append(int, _CharTp);
2023-01-12 14:46:08 -08:00
void append(const _CharTp*, int);
2022-12-01 18:19:53 +02:00
int _eq_helper(const basic_string& other) const;
bool operator==(const basic_string& other) const;
2026-03-08 00:37:24 -07:00
bool operator!=(const basic_string& other) const;
2022-12-05 23:35:31 +02:00
bool operator<(const basic_string& other) const;
2022-12-01 18:19:53 +02:00
2023-01-12 14:46:08 -08:00
const _CharTp* data() const { return x0_ptr; }
2023-01-11 18:43:43 -08:00
void PutTo(COutputStream& out) const;
2023-01-12 14:46:08 -08:00
const _CharTp at(int idx) const { return data()[idx]; }
};
2022-12-01 18:19:53 +02:00
template < typename _CharTp, typename Traits, typename Alloc >
bool basic_string< _CharTp, Traits, Alloc >::operator==(const basic_string& other) const {
return _eq_helper(other) == 0;
}
2026-03-08 00:37:24 -07:00
template < typename _CharTp, typename Traits, typename Alloc >
bool basic_string< _CharTp, Traits, Alloc >::operator!=(const basic_string& other) const {
return _eq_helper(other) != 0;
}
2022-12-05 23:35:31 +02:00
template < typename _CharTp, typename Traits, typename Alloc >
bool basic_string< _CharTp, Traits, Alloc >::operator<(const basic_string& other) const {
return _eq_helper(other) < 0;
}
// template <>
2022-10-16 19:35:43 +03:00
// const char basic_string<char>::mNull = 0;
// template <>
2022-10-16 19:35:43 +03:00
// const wchar_t basic_string<wchar_t>::mNull = 0;
typedef basic_string< wchar_t > wstring;
typedef basic_string< char > string;
#ifdef __MWERKS__
__declspec(weak) // TODO
#else
static
#endif
wstring wstring_l(const wchar_t* data) {
return wstring(wstring::literal_t(), data);
}
string string_l(const char* data);
// {
// return string(string::literal_t(), data);
// }
2022-08-12 21:26:00 -04:00
2023-01-06 14:30:08 +02:00
string operator+(const string& a, const string& b);
2026-03-27 12:50:54 -06:00
wstring operator+(const wstring& a, const wstring& b);
2023-01-06 14:30:08 +02:00
// {
// string result(a);
// result.append(b);
// return result;
// }
static inline string operator+(const string& a, char c) {
2023-01-06 14:30:08 +02:00
string result(a);
result.append(1, c);
return result;
}
2022-11-01 01:46:20 +02:00
2026-03-08 00:37:24 -07:00
static inline string operator+(const string& a, const char* c) {
string result(a);
result.append(c, -1);
return result;
}
2022-08-12 21:26:00 -04:00
CHECK_SIZEOF(string, 0x10)
} // namespace rstl
#endif // _RSTL_STRING