etl/docs/strings/string_view.md
John Wellbelove 4a88884b39
Issue/add hugo support for documentation (#1449)
* Add ranges

* Initial Hugo setup

* Work in progress

* Added selection for local or remote site

* Updated to 'light' theme

* Changed to using Hextra Hugo theme

* Changed to using Hextra Hugo theme

* Changed to Hextra Hugo theme

* Change to Hextra Hugo theme

* Updated Hugo setup.

* Updated Hugo setup.

# Conflicts:
#	docs/releases/_index.md

* Work in progress

* Added new fonts

Added new documentation

* Latest documentation updates

* Latest documentation updates

# Conflicts:
#	docs/containers/array.md
#	docs/containers/array_view.md
#	docs/containers/array_wrapper.md
#	docs/containers/bip_buffer_spsc_atomic.md
#	docs/containers/bitset.md
#	docs/containers/indirect_vector.md
#	docs/containers/vector.md
#	docs/getting-started/compilers.md

* Added bloom_filter markdown doc

* Added more documentation

Updated CSS for light and dark modes

* Fixed some menus

Added mode documentation files

* Updated CSS rules

Added badges to home page
Added uniqur_ptr + pool tutorial

* Fixed formatting on the home page markdown

Modified light amd dark code formatting

* Updated unique_ptr-with-pool

* Added container and shared message tutorials

* Updates to documentation

* Added const_multimap

* Updated source-formatting.md

* Added initial raw text files form Web site editor

* Innore coverage build directory

* Exported raw text documentation files from the web site editor

* Hugo updates

* Added Hugo intalation and markdown descriptions

* More addition to the documentation

* Added closure.md and updates to delegate.md

* Added format.md

* Added documentation for etl::delegate_observable, etl::function, Base64 codec

* Added io_port documentation

* Added basic_format_spec

* Added documentation for string_stream and string utilities.

* Added more documentation

Updated the documentation CSS

* Added documentation for clocks, day, duration

* Added more documentation for chrono classes

Updated callouts

* More chrono documentation

* Completed chrono documentation

* Maths functions documentation

* Completed maths documentation

* Completed maths documentation

* Completed maths documentation

* Completed maths documentation

* Added multiple documentation files

* Added iterator.md

* Added debug_count.md and versions.md

* Added debug_count.md and versions.md

* Added more documentation

* More documentation

* Added some design pattern documentation

Modified some of the layout files
Modified the About documentation

* Converted more documentation pages

Modified the site CSS

* Added more documentation

Moced some documentation files to new directories

* Added more documentation

Tweaks to CSS

* Added callback_timer_deferred_locked documentation

* Added callback_timer_locked documentation

* More documentation updates

* More documentation updates

* More documentation updates

* New documentation files.

Harmonised file name format

* New documentation files.

* Multiple document updates

* Multiple document updates

* Final conversion of web pages

* Updates before PR

* Updates before PR

* Updates before PR

# Conflicts:
#	docs/blog/_index.md

* Final pre PR updates

* Updates to message framework documentation

* Renamed directory

* Fix spelling

* Added author and date to blog files

Moved documentation files merged from development

* Fixed 'Description' typo

* Fix typos

# Conflicts:
#	docs/IO/io_port.md
#	docs/containers/sets/const-multiset.md
#	docs/containers/sets/const-set.md
#	docs/maths/correlation.md
#	docs/maths/gamma.md

* Renamed two files to lower case

* Minor renaming

* Added author and date

* Updated callout on bresenham_line.md

Added support for showing the ETL version on the documentation first page, by copying the version.txt file as a hugo asset.
Updated the Python 'update_release.py' to copy 'version.txt'

* Replace space in filename with hyphen.

Added more information to hugo-commands.md

* Replace space in filename with hyphen.

Added more information to hugo-commands.md

# Conflicts:
#	docs/getting-started/view-the-docs-locally/hugo-commands.md

* Added a link to pseudo_moving_average.md

* Updated title pages for groups

* Fixed missing 404 for non-existent pages

* Fixed coordinate variable names in the 'Calculating the intersection' example

---------

Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.co.uk>
2026-06-06 13:12:44 +01:00

437 lines
9.2 KiB
Markdown

---
title: "basic_string_view"
---
{{< callout type="info">}}
Header: `string_view.h`
Similar to:
`std::basic_string_view`
`std::string_view`
`std::wstring_view`
`std::u8string_view`
`std::u16string_view`
`std::u32string_view`
{{< /callout >}}
This class implements a view in to a range of a `C` string, `etl::string` (+ variants), `std::string` (+ variants).
## Classes
```cpp
etl::basic_string_view<typename T, typename TTraits = etl::char_traits<T>>
```
## Types
```cpp
etl::string_view
etl::wstring_view
etl::u8string_view;
etl::u16string_view
etl::u32string_view
```
## Member types
```cpp
value_type T
size_type std::size_t
difference_type std::ptrdiff_t
reference value_type&
const_reference const value_type&
pointer value_type*
const_pointer const value_type*
iterator Random access iterator
const_iterator Constant random access iterator
reverse_iterator std::reverse_iterator<iterator>
const_reverse_iterator std::reverse_iterator<const_iterator>
```
## Constants
```cpp
size_t npos
```
**Description**
An end of view indicator by the functions that expect a view index.
An error indicator for functions that return a view index.
## Constructors
```cpp
ETL_CONSTEXPR basic_string_view()
```
**Description**
Default constructor.
---
```cpp
ETL_CONSTEXPR basic_string_view(const T* begin, size_t size)
```
**Description**
Construct from a start and size.
---
```cpp
ETL_CONSTEXPR basic_string_view(const T* begin)
```
**Description**
Construct from a zero terminated string.
---
```cpp
ETL_CONSTEXPR basic_string_view(const etl::basic_string_view& other)
```
**Description**
Copy constructor.
## Modifiers
```cpp
void remove_prefix(size_t n)
```
**Description**
Shrinks the view from the front.
Undefined behaviour if n is larger than the string.
---
```cpp
void remove_suffix(size_t n)
```
**Description**
Shrinks the view from the back.
Undefined behaviour if n is larger than the string.
---
```cpp
void swap(etl::basic_string_view<T, TTraits>& view)
```
**Description**
Swaps with another view.
## Element access
```cpp
const T& at(size_t i) const
```
**Description**
Returns a const reference to the indexed element.
Emits an `etl::string_view_uninitialised` if the view is not initialised.
Emits an `etl::string_view_bounds` if the index is not in range.
If asserts or exceptions are not enabled then undefined behaviour occurs.
---
```cpp
const_reference operator[](size_t i) const
```
**Description**
Returns a const reference to the indexed element.
Undefined behaviour if the string is empty.
---
```cpp
const_reference front() const
```
**Description**
Returns a const reference to the first element.
Undefined behaviour if the string is empty.
---
```cpp
const_reference back() const
```
**Description**
Returns a const reference to the last element.
Undefined behaviour if the string is empty.
---
```cpp
const_pointer data() const
```
**Description**
Returns a const pointer to the first element.
Undefined behaviour if the string is empty.
## Iterators
```cpp
const_iterator begin() const
const_iterator cbegin() const
```
**Description**
Returns an iterator to the beginning of the string view.
---
```cpp
const_iterator end() const
const_iterator cend() const
```
**Description**
Returns an iterator to the end of the string view.
---
```cpp
const_iterator rbegin() const
const_iterator crbegin() const
```
**Description**
Returns a reverse iterator to the beginning of the string view.
---
```cpp
const_iterator rend() const
const_iterator crend() const
```
**Description**
Returns a reverse iterator to the end of the string view.
## Operations
```cpp
size_t copy(T* destination, size_t count, size_t position = 0) const
```
**Description**
Copies the sub-string at position for count characters or size() - count, whichever smaller, to the string pointed to by destination.
---
```cpp
basic_string_view substr(size_t position = 0, size_t count = npos) const
```
**Description**
Returns a view which is the sub-string at position for count characters or size() - count, whichever smaller.
## Capacity
```cpp
bool empty() const
```
**Description**
Returns true if the size of the string view is zero, otherwise false.
---
```cpp
size_t size() const
```
**Description**
Returns the size of the view.
---
```cpp
size_t length() const
```
**Description**
Alternative for size().
---
```cpp
size_t max_size() const
```
**Description**
Returns the maximum possible size of the view.
## Compare
```cpp
int compare(basic_string_view v) const
int compare(size_t position1, size_t count1, basic_string_view view) const
int compare(size_t position1, size_t count1,
basic_string_view view,
size_t position2, size_t count2) const
int compare(const T* text) const
int compare(size_t position1, size_t count, const T* text) const
int compare(size_t position1, size_t count1, const T* text, size_t count2) const
```
**Description**
Compares two character sequences.
## Search
```cpp
bool starts_with(etl::basic_string_view view) const
bool starts_with(T x) const
bool starts_with(const T* x) const
```
**Description**
Checks if the string view begins with the given prefix.
---
```cpp
bool ends_with(etl::basic_string_view view) const
bool ends_with(T x) const
bool ends_with(const T* x) const
```
**Description**
Checks if the string view ends with the given prefix.
---
```cpp
size_t find(basic_string_view view, size_type position = 0) const
size_t find(T c, size_t position = 0) const
size_t find(const T* text, size_t position, size_t count) const
size_t find(const T* text, size_t position = 0) const;
```
**Description**
Finds the first sub-string equal to the given character sequence.
---
```cpp
size_t rfind(basic_string_view view, size_t position = npos) const
size_t rfind(T c, size_t position = npos) const
size_t rfind(const T* text, size_t position, size_t count) const
size_t rfind(const T* text, size_t position = npos) const
```
**Description**
Finds the last sub-string equal to the given character sequence.
---
```cpp
size_t find_first_of(basic_string_view view, size_t position = 0) const
size_t find_first_of(T c, size_t position = 0) const
size_t find_first_of(const T* text, size_t position, size_t count) const
size_t find_first_of(const T* text, size_t position = 0) const
```
**Description**
Finds the first character equal to any of the characters in the given character sequence.
---
```cpp
size_t find_last_of(basic_string_view view, size_t position = npos) const
size_t find_last_of(T c, size_t position = npos) const
size_t find_last_of(const T* text, size_t position, size_type count) const
size_t find_last_of(const T* text, size_t position = npos) const
```
**Description**
Finds the last character equal to any of the characters in the given character sequence.
---
```cpp
size_t find_first_not_of(basic_string_view view, size_t position = 0) const
size_t find_first_not_of(T c, size_t position = 0) const
size_t find_first_not_of(const T* text, size_t position, size_t count) const
size_t find_first_not_of(const T* text, size_t position = 0) const
```
**Description**
Finds the first character not equal to any of the characters in the given character sequence.
---
```cpp
size_t find_last_not_of(basic_string_view view, size_t position = npos) const
size_t find_last_not_of(T c, size_t position = npos) const
size_t find_last_not_of(const T* text, size_t position, size_t count) const
size_t find_last_not_of(const T* text, size_t position = npos) const
```
**Description**
Finds the first character not equal to any of the characters in the given character sequence.
## Non-member functions
**Lexicographically comparisons**
```cpp
operator ==
```
**Description**
`true` if the contents of the lists are equal, otherwise `false`.
---
```cpp
operator !=
```
**Description**
`true` if the contents of the lists are not equal, otherwise `false`.
---
```cpp
operator <
```
**Description**
`true` if the contents of the lhs are lexicographically less than the contents of the rhs, otherwise `false`.
---
```cpp
operator <=
```
**Description**
`true` if the contents of the lhs are lexicographically less than or equal to the contents of the rhs, otherwise `false`.
---
```cpp
operator >
```
**Description**
`true` if the contents of the lhs are lexicographically greater than the contents of the rhs, otherwise `false`.
---
```cpp
operator >=
```
**Description**
`true` if the contents of the lhs are lexicographically greater than or equal to the contents of the rhs, otherwise `false`.
---
```cpp
void swap(etl::basic_string_view<T, TTraits> lhs,
etl::basic_string_view<T, TTraits> rhs)
```
Swaps two views.
## Hash
```cpp
etl::hash<etl::string_view>
etl::hash<etl::wstring_view>
etl::hash<etl::u8string_view>
etl::hash<etl::u16string_view>
etl::hash<etl::u32string_view>
```
---
**Example**
```cpp
etl::string<10> greeting("Hello World");
using View = etl::string_view;
View view(greeting.begin() + 2, greeting.size() - 4);
void Print(const View& view)
{
std::cout << std::string(view.begin(), view.end());
}
Print(view); // Prints "llo Wo"
size_t hashvalue = etl::hash<View>()(view);
```