BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_indexspanutil.h
Go to the documentation of this file.
1/// @file bdlb_indexspanutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_indexspanutil.h -*-C++-*-
8#ifndef INCLUDED_BDLB_INDEXSPANUTIL
9#define INCLUDED_BDLB_INDEXSPANUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_indexspanutil bdlb_indexspanutil
15/// @brief Provide functions that operate on `IndexSpan` objects.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_indexspanutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_indexspanutil-purpose"> Purpose</a>
25/// * <a href="#bdlb_indexspanutil-classes"> Classes </a>
26/// * <a href="#bdlb_indexspanutil-description"> Description </a>
27/// * <a href="#bdlb_indexspanutil-usage"> Usage </a>
28/// * <a href="#bdlb_indexspanutil-example1-taking-a-ipv6-address-out-of-a-uri"> Example1: Taking a IPv6 address out of a URI </a>
29///
30/// # Purpose {#bdlb_indexspanutil-purpose}
31/// Provide functions that operate on `IndexSpan` objects.
32///
33/// # Classes {#bdlb_indexspanutil-classes}
34///
35/// - bdlb::IndexSpanUtil: namespace for functions that operate on `IndexSpan`
36///
37/// @see bdlb_indexspan
38///
39/// # Description {#bdlb_indexspanutil-description}
40/// This component provides a struct, `IndexSpanUtil`, that serves
41/// as a namespace for utility functions that operate on `IndexSpan` objects.
42/// At the moment the only function provided is `shrink`, and it creates a new
43/// `IndexSpan` that represents a (possibly) smaller span than the argument.
44///
45/// ## Usage {#bdlb_indexspanutil-usage}
46///
47///
48/// This section illustrates intended use of this component.
49///
50/// ### Example1: Taking a IPv6 address out of a URI {#bdlb_indexspanutil-example1-taking-a-ipv6-address-out-of-a-uri}
51///
52///
53/// Suppose we have a class that stores a parsed URL using a string to store the
54/// full URL and `IndexSpan` objects to describe the individual parts of the
55/// URL, and we want to add accessors that handle the case when the host part of
56/// the URL is an IPv6 address, such as "http://[ff:fe:9]/index.html". As
57/// observed, an IPv6 address is indicated by the `[` and `]` characters (the
58/// URL is ill formed if the closing `]` is not present). We want to implement
59/// two methods, one to query if the host part of the URL is IPv6 (`isIPv6Host`)
60/// and another to get the IPv6 address (the part without the square brackets)
61/// if the host is actually an IPv6 address (`getIPv6Host`).
62///
63/// First, let us create a `ParsedUrl` class. For brevity, the class has only
64/// those parts that are needed to implement `isIPv6Host` and `getIPv6Host`.
65/// @code
66/// class ParsedUrl {
67/// private:
68/// // DATA
69/// bsl::string d_url;
70/// bdlb::IndexSpan d_host;
71///
72/// public:
73/// // CREATORS
74///
75/// /// Create a `ParsedUrl` from the specified `url`, and `host`.
76/// ParsedUrl(const bslstl::StringRef& url, bdlb::IndexSpan host)
77/// : d_url(url)
78/// , d_host(host)
79/// {
80/// }
81///
82/// // ACCESSORS
83///
84/// /// Return `true` if the host part represents an IPv6 address and
85/// /// `false` otherwise.
86/// bool isIPv6Host() const;
87///
88/// /// Return a string reference to the IPv6 address in the host part
89/// /// of this URL. The behavior is undefined unless
90/// /// `isIPv6Host() == true` for this object.
91/// bslstl::StringRef getIPv6Host() const;
92/// };
93/// @endcode
94/// Next, we implement `isIPv6Host`.
95/// @code
96/// bool ParsedUrl::isIPv6Host() const
97/// {
98/// return !d_host.isEmpty() && '[' == d_url[d_host.position()];
99/// }
100/// @endcode
101/// Then, to make the accessors simple (and readable), we implement a helper
102/// function that creates a `StringRef` from a `StringRef` and an `IndexSpan`.
103/// (Don't do this in real code, use `IndexSpanStringUtil::bind` that is
104/// levelized above this component - so we cannot use it here.)
105/// @code
106/// /// Return a string reference to the substring of the specified `full`
107/// /// thing defined by the specified `part`.
108/// bslstl::StringRef bindSpan(const bslstl::StringRef& full,
109/// const bdlb::IndexSpan& part)
110/// {
111/// BSLS_ASSERT(part.position() <= full.length());
112/// BSLS_ASSERT(part.position() + part.length() <= full.length());
113///
114/// return bslstl::StringRef(full.data() + part.position(), part.length());
115/// }
116/// @endcode
117/// Next, we implement `getIPv6Host` using `bdlb::IndexSpanUtil::shrink`.
118/// @code
119/// bslstl::StringRef ParsedUrl::getIPv6Host() const
120/// {
121/// BSLS_ASSERT(isIPv6Host());
122///
123/// return bindSpan(d_url, bdlb::IndexSpanUtil::shrink(d_host, 1, 1));
124/// }
125/// @endcode
126/// Finally, we verify the two methods with URLs.
127/// @code
128/// ParsedUrl pu1("https://host/path/", bdlb::IndexSpan(8, 4));
129/// assert(false == pu1.isIPv6Host());
130///
131/// ParsedUrl pu2("https://[12:3:fe:9]/path/", bdlb::IndexSpan(8, 11));
132/// assert(true == pu2.isIPv6Host());
133/// assert("12:3:fe:9" == pu2.getIPv6Host());
134/// @endcode
135/// @}
136/** @} */
137/** @} */
138
139/** @addtogroup bdl
140 * @{
141 */
142/** @addtogroup bdlb
143 * @{
144 */
145/** @addtogroup bdlb_indexspanutil
146 * @{
147 */
148
149#include <bdlscm_version.h>
150
151#include <bdlb_indexspan.h>
152
153#include <bsls_assert.h>
154
155
156namespace bdlb {
157
158 // ====================
159 // struct IndexSpanUtil
160 // ====================
161
162/// This struct serves as a namespace for utility functions that operate on
163/// `IndexSpan` objects.
165 public:
166 // CLASS METHODS
167
168 /// Return an `IndexSpan` object transformed from the specified
169 /// `original` using the specified `shrinkBegin` and `shrinkEnd` so that
170 /// its position is `original.position() + shrinkBegin` and whose length
171 /// is `original.length() - (shrinkBegin + shrinkEnd)`. The behavior is
172 /// undefined unless `shrinkBegin + shrinkEnd <= original.length()`.
173 static IndexSpan shrink(const IndexSpan& original,
174 IndexSpan::size_type shrinkBegin,
175 IndexSpan::size_type shrinkEnd);
176};
177
178// ============================================================================
179// INLINE DEFINITIONS
180// ============================================================================
181
182 // --------------------
183 // struct IndexSpanUtil
184 // --------------------
185
186// CLASS METHODS
187inline
189 IndexSpan::size_type shrinkBegin,
190 IndexSpan::size_type shrinkEnd)
191{
192 BSLS_ASSERT(shrinkBegin + shrinkEnd <= original.length());
193
194 return IndexSpan(original.position() + shrinkBegin,
195 original.length() - (shrinkBegin + shrinkEnd));
196}
197
198} // close package namespace
199
200
201#endif
202
203// ----------------------------------------------------------------------------
204// Copyright 2018 Bloomberg Finance L.P.
205//
206// Licensed under the Apache License, Version 2.0 (the "License");
207// you may not use this file except in compliance with the License.
208// You may obtain a copy of the License at
209//
210// http://www.apache.org/licenses/LICENSE-2.0
211//
212// Unless required by applicable law or agreed to in writing, software
213// distributed under the License is distributed on an "AS IS" BASIS,
214// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
215// See the License for the specific language governing permissions and
216// limitations under the License.
217// ----------------------------- END-OF-FILE ----------------------------------
218
219/** @} */
220/** @} */
221/** @} */
Definition bdlb_indexspan.h:309
size_type position() const
Return the position attribute.
Definition bdlb_indexspan.h:448
bsl::size_t size_type
The type of the position and length attributes.
Definition bdlb_indexspan.h:315
size_type length() const
Return the length attribute.
Definition bdlb_indexspan.h:442
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_indexspanutil.h:164
static IndexSpan shrink(const IndexSpan &original, IndexSpan::size_type shrinkBegin, IndexSpan::size_type shrinkEnd)
Definition bdlb_indexspanutil.h:188