cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
string_view.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstddef> // IWYU pragma: keep
23#include <cstring>
24#include <string>
25
27
28namespace stat_bench {
29namespace util {
30
35public:
42 constexpr StringView(const char* data, std::size_t size) noexcept
43 : data_(data), size_(size) {}
44
48 constexpr StringView() noexcept : StringView("", 0) {}
49
55 StringView( // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
56 const std::string& str) noexcept
57 : StringView(str.data(), str.size()) {}
58
64 StringView( // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
65 const char* str) noexcept
66 : StringView(str, std::strlen(str)) {}
67
73 [[nodiscard]] constexpr auto data() const noexcept -> const char* {
74 return data_;
75 }
76
82 [[nodiscard]] constexpr auto size() const noexcept -> std::size_t {
83 return size_;
84 }
85
92 [[nodiscard]] constexpr auto empty() const noexcept -> bool {
93 return size_ == 0;
94 }
95
102 [[nodiscard]] auto at(std::size_t index) const -> char {
103 if (index >= size_) {
104 throw StatBenchException("Invalid index in StringView::at.");
105 }
106 return data_[index];
107 }
108
115 [[nodiscard]] auto substr(std::size_t pos) const -> StringView {
116 if (pos > size_) {
117 throw StatBenchException(
118 "Invalid arguments in StringView::substr.");
119 }
120 return StringView(data_ + pos, size_ - pos);
121 }
122
123private:
125 const char* data_;
126
128 std::size_t size_;
129};
130
131} // namespace util
132} // namespace stat_bench
Class of exceptions in this library.
auto substr(std::size_t pos) const -> StringView
Get a part of the string.
auto at(std::size_t index) const -> char
Get a character in the string.
constexpr auto data() const noexcept -> const char *
Get the pointer to the string.
Definition string_view.h:73
StringView(const char *str) noexcept
Constructor of implicit conversion from raw string literals.
Definition string_view.h:64
constexpr StringView() noexcept
Constructor of an empty string.
Definition string_view.h:48
constexpr auto size() const noexcept -> std::size_t
Get the size of the string.
Definition string_view.h:82
constexpr auto empty() const noexcept -> bool
Check whether this string is empty.
Definition string_view.h:92
StringView(const std::string &str) noexcept
Constructor of implicit conversion from std::string objects.
Definition string_view.h:55
constexpr StringView(const char *data, std::size_t size) noexcept
Constructor.
Definition string_view.h:42
Namespace of utility functions and classes.
Namespace of stat_bench source codes.
STL namespace.
Definition of StatBenchException class.