Skip to content

Commit

Permalink
Turn String_view into template Basic_string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Oct 19, 2016
1 parent 7ae7166 commit be48e51
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 38 deletions.
41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,38 @@ Remarks: This function shall not participate in overload resolution unless `std:

namespace jegp {

class String_view;
template <class charT, class traits = std::char_traits<charT>>
class Basic_string_view;

using String_view = Basic_string_view<char>;
using u16String_view = Basic_string_view<char16_t>;
using u32String_view = Basic_string_view<char32_t>;
using wString_view = Basic_string_view<wchar_t>;

} // jegp namespace
```
#### 2.1 Class `String_view`
#### 2.1 Class template `Basic_string_view`
A mutable string view.
Describes a mutable string view.
```C++
class String_view : public std::experimental::string_view {
using Base = std::experimental::string_view; // exposition only
template <class charT, class traits = std::char_traits<charT>>
class Basic_string_view
: public std::experimental::basic_string_view<charT,traits> {
private:
using Base =
std::experimental::basic_string_view<charT,traits>; // exposition only
public:
using typename Base::pointer;
using typename Base::reference;
using iterator = pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using typename Base::size_type;
constexpr String_view() noexcept = default;
constexpr String_view(char* str);
constexpr String_view(char* str, size_type len);
constexpr Basic_string_view() noexcept = default;
constexpr Basic_string_view(charT* str);
constexpr Basic_string_view(charT* str, size_type len);
using Base::begin;
using Base::end;
Expand All @@ -88,15 +101,16 @@ public:
using Base::data;
constexpr pointer data() noexcept;
constexpr String_view substr(size_type pos = 0, size_type n = npos) const;
constexpr Basic_string_view substr(
size_type pos = 0, size_type n = Base::npos) const;
};
```

##### 2.1.1 Construction

```C++
constexpr String_view(char* str);
constexpr String_view(char* str, size_type len);
constexpr Basic_string_view(charT* str);
constexpr Basic_string_view(charT* str, size_type len);
```
Effects: Initializes the base with the same argument list.
Expand All @@ -110,7 +124,7 @@ Returns: `data()`.
```C++
constexpr iterator end() noexcept;
```
Returns: `begin() + size()`.
Returns: `begin() + Base::size()`.

```C++
reverse_iterator rbegin() noexcept;
Expand Down Expand Up @@ -152,6 +166,7 @@ Returns: `const_cast<pointer>(Base::data())`.
##### 2.1.4 String operations

```C++
constexpr String_view substr(size_type pos = 0, size_type n = npos) const;
constexpr Basic_string_view substr(size_type pos = 0, size_type n = Base::npos)
const;
```
Returns: A string view that equals the value of `Base::substr(pos,n)`.
78 changes: 53 additions & 25 deletions include/jegp/String_view.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
#ifndef JEGP_STRING_VIEW_HPP
#define JEGP_STRING_VIEW_HPP

#include <iterator>
#include <string> // char_traits
#include <iterator> // reverse_iterator
#include <experimental/string_view>

namespace jegp {

class String_view : public std::experimental::string_view {
using Base = std::experimental::string_view;
template <class charT, class traits = std::char_traits<charT>>
class Basic_string_view
: public std::experimental::basic_string_view<charT,traits> {
private:
using Base = std::experimental::basic_string_view<charT,traits>;

constexpr String_view(Base) noexcept;
constexpr Basic_string_view(Base) noexcept;
public:
using typename Base::pointer;
using typename Base::reference;
using iterator = pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using typename Base::size_type;

constexpr String_view() noexcept = default;
constexpr String_view(char* str);
constexpr String_view(char* str, size_type len);
constexpr Basic_string_view() noexcept = default;
constexpr Basic_string_view(charT* str);
constexpr Basic_string_view(charT* str, size_type len);

using Base::begin;
using Base::end;
Expand All @@ -41,9 +48,15 @@ class String_view : public std::experimental::string_view {
using Base::data;
constexpr pointer data() noexcept;

constexpr String_view substr(size_type pos = 0, size_type n = npos) const;
constexpr Basic_string_view substr(
size_type pos = 0, size_type n = Base::npos) const;
};

using String_view = Basic_string_view<char>;
using u16String_view = Basic_string_view<char16_t>;
using u32String_view = Basic_string_view<char32_t>;
using wString_view = Basic_string_view<wchar_t>;

} // jegp namespace

//------------------------------------------------------------------------------
Expand All @@ -52,70 +65,85 @@ class String_view : public std::experimental::string_view {

namespace jegp {

constexpr String_view::String_view(Base b) noexcept
: std::experimental::string_view{b}
template <class C, class T>
constexpr Basic_string_view<C,T>::Basic_string_view(Base b) noexcept
: Base{b}
{
}

// Construction ----------------------------------------------------------------

constexpr String_view::String_view(char* str)
: std::experimental::string_view{str}
template <class charT, class T>
constexpr Basic_string_view<charT,T>::Basic_string_view(charT* str)
: Base{str}
{
}
constexpr String_view::String_view(char* str, size_type len)
: std::experimental::string_view{str,len}
template <class charT, class T>
constexpr Basic_string_view<charT,T>::Basic_string_view(
charT* str, size_type len)
: Base{str,len}
{
}

// Iterator support ------------------------------------------------------------

constexpr auto String_view::begin() noexcept -> iterator
template <class C, class T>
constexpr auto Basic_string_view<C,T>::begin() noexcept -> iterator
{
return data();
}
constexpr auto String_view::end() noexcept -> iterator
template <class C, class T>
constexpr auto Basic_string_view<C,T>::end() noexcept -> iterator
{
return begin() + size();
return begin() + Base::size();
}

inline /*constexpr*/ auto String_view::rbegin() noexcept -> reverse_iterator
template <class C, class T>
/*constexpr*/ auto Basic_string_view<C,T>::rbegin() noexcept -> reverse_iterator
{
return reverse_iterator{end()};
}
inline /*constexpr*/ auto String_view::rend() noexcept -> reverse_iterator
template <class C, class T>
/*constexpr*/ auto Basic_string_view<C,T>::rend() noexcept -> reverse_iterator
{
return reverse_iterator{begin()};
}

// Element access --------------------------------------------------------------

constexpr auto String_view::operator[](size_type pos) -> reference
template <class C, class T>
constexpr auto Basic_string_view<C,T>::operator[](size_type pos) -> reference
{
return const_cast<reference>(Base::operator[](pos));
}
constexpr auto String_view::at(size_type pos) -> reference
template <class C, class T>
constexpr auto Basic_string_view<C,T>::at(size_type pos) -> reference
{
return const_cast<reference>(Base::at(pos));
}

constexpr auto String_view::front() -> reference
template <class C, class T>
constexpr auto Basic_string_view<C,T>::front() -> reference
{
return const_cast<reference>(Base::front());
}
constexpr auto String_view::back() -> reference
template <class C, class T>
constexpr auto Basic_string_view<C,T>::back() -> reference
{
return const_cast<reference>(Base::back());
}

constexpr auto String_view::data() noexcept -> pointer
template <class C, class T>
constexpr auto Basic_string_view<C,T>::data() noexcept -> pointer
{
return const_cast<pointer>(Base::data());
}

// String operations -----------------------------------------------------------

constexpr String_view String_view::substr(size_type pos, size_type n) const
template <class C, class T>
constexpr auto Basic_string_view<C,T>::substr(size_type pos, size_type n) const
-> Basic_string_view
{
return Base::substr(pos,n);
}
Expand Down

0 comments on commit be48e51

Please sign in to comment.