SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_string_view.h
Go to the documentation of this file.
1/*****************************************************************************
2
3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4 more contributor license agreements. See the NOTICE file distributed
5 with this work for additional information regarding copyright ownership.
6 Accellera licenses this file to you under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with the
8 License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 implied. See the License for the specific language governing
16 permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22 sc_string_view.h -- Non-owning, constant reference to a character string
23
24 Original Author: Philipp A Hartmann, Intel
25
26 Intended to follow std::string_view as defined by ISO/IEC 14882:2017.
27
28 NOTE: Once C++20 is adopted, all uses can be replaced by std::string_view.
29
30 *****************************************************************************/
31
32#ifndef SYSC_UTILS_STRING_VIEW_H_INCLUDED_
33#define SYSC_UTILS_STRING_VIEW_H_INCLUDED_
34
36#include <string>
37#include <type_traits> // std::enable_if, std::is_convertible
38#include <string_view>
39
40namespace sc_core {
41
43// TODO: add ABI guard against inconsistent configurations
45 : public std::string_view
46{
47 typedef std::string_view base_type;
48 template<typename T> struct enable_if_convertible
49 : std::enable_if<std::is_convertible<T,base_type>::value > {};
50public:
51 // inherit constructors from base class
52 using base_type::base_type;
53
54 // allow same conversions as base class, needed for C++17
55 template<typename T>
56 /* constexpr */ sc_string_view( const T& s
57 , typename enable_if_convertible<T>::type* = 0)
58 : base_type(s) {}
59
60 // helper function to create an explicit string copy
61 inline std::string str() const
62 { return std::string( data(), size() ); }
63
64#if SC_CPLUSPLUS <= 201703L // before C++20
65 /* constexpr */ bool starts_with(base_type sv) const /* noexcept */
66 { return size() >= sv.size() && compare(0, sv.size(), sv) == 0; }
67 /* constexpr */ bool starts_with(char c) const /* noexcept */
68 { return !empty() && front() == c; }
69 /* constexpr */ bool starts_with(const char* s) const /* noexcept */
70 { return starts_with( sc_string_view(s) ); }
71
72 /* constexpr */ bool ends_with(base_type sv) const /* noexcept */
73 { return size() >= sv.size() && compare(size() - sv.size(), npos, sv) == 0; }
74 /* constexpr */ bool ends_with(char c) const /* noexcept */
75 { return !empty() && back() == c; }
76 /* constexpr */ bool ends_with(const char* s) const /* noexcept */
77 { return ends_with( sc_string_view(s) ); }
78#endif // before C++20
79}; // class sc_string_view
80
83{
84 typedef sc_string_view base_type;
85
86public:
87 /* constexpr */ sc_zstring_view() /* noexcept */
88 : base_type("") {}
89
90 /* constexpr */ sc_zstring_view(const char* s) /* noexcept */
91 : base_type(SC_LIKELY_(s) ? s : "") {}
92
93 sc_zstring_view(const std::string& s) /* noexcept */
94 : base_type(s.c_str()) {} // enforce ending string at first \0 character
95
96 /* constexpr */ void swap(sc_zstring_view& s) /* noexcept */
97 { base_type::swap(s); }
98
99 /* constexpr */ const char* c_str() const /* noexcept */
100 { return data(); }
101
102private:
103 // Hide invariant-breaking function
104 using sc_string_view::remove_suffix;
105}; // class sc_zstring_view
106
107} // namespace sc_core
108
109#endif // SYSC_UTILS_STRING_VIEW_H_INCLUDED_
110// Taf!
#define SC_LIKELY_(x)
Definition: sc_cmnhdr.h:86
#define SC_API
Definition: sc_cmnhdr.h:148
non-owning, constant reference to a string
bool starts_with(const char *s) const
bool ends_with(char c) const
std::string str() const
bool ends_with(const char *s) const
bool starts_with(char c) const
bool starts_with(base_type sv) const
sc_string_view(const T &s, typename enable_if_convertible< T >::type *=0)
bool ends_with(base_type sv) const
non-owning, constant reference to null-terminated string
sc_zstring_view(const char *s)
void swap(sc_zstring_view &s)
const char * c_str() const
sc_zstring_view(const std::string &s)