BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_indexspanstringutil.h
Go to the documentation of this file.
1/// @file bdlb_indexspanstringutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_indexspanstringutil.h -*-C++-*-
8#ifndef INCLUDED_BDLB_INDEXSPANSTRINGUTIL
9#define INCLUDED_BDLB_INDEXSPANSTRINGUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_indexspanstringutil bdlb_indexspanstringutil
15/// @brief Provide functions that operate on `IndexSpan` and string objects.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_indexspanstringutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_indexspanstringutil-purpose"> Purpose</a>
25/// * <a href="#bdlb_indexspanstringutil-classes"> Classes </a>
26/// * <a href="#bdlb_indexspanstringutil-description"> Description </a>
27/// * <a href="#bdlb_indexspanstringutil-usage"> Usage </a>
28/// * <a href="#bdlb_indexspanstringutil-example-1-creating-indexspan-objects-safely"> Example 1: Creating IndexSpan Objects Safely </a>
29/// * <a href="#bdlb_indexspanstringutil-example-2-creating-string-views"> Example 2: Creating String Views </a>
30///
31/// # Purpose {#bdlb_indexspanstringutil-purpose}
32/// Provide functions that operate on `IndexSpan` and string objects.
33///
34/// # Classes {#bdlb_indexspanstringutil-classes}
35///
36/// - bdlb::IndexSpanStringUtil: namespace for methods on `IndexSpan` and strings
37///
38/// @see bdlb_indexspan
39///
40/// # Description {#bdlb_indexspanstringutil-description}
41/// This component provides a struct, `IndexSpanStringUtil`, that
42/// serves as a namespace for utility functions that operate on `IndexSpan` and
43/// string objects. This component is designed to work on several
44/// representations of strings, including `bsl::string`, `bsl::string_view`, and
45/// it is also backwards compatible with `bslstl::StringRef`. Key operations of
46/// this utility include `bind`, for creating a `bsl::string_view` from an
47/// `IndexSpan` and a `string`, and the `create` methods that provide a way to
48/// create `IndexSpan` objects of a string object (of any kind) using positions
49/// defined by iterators and/or positions and/or length.
50///
51/// ## Usage {#bdlb_indexspanstringutil-usage}
52///
53///
54/// This section illustrates intended use of this component.
55///
56/// ### Example 1: Creating IndexSpan Objects Safely {#bdlb_indexspanstringutil-example-1-creating-indexspan-objects-safely}
57///
58///
59/// Suppose that we are creating a parser, and we want the results of the
60/// parsing to be stored in `IndexSpan` objects. The parser will have either a
61/// pointer (or "begin" iterator) into the original string input and then
62/// another pointer (or iterator) or a length to identify the end of the input.
63/// Turning the beginning and ending identifiers into an `IndexSpan` is a simple
64/// calculation, but one that is verbose and potentially error prone. Instead
65/// of implementing the calculation ourselves we use the convenience function
66/// `create` from `IndexSpanStringUtil`.
67///
68/// First, we define a string that we want to parse:
69/// @code
70/// const bsl::string full("James Tiberius Kirk");
71/// @endcode
72/// Then, we implement the parsing of the first name:
73/// @code
74/// typedef bsl::string::const_iterator Cit;
75/// Cit it = bsl::find(full.begin(), full.end(), ' ');
76/// // Error checking omitted since we know the string
77/// bdlb::IndexSpan first = bdlb::IndexSpanStringUtil::create(full,
78/// full.begin(),
79/// it);
80/// @endcode
81/// Next, we implement the parsing of the middle name, this time using length,
82/// rather than an end iterator (demonstrating an alternative `create` overload
83/// provided by `IndexSpanStringUtil`):
84/// @code
85/// ++it; // Skip the space
86/// Cit it2 = bsl::find(it, full.end(), ' ');
87/// bdlb::IndexSpan middle;
88/// if (full.end() != it2) {
89/// middle = bdlb::IndexSpanStringUtil::create(full, it, it2 - it);
90/// it = it2 + 1;
91/// }
92/// @endcode
93/// Then, we create the `IndexSpan` for the last name, using two positions:
94/// @code
95/// bdlb::IndexSpan last = bdlb::IndexSpanStringUtil::createFromPositions(
96/// full,
97/// it - full.begin(),
98/// full.length());
99/// @endcode
100/// Finally, we verify that the resulting `IndexSpan` objects correctly describe
101/// the parsed names of the `full` name:
102/// @code
103/// assert(full.substr(first.position(), first.length()) == "James");
104///
105/// assert(full.substr(middle.position(), middle.length()) == "Tiberius");
106///
107/// assert(full.substr(last.position(), last.length()) == "Kirk");
108/// @endcode
109///
110/// ### Example 2: Creating String Views {#bdlb_indexspanstringutil-example-2-creating-string-views}
111///
112///
113/// Suppose that we have `IndexSpan` objects that define the `first`, `middle`,
114/// and `last` part of a string that has a full name in it and we want to get
115/// actual string views that correspond to those parts of the string. The
116/// `bind` functions of `IndexSpanStringUtil` provide that functionality. The
117/// `bind` functions return a `bsl::string_view` into the original string (so
118/// the characters of the string are not copied). Note that this example builds
119/// on Example 1.
120///
121/// First, we define a string view of the parsed string to show that `bind`
122/// works both on strings and string views:
123/// @code
124/// const bsl::string_view fullView(full);
125/// @endcode
126/// Then we demonstrate binding `IndexSpan` object to that view:
127/// @code
128/// assert(bdlb::IndexSpanStringUtil::bind(fullView, first) == "James");
129///
130/// assert(bdlb::IndexSpanStringUtil::bind(fullView, middle) == "Tiberius");
131///
132/// assert(bdlb::IndexSpanStringUtil::bind(fullView, last) == "Kirk");
133/// @endcode
134/// Finally we demonstrate binding `IndexSpan` object to a `bsl::string`:
135/// @code
136/// assert(bdlb::IndexSpanStringUtil::bind(full, first) == "James");
137///
138/// assert(bdlb::IndexSpanStringUtil::bind(full, middle) == "Tiberius");
139///
140/// assert(bdlb::IndexSpanStringUtil::bind(full, last) == "Kirk");
141/// @endcode
142/// @}
143/** @} */
144/** @} */
145
146/** @addtogroup bdl
147 * @{
148 */
149/** @addtogroup bdlb
150 * @{
151 */
152/** @addtogroup bdlb_indexspanstringutil
153 * @{
154 */
155
156#include <bdlscm_version.h>
157
158#include <bdlb_indexspan.h>
159
160#include <bsls_assert.h>
161
162#include <bsl_functional.h>
163#include <bsl_string.h>
164#include <bsl_string_view.h>
165
166#include <bsls_libraryfeatures.h>
167
168#include <string>
169
170
171namespace bdlb {
172
173 // ==========================
174 // struct IndexSpanStringUtil
175 // ==========================
176
177/// This struct serves as a namespace for utility functions that operate on
178/// `IndexSpan` and string objects.
179///
180/// See @ref bdlb_indexspanstringutil
182
183 private:
184 // PRIVATE CLASS METHODS
185
186 /// Return a string view to the substring of the specified `string` as
187 /// described by the specified `span`, meaning the substring starting at
188 /// the `span.position()` index in `string` and having `span.length()` characters.
189 ///
190 /// \pre The behavior is undefined unless
191 /// `span.position() <= string.length()` and
192 /// `span.position() + span.length() <= string.length()`.
193 template <class CHAR_TYPE, class CHAR_TRAITS>
196 const IndexSpan& span);
197
198 /// Return an `IndexSpan` describing the substring of the specified
199 /// `string` as starting at the specified `begin` and ending at the
200 /// character preceding the specified `end`.
201 ///
202 /// \pre The behavior is undefined unless `begin <= string.end()`, `end <= string.end()`, and
203 /// `begin <= end`.
204 template <class CHAR_TYPE, class CHAR_TRAITS>
205 static IndexSpan createFromPosImp(
209
210 /// Return an `IndexSpan` describing the substring of the specified
211 /// `string` as starting at the specified `begin` and having the specified `length`.
212 ///
213 /// \pre The behavior is undefined unless
214 /// `begin <= string.length()` and `begin + length <= string.length()`.
215 template <class CHAR_TYPE, class CHAR_TRAITS>
216 static IndexSpan createImp(
219 IndexSpan::size_type length);
220
221 /// Return an `IndexSpan` describing the substring of the specified
222 /// `string` as defined by the `begin()` and `end()` of the specified `subString`.
223 ///
224 /// \pre The behavior is undefined unless
225 /// `subString.begin() <= string.end()`,
226 /// `subString.end() <= string.end()`,
227 /// `subString.begin() >= string.begin()`, and
228 /// `subString.end() <= string.end()`.
229 template <class CHAR_TYPE, class CHAR_TRAITS>
230 static IndexSpan createImp(
233
234 /// Return an `IndexSpan` describing the substring of the specified
235 /// `string` starting at the specified `begin` and having the specified `length`.
236 ///
237 /// \pre The behavior is undefined unless
238 /// `string.begin() <= begin`, `begin <= string.end()`, and
239 /// `begin + length <= string.end()`.
240 template <class CHAR_TYPE, class CHAR_TRAITS>
241 static IndexSpan createImp(
243 typename bsl::basic_string_view<CHAR_TYPE,
244 CHAR_TRAITS>::const_iterator begin,
245 IndexSpan::size_type length);
246
247 /// Return an `IndexSpan` describing the substring of the specified
248 /// `string` starting at the specified `begin` and ending (not including) the specified `end`.
249 ///
250 /// \pre The behavior is undefined unless
251 /// `begin >= string.begin()`, `end >= string.begin()`,
252 /// `begin <= string.end()`, `end <= string.end()`, and `begin <= end`.
253 template <class CHAR_TYPE, class CHAR_TRAITS>
254 static IndexSpan createImp(
256 typename bsl::basic_string_view<CHAR_TYPE,
257 CHAR_TRAITS>::const_iterator begin,
258 typename bsl::basic_string_view<CHAR_TYPE,
259 CHAR_TRAITS>::const_iterator end);
260
261 /// Return an `IndexSpan` describing the substring of the specified
262 /// `string` starting at the specified `begin` and having the specified `length`.
263 ///
264 /// \pre The behavior is undefined unless
265 /// `string.begin() <= begin`, `begin <= string.end()`, and
266 /// `begin + length <= string.end()`.
267 template <class CHAR_TYPE>
268 static IndexSpan createImp(
271 IndexSpan::size_type length);
272
273 /// Return an `IndexSpan` describing the substring of the specified
274 /// `string` starting at the specified `begin` and ending (not including) the specified `end`.
275 ///
276 /// \pre The behavior is undefined unless
277 /// `begin >= string.begin()`, `end >= string.begin()`,
278 /// `begin <= string.end()`, `end <= string.end()`, and `begin <= end`.
279 template <class CHAR_TYPE>
280 static IndexSpan createImp(
284
285 /// Return an `IndexSpan` describing the substring of the specified
286 /// `string` starting at the specified `begin` and having the specified `length`.
287 ///
288 /// \pre The behavior is undefined unless
289 /// `begin >= string.begin()`, `begin <= string.end()`, and
290 /// `begin + length <= string.end()`.
291 template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
292 static IndexSpan createImp(
293 const bsl::basic_string<CHAR_TYPE,
294 CHAR_TRAITS,
295 ALLOCATOR>& string,
296 typename bsl::basic_string<CHAR_TYPE,
297 CHAR_TRAITS,
298 ALLOCATOR>::const_iterator begin,
299 IndexSpan::size_type length);
300 template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
301 static IndexSpan createImp(
302 const std::basic_string<CHAR_TYPE,
303 CHAR_TRAITS,
304 ALLOCATOR>& string,
305 typename std::basic_string<CHAR_TYPE,
306 CHAR_TRAITS,
307 ALLOCATOR>::const_iterator begin,
308 IndexSpan::size_type length);
309
310 /// Return an `IndexSpan` describing the substring of the specified
311 /// `string` starting at the specified `begin` and ending (not including) the specified `end`.
312 ///
313 /// \pre The behavior is undefined unless
314 /// `begin >= string.begin()`, `begin <= string.end()`,
315 /// `end <= string.end()`, and `begin <= end`.
316 template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
317 static IndexSpan createImp(
318 const bsl::basic_string<CHAR_TYPE,
319 CHAR_TRAITS,
320 ALLOCATOR>& string,
321 typename bsl::basic_string<CHAR_TYPE,
322 CHAR_TRAITS,
323 ALLOCATOR>::const_iterator begin,
324 typename bsl::basic_string<CHAR_TYPE,
325 CHAR_TRAITS,
326 ALLOCATOR>::const_iterator end);
327 template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
328 static IndexSpan createImp(
329 const std::basic_string<CHAR_TYPE,
330 CHAR_TRAITS,
331 ALLOCATOR>& string,
332 typename std::basic_string<CHAR_TYPE,
333 CHAR_TRAITS,
334 ALLOCATOR>::const_iterator begin,
335 typename std::basic_string<CHAR_TYPE,
336 CHAR_TRAITS,
337 ALLOCATOR>::const_iterator end);
338
339 public:
340 // CLASS METHODS
341
342 /// Return a string reference to the substring of the specified `string`
343 /// as described by the specified `span`, meaning the substring starting
344 /// at the `span.position()` index in `string` and having `span.length()` characters.
345 ///
346 /// \pre The behavior is undefined unless
347 /// `span.position() <= string.length()` and
348 /// `span.position() + span.length() <= string.length()`.
349 static bsl::string_view bind(const bsl::string_view& string,
350 const IndexSpan& span);
351 static bsl::wstring_view bind(const bsl::wstring_view& string,
352 const IndexSpan& span);
353
354 /// Return an `IndexSpan` describing the substring of the specified
355 /// `string` starting at the specified `begin` and ending (not including) the specified `end`.
356 ///
357 /// \pre The behavior is undefined unless
358 /// `begin <= string.length()` and `end <= string.length()`.
365
366 /// Return an `IndexSpan` describing the substring of the specified
367 /// `string` starting at the specified `begin` and having the specified `length`.
368 ///
369 /// \pre The behavior is undefined unless
370 /// `begin <= string.length()` and `begin + length <= string.length()`.
371 static IndexSpan create(const bsl::string_view& string,
373 IndexSpan::size_type length);
374 static IndexSpan create(const bsl::wstring_view& string,
376 IndexSpan::size_type length);
377
378 /// Return an `IndexSpan` describing the substring of the specified
379 /// `string` as defined by the `begin()` and `end()` of the specified `subString`.
380 ///
381 /// \pre The behavior is undefined unless
382 /// `subString.begin() <= string.end()`,
383 /// `subString.end() <= string.end()`,
384 /// `subString.begin() >= string.begin()`, and
385 /// `subString.end() <= string.begin()`.
386 static IndexSpan create(const bsl::string_view& string,
387 const bsl::string_view& subString);
388 static IndexSpan create(const bsl::wstring_view& string,
389 const bsl::wstring_view& subString);
390
391 /// Return an `IndexSpan` describing the substring of the specified
392 /// `string` starting at the specified `begin` and having the specified `length`.
393 ///
394 /// \pre The behavior is undefined unless
395 /// `begin >= string.begin()`, `begin <= string.end()`, and
396 /// `begin + length <= string.end()`.
397 static IndexSpan create(const bsl::string_view& string,
399 IndexSpan::size_type length);
400 static IndexSpan create(const bsl::wstring_view& string,
402 IndexSpan::size_type length);
403 static IndexSpan create(const bslstl::StringRef& string,
405 IndexSpan::size_type length);
406 static IndexSpan create(const bslstl::StringRefWide& string,
408 IndexSpan::size_type length);
409 static IndexSpan create(const bsl::string& string,
411 IndexSpan::size_type length);
412 static IndexSpan create(const bsl::wstring& string,
414 IndexSpan::size_type length);
415 static IndexSpan create(const std::string& string,
416 std::string::const_iterator begin,
417 IndexSpan::size_type length);
418 static IndexSpan create(const std::wstring& string,
419 std::wstring::const_iterator begin,
420 IndexSpan::size_type length);
421#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
422 static IndexSpan create(const std::pmr::string& string,
423 std::pmr::string::const_iterator begin,
424 IndexSpan::size_type length);
425 static IndexSpan create(const std::pmr::wstring& string,
426 std::pmr::wstring::const_iterator begin,
427 IndexSpan::size_type length);
428#endif
429
430 /// Return an `IndexSpan` describing the substring of the specified
431 /// `string` starting at the specified `begin` and ending (not including) the specified `end`.
432 ///
433 /// \pre The behavior is undefined unless
434 /// `begin >= string.begin()`, `begin <= string.end()`,
435 /// `end <= string.end()`, and `begin <= end`.
436 static IndexSpan create(const bsl::string_view& string,
439 static IndexSpan create(const bsl::wstring_view& string,
442 static IndexSpan create(const bslstl::StringRef& string,
445 static IndexSpan create(const bslstl::StringRefWide& string,
448 static IndexSpan create(const bsl::string& string,
451 static IndexSpan create(const bsl::wstring& string,
454 static IndexSpan create(const std::string& string,
455 std::string::const_iterator begin,
456 std::string::const_iterator end);
457 static IndexSpan create(const std::wstring& string,
458 std::wstring::const_iterator begin,
459 std::wstring::const_iterator end);
460#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
461 static IndexSpan create(const std::pmr::string& string,
462 std::pmr::string::const_iterator begin,
463 std::pmr::string::const_iterator end);
464 static IndexSpan create(const std::pmr::wstring& string,
465 std::pmr::wstring::const_iterator begin,
466 std::pmr::wstring::const_iterator end);
467#endif
468};
469
470// ============================================================================
471// INLINE DEFINITIONS
472// ============================================================================
473
474 // --------------------------
475 // struct IndexSpanStringUtil
476 // --------------------------
477
478// PRIVATE CLASS METHODS
479template <class CHAR_TYPE, class CHAR_TRAITS>
480inline
482IndexSpanStringUtil::bindImp(
484 const IndexSpan& span)
485{
486 BSLS_ASSERT(span.position() <= string.length());
487 BSLS_ASSERT(span.position() + span.length() <= string.length());
488
489 return bslstl::StringRefImp<CHAR_TYPE>(string.data() + span.position(),
490 span.length());
491}
492
493template <class CHAR_TYPE, class CHAR_TRAITS>
494inline
495IndexSpan
496IndexSpanStringUtil::createFromPosImp(
500{
501 BSLS_ASSERT(begin <= string.size());
502 BSLS_ASSERT(end <= string.size());
503 BSLS_ASSERT(begin <= end);
504
505 (void)string; // remove unused parameter warnings
506
507 return IndexSpan(begin, end - begin);
508}
509
510template <class CHAR_TYPE, class CHAR_TRAITS>
511inline
512IndexSpan
513IndexSpanStringUtil::createImp(
517{
518 BSLS_ASSERT(begin <= string.size());
519 BSLS_ASSERT(begin + length <= string.size());
520
521 (void)string; // remove unused parameter warnings
522
523 return IndexSpan(begin, length);
524}
525
526template <class CHAR_TYPE, class CHAR_TRAITS>
527inline
528IndexSpan
529IndexSpanStringUtil::createImp(
532{
533 typedef
535 const_pointer;
536
537 bsl::less_equal<const_pointer> lessEqual;
538 bsl::greater_equal<const_pointer> greaterEqual;
539
540 const_pointer stringBegin = string.data();
541 const_pointer stringEnd = string.data() + string.length();
542 const_pointer subStringBegin = subString.data();
543 const_pointer subStringEnd = subString.data() + subString.length();
544
545 // Suppress unused variable warnings
546 (void)lessEqual;
547 (void)greaterEqual;
548 (void)stringEnd;
549 (void)subStringEnd;
550
551 BSLS_ASSERT(lessEqual( subStringBegin, stringEnd ));
552 BSLS_ASSERT(lessEqual( subStringEnd, stringEnd ));
553 BSLS_ASSERT(greaterEqual(subStringBegin, stringBegin));
554 BSLS_ASSERT(greaterEqual(subStringEnd, stringBegin));
555
556 return IndexSpan(subStringBegin - stringBegin, subString.length());
557}
558
559template <class CHAR_TYPE, class CHAR_TRAITS>
560inline
561IndexSpan
562IndexSpanStringUtil::createImp(
564 typename bsl::basic_string_view<CHAR_TYPE,
565 CHAR_TRAITS>::const_iterator begin,
567{
568 BSLS_ASSERT(begin >= string.begin());
569 BSLS_ASSERT(begin <= string.end());
570 BSLS_ASSERT(begin + length <= string.end());
571
572 return IndexSpan(begin - string.begin(), length);
573}
574
575template <class CHAR_TYPE, class CHAR_TRAITS>
576inline
577IndexSpan
578IndexSpanStringUtil::createImp(
580 typename bsl::basic_string_view<CHAR_TYPE,
581 CHAR_TRAITS>::const_iterator begin,
582 typename bsl::basic_string_view<CHAR_TYPE,
583 CHAR_TRAITS>::const_iterator end)
584{
585 BSLS_ASSERT(begin >= string.begin());
586 BSLS_ASSERT(begin <= end);
587 BSLS_ASSERT(begin <= string.end());
588 BSLS_ASSERT(end <= string.end());
589
590 return IndexSpan(begin - string.begin(), end - begin);
591}
592
593template <class CHAR_TYPE>
594inline
595IndexSpan
596IndexSpanStringUtil::createImp(
600{
601 BSLS_ASSERT(begin >= string.begin());
602 BSLS_ASSERT(begin <= string.end() );
603 BSLS_ASSERT(begin + length <= string.end() );
604
605 return IndexSpan(begin - string.begin(), length);
606}
607
608template <class CHAR_TYPE>
609inline
610IndexSpan
611IndexSpanStringUtil::createImp(
615{
616 BSLS_ASSERT(begin >= string.begin());
617 BSLS_ASSERT(begin <= end );
618 BSLS_ASSERT(begin <= string.end() );
619 BSLS_ASSERT(end <= string.end() );
620
621 return IndexSpan(begin - string.begin(), end - begin);
622}
623
624template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
625inline
626IndexSpan
627IndexSpanStringUtil::createImp(
628 const bsl::basic_string<CHAR_TYPE,
629 CHAR_TRAITS,
630 ALLOCATOR>& string,
631 typename bsl::basic_string<CHAR_TYPE,
632 CHAR_TRAITS,
633 ALLOCATOR>::const_iterator begin,
635{
636 BSLS_ASSERT(begin >= string.begin());
637 BSLS_ASSERT(begin <= string.end());
638 BSLS_ASSERT(begin + length <= string.end());
639
640 return IndexSpan(begin - string.begin(), length);
641}
642
643template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
644inline
645IndexSpan
646IndexSpanStringUtil::createImp(
647 const std::basic_string<CHAR_TYPE,
648 CHAR_TRAITS,
649 ALLOCATOR>& string,
650 typename std::basic_string<CHAR_TYPE,
651 CHAR_TRAITS,
652 ALLOCATOR>::const_iterator begin,
654{
655 BSLS_ASSERT(begin >= string.begin());
656 BSLS_ASSERT(begin <= string.end());
657 BSLS_ASSERT(begin + length <= string.end());
658
659 return IndexSpan(begin - string.begin(), length);
660}
661
662template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
663inline
664IndexSpan
665IndexSpanStringUtil::createImp(
666 const bsl::basic_string<CHAR_TYPE,
667 CHAR_TRAITS,
668 ALLOCATOR>& string,
669 typename bsl::basic_string<CHAR_TYPE,
670 CHAR_TRAITS,
671 ALLOCATOR>::const_iterator begin,
672 typename bsl::basic_string<CHAR_TYPE,
673 CHAR_TRAITS,
674 ALLOCATOR>::const_iterator end)
675{
676 BSLS_ASSERT(begin >= string.begin());
677 BSLS_ASSERT(begin <= end);
678 BSLS_ASSERT(begin <= string.end());
679 BSLS_ASSERT(end <= string.end());
680
681 return IndexSpan(begin - string.begin(), end - begin);
682}
683
684template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
685inline
686IndexSpan
687IndexSpanStringUtil::createImp(
688 const std::basic_string<CHAR_TYPE,
689 CHAR_TRAITS,
690 ALLOCATOR>& string,
691 typename std::basic_string<CHAR_TYPE,
692 CHAR_TRAITS,
693 ALLOCATOR>::const_iterator begin,
694 typename std::basic_string<CHAR_TYPE,
695 CHAR_TRAITS,
696 ALLOCATOR>::const_iterator end)
697{
698 BSLS_ASSERT(begin >= string.begin());
699 BSLS_ASSERT(begin <= end);
700 BSLS_ASSERT(begin <= string.end());
701 BSLS_ASSERT(end <= string.end());
702
703 return IndexSpan(begin - string.begin(), end - begin);
704}
705
706// CLASS METHODS
707inline
710 const IndexSpan& span)
711{
712 return bindImp(string, span);
713}
714
715inline
718 const IndexSpan& span)
719{
720 return bindImp(string, span);
721}
722
723inline
728{
729 return createFromPosImp(string, begin, end);
730}
731
732inline
737{
738 return createFromPosImp(string, begin, end);
739}
740
741inline
746{
747 return createImp(string, begin, length);
748}
749
750inline
755{
756 return createImp(string, begin, length);
757}
758
759inline
762 const bsl::string_view& subString)
763{
764 return createImp(string, subString);
765}
766
767inline
770 const bsl::wstring_view& subString)
771{
772 return createImp(string, subString);
773}
774
775inline
780{
781 return createImp(string, begin, length);
782}
783
784inline
789{
790 return createImp(string, begin, length);
791}
792
793inline
798{
799 return createImp(string, begin, length);
800}
801
802inline
807{
808 return createImp(string, begin, length);
809}
810
811inline
816{
817 return createImp(string, begin, length);
818}
819
820inline
825{
826 return createImp(string, begin, length);
827}
828
829inline
831IndexSpanStringUtil::create(const std::string& string,
832 std::string::const_iterator begin,
834{
835 return createImp(string, begin, length);
836}
837
838inline
840IndexSpanStringUtil::create(const std::wstring& string,
841 std::wstring::const_iterator begin,
843{
844 return createImp(string, begin, length);
845}
846
847#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
848inline
850IndexSpanStringUtil::create(const std::pmr::string& string,
851 std::pmr::string::const_iterator begin,
853{
854 return createImp(string, begin, length);
855}
856
857inline
858IndexSpan
859IndexSpanStringUtil::create(const std::pmr::wstring& string,
860 std::pmr::wstring::const_iterator begin,
862{
863 return createImp(string, begin, length);
864}
865#endif
866
867inline
868IndexSpan
872{
873 return createImp(string, begin, end);
874}
875
876inline
881{
882 return createImp(string, begin, end);
883}
884
885inline
890{
891 return createImp(string, begin, end);
892}
893
894inline
899{
900 return createImp(string, begin, end);
901}
902
903inline
908{
909 return createImp(string, begin, end);
910}
911
912inline
917{
918 return createImp(string, begin, end);
919}
920
921inline
923IndexSpanStringUtil::create(const std::string& string,
924 std::string::const_iterator begin,
925 std::string::const_iterator end)
926{
927 return createImp(string, begin, end);
928}
929
930inline
932IndexSpanStringUtil::create(const std::wstring& string,
933 std::wstring::const_iterator begin,
934 std::wstring::const_iterator end)
935{
936 return createImp(string, begin, end);
937}
938
939#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
940inline
942IndexSpanStringUtil::create(const std::pmr::string& string,
943 std::pmr::string::const_iterator begin,
944 std::pmr::string::const_iterator end)
945{
946 return createImp(string, begin, end);
947}
948
949inline
950IndexSpan
951IndexSpanStringUtil::create(const std::pmr::wstring& string,
952 std::pmr::wstring::const_iterator begin,
953 std::pmr::wstring::const_iterator end)
954{
955 return createImp(string, begin, end);
956}
957#endif
958
959} // close package namespace
960
961
962#endif
963
964// ----------------------------------------------------------------------------
965// Copyright 2021 Bloomberg Finance L.P.
966//
967// Licensed under the Apache License, Version 2.0 (the "License");
968// you may not use this file except in compliance with the License.
969// You may obtain a copy of the License at
970//
971// http://www.apache.org/licenses/LICENSE-2.0
972//
973// Unless required by applicable law or agreed to in writing, software
974// distributed under the License is distributed on an "AS IS" BASIS,
975// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
976// See the License for the specific language governing permissions and
977// limitations under the License.
978// ----------------------------- END-OF-FILE ----------------------------------
979
980/** @} */
981/** @} */
982/** @} */
Definition bdlb_indexspan.h:309
size_type position() const
Return the position attribute.
Definition bdlb_indexspan.h:453
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:447
Definition bslstl_stringview.h:471
const value_type * const_pointer
Definition bslstl_stringview.h:478
BSLS_KEYWORD_CONSTEXPR size_type length() const BSLS_KEYWORD_NOEXCEPT
Return the length of this view.
Definition bslstl_stringview.h:1913
BSLS_KEYWORD_CONSTEXPR const_pointer data() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1988
const value_type * const_iterator
Definition bslstl_stringview.h:481
Definition bslstl_string.h:1252
const CHAR_TYPE * const_iterator
Definition bslstl_string.h:1282
Definition bslstl_stringref.h:374
const CHAR_TYPE * const_iterator
Definition bslstl_stringref.h:386
#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
bsl::size_t size(const TYPE &array)
Return the number of elements in the specified array.
Definition bdlb_algorithmworkaroundutil.h:74
T::iterator begin(T &container)
Definition bslstl_iterator.h:1593
T::iterator end(T &container)
Definition bslstl_iterator.h:1621
Definition bdlb_indexspanstringutil.h:181
static bsl::string_view bind(const bsl::string_view &string, const IndexSpan &span)
Definition bdlb_indexspanstringutil.h:709
static IndexSpan createFromPositions(const bsl::string_view &string, IndexSpan::size_type begin, IndexSpan::size_type end)
Definition bdlb_indexspanstringutil.h:725
static IndexSpan create(const bsl::string_view &string, IndexSpan::size_type begin, IndexSpan::size_type length)
Definition bdlb_indexspanstringutil.h:743