working string

This commit is contained in:
2024-03-23 22:12:18 +01:00
parent c5e93c0484
commit b3171fe3cc
8 changed files with 95 additions and 69 deletions

View File

@@ -102,6 +102,16 @@ inline void strcpy(const char *src, char *dst) {
dst[i] = '\0';
}
inline void strncpy(char *dst, const char *src, size_t bufsize) {
int i = 0;
while (src[i] != '\0' && (i < (bufsize - 1))) {
dst[i] = src[i];
i++;
}
dst[i] = '\0';
}
#ifdef __cplusplus
}
#endif

View File

@@ -15,7 +15,9 @@
#include "TtyManager.hpp"
#include "stl/map"
#include "stl/pair.h"
#include "stl/string"
#include "stl/vector.h"
class SharedPtrTester {
@@ -288,5 +290,12 @@ int test_templates() {
test.push_back(1);
test.push_back(2);
cgistd::map<uint64_t, cgistd::vector<int>> map;
map[2] = cgistd::vector<int>();
map[2].push_back(2);
cgistd::string s = "hello world \n";
writestr_no_yield(s.c_str());
return 0;
}

View File

@@ -15,7 +15,7 @@
#define __SGI_STL_CHAR_TRAITS_H
#include "string.h"
#include "wchar.h"
//#include "wchar.h"
#if defined(__STL_USE_NEW_IOSTREAMS) && !defined(__SGI_STL_IOSFWD)
#include "iosfwd"
@@ -137,9 +137,9 @@ public:
// Specialization for wchar_t.
__STL_TEMPLATE_NULL class char_traits<wchar_t>
: public __char_traits_base<wchar_t, wint_t>
{};
//__STL_TEMPLATE_NULL class char_traits<wchar_t>
// : public __char_traits_base<wchar_t, wint_t>
//{};
__STL_END_NAMESPACE

View File

@@ -23,6 +23,8 @@
__STL_BEGIN_NAMESPACE
#include "string.h"
class __Named_exception : public __STL_EXCEPTION_BASE {
public:
__Named_exception(const string& __str) {

View File

@@ -965,6 +965,9 @@ _OutputIter rotate_copy(_ForwardIter __first, _ForwardIter __middle,
// whether we're using rand (part of the standard C library) or lrand48
// (not standard, but a much better choice whenever it's available).
// FIXME:
#define __STL_NO_DRAND48
template <class _Distance>
inline _Distance __random_number(_Distance __n) {
#ifdef __STL_NO_DRAND48

View File

@@ -552,6 +552,8 @@
# define __STD_QUALIFIER
# endif
#define __STL_NO_EXCEPTION_HEADER
# ifdef __STL_USE_EXCEPTIONS
# define __STL_TRY try
# define __STL_CATCH_ALL catch(...)

View File

@@ -27,7 +27,7 @@ template <class _CharT,
class basic_string;
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
//typedef basic_string<wchar_t> wstring;
static const char* __get_c_string(const string&);

View File

@@ -16,7 +16,7 @@
#include "stl_config.h"
#include "stl_string_fwd.h"
#include "ctype.h"
//#include "ctype.h"
#include "functional"
#include "stl_ctraits_fns.h"
#include "stdexcept"
@@ -1097,15 +1097,15 @@ public: // Helper function for compare.
// Non-inline declarations.
template <class _CharT, class _Traits, class _Alloc>
const basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>::npos
const typename basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>::npos
= (basic_string<_CharT,_Traits,_Alloc>::size_type) -1;
// Change the string's capacity so that it is large enough to hold
// at least __res_arg elements, plus the terminating null. Note that,
// if __res_arg < capacity(), this member function may actually decrease
// the string's capacity.
template <class _CharT, class _Traits, class _Alloc>
template <class _CharT, class _Traits, class _Alloc>
void basic_string<_CharT,_Traits,_Alloc>::reserve(size_type __res_arg) {
if (__res_arg > max_size())
_M_throw_length_error();
@@ -1118,7 +1118,7 @@ void basic_string<_CharT,_Traits,_Alloc>::reserve(size_type __res_arg) {
__new_finish = uninitialized_copy(_M_start, _M_finish, __new_start);
_M_construct_null(__new_finish);
}
__STL_UNWIND((destroy(__new_start, __new_finish),
__STL_UNWIND((destroy(__new_start, __new_finish),
_M_deallocate(__new_start, __n)));
destroy(_M_start, _M_finish + 1);
@@ -1128,8 +1128,8 @@ void basic_string<_CharT,_Traits,_Alloc>::reserve(size_type __res_arg) {
_M_end_of_storage = __new_start + __n;
}
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>&
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>&
basic_string<_CharT,_Traits,_Alloc>::append(size_type __n, _CharT __c) {
if (__n > max_size() || size() > max_size() - __n)
_M_throw_length_error();
@@ -1149,10 +1149,10 @@ basic_string<_CharT,_Traits,_Alloc>::append(size_type __n, _CharT __c) {
#ifdef __STL_MEMBER_TEMPLATES
template <class _Tp, class _Traits, class _Alloc>
template <class _Tp, class _Traits, class _Alloc>
template <class _InputIterator>
basic_string<_Tp, _Traits, _Alloc>&
basic_string<_Tp, _Traits, _Alloc>::append(_InputIterator __first,
basic_string<_Tp, _Traits, _Alloc>&
basic_string<_Tp, _Traits, _Alloc>::append(_InputIterator __first,
_InputIterator __last,
input_iterator_tag) {
for ( ; __first != __last ; ++__first)
@@ -1160,10 +1160,10 @@ basic_string<_Tp, _Traits, _Alloc>::append(_InputIterator __first,
return *this;
}
template <class _Tp, class _Traits, class _Alloc>
template <class _Tp, class _Traits, class _Alloc>
template <class _ForwardIter>
basic_string<_Tp, _Traits, _Alloc>&
basic_string<_Tp, _Traits, _Alloc>::append(_ForwardIter __first,
basic_string<_Tp, _Traits, _Alloc>&
basic_string<_Tp, _Traits, _Alloc>::append(_ForwardIter __first,
_ForwardIter __last,
forward_iterator_tag) {
if (__first != __last) {
@@ -1189,7 +1189,7 @@ basic_string<_Tp, _Traits, _Alloc>::append(_ForwardIter __first,
_M_deallocate_block();
_M_start = __new_start;
_M_finish = __new_finish;
_M_end_of_storage = __new_start + __len;
_M_end_of_storage = __new_start + __len;
}
else {
_ForwardIter __f1 = __first;
@@ -1203,13 +1203,13 @@ basic_string<_Tp, _Traits, _Alloc>::append(_ForwardIter __first,
_M_finish += __n;
}
}
return *this;
return *this;
}
#else /* __STL_MEMBER_TEMPLATES */
template <class _Tp, class _Traits, class _Alloc>
basic_string<_Tp, _Traits, _Alloc>&
template <class _Tp, class _Traits, class _Alloc>
basic_string<_Tp, _Traits, _Alloc>&
basic_string<_Tp, _Traits, _Alloc>::append(const _Tp* __first,
const _Tp* __last)
{
@@ -1233,7 +1233,7 @@ basic_string<_Tp, _Traits, _Alloc>::append(const _Tp* __first,
_M_deallocate_block();
_M_start = __new_start;
_M_finish = __new_finish;
_M_end_of_storage = __new_start + __len;
_M_end_of_storage = __new_start + __len;
}
else {
const _Tp* __f1 = __first;
@@ -1247,13 +1247,13 @@ basic_string<_Tp, _Traits, _Alloc>::append(const _Tp* __first,
_M_finish += __n;
}
}
return *this;
return *this;
}
#endif /* __STL_MEMBER_TEMPLATES */
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>&
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>&
basic_string<_CharT,_Traits,_Alloc>::assign(size_type __n, _CharT __c) {
if (__n <= size()) {
_Traits::assign(_M_start, __n, __c);
@@ -1268,7 +1268,7 @@ basic_string<_CharT,_Traits,_Alloc>::assign(size_type __n, _CharT __c) {
#ifdef __STL_MEMBER_TEMPLATES
template <class _CharT, class _Traits, class _Alloc>
template <class _CharT, class _Traits, class _Alloc>
template <class _InputIter>
basic_string<_CharT,_Traits,_Alloc>& basic_string<_CharT,_Traits,_Alloc>
::_M_assign_dispatch(_InputIter __f, _InputIter __l, __false_type)
@@ -1288,9 +1288,9 @@ basic_string<_CharT,_Traits,_Alloc>& basic_string<_CharT,_Traits,_Alloc>
#endif /* __STL_MEMBER_TEMPLATES */
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>&
basic_string<_CharT,_Traits,_Alloc>::assign(const _CharT* __f,
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>&
basic_string<_CharT,_Traits,_Alloc>::assign(const _CharT* __f,
const _CharT* __l)
{
const ptrdiff_t __n = __l - __f;
@@ -1306,7 +1306,7 @@ basic_string<_CharT,_Traits,_Alloc>::assign(const _CharT* __f,
}
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>::iterator
typename basic_string<_CharT,_Traits,_Alloc>::iterator
basic_string<_CharT,_Traits,_Alloc>
::_M_insert_aux(basic_string<_CharT,_Traits,_Alloc>::iterator __p,
_CharT __c)
@@ -1331,7 +1331,7 @@ basic_string<_CharT,_Traits,_Alloc>
__new_finish = uninitialized_copy(__p, _M_finish, __new_finish);
_M_construct_null(__new_finish);
}
__STL_UNWIND((destroy(__new_start,__new_finish),
__STL_UNWIND((destroy(__new_start,__new_finish),
_M_deallocate(__new_start,__len)));
destroy(_M_start, _M_finish + 1);
_M_deallocate_block();
@@ -1366,13 +1366,13 @@ void basic_string<_CharT,_Traits,_Alloc>
uninitialized_copy(__position, __old_finish + 1, _M_finish);
_M_finish += __elems_after;
}
__STL_UNWIND((destroy(__old_finish + 1, _M_finish),
__STL_UNWIND((destroy(__old_finish + 1, _M_finish),
_M_finish = __old_finish));
_Traits::assign(__position, __elems_after + 1, __c);
}
}
else {
const size_type __old_size = size();
const size_type __old_size = size();
const size_type __len = __old_size + max(__old_size, __n) + 1;
iterator __new_start = _M_allocate(__len);
iterator __new_finish = __new_start;
@@ -1389,7 +1389,7 @@ void basic_string<_CharT,_Traits,_Alloc>
_M_deallocate_block();
_M_start = __new_start;
_M_finish = __new_finish;
_M_end_of_storage = __new_start + __len;
_M_end_of_storage = __new_start + __len;
}
}
}
@@ -1399,7 +1399,7 @@ void basic_string<_CharT,_Traits,_Alloc>
template <class _Tp, class _Traits, class _Alloc>
template <class _InputIter>
void basic_string<_Tp, _Traits, _Alloc>::insert(iterator __p,
_InputIter __first,
_InputIter __first,
_InputIter __last,
input_iterator_tag)
{
@@ -1411,9 +1411,9 @@ void basic_string<_Tp, _Traits, _Alloc>::insert(iterator __p,
template <class _CharT, class _Traits, class _Alloc>
template <class _ForwardIter>
void
void
basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
_ForwardIter __first,
_ForwardIter __first,
_ForwardIter __last,
forward_iterator_tag)
{
@@ -1440,13 +1440,13 @@ basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
uninitialized_copy(__position, __old_finish + 1, _M_finish);
_M_finish += __elems_after;
}
__STL_UNWIND((destroy(__old_finish + 1, _M_finish),
__STL_UNWIND((destroy(__old_finish + 1, _M_finish),
_M_finish = __old_finish));
_M_copy(__first, __mid, __position);
}
}
else {
const size_type __old_size = size();
const size_type __old_size = size();
const size_type __len
= __old_size + max(__old_size, static_cast<size_type>(__n)) + 1;
pointer __new_start = _M_allocate(__len);
@@ -1464,7 +1464,7 @@ basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
_M_deallocate_block();
_M_start = __new_start;
_M_finish = __new_finish;
_M_end_of_storage = __new_start + __len;
_M_end_of_storage = __new_start + __len;
}
}
}
@@ -1472,9 +1472,9 @@ basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
#else /* __STL_MEMBER_TEMPLATES */
template <class _CharT, class _Traits, class _Alloc>
void
void
basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
const _CharT* __first,
const _CharT* __first,
const _CharT* __last)
{
if (__first != __last) {
@@ -1499,13 +1499,13 @@ basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
uninitialized_copy(__position, __old_finish + 1, _M_finish);
_M_finish += __elems_after;
}
__STL_UNWIND((destroy(__old_finish + 1, _M_finish),
__STL_UNWIND((destroy(__old_finish + 1, _M_finish),
_M_finish = __old_finish));
_M_copy(__first, __mid, __position);
}
}
else {
const size_type __old_size = size();
const size_type __old_size = size();
const size_type __len
= __old_size + max(__old_size, static_cast<size_type>(__n)) + 1;
pointer __new_start = _M_allocate(__len);
@@ -1523,7 +1523,7 @@ basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
_M_deallocate_block();
_M_start = __new_start;
_M_finish = __new_finish;
_M_end_of_storage = __new_start + __len;
_M_end_of_storage = __new_start + __len;
}
}
}
@@ -1554,7 +1554,7 @@ template <class _InputIter>
basic_string<_CharT,_Traits,_Alloc>&
basic_string<_CharT,_Traits,_Alloc>
::replace(iterator __first, iterator __last, _InputIter __f, _InputIter __l,
input_iterator_tag)
input_iterator_tag)
{
for ( ; __first != __last && __f != __l; ++__first, ++__f)
_Traits::assign(*__first, *__f);
@@ -1572,7 +1572,7 @@ basic_string<_CharT,_Traits,_Alloc>&
basic_string<_CharT,_Traits,_Alloc>
::replace(iterator __first, iterator __last,
_ForwardIter __f, _ForwardIter __l,
forward_iterator_tag)
forward_iterator_tag)
{
difference_type __n = 0;
distance(__f, __l, __n);
@@ -1615,24 +1615,24 @@ basic_string<_CharT,_Traits,_Alloc>
#endif /* __STL_MEMBER_TEMPLATES */
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>::size_type
typename basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>
::find(const _CharT* __s, size_type __pos, size_type __n) const
::find(const _CharT* __s, size_type __pos, size_type __n) const
{
if (__pos + __n > size())
return npos;
else {
const const_iterator __result =
search(_M_start + __pos, _M_finish,
search(_M_start + __pos, _M_finish,
__s, __s + __n, _Eq_traits<_Traits>());
return __result != _M_finish ? __result - begin() : npos;
}
}
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>::size_type
typename basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>
::find(_CharT __c, size_type __pos) const
::find(_CharT __c, size_type __pos) const
{
if (__pos >= size())
return npos;
@@ -1642,12 +1642,12 @@ basic_string<_CharT,_Traits,_Alloc>
bind2nd(_Eq_traits<_Traits>(), __c));
return __result != _M_finish ? __result - begin() : npos;
}
}
}
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>::size_type
typename basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>
::rfind(const _CharT* __s, size_type __pos, size_type __n) const
::rfind(const _CharT* __s, size_type __pos, size_type __n) const
{
const size_t __len = size();
@@ -1665,9 +1665,9 @@ basic_string<_CharT,_Traits,_Alloc>
}
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>::size_type
typename basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>
::rfind(_CharT __c, size_type __pos) const
::rfind(_CharT __c, size_type __pos) const
{
const size_type __len = size();
@@ -1683,7 +1683,7 @@ basic_string<_CharT,_Traits,_Alloc>
}
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>::size_type
typename basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>
::find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
{
@@ -1699,7 +1699,7 @@ basic_string<_CharT,_Traits,_Alloc>
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>::size_type
typename basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>
::find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
{
@@ -1719,7 +1719,7 @@ basic_string<_CharT,_Traits,_Alloc>
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>::size_type
typename basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>
::find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
{
@@ -1733,7 +1733,7 @@ basic_string<_CharT,_Traits,_Alloc>
}
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>::size_type
typename basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>
::find_first_not_of(_CharT __c, size_type __pos) const
{
@@ -1745,12 +1745,12 @@ basic_string<_CharT,_Traits,_Alloc>
not1(bind2nd(_Eq_traits<_Traits>(), __c)));
return __result != _M_finish ? __result - begin() : npos;
}
}
}
template <class _CharT, class _Traits, class _Alloc>
basic_string<_CharT,_Traits,_Alloc>::size_type
typename basic_string<_CharT,_Traits,_Alloc>::size_type
basic_string<_CharT,_Traits,_Alloc>
::find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
::find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
{
const size_type __len = size();
@@ -1767,9 +1767,9 @@ basic_string<_CharT,_Traits,_Alloc>
}
template <class _Tp, class _Traits, class _Alloc>
basic_string<_Tp, _Traits, _Alloc>::size_type
typename basic_string<_Tp, _Traits, _Alloc>::size_type
basic_string<_Tp, _Traits, _Alloc>
::find_last_not_of(_Tp __c, size_type __pos) const
::find_last_not_of(_Tp __c, size_type __pos) const
{
const size_type __len = size();
@@ -2015,7 +2015,7 @@ inline void swap(basic_string<_CharT,_Traits,_Alloc>& __x,
#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
// I/O.
// I/O.
//#ifndef __STL_USE_NEW_IOSTREAMS
//__STL_END_NAMESPACE
@@ -2373,7 +2373,7 @@ __STL_BEGIN_NAMESPACE
template <class _CharT, class _Traits, class _Alloc>
size_t __stl_string_hash(const basic_string<_CharT,_Traits,_Alloc>& __s) {
unsigned long __h = 0;
for (basic_string<_CharT,_Traits,_Alloc>::const_iterator __i = __s.begin();
for (typename basic_string<_CharT,_Traits,_Alloc>::const_iterator __i = __s.begin();
__i != __s.end();
++__i)
__h = 5*__h + *__i;