diff --git a/basic_string.h b/basic_string.h new file mode 100644 index 00000000..7ba871e9 --- /dev/null +++ b/basic_string.h @@ -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 +#include +#include + +#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 + //*************************************************************************** + /// 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 + { + public: + + static const size_t MAX_SIZE = MAX_SIZE_; + + //************************************************************************* + /// Constructor. + //************************************************************************* + basic_string() + : ibasic_string(reinterpret_cast(&buffer), MAX_SIZE) + { + ibasic_string::initialise(); + } + + //************************************************************************* + /// Constructor, with size. + ///\param initialSize The initial size of the basic_string. + //************************************************************************* + explicit basic_string(size_t initialSize) + : ibasic_string(reinterpret_cast(&buffer), MAX_SIZE) + { + ibasic_string::initialise(); + ibasic_string::resize(initialSize); + } + + //************************************************************************* + /// Constructor, from null terminated text. + ///\param text The initial text of the basic_string. + //************************************************************************* + basic_string(const T* text) + : ibasic_string(reinterpret_cast(&buffer), MAX_SIZE) + { + ibasic_string::initialise(); + ibasic_string::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::parameter_t value) + : ibasic_string(reinterpret_cast(&buffer), MAX_SIZE) + { + ibasic_string::initialise(); + ibasic_string::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 + basic_string(TIterator first, TIterator last) + : ibasic_string(reinterpret_cast(&buffer), MAX_SIZE) + { + ibasic_string::assign(first, last); + } + + //************************************************************************* + /// Assignment operator. + //************************************************************************* + basic_string& operator = (const basic_string& rhs) + { + if (&rhs != this) + { + ibasic_string::assign(rhs.cbegin(), rhs.cend()); + } + + return *this; + } + + private: + + typename etl::aligned_storage::value>::type buffer; + }; +} + +#endif diff --git a/char_traits.h b/char_traits.h new file mode 100644 index 00000000..98d65f22 --- /dev/null +++ b/char_traits.h @@ -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 struct char_traits; + + //*************************************************************************** + /// Specialisation for char. + //*************************************************************************** + template<> struct char_traits + { + 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 + { + 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 + { + 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 + { + 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