BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_stringrefutil.h
Go to the documentation of this file.
1/// @file bdlb_stringrefutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_stringrefutil.h -*-C++-*-
8#ifndef INCLUDED_BDLB_STRINGREFUTIL
9#define INCLUDED_BDLB_STRINGREFUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_stringrefutil bdlb_stringrefutil
15/// @brief <span style="color: var(--deprecated-color-dark)">DEPRECATED:</span> Provide utility functions on `bslstl::StringRef`-erenced strings.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_stringrefutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_stringrefutil-purpose"> Purpose</a>
25/// * <a href="#bdlb_stringrefutil-classes"> Classes </a>
26/// * <a href="#bdlb_stringrefutil-description"> Description </a>
27/// * <a href="#bdlb_stringrefutil-synopsis-of-bslstl-stringref"> Synopsis of bslstl::StringRef </a>
28/// * <a href="#bdlb_stringrefutil-function-synopsis"> Function Synopsis </a>
29/// * <a href="#bdlb_stringrefutil-character-encoding"> Character Encoding </a>
30/// * <a href="#bdlb_stringrefutil-caseless-comparisons"> Caseless Comparisons </a>
31/// * <a href="#bdlb_stringrefutil-whitespace-character-specification"> Whitespace Character Specification </a>
32/// * <a href="#bdlb_stringrefutil-usage"> Usage </a>
33/// * <a href="#bdlb_stringrefutil-example-1-trimming-whitespace"> Example 1: Trimming Whitespace </a>
34///
35/// # Purpose {#bdlb_stringrefutil-purpose}
36/// Provide utility functions on `bslstl::StringRef`-erenced strings.
37///
38/// @deprecated Use bdlb_stringviewutil instead.
39///
40/// # Classes {#bdlb_stringrefutil-classes}
41///
42/// - bdlb::StringRefUtil: namespace for functions on `bslstl::StringRef` strings
43///
44/// @see bdlb_string, bslstl_stringref
45///
46/// # Description {#bdlb_stringrefutil-description}
47/// This component defines a utility `struct`,
48/// `bdlb::StringRefUtil`, that provides a suite of functions that operate on
49/// `bslstl::StringRef` references to string data.
50///
51/// ## Synopsis of bslstl::StringRef {#bdlb_stringrefutil-synopsis-of-bslstl-stringref}
52///
53///
54/// The `bslstl::StringRef` class provides `bsl::string`-like access to an array
55/// of bytes that need not be null terminated and that can have non-ASCII values
56/// (i.e., `[128 .. 255]`). Although a `bslstl::StringRef` object can itself be
57/// changed, it cannot change its referent data (the array of bytes). The
58/// lifetime of the referent data must exceed that of all `bslstl::StringRef`
59/// objects referring to it. Equality comparison of `bslstl::StringRef` objects
60/// compares the content of the referent data (not whether or not the object
61/// refer to the same array of bytes). See @ref bslstl_stringref for full
62/// details.
63///
64/// ## Function Synopsis {#bdlb_stringrefutil-function-synopsis}
65///
66///
67/// The table below provides an outline of the functions provided by this
68/// component.
69/// @code
70/// Function Purpose
71/// -------------------------- --------------------------------------------
72/// areEqualCaseless(SR, SR) case-insensitive equality comparison
73/// lowerCaseCmp(SR, SR) lexical comparison of lower-case conversion
74/// upperCaseCmp(SR, SR) lexical comparison of upper-case conversion
75///
76/// ltrim(SR) exclude whitespace from left side of string
77/// rtrim(SR) exclude whitespace from right side of string
78/// trim(SR) exclude whitespace from both sides of string
79///
80/// substr(SR, pos, num) substring, `num` characters from `pos`
81///
82/// strstr (SR, SUBSR) find first substring in string
83/// strstrCaseless (SR, SUBSR) find first substring in string, case insensitive
84/// strrstr (SR, SUBSR) find last substring in string
85/// strrstrCaseless(SR, SUBSR) find last substring in string, case insensitive
86/// @endcode
87///
88/// Since `bslstl::StringRef` objects know the length of the referent data these
89/// utility functions can make certain performance improvements over the
90/// classic, similarly named C language functions.
91///
92/// ## Character Encoding {#bdlb_stringrefutil-character-encoding}
93///
94///
95/// These utilities assume ASCII encoding for character data when doing case
96/// conversions and when determining if a character is in the whitespace
97/// character set.
98///
99/// ### Caseless Comparisons {#bdlb_stringrefutil-caseless-comparisons}
100///
101///
102/// Caseless (i.e., case-insensitive) comparisons treat characters in the
103/// sequence `[a .. z]` as equivalent to the respective characters in the
104/// sequence `[A .. Z]`. This equivalence matches that of `bsl::toupper`.
105///
106/// ### Whitespace Character Specification {#bdlb_stringrefutil-whitespace-character-specification}
107///
108///
109/// The following characters are classified as "whitespace":
110/// @code
111/// Character Description
112/// --------- ---------------
113/// ' ' blank-space
114/// '\f' form-feed
115/// '\n' newline
116/// '\r' carriage return
117/// '\t' horizontal tab
118/// '\v' vertical tab
119/// @endcode
120/// This classification matches that of `bsl::isspace`.
121///
122/// ## Usage {#bdlb_stringrefutil-usage}
123///
124///
125/// This section illustrates the intended use of this component.
126///
127/// ### Example 1: Trimming Whitespace {#bdlb_stringrefutil-example-1-trimming-whitespace}
128///
129///
130/// Many applications must normalize user input by removing leading and trailing
131/// whitespace characters to obtain the essential text that is the intended
132/// input. Naturally, one would prefer to do this as efficiently as possible.
133///
134/// Suppose the response entered by a user is captured in `rawInput` below:
135/// @code
136/// const char * const rawInput = " \t\r\n Hello, world! \r\n";
137/// //1234 5 6 789 1234 5 6
138/// // 123456789ABCD
139/// // Note lengths of whitespace and
140/// // non-whitespace substrings for later.
141/// @endcode
142/// First, for this pedagogical example, we copy the contents at `rawInput` for
143/// later reference:
144/// @code
145/// const bsl::string copyRawInput(rawInput);
146/// @endcode
147/// Then, we create a `bslstl::StringRef` object referring to the raw data.
148/// Given a single argument of `const char *`, the constructor assumes the data
149/// is a null-terminated string and implicitly calculates the length for the
150/// reference:
151/// @code
152/// bslstl::StringRef text(rawInput);
153///
154/// assert(rawInput == text.data());
155/// assert(9 + 13 + 6 == text.length());
156/// @endcode
157/// Now, we invoke the `bdlb::StringRefUtil::trim` method to find the "Hello,
158/// world!" sequence in `rawInput`.
159/// @code
160/// bslstl::StringRef textOfInterest = bdlb::StringRefUtil::trim(text);
161/// @endcode
162/// Finally, we observe the results:
163/// @code
164/// assert("Hello, world!" == textOfInterest); // content comparison
165/// assert(13 == textOfInterest.length());
166///
167/// assert(text.data() + 9 == textOfInterest.data());
168/// assert(text.length() - 9 - 6 == textOfInterest.length());
169///
170/// assert(rawInput == copyRawInput); // content comparison
171/// @endcode
172/// Notice that, as expected, the `textOfInterest` object refers to the "Hello,
173/// world!" sub-sequence within the `rawInput` byte array while the data at
174/// `rawInput` remains *unchanged*.
175/// @}
176/** @} */
177/** @} */
178
179/** @addtogroup bdl
180 * @{
181 */
182/** @addtogroup bdlb
183 * @{
184 */
185/** @addtogroup bdlb_stringrefutil
186 * @{
187 */
188
189#include <bdlscm_version.h>
190
191#include <bsls_review.h>
192
193#include <bsl_string.h> // 'bslstl::StringRef'
194
195
196namespace bdlb {
197 // ====================
198 // struct StringRefUtil
199 // ====================
200
201/// This `struct` provides a namespace for a suite of functions on
202/// `bslstl::StringRef` references to strings.
203///
204/// See @ref bdlb_stringrefutil
206
207 // PUBLIC TYPES
208
209 /// Size type of string references.
211
212 // PUBLIC CLASS DATA
213
214 /// Value used to denote "not-a-position", guaranteed to be outside the
215 /// `range[0 .. INT_MAX]`.
216 static const size_type k_NPOS = ~size_type(0);
217
218 // CLASS METHODS
219 // Comparison
220
221 /// Compare (the referent data of) the specified `lhs` and `rhs`.
222 /// Return `true` if `lhs` and `rhs` are equal up to a case conversion,
223 /// and `false` otherwise. See {Caseless Comparisons}.
224 static bool areEqualCaseless(const bslstl::StringRef& lhs,
225 const bslstl::StringRef& rhs);
226
227 /// Compare (the referent data of) the specified `lhs` and `rhs`.
228 /// Return 1 if, after a conversion to lower case, `lhs` is greater than
229 /// `rhs`, 0 if `lhs` and `rhs` are equal up to a case conversion, and
230 /// -1 otherwise. See {Caseless Comparisons}.
231 static int lowerCaseCmp(const bslstl::StringRef& lhs,
232 const bslstl::StringRef& rhs);
233
234 /// Compare (the referent data of) the specified `lhs` and `rhs`.
235 /// Return 1 if, after a conversion to upper case, `lhs` is greater than
236 /// `rhs`, 0 if `lhs` and `rhs` are equal up to a case conversion, and
237 /// -1 otherwise. See {Caseless Comparisons}.
238 static int upperCaseCmp(const bslstl::StringRef& lhs,
239 const bslstl::StringRef& rhs);
240
241 // Trim
242
243 /// Return a `bslstl::StringRef` object referring to the substring of
244 /// (the referent data of) the specified `string` that excludes all
245 /// leading whitespace. See {Whitespace Character Specification}. If
246 /// `string` consists entirely of whitespace, return a zero-length
247 /// reference to the end of `string` (i.e.,
248 /// `bslstl::StringRef(string.end(), 0)`).
250
251 /// Return a `bslstl::StringRef` object referring to the substring of
252 /// (the referent data of) the specified `string` that excludes all
253 /// trailing whitespace. See {Whitespace Character Specification}. If
254 /// `string` consists entirely of whitespace, return a zero-length
255 /// reference to the beginning of (the referent data of) `string`
256 /// (i.e., `bslstl::StringRef(string.data(), 0)`).
258
259 /// Return a `bslstl::StringRef` object referring to the substring of
260 /// (the referent data of) the specified `string` that excludes all
261 /// leading and trailing whitespace. See {Whitespace Character
262 /// Specification}. If `string` consists entirely of whitespace, return
263 /// a zero-length reference to the beginning of (the referent data of)
264 /// `string` (i.e., `bslstl::StringRef(string.data(), 0)`).
265 static bslstl::StringRef trim(const bslstl::StringRef& string);
266
267 // Create `subString`
268
269 /// Return a string whose value is the substring starting at the
270 /// optionally specified `position` in the specified `string`, of length
271 /// the optionally specified `numChars` or `length() - position`,
272 /// whichever is smaller. If `position` is not specified, 0 is used
273 /// (i.e., the substring is from the beginning of this string). If
274 /// `numChars` is not specified, `k_NPOS` is used (i.e., the entire
275 /// suffix from `position` to the end of the string is returned).
276 ///
277 /// \pre The behavior is undefined unless `position` is within the string
278 /// boundaries (`0 <= position <= string.length()`).
280 const bslstl::StringRef& string,
281 size_type position = 0,
282 size_type numChars = k_NPOS);
283
284 // Find `subString`
285
286 /// Return a `bslstl::StringRef` object referring to the first
287 /// occurrence in (the referent data of) the specified `string` at which
288 /// (the referent data of) the specified `subString` is found, or
289 /// `bslstl::StringRef()` if there is no such occurrence. If
290 /// `subString` has zero length then a zero-length reference to the
291 /// beginning of `string` is returned (i.e.,
292 /// `bslstl::StringRef(string.data(), 0)`);
294 const bslstl::StringRef& subString);
295
296 /// Return a `bslstl::StringRef` object referring to the first
297 /// occurrence in (the referent data of) the specified `string` at which
298 /// (the referent data of) the specified `subString` is found using
299 /// case-insensitive comparisons, or `bslstl::StringRef()` if there is
300 /// no such occurrence. See {Caseless Comparisons}. If `subString` has
301 /// zero length then a zero-length reference to the beginning of
302 /// `string` is returned (i.e., `bslstl::StringRef(string.data(), 0)`);
304 const bslstl::StringRef& string,
305 const bslstl::StringRef& subString);
306
307 /// Return a `bslstl::StringRef` object referring to the last occurrence
308 /// in (the referent data of) the specified `string` at which (the
309 /// referent data of) the specified `subString` is found, or
310 /// `bslstl::StringRef()` if there is no such occurrence. If
311 /// `subString` has zero length then a zero-length reference to the end
312 /// of `string` is returned (i.e.,
313 /// `bslstl::StringRef(string.end(), 0)`);
315 const bslstl::StringRef& subString);
316
317 /// Return a `bslstl::StringRef` object referring to the last occurrence
318 /// in (the referent data of) the specified `string` at which (the
319 /// referent data of) the specified `subString` is found using
320 /// case-insensitive comparisons, or `bslstl::StringRef()` if there is
321 /// no such occurrence. See {Caseless Comparisons}. If `subString` has
322 /// zero length then a zero-length reference to the end of `string` is
323 /// returned (i.e., `bslstl::StringRef(string.end(), 0)`);
325 const bslstl::StringRef& string,
326 const bslstl::StringRef& subString);
327
328 // Find first/last of/not of
329
330 /// Return the position of the *first* occurrence of a character
331 /// belonging to the specified `characters`, if such an occurrence can
332 /// can be found in the specified `string` (on or *after* the
333 /// optionally specified `position` if such a `position` is specified),
334 /// and return `k_NPOS` otherwise.
335 static
337 const bslstl::StringRef& characters,
338 size_type position = 0u);
339
340 /// Return the position of the *last* occurrence of a character
341 /// belonging to the specified `characters`, if such an occurrence can
342 /// can be found in the specified `string` (on or *before* the
343 /// optionally specified `position` if such a `position` is specified),
344 /// and return `k_NPOS` otherwise.
345 static
347 const bslstl::StringRef& characters,
348 size_type position = k_NPOS);
349
350 /// Return the position of the *first* occurrence of a character *not*
351 /// belonging to the specified `characters`, if such an occurrence can
352 /// be found in the specified `string` (on or *after* the optionally
353 /// specified `position` if such a `position` is specified), and return
354 /// `k_NPOS` otherwise.
355 static
357 const bslstl::StringRef& characters,
358 size_type position = 0u);
359
360 /// Return the position of the *last* occurrence of a character *not*
361 /// belonging to the specified `characters`, if such an occurrence can
362 /// be found in the specified `string` (on or *before* the optionally
363 /// specified `position` if such a `position` is specified), and return
364 /// `k_NPOS` otherwise.
365 static
367 const bslstl::StringRef& characters,
368 size_type position = k_NPOS);
369};
370
371// ============================================================================
372// INLINE DEFINITIONS
373// ============================================================================
374
375 // --------------------
376 // struct StringRefUtil
377 // --------------------
378
379// CLASS METHODS
380
381 // Comparison
382
383inline
385 const bslstl::StringRef& rhs)
386{
387 if (lhs.length() != rhs.length()) {
388 return false; // RETURN
389 }
390
391 return 0 == lowerCaseCmp(lhs, rhs);
392}
393
394 // Trim
395
396inline
398{
399 return ltrim(rtrim(string));
400}
401
402 // Substring
403
404inline
406 size_type position,
407 size_type numChars)
408{
409 BSLS_ASSERT(position <= string.length());
410
411 return bslstl::StringRef(string, position, numChars);
412}
413
414} // close package namespace
415
416
417#endif
418
419// ----------------------------------------------------------------------------
420// Copyright 2016 Bloomberg Finance L.P.
421//
422// Licensed under the Apache License, Version 2.0 (the "License"); you may not
423// use this file except in compliance with the License. You may obtain a copy
424// of the License at
425//
426// http://www.apache.org/licenses/LICENSE-2.0
427//
428// Unless required by applicable law or agreed to in writing, software
429// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
430// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
431// License for the specific language governing permissions and limitations
432// under the License.
433// ----------------------------- END-OF-FILE ----------------------------------
434
435/** @} */
436/** @} */
437/** @} */
Definition bslstl_stringref.h:374
std::size_t size_type
Standard Library general container requirements.
Definition bslstl_stringref.h:391
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlb_algorithmworkaroundutil.h:74
StringRefImp< char > StringRef
Definition bslstl_stringref.h:725
Definition bdlb_stringrefutil.h:205
static size_type findFirstNotOf(const bslstl::StringRef &string, const bslstl::StringRef &characters, size_type position=0u)
static bslstl::StringRef substr(const bslstl::StringRef &string, size_type position=0, size_type numChars=k_NPOS)
Definition bdlb_stringrefutil.h:405
static size_type findLastNotOf(const bslstl::StringRef &string, const bslstl::StringRef &characters, size_type position=k_NPOS)
bslstl::StringRef::size_type size_type
Size type of string references.
Definition bdlb_stringrefutil.h:210
static size_type findFirstOf(const bslstl::StringRef &string, const bslstl::StringRef &characters, size_type position=0u)
static size_type findLastOf(const bslstl::StringRef &string, const bslstl::StringRef &characters, size_type position=k_NPOS)
static int upperCaseCmp(const bslstl::StringRef &lhs, const bslstl::StringRef &rhs)
static bslstl::StringRef strstr(const bslstl::StringRef &string, const bslstl::StringRef &subString)
static bslstl::StringRef trim(const bslstl::StringRef &string)
Definition bdlb_stringrefutil.h:397
static bool areEqualCaseless(const bslstl::StringRef &lhs, const bslstl::StringRef &rhs)
Definition bdlb_stringrefutil.h:384
static bslstl::StringRef strstrCaseless(const bslstl::StringRef &string, const bslstl::StringRef &subString)
static const size_type k_NPOS
Definition bdlb_stringrefutil.h:216
static bslstl::StringRef rtrim(const bslstl::StringRef &string)
static int lowerCaseCmp(const bslstl::StringRef &lhs, const bslstl::StringRef &rhs)
static bslstl::StringRef ltrim(const bslstl::StringRef &string)
static bslstl::StringRef strrstrCaseless(const bslstl::StringRef &string, const bslstl::StringRef &subString)
static bslstl::StringRef strrstr(const bslstl::StringRef &string, const bslstl::StringRef &subString)