/builds/MusicScience37Projects/utility-libraries/cpp-stat-bench/include/stat_bench/util/string_view.h
Line | Count | Source |
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 | | */ |
16 | | /*! |
17 | | * \file |
18 | | * \brief Declaration of check_glob_pattern function. |
19 | | */ |
20 | | #pragma once |
21 | | |
22 | | #include <cstddef> // IWYU pragma: keep |
23 | | #include <cstring> |
24 | | #include <string> |
25 | | |
26 | | #include "stat_bench/stat_bench_exception.h" |
27 | | |
28 | | namespace stat_bench { |
29 | | namespace util { |
30 | | |
31 | | /*! |
32 | | * \brief Class of view of strings. |
33 | | */ |
34 | | class StringView { |
35 | | public: |
36 | | /*! |
37 | | * \brief Constructor. |
38 | | * |
39 | | * \param[in] data Pointer to the string. |
40 | | * \param[in] size Size of the string. |
41 | | */ |
42 | | constexpr StringView(const char* data, std::size_t size) noexcept |
43 | 664 | : data_(data), size_(size) {} |
44 | | |
45 | | /*! |
46 | | * \brief Constructor of an empty string. |
47 | | */ |
48 | 206 | constexpr StringView() noexcept : StringView("", 0) {} |
49 | | |
50 | | /*! |
51 | | * \brief Constructor of implicit conversion from std::string objects. |
52 | | * |
53 | | * \param[in] str String. |
54 | | */ |
55 | | StringView( // NOLINT(google-explicit-constructor, hicpp-explicit-conversions) |
56 | | const std::string& str) noexcept |
57 | 49 | : StringView(str.data(), str.size()) {} |
58 | | |
59 | | /*! |
60 | | * \brief Constructor of implicit conversion from raw string literals. |
61 | | * |
62 | | * \param[in] str String. |
63 | | */ |
64 | | StringView( // NOLINT(google-explicit-constructor, hicpp-explicit-conversions) |
65 | | const char* str) noexcept |
66 | 165 | : StringView(str, std::strlen(str)) {} |
67 | | |
68 | | /*! |
69 | | * \brief Get the pointer to the string. |
70 | | * |
71 | | * \return Pointer to the string. |
72 | | */ |
73 | 184 | [[nodiscard]] constexpr auto data() const noexcept -> const char* { |
74 | 184 | return data_; |
75 | 184 | } |
76 | | |
77 | | /*! |
78 | | * \brief Get the size of the string. |
79 | | * |
80 | | * \return Size of the string. |
81 | | */ |
82 | 986 | [[nodiscard]] constexpr auto size() const noexcept -> std::size_t { |
83 | 986 | return size_; |
84 | 986 | } |
85 | | |
86 | | /*! |
87 | | * \brief Check whether this string is empty. |
88 | | * |
89 | | * \retval true This string is empty. |
90 | | * \retval false This string is not empty. |
91 | | */ |
92 | 490 | [[nodiscard]] constexpr auto empty() const noexcept -> bool { |
93 | 490 | return size_ == 0; |
94 | 490 | } |
95 | | |
96 | | /*! |
97 | | * \brief Get a character in the string. |
98 | | * |
99 | | * \param[in] index Index of the character to get. |
100 | | * \return Character. |
101 | | */ |
102 | 829 | [[nodiscard]] auto at(std::size_t index) const -> char { |
103 | 829 | if (index >= size_) { |
104 | 0 | throw StatBenchException("Invalid index in StringView::at."); |
105 | 0 | } |
106 | 829 | return data_[index]; |
107 | 829 | } |
108 | | |
109 | | /*! |
110 | | * \brief Get a part of the string. |
111 | | * |
112 | | * \param[in] pos Index of the starting position. |
113 | | * \return Created part of the string. |
114 | | */ |
115 | 244 | [[nodiscard]] auto substr(std::size_t pos) const -> StringView { |
116 | 244 | if (pos > size_) { |
117 | 0 | throw StatBenchException( |
118 | 0 | "Invalid arguments in StringView::substr."); |
119 | 0 | } |
120 | 244 | return StringView(data_ + pos, size_ - pos); |
121 | 244 | } |
122 | | |
123 | | private: |
124 | | //! Pointer to the string. |
125 | | const char* data_; |
126 | | |
127 | | //! Size of the string. |
128 | | std::size_t size_; |
129 | | }; |
130 | | |
131 | | } // namespace util |
132 | | } // namespace stat_bench |