Skip to content

Commit

Permalink
Add constexpr to some str_view class member functions (#23)
Browse files Browse the repository at this point in the history
* add UPA_CONSTEXPR_14 macro

* add workflow to compile with g++ using C++14
  • Loading branch information
rmisev authored Nov 4, 2023
1 parent 28ee85f commit 9cf4415
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ jobs:
cmake_options: "-DURL_AMALGAMATED=ON -DURL_BUILD_EXAMPLES=ON"
before_cmake: tools/amalgamate.sh

- name: g++-9 C++14
cxx_compiler: g++-9
cxx_standard: 14
cmake_options: "-DURL_BUILD_EXAMPLES=ON"

- name: g++-9 C++11
cxx_compiler: g++-9
cxx_standard: 11
Expand Down
6 changes: 5 additions & 1 deletion include/upa/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
#endif

// Define UPA_CPP_14 if compiler supports C++14 or later
#if defined(_MSVC_LANG) ? (_MSVC_LANG >= 201402) : (__cplusplus >= 201402)
// Note: Visual Studio 2015 (14.0; _MSC_VER == 1900) lacks sufficient C++14 support
#if defined(_MSVC_LANG) ? (_MSVC_LANG >= 201402 && _MSC_VER > 1900) : (__cplusplus >= 201402)
# define UPA_CPP_14
# define UPA_CONSTEXPR_14 constexpr
#else
# define UPA_CONSTEXPR_14 inline
#endif

#endif // UPA_CONFIG_H
32 changes: 18 additions & 14 deletions include/upa/str_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
#ifndef UPA_STR_VIEW_H
#define UPA_STR_VIEW_H

#include "config.h"
#include <algorithm>
#include <string>
#include <type_traits>

namespace upa {

/// @brief A very simplified implementation of the basic_string_view
///
/// It is used when compiling with C++11 or C++14 compilers.
template<typename CharT, typename Traits = std::char_traits<CharT>>
class str_view {
public:
Expand All @@ -31,41 +35,41 @@ class str_view {
// static constexpr size_type npos = size_type(-1);

// constructors
str_view() noexcept = default;
str_view(const str_view&) noexcept = default;
str_view(const CharT* ptr, size_type len) : ptr_(ptr), len_(len) {}
constexpr str_view() noexcept = default;
constexpr str_view(const str_view&) noexcept = default;
constexpr str_view(const CharT* ptr, size_type len) : ptr_(ptr), len_(len) {}
str_view(const CharT* ptr) : ptr_(ptr), len_(Traits::length(ptr)) {}

// assignment
str_view& operator=(const str_view&) noexcept = default;
UPA_CONSTEXPR_14 str_view& operator=(const str_view&) noexcept = default;

// destructor
~str_view() noexcept = default;

// iterator support
const_iterator begin() const noexcept { return ptr_; }
const_iterator end() const noexcept { return ptr_ + len_; }
constexpr const_iterator begin() const noexcept { return ptr_; }
constexpr const_iterator end() const noexcept { return ptr_ + len_; }

// capacity
size_type size() const noexcept { return len_; }
size_type length() const noexcept { return len_; }
bool empty() const noexcept { return len_ == 0; }
constexpr size_type size() const noexcept { return len_; }
constexpr size_type length() const noexcept { return len_; }
constexpr bool empty() const noexcept { return len_ == 0; }

// element access
const_reference operator[](size_type ind) const {
constexpr const_reference operator[](size_type ind) const {
return ptr_[ind];
}
const_pointer data() const noexcept { return ptr_; }
constexpr const_pointer data() const noexcept { return ptr_; }

// modifiers
void remove_prefix(size_type n) {
UPA_CONSTEXPR_14 void remove_prefix(size_type n) {
ptr_ += n;
len_ -= n;
}
void remove_suffix(size_type n) {
UPA_CONSTEXPR_14 void remove_suffix(size_type n) {
len_ -= n;
}
void swap(str_view& x) noexcept {
UPA_CONSTEXPR_14 void swap(str_view& x) noexcept {
const str_view tmp{x};
x = *this;
*this = tmp;
Expand Down

0 comments on commit 9cf4415

Please sign in to comment.