mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-18 01:46:08 +08:00
Initial parts of string support. Incomplete.
This commit is contained in:
parent
d1651535e1
commit
bbf835a8a6
139
basic_string.h
Normal file
139
basic_string.h
Normal file
@ -0,0 +1,139 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2016 jwellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __ETL_BASIC_STRING__
|
||||
#define __ETL_BASIC_STRING__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <iterator>
|
||||
|
||||
#include "ibasic_string.h"
|
||||
#include "container.h"
|
||||
#include "alignment.h"
|
||||
#include "array.h"
|
||||
|
||||
//*****************************************************************************
|
||||
///\defgroup basic_string basic_string
|
||||
/// A basic_string with the capacity defined at compile time.
|
||||
///\ingroup containers
|
||||
//*****************************************************************************
|
||||
|
||||
namespace etl
|
||||
{
|
||||
template <typename T, const size_t MAX_SIZE_>
|
||||
//***************************************************************************
|
||||
/// A basic_string implementation that uses a fixed size buffer.
|
||||
///\tparam T The element type.
|
||||
///\tparam MAX_SIZE_ The maximum number of elements that can be stored.
|
||||
///\ingroup basic_string
|
||||
//***************************************************************************
|
||||
class basic_string : public ibasic_string<T>
|
||||
{
|
||||
public:
|
||||
|
||||
static const size_t MAX_SIZE = MAX_SIZE_;
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
//*************************************************************************
|
||||
basic_string()
|
||||
: ibasic_string<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
|
||||
{
|
||||
ibasic_string<T>::initialise();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor, with size.
|
||||
///\param initialSize The initial size of the basic_string.
|
||||
//*************************************************************************
|
||||
explicit basic_string(size_t initialSize)
|
||||
: ibasic_string<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
|
||||
{
|
||||
ibasic_string<T>::initialise();
|
||||
ibasic_string<T>::resize(initialSize);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor, from null terminated text.
|
||||
///\param text The initial text of the basic_string.
|
||||
//*************************************************************************
|
||||
basic_string(const T* text)
|
||||
: ibasic_string<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
|
||||
{
|
||||
ibasic_string<T>::initialise();
|
||||
ibasic_string<T>::assign(text, text + strlen)
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor, from initial size and value.
|
||||
///\param initialSize The initial size of the basic_string.
|
||||
///\param value The value to fill the basic_string with.
|
||||
//*************************************************************************
|
||||
basic_string(size_t initialSize, typename ibasic_string<T>::parameter_t value)
|
||||
: ibasic_string<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
|
||||
{
|
||||
ibasic_string<T>::initialise();
|
||||
ibasic_string<T>::resize(initialSize, value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor, from an iterator range.
|
||||
///\tparam TIterator The iterator type.
|
||||
///\param first The iterator to the first element.
|
||||
///\param last The iterator to the last element + 1.
|
||||
//*************************************************************************
|
||||
template <typename TIterator>
|
||||
basic_string(TIterator first, TIterator last)
|
||||
: ibasic_string<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
|
||||
{
|
||||
ibasic_string<T>::assign(first, last);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Assignment operator.
|
||||
//*************************************************************************
|
||||
basic_string& operator = (const basic_string& rhs)
|
||||
{
|
||||
if (&rhs != this)
|
||||
{
|
||||
ibasic_string<T>::assign(rhs.cbegin(), rhs.cend());
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
typename etl::aligned_storage<sizeof(T) * MAX_SIZE, etl::alignment_of<T>::value>::type buffer;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
336
char_traits.h
Normal file
336
char_traits.h
Normal file
@ -0,0 +1,336 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2016 jwellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __ETL_CHAR_TRAITS__
|
||||
#define __ETL_CHAR_TRAITS__
|
||||
|
||||
#include "stdint.h"
|
||||
#include "algorithms.h"
|
||||
|
||||
//*****************************************************************************
|
||||
///\defgroup char_traits char_traits
|
||||
/// Character traits
|
||||
///\ingroup string
|
||||
//*****************************************************************************
|
||||
|
||||
namespace etl
|
||||
{
|
||||
template<typename T> struct char_traits;
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation for char.
|
||||
//***************************************************************************
|
||||
template<> struct char_traits<char>
|
||||
{
|
||||
typedef char char_type;
|
||||
typedef int int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef char state_type;
|
||||
|
||||
//*************************************************************************
|
||||
static bool eq(char_type a, char_type b)
|
||||
{
|
||||
return a == b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static bool lt(char_type a, char_type b)
|
||||
{
|
||||
return a < b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static size_t length(const char* str)
|
||||
{
|
||||
size_t count = 0;
|
||||
|
||||
if (str != 0)
|
||||
{
|
||||
while (*str++ != 0)
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static void assign(char_type& r, const char_type& c)
|
||||
{
|
||||
r = a;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static char_type* assign(char_type* p, size_t n, char_type c)
|
||||
{
|
||||
if (p != 0)
|
||||
{
|
||||
std::fill_n(p, n, c);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static char_type* move(char_type* dest, const char_type* src, std::size_t count)
|
||||
{
|
||||
if ((dest < src) || (dest > (src + count))
|
||||
{
|
||||
etl::copy_n(src, src + count, dest);
|
||||
}
|
||||
else
|
||||
{
|
||||
etl::copy_n(std::reverse_iterator(src + count),
|
||||
std::reverse_iterator(src),
|
||||
std::reverse_iterator(dest + count));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation for wchar_t
|
||||
//***************************************************************************
|
||||
template <> struct char_traits<wchar_t>
|
||||
{
|
||||
typedef wchar_t char_type;
|
||||
typedef wchar_t int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef char state_type;
|
||||
|
||||
//*************************************************************************
|
||||
static bool eq(char_type a, char_type b)
|
||||
{
|
||||
return a == b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static bool lt(char_type a, char_type b)
|
||||
{
|
||||
return a < b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static size_t length(const wchar_t* str)
|
||||
{
|
||||
size_t count = 0;
|
||||
|
||||
if (str != 0)
|
||||
{
|
||||
while (*str++ != 0)
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static void assign(char_type& r, const char_type& c)
|
||||
{
|
||||
r = a;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static char_type* assign(char_type* p, size_t n, char_type c)
|
||||
{
|
||||
if (p != 0)
|
||||
{
|
||||
std::fill_n(p, n, c);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static char_type* move(char_type* dest, const char_type* src, std::size_t count)
|
||||
{
|
||||
if ((dest < src) || (dest > (src + count))
|
||||
{
|
||||
etl::copy_n(src, src + count, dest);
|
||||
}
|
||||
else
|
||||
{
|
||||
etl::copy_n(std::reverse_iterator(src + count),
|
||||
std::reverse_iterator(src),
|
||||
std::reverse_iterator(dest + count));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation for char16_t.
|
||||
//***************************************************************************
|
||||
template <> struct char_traits<char16_t>
|
||||
{
|
||||
typedef char16_t char_type;
|
||||
typedef uint_least16_t int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef char state_type;
|
||||
|
||||
//*************************************************************************
|
||||
static bool eq(char_type a, char_type b)
|
||||
{
|
||||
return a == b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static bool lt(char_type a, char_type b)
|
||||
{
|
||||
return a < b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static size_t length(const char16_t* str)
|
||||
{
|
||||
size_t count = 0;
|
||||
|
||||
if (str != 0)
|
||||
{
|
||||
while (*str++ != 0)
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static void assign(char_type& r, const char_type& c)
|
||||
{
|
||||
r = a;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static char_type* assign(char_type* p, size_t n, char_type c)
|
||||
{
|
||||
if (p != 0)
|
||||
{
|
||||
std::fill_n(p, n, c);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static char_type* move(char_type* dest, const char_type* src, std::size_t count)
|
||||
{
|
||||
if ((dest < src) || (dest > (src + count))
|
||||
{
|
||||
etl::copy_n(src, src + count, dest);
|
||||
}
|
||||
else
|
||||
{
|
||||
etl::copy_n(std::reverse_iterator(src + count),
|
||||
std::reverse_iterator(src),
|
||||
std::reverse_iterator(dest + count));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation for char32_t.
|
||||
//***************************************************************************
|
||||
template <> struct char_traits<char32_t>
|
||||
{
|
||||
typedef char16_t char_type;
|
||||
typedef uint_least32_t int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef char state_type;
|
||||
|
||||
//*************************************************************************
|
||||
static bool eq(char_type a, char_type b)
|
||||
{
|
||||
return a == b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static bool lt(char_type a, char_type b)
|
||||
{
|
||||
return a < b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static size_t length(const char32_t* str)
|
||||
{
|
||||
size_t count = 0;
|
||||
|
||||
if (str != 0)
|
||||
{
|
||||
while (*str++ != 0)
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static void assign(char_type& r, const char_type& c)
|
||||
{
|
||||
r = a;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static char_type* assign(char_type* p, size_t n, char_type c)
|
||||
{
|
||||
if (p != 0)
|
||||
{
|
||||
std::fill_n(p, n, c);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static char_type* move(char_type* dest, const char_type* src, std::size_t count)
|
||||
{
|
||||
if ((dest < src) || (dest > (src + count))
|
||||
{
|
||||
etl::copy_n(src, src + count, dest);
|
||||
}
|
||||
else
|
||||
{
|
||||
etl::copy_n(std::reverse_iterator(src + count),
|
||||
std::reverse_iterator(src),
|
||||
std::reverse_iterator(dest + count));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user