Skip to content

Commit

Permalink
Add String_view, a mutable string view
Browse files Browse the repository at this point in the history
Add header <jegp/String_view.hpp> for defining String_view.
Numerate specification clauses.
  • Loading branch information
JohelEGP committed Sep 3, 2016
1 parent df53661 commit ba8003f
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 2 deletions.
125 changes: 123 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ Generic library components for my C++ projects.

* A partial implementation of the upcoming standard following C++14
- Variable type traits
* Technical Specifications
- Library Fundamentals
+ `<experimental/string_view>`

## Specification

This project uses [Semantic Versioning 2.0.0](http://semver.org/spec/v2.0.0.html).

This specification is the public API.

### Header `<jegp/utility.hpp>` synopsis
### 1 Header `<jegp/utility.hpp>` synopsis

```C++
namespace jegp {
Expand All @@ -24,7 +27,7 @@ constexpr auto underlying_value(Enum e) noexcept;
} // jegp namespace
```
#### `underlying_value`
#### 1.1 `underlying_value`
Casts an enumeration value to its underlying type.
Expand All @@ -34,3 +37,121 @@ constexpr auto underlying_value(Enum e) noexcept;
```
Returns: `static_cast<std::underlying_type_t<Enum>>(e)`.<br/>
Remarks: This function shall not participate in overload resolution unless `std::is_enum_v<Enum>` is `true`.

### 2 Header `<jegp/String_view.hpp>` synopsis

```C++
#include <experimental/string_view>

namespace jegp {

class String_view;

} // jegp namespace
```
#### 2.1 Class `String_view`
A mutable string view.
```C++
class String_view : public std::experimental::string_view {
using Base = std::experimental::string_view; // exposition only
public:
constexpr String_view() noexcept = default;
constexpr String_view(char* str);
constexpr String_view(char* str, size_type len);
using iterator = pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using Base::begin;
using Base::end;
constexpr iterator begin() noexcept;
constexpr iterator end() noexcept;
using Base::rbegin;
using Base::rend;
reverse_iterator rbegin() noexcept;
reverse_iterator rend() noexcept;
using Base::operator[];
using Base::at;
constexpr reference operator[](size_type pos);
constexpr reference at(size_type pos);
using Base::front;
using Base::back;
constexpr reference front();
constexpr reference back();
using Base::data;
constexpr pointer data() noexcept;
constexpr String_view substr(size_type pos = 0, size_type n = npos) const;
};
```

##### 2.1.1 Construction

```C++
constexpr String_view(char* str);
constexpr String_view(char* str, size_type len);
```
Effects: Initializes the base with the same argument list.
##### 2.1.2 Iterator support
```C++
constexpr iterator begin() noexcept;
```
Returns: `data()`.

```C++
constexpr iterator end() noexcept;
```
Returns: `begin() + size()`.

```C++
reverse_iterator rbegin() noexcept;
```
Returns: `reverse_iterator{end()}`.

```C++
reverse_iterator rend() noexcept;
```
Returns: `reverse_iterator{begin()}`.

##### 2.1.3 Element access

```C++
constexpr reference operator[](size_type pos);
```
Effects: Equivalent to `return const_cast<reference>(Base::operator[](pos));`.

```C++
constexpr reference at(size_type pos);
```
Effects: Equivalent to `return const_cast<reference>(Base::at(pos));`.
```C++
constexpr reference front();
```
Effects: Equivalent to `return const_cast<reference>(Base::front());`.

```C++
constexpr reference back();
```
Effects: Equivalent to `return const_cast<reference>(Base::back());`.

```C++
constexpr pointer data() noexcept;
```
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;
```
Returns: A string view that equals `Base{*this}.substr(pos,n)`.
125 changes: 125 additions & 0 deletions include/jegp/String_view.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#ifndef JEGP_STRING_VIEW_HPP
#define JEGP_STRING_VIEW_HPP

#include <iterator>
#include <experimental/string_view>

namespace jegp {

class String_view : public std::experimental::string_view {
using Base = std::experimental::string_view;

constexpr String_view(Base) noexcept;
public:
constexpr String_view() noexcept = default;
constexpr String_view(char* str);
constexpr String_view(char* str, size_type len);

using iterator = pointer;
using reverse_iterator = std::reverse_iterator<iterator>;

using Base::begin;
using Base::end;
constexpr iterator begin() noexcept;
constexpr iterator end() noexcept;

using Base::rbegin;
using Base::rend;
/*constexpr*/ reverse_iterator rbegin() noexcept;
/*constexpr*/ reverse_iterator rend() noexcept;

using Base::operator[];
using Base::at;
constexpr reference operator[](size_type pos);
constexpr reference at(size_type pos);

using Base::front;
using Base::back;
constexpr reference front();
constexpr reference back();

using Base::data;
constexpr pointer data() noexcept;

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

} // jegp namespace

//------------------------------------------------------------------------------
// "String_view.ipp" -----------------------------------------------------------
//------------------------------------------------------------------------------

namespace jegp {

constexpr String_view::String_view(Base b) noexcept
: std::experimental::string_view{b}
{
}

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

constexpr String_view::String_view(char* str)
: std::experimental::string_view{str}
{
}
constexpr String_view::String_view(char* str, size_type len)
: std::experimental::string_view{str,len}
{
}

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

constexpr auto String_view::begin() noexcept -> iterator
{
return data();
}
constexpr auto String_view::end() noexcept -> iterator
{
return begin() + size();
}

inline /*constexpr*/ auto String_view::rbegin() noexcept -> reverse_iterator
{
return reverse_iterator{end()};
}
inline /*constexpr*/ auto String_view::rend() noexcept -> reverse_iterator
{
return reverse_iterator{begin()};
}

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

constexpr auto String_view::operator[](size_type pos) -> reference
{
return const_cast<reference>(Base::operator[](pos));
}
constexpr auto String_view::at(size_type pos) -> reference
{
return const_cast<reference>(Base::at(pos));
}

constexpr auto String_view::front() -> reference
{
return const_cast<reference>(Base::front());
}
constexpr auto String_view::back() -> reference
{
return const_cast<reference>(Base::back());
}

constexpr auto String_view::data() noexcept -> pointer
{
return const_cast<pointer>(Base::data());
}

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

constexpr String_view String_view::substr(size_type pos, size_type n) const
{
return Base::substr(pos,n);
}

} // jegp namespace

#endif // JEGP_STRING_VIEW_HPP

0 comments on commit ba8003f

Please sign in to comment.