Revert "Update TinySTL (attempt 2). (#341)"

This reverts commit 73966ef218.
This commit is contained in:
Бранимир Караџић
2025-01-30 11:42:36 -08:00
parent 73966ef218
commit fae06fa431
17 changed files with 271 additions and 1222 deletions

View File

@@ -1,5 +1,5 @@
/*-
* Copyright 2012-2018 Matthew Endsley
* Copyright 2012 Matthew Endsley
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
@@ -27,37 +27,34 @@
#ifndef TINYSTL_STRING_H
#define TINYSTL_STRING_H
#include <tinystl/allocator.h>
#include <tinystl/stddef.h>
#include <tinystl/hash.h>
#include <string.h> // strlen
#include "stddef.h"
#include "hash.h"
namespace tinystl {
template<typename Allocator>
class basic_string {
template<typename Alloc>
class stringT {
public:
basic_string();
basic_string(const basic_string& other);
basic_string(basic_string&& other);
basic_string(const char* sz);
basic_string(const char* sz, size_t len);
~basic_string();
stringT();
stringT(const stringT<Alloc>& other);
stringT(const char* sz);
stringT(const char* sz, size_t len);
~stringT();
basic_string& operator=(const basic_string& other);
basic_string& operator=(basic_string&& other);
stringT<Alloc>& operator=(const stringT<Alloc>& other);
const char* c_str() const;
size_t size() const;
bool empty() const;
void reserve(size_t size);
void resize(size_t size);
void reserve(size_t _size);
void resize(size_t _size);
void clear();
void append(const char* first, const char* last);
void assign(const char* s, size_t n);
void append(const char* str);
void shrink_to_fit();
void swap(basic_string& other);
void swap(stringT<Alloc>& other);
private:
typedef char* pointer;
@@ -66,11 +63,13 @@ namespace tinystl {
pointer m_capacity;
static const size_t c_nbuffer = 12;
char m_buffer[12]{0};
char m_buffer[12];
};
template<typename allocator>
inline basic_string<allocator>::basic_string()
typedef stringT<TINYSTL_ALLOCATOR> string;
template<typename Alloc>
inline stringT<Alloc>::stringT()
: m_first(m_buffer)
, m_last(m_buffer)
, m_capacity(m_buffer + c_nbuffer)
@@ -78,8 +77,8 @@ namespace tinystl {
resize(0);
}
template<typename allocator>
inline basic_string<allocator>::basic_string(const basic_string& other)
template<typename Alloc>
inline stringT<Alloc>::stringT(const stringT<Alloc>& other)
: m_first(m_buffer)
, m_last(m_buffer)
, m_capacity(m_buffer + c_nbuffer)
@@ -88,27 +87,8 @@ namespace tinystl {
append(other.m_first, other.m_last);
}
template<typename allocator>
inline basic_string<allocator>::basic_string(basic_string&& other)
{
if (other.m_first == other.m_buffer) {
m_first = m_buffer;
m_last = m_buffer;
m_capacity = m_buffer + c_nbuffer;
reserve(other.size());
append(other.m_first, other.m_last);
} else {
m_first = other.m_first;
m_last = other.m_last;
m_capacity = other.m_capacity;
}
other.m_first = other.m_last = other.m_buffer;
other.m_capacity = other.m_buffer + c_nbuffer;
other.resize(0);
}
template<typename allocator>
inline basic_string<allocator>::basic_string(const char* sz)
template<typename Alloc>
inline stringT<Alloc>::stringT(const char* sz)
: m_first(m_buffer)
, m_last(m_buffer)
, m_capacity(m_buffer + c_nbuffer)
@@ -121,8 +101,8 @@ namespace tinystl {
append(sz, sz + len);
}
template<typename allocator>
inline basic_string<allocator>::basic_string(const char* sz, size_t len)
template<typename Alloc>
inline stringT<Alloc>::stringT(const char* sz, size_t len)
: m_first(m_buffer)
, m_last(m_buffer)
, m_capacity(m_buffer + c_nbuffer)
@@ -131,73 +111,68 @@ namespace tinystl {
append(sz, sz + len);
}
template<typename allocator>
inline basic_string<allocator>::~basic_string() {
template<typename Alloc>
inline stringT<Alloc>::~stringT() {
if (m_first != m_buffer)
allocator::static_deallocate(m_first, m_capacity - m_first);
Alloc::static_deallocate(m_first, m_capacity - m_first);
}
template<typename allocator>
inline basic_string<allocator>& basic_string<allocator>::operator=(const basic_string& other) {
basic_string(other).swap(*this);
template<typename Alloc>
inline stringT<Alloc>& stringT<Alloc>::operator=(const stringT<Alloc>& other) {
stringT<Alloc>(other).swap(*this);
return *this;
}
template<typename allocator>
inline basic_string<allocator>& basic_string<allocator>::operator=(basic_string&& other) {
basic_string(static_cast<basic_string&&>(other)).swap(*this);
return *this;
}
template<typename allocator>
inline const char* basic_string<allocator>::c_str() const {
template<typename Alloc>
inline const char* stringT<Alloc>::c_str() const {
return m_first;
}
template<typename allocator>
inline size_t basic_string<allocator>::size() const
template<typename Alloc>
inline size_t stringT<Alloc>::size() const
{
return (size_t)(m_last - m_first);
}
template<typename allocator>
inline void basic_string<allocator>::reserve(size_t capacity) {
if (m_first + capacity + 1 <= m_capacity)
template<typename Alloc>
inline bool stringT<Alloc>::empty() const
{
return 0 == size();
}
template<typename Alloc>
inline void stringT<Alloc>::reserve(size_t capacity) {
if (m_first + capacity + 1 <= m_capacity) {
return;
}
const size_t size = (size_t)(m_last - m_first);
const size_t _size = (size_t)(m_last - m_first);
pointer newfirst = (pointer)allocator::static_allocate(capacity + 1);
for (pointer it = m_first, newit = newfirst, end = m_last; it != end; ++it, ++newit)
pointer newfirst = (pointer)Alloc::static_allocate(capacity + 1);
for (pointer it = m_first, newit = newfirst, end = m_last; it != end; ++it, ++newit) {
*newit = *it;
if (m_first != m_buffer)
allocator::static_deallocate(m_first, m_capacity - m_first);
}
if (m_first != m_buffer) {
Alloc::static_deallocate(m_first, m_capacity - m_first);
}
m_first = newfirst;
m_last = newfirst + size;
m_last = newfirst + _size;
m_capacity = m_first + capacity;
}
template<typename allocator>
inline void basic_string<allocator>::resize(size_t size) {
const size_t prevSize = m_last-m_first;
reserve(size);
if (size > prevSize)
for (pointer it = m_last, end = m_first + size + 1; it < end; ++it)
*it = 0;
else if (m_last != m_first)
m_first[size] = 0;
template<typename Alloc>
inline void stringT<Alloc>::resize(size_t _size) {
reserve(_size);
for (pointer it = m_last, end = m_first + _size + 1; it < end; ++it)
*it = 0;
m_last = m_first + size;
m_last += _size;
}
template<typename allocator>
inline void basic_string<allocator>::clear() {
resize(0);
}
template<typename allocator>
inline void basic_string<allocator>::append(const char* first, const char* last) {
template<typename Alloc>
inline void stringT<Alloc>::append(const char* first, const char* last) {
const size_t newsize = (size_t)((m_last - m_first) + (last - first) + 1);
if (m_first + newsize > m_capacity)
reserve((newsize * 3) / 2);
@@ -207,62 +182,46 @@ namespace tinystl {
*m_last = 0;
}
template<typename allocator>
inline void basic_string<allocator>::assign(const char* sz, size_t n) {
clear();
append(sz, sz+n);
template<typename Alloc>
inline void stringT<Alloc>::append(const char* str) {
append(str, str + strlen(str) );
}
template<typename allocator>
inline void basic_string<allocator>::shrink_to_fit() {
if (m_first == m_buffer) {
} else if (m_last == m_first) {
const size_t capacity = (size_t)(m_capacity - m_first);
if (capacity)
allocator::static_deallocate(m_first, capacity+1);
m_capacity = m_first;
} else if (m_capacity != m_last) {
const size_t size = (size_t)(m_last - m_first);
char* newfirst = (pointer)allocator::static_allocate(size+1);
for (pointer in = m_first, out = newfirst; in != m_last + 1; ++in, ++out)
*out = *in;
if (m_first != m_capacity)
allocator::static_deallocate(m_first, m_capacity+1-m_first);
m_first = newfirst;
m_last = newfirst+size;
m_capacity = m_last;
}
}
template<typename Alloc>
inline void stringT<Alloc>::swap(stringT<Alloc>& other) {
const pointer tfirst = m_first, tlast = m_last, tcapacity = m_capacity;
m_first = other.m_first, m_last = other.m_last, m_capacity = other.m_capacity;
other.m_first = tfirst, other.m_last = tlast, other.m_capacity = tcapacity;
template<typename allocator>
inline void basic_string<allocator>::swap(basic_string& other) {
{
const pointer tfirst = m_first, tlast = m_last, tcapacity = m_capacity;
m_first = other.m_first, m_last = other.m_last, m_capacity = other.m_capacity;
other.m_first = tfirst, other.m_last = tlast, other.m_capacity = tcapacity;
}
char tbuffer[c_nbuffer];
if (m_first == other.m_buffer)
for (pointer it = other.m_buffer, end = m_last, out = tbuffer; it != end; ++it, ++out)
*out = *it;
for (size_t i = 0; i < c_nbuffer; ++i) {
const char temp = m_buffer[i];
m_buffer[i] = other.m_buffer[i];
other.m_buffer[i] = temp;
}
if (m_first == other.m_buffer) {
int len = m_last - m_first;
m_first = m_buffer;
m_last = m_buffer + len;
m_capacity = m_buffer + c_nbuffer;
}
if (other.m_first == m_buffer) {
int len = other.m_last - other.m_first;
other.m_last = other.m_last - other.m_first + other.m_buffer;
other.m_first = other.m_buffer;
other.m_last = other.m_buffer + len;
other.m_capacity = other.m_buffer + c_nbuffer;
for (pointer it = other.m_first, end = other.m_last, in = m_buffer; it != end; ++it, ++in)
*it = *in;
*other.m_last = 0;
}
if (m_first == other.m_buffer) {
m_last = m_last - m_first + m_buffer;
m_first = m_buffer;
m_capacity = m_buffer + c_nbuffer;
for (pointer it = m_first, end = m_last, in = tbuffer; it != end; ++it, ++in)
*it = *in;
*m_last = 0;
}
}
template<typename allocatorl, typename allocatorr>
inline bool operator==(const basic_string<allocatorl>& lhs, const basic_string<allocatorr>& rhs) {
template<typename Alloc>
inline bool operator==(const stringT<Alloc>& lhs, const stringT<Alloc>& rhs) {
typedef const char* pointer;
const size_t lsize = lhs.size(), rsize = rhs.size();
@@ -278,12 +237,10 @@ namespace tinystl {
return true;
}
template<typename allocator>
static inline size_t hash(const basic_string<allocator>& value) {
template<typename Alloc>
static inline size_t hash(const stringT<Alloc>& value) {
return hash_string(value.c_str(), value.size());
}
typedef basic_string<TINYSTL_ALLOCATOR> string;
}
#endif