BDE 4.14.0 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.
180
181 private:
182 // PRIVATE CLASS METHODS
183
184 /// Return a string view to the substring of the specified `string` as
185 /// described by the specified `span`, meaning the substring starting at
186 /// the `span.position()` index in `string` and having `span.length()`
187 /// characters. The behavior is undefined unless
188 /// `span.position() <= string.length()` and
189 /// `span.position() + span.length() <= string.length()`.
190 template <class CHAR_TYPE, class CHAR_TRAITS>
193 const IndexSpan& span);
194
195 /// Return an `IndexSpan` describing the substring of the specified
196 /// `string` as starting at the specified `begin` and ending at the
197 /// character preceding the specified `end`. The behavior is undefined
198 /// unless `begin <= string.end()`, `end <= string.end()`, and
199 /// `begin <= end`.
200 template <class CHAR_TYPE, class CHAR_TRAITS>
201 static IndexSpan createFromPosImp(
205
206 /// Return an `IndexSpan` describing the substring of the specified
207 /// `string` as starting at the specified `begin` and having the
208 /// specified `length`. The behavior is undefined unless
209 /// `begin <= string.length()` and `begin + length <= string.length()`.
210 template <class CHAR_TYPE, class CHAR_TRAITS>
211 static IndexSpan createImp(
214 IndexSpan::size_type length);
215
216 /// Return an `IndexSpan` describing the substring of the specified
217 /// `string` as defined by the `begin()` and `end()` of the specified
218 /// `subString`. The behavior is undefined unless
219 /// `subString.begin() <= string.end()`,
220 /// `subString.end() <= string.end()`,
221 /// `subString.begin() >= string.begin()`, and
222 /// `subString.end() <= string.end()`.
223 template <class CHAR_TYPE, class CHAR_TRAITS>
224 static IndexSpan createImp(
227
228 /// Return an `IndexSpan` describing the substring of the specified
229 /// `string` starting at the specified `begin` and having the specified
230 /// `length`. The behavior is undefined unless
231 /// `string.begin() <= begin`, `begin <= string.end()`, and
232 /// `begin + length <= string.end()`.
233 template <class CHAR_TYPE, class CHAR_TRAITS>
234 static IndexSpan createImp(
236 typename bsl::basic_string_view<CHAR_TYPE,
237 CHAR_TRAITS>::const_iterator begin,
238 IndexSpan::size_type length);
239
240 /// Return an `IndexSpan` describing the substring of the specified
241 /// `string` starting at the specified `begin` and ending (not
242 /// including) the specified `end`. The behavior is undefined unless
243 /// `begin >= string.begin()`, `end >= string.begin()`,
244 /// `begin <= string.end()`, `end <= string.end()`, and `begin <= end`.
245 template <class CHAR_TYPE, class CHAR_TRAITS>
246 static IndexSpan createImp(
248 typename bsl::basic_string_view<CHAR_TYPE,
249 CHAR_TRAITS>::const_iterator begin,
250 typename bsl::basic_string_view<CHAR_TYPE,
251 CHAR_TRAITS>::const_iterator end);
252
253 /// Return an `IndexSpan` describing the substring of the specified
254 /// `string` starting at the specified `begin` and having the specified
255 /// `length`. The behavior is undefined unless
256 /// `string.begin() <= begin`, `begin <= string.end()`, and
257 /// `begin + length <= string.end()`.
258 template <class CHAR_TYPE>
259 static IndexSpan createImp(
262 IndexSpan::size_type length);
263
264 /// Return an `IndexSpan` describing the substring of the specified
265 /// `string` starting at the specified `begin` and ending (not
266 /// including) the specified `end`. The behavior is undefined unless
267 /// `begin >= string.begin()`, `end >= string.begin()`,
268 /// `begin <= string.end()`, `end <= string.end()`, and `begin <= end`.
269 template <class CHAR_TYPE>
270 static IndexSpan createImp(
274
275 /// Return an `IndexSpan` describing the substring of the specified
276 /// `string` starting at the specified `begin` and having the specified
277 /// `length`. The behavior is undefined unless
278 /// `begin >= string.begin()`, `begin <= string.end()`, and
279 /// `begin + length <= string.end()`.
280 template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
281 static IndexSpan createImp(
282 const bsl::basic_string<CHAR_TYPE,
283 CHAR_TRAITS,
284 ALLOCATOR>& string,
285 typename bsl::basic_string<CHAR_TYPE,
286 CHAR_TRAITS,
287 ALLOCATOR>::const_iterator begin,
288 IndexSpan::size_type length);
289 template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
290 static IndexSpan createImp(
291 const std::basic_string<CHAR_TYPE,
292 CHAR_TRAITS,
293 ALLOCATOR>& string,
294 typename std::basic_string<CHAR_TYPE,
295 CHAR_TRAITS,
296 ALLOCATOR>::const_iterator begin,
297 IndexSpan::size_type length);
298
299 /// Return an `IndexSpan` describing the substring of the specified
300 /// `string` starting at the specified `begin` and ending (not
301 /// including) the specified `end`. The behavior is undefined unless
302 /// `begin >= string.begin()`, `begin <= string.end()`,
303 /// `end <= string.end()`, and `begin <= end`.
304 template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
305 static IndexSpan createImp(
306 const bsl::basic_string<CHAR_TYPE,
307 CHAR_TRAITS,
308 ALLOCATOR>& string,
309 typename bsl::basic_string<CHAR_TYPE,
310 CHAR_TRAITS,
311 ALLOCATOR>::const_iterator begin,
312 typename bsl::basic_string<CHAR_TYPE,
313 CHAR_TRAITS,
314 ALLOCATOR>::const_iterator end);
315 template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
316 static IndexSpan createImp(
317 const std::basic_string<CHAR_TYPE,
318 CHAR_TRAITS,
319 ALLOCATOR>& string,
320 typename std::basic_string<CHAR_TYPE,
321 CHAR_TRAITS,
322 ALLOCATOR>::const_iterator begin,
323 typename std::basic_string<CHAR_TYPE,
324 CHAR_TRAITS,
325 ALLOCATOR>::const_iterator end);
326
327 public:
328 // CLASS METHODS
329
330 /// Return a string reference to the substring of the specified `string`
331 /// as described by the specified `span`, meaning the substring starting
332 /// at the `span.position()` index in `string` and having
333 /// `span.length()` characters. The behavior is undefined unless
334 /// `span.position() <= string.length()` and
335 /// `span.position() + span.length() <= string.length()`.
336 static bsl::string_view bind(const bsl::string_view& string,
337 const IndexSpan& span);
338 static bsl::wstring_view bind(const bsl::wstring_view& string,
339 const IndexSpan& span);
340
344 /// Return an `IndexSpan` describing the substring of the specified
345 /// `string` starting at the specified `begin` and ending (not
346 /// including) the specified `end`. The behavior is undefined unless
347 /// `begin <= string.length()` and `end <= string.length()`.
351
352 static IndexSpan create(const bsl::string_view& string,
354 IndexSpan::size_type length);
355 /// Return an `IndexSpan` describing the substring of the specified
356 /// `string` starting at the specified `begin` and having the specified
357 /// `length`. The behavior is undefined unless
358 /// `begin <= string.length()` and `begin + length <= string.length()`.
359 static IndexSpan create(const bsl::wstring_view& string,
361 IndexSpan::size_type length);
362
363 static IndexSpan create(const bsl::string_view& string,
364 const bsl::string_view& subString);
365 /// Return an `IndexSpan` describing the substring of the specified
366 /// `string` as defined by the `begin()` and `end()` of the specified
367 /// `subString`. The behavior is undefined unless
368 /// `subString.begin() <= string.end()`,
369 /// `subString.end() <= string.end()`,
370 /// `subString.begin() >= string.begin()`, and
371 /// `subString.end() <= string.begin()`.
372 static IndexSpan create(const bsl::wstring_view& string,
373 const bsl::wstring_view& subString);
374
375 static IndexSpan create(const bsl::string_view& string,
377 IndexSpan::size_type length);
378 static IndexSpan create(const bsl::wstring_view& string,
380 IndexSpan::size_type length);
381 static IndexSpan create(const bslstl::StringRef& string,
383 IndexSpan::size_type length);
384 static IndexSpan create(const bslstl::StringRefWide& string,
386 IndexSpan::size_type length);
387 static IndexSpan create(const bsl::string& string,
389 IndexSpan::size_type length);
390 static IndexSpan create(const bsl::wstring& string,
392 IndexSpan::size_type length);
393 static IndexSpan create(const std::string& string,
394 std::string::const_iterator begin,
395 IndexSpan::size_type length);
396 /// Return an `IndexSpan` describing the substring of the specified
397 /// `string` starting at the specified `begin` and having the specified
398 /// `length`. The behavior is undefined unless
399 /// `begin >= string.begin()`, `begin <= string.end()`, and
400 /// `begin + length <= string.end()`.
401 static IndexSpan create(const std::wstring& string,
402 std::wstring::const_iterator begin,
403 IndexSpan::size_type length);
404#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
405 static IndexSpan create(const std::pmr::string& string,
406 std::pmr::string::const_iterator begin,
407 IndexSpan::size_type length);
408 static IndexSpan create(const std::pmr::wstring& string,
409 std::pmr::wstring::const_iterator begin,
410 IndexSpan::size_type length);
411#endif
412
413 static IndexSpan create(const bsl::string_view& string,
416 static IndexSpan create(const bsl::wstring_view& string,
419 static IndexSpan create(const bslstl::StringRef& string,
422 static IndexSpan create(const bslstl::StringRefWide& string,
425 static IndexSpan create(const bsl::string& string,
428 static IndexSpan create(const bsl::wstring& string,
431 static IndexSpan create(const std::string& string,
432 std::string::const_iterator begin,
433 std::string::const_iterator end);
434 /// Return an `IndexSpan` describing the substring of the specified
435 /// `string` starting at the specified `begin` and ending (not
436 /// including) the specified `end`. The behavior is undefined unless
437 /// `begin >= string.begin()`, `begin <= string.end()`,
438 /// `end <= string.end()`, and `begin <= end`.
439 static IndexSpan create(const std::wstring& string,
440 std::wstring::const_iterator begin,
441 std::wstring::const_iterator end);
442#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
443 static IndexSpan create(const std::pmr::string& string,
444 std::pmr::string::const_iterator begin,
445 std::pmr::string::const_iterator end);
446 static IndexSpan create(const std::pmr::wstring& string,
447 std::pmr::wstring::const_iterator begin,
448 std::pmr::wstring::const_iterator end);
449#endif
450};
451
452// ============================================================================
453// INLINE DEFINITIONS
454// ============================================================================
455
456 // --------------------------
457 // struct IndexSpanStringUtil
458 // --------------------------
459
460// PRIVATE CLASS METHODS
461template <class CHAR_TYPE, class CHAR_TRAITS>
462inline
464IndexSpanStringUtil::bindImp(
466 const IndexSpan& span)
467{
468 BSLS_ASSERT(span.position() <= string.length());
469 BSLS_ASSERT(span.position() + span.length() <= string.length());
470
471 return bslstl::StringRefImp<CHAR_TYPE>(string.data() + span.position(),
472 span.length());
473}
474
475template <class CHAR_TYPE, class CHAR_TRAITS>
476inline
477IndexSpan
478IndexSpanStringUtil::createFromPosImp(
482{
483 BSLS_ASSERT(begin <= string.size());
484 BSLS_ASSERT(end <= string.size());
485 BSLS_ASSERT(begin <= end);
486
487 (void)string; // remove unused parameter warnings
488
489 return IndexSpan(begin, end - begin);
490}
491
492template <class CHAR_TYPE, class CHAR_TRAITS>
493inline
494IndexSpan
495IndexSpanStringUtil::createImp(
499{
500 BSLS_ASSERT(begin <= string.size());
501 BSLS_ASSERT(begin + length <= string.size());
502
503 (void)string; // remove unused parameter warnings
504
505 return IndexSpan(begin, length);
506}
507
508template <class CHAR_TYPE, class CHAR_TRAITS>
509inline
510IndexSpan
511IndexSpanStringUtil::createImp(
514{
515 typedef
517 const_pointer;
518
519 bsl::less_equal<const_pointer> lessEqual;
520 bsl::greater_equal<const_pointer> greaterEqual;
521
522 const_pointer stringBegin = string.data();
523 const_pointer stringEnd = string.data() + string.length();
524 const_pointer subStringBegin = subString.data();
525 const_pointer subStringEnd = subString.data() + subString.length();
526
527 // Suppress unused variable warnings
528 (void)lessEqual;
529 (void)greaterEqual;
530 (void)stringEnd;
531 (void)subStringEnd;
532
533 BSLS_ASSERT(lessEqual( subStringBegin, stringEnd ));
534 BSLS_ASSERT(lessEqual( subStringEnd, stringEnd ));
535 BSLS_ASSERT(greaterEqual(subStringBegin, stringBegin));
536 BSLS_ASSERT(greaterEqual(subStringEnd, stringBegin));
537
538 return IndexSpan(subStringBegin - stringBegin, subString.length());
539}
540
541template <class CHAR_TYPE, class CHAR_TRAITS>
542inline
543IndexSpan
544IndexSpanStringUtil::createImp(
546 typename bsl::basic_string_view<CHAR_TYPE,
547 CHAR_TRAITS>::const_iterator begin,
549{
550 BSLS_ASSERT(begin >= string.begin());
551 BSLS_ASSERT(begin <= string.end());
552 BSLS_ASSERT(begin + length <= string.end());
553
554 return IndexSpan(begin - string.begin(), length);
555}
556
557template <class CHAR_TYPE, class CHAR_TRAITS>
558inline
559IndexSpan
560IndexSpanStringUtil::createImp(
562 typename bsl::basic_string_view<CHAR_TYPE,
563 CHAR_TRAITS>::const_iterator begin,
564 typename bsl::basic_string_view<CHAR_TYPE,
565 CHAR_TRAITS>::const_iterator end)
566{
567 BSLS_ASSERT(begin >= string.begin());
568 BSLS_ASSERT(begin <= end);
569 BSLS_ASSERT(begin <= string.end());
570 BSLS_ASSERT(end <= string.end());
571
572 return IndexSpan(begin - string.begin(), end - begin);
573}
574
575template <class CHAR_TYPE>
576inline
577IndexSpan
578IndexSpanStringUtil::createImp(
582{
583 BSLS_ASSERT(begin >= string.begin());
584 BSLS_ASSERT(begin <= string.end() );
585 BSLS_ASSERT(begin + length <= string.end() );
586
587 return IndexSpan(begin - string.begin(), length);
588}
589
590template <class CHAR_TYPE>
591inline
592IndexSpan
593IndexSpanStringUtil::createImp(
597{
598 BSLS_ASSERT(begin >= string.begin());
599 BSLS_ASSERT(begin <= end );
600 BSLS_ASSERT(begin <= string.end() );
601 BSLS_ASSERT(end <= string.end() );
602
603 return IndexSpan(begin - string.begin(), end - begin);
604}
605
606template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
607inline
608IndexSpan
609IndexSpanStringUtil::createImp(
610 const bsl::basic_string<CHAR_TYPE,
611 CHAR_TRAITS,
612 ALLOCATOR>& string,
613 typename bsl::basic_string<CHAR_TYPE,
614 CHAR_TRAITS,
615 ALLOCATOR>::const_iterator begin,
617{
618 BSLS_ASSERT(begin >= string.begin());
619 BSLS_ASSERT(begin <= string.end());
620 BSLS_ASSERT(begin + length <= string.end());
621
622 return IndexSpan(begin - string.begin(), length);
623}
624
625template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
626inline
627IndexSpan
628IndexSpanStringUtil::createImp(
629 const std::basic_string<CHAR_TYPE,
630 CHAR_TRAITS,
631 ALLOCATOR>& string,
632 typename std::basic_string<CHAR_TYPE,
633 CHAR_TRAITS,
634 ALLOCATOR>::const_iterator begin,
636{
637 BSLS_ASSERT(begin >= string.begin());
638 BSLS_ASSERT(begin <= string.end());
639 BSLS_ASSERT(begin + length <= string.end());
640
641 return IndexSpan(begin - string.begin(), length);
642}
643
644template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
645inline
646IndexSpan
647IndexSpanStringUtil::createImp(
648 const bsl::basic_string<CHAR_TYPE,
649 CHAR_TRAITS,
650 ALLOCATOR>& string,
651 typename bsl::basic_string<CHAR_TYPE,
652 CHAR_TRAITS,
653 ALLOCATOR>::const_iterator begin,
654 typename bsl::basic_string<CHAR_TYPE,
655 CHAR_TRAITS,
656 ALLOCATOR>::const_iterator end)
657{
658 BSLS_ASSERT(begin >= string.begin());
659 BSLS_ASSERT(begin <= end);
660 BSLS_ASSERT(begin <= string.end());
661 BSLS_ASSERT(end <= string.end());
662
663 return IndexSpan(begin - string.begin(), end - begin);
664}
665
666template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
667inline
668IndexSpan
669IndexSpanStringUtil::createImp(
670 const std::basic_string<CHAR_TYPE,
671 CHAR_TRAITS,
672 ALLOCATOR>& string,
673 typename std::basic_string<CHAR_TYPE,
674 CHAR_TRAITS,
675 ALLOCATOR>::const_iterator begin,
676 typename std::basic_string<CHAR_TYPE,
677 CHAR_TRAITS,
678 ALLOCATOR>::const_iterator end)
679{
680 BSLS_ASSERT(begin >= string.begin());
681 BSLS_ASSERT(begin <= end);
682 BSLS_ASSERT(begin <= string.end());
683 BSLS_ASSERT(end <= string.end());
684
685 return IndexSpan(begin - string.begin(), end - begin);
686}
687
688// CLASS METHODS
689inline
692 const IndexSpan& span)
693{
694 return bindImp(string, span);
695}
696
697inline
700 const IndexSpan& span)
701{
702 return bindImp(string, span);
703}
704
705inline
710{
711 return createFromPosImp(string, begin, end);
712}
713
714inline
719{
720 return createFromPosImp(string, begin, end);
721}
722
723inline
728{
729 return createImp(string, begin, length);
730}
731
732inline
737{
738 return createImp(string, begin, length);
739}
740
741inline
744 const bsl::string_view& subString)
745{
746 return createImp(string, subString);
747}
748
749inline
752 const bsl::wstring_view& subString)
753{
754 return createImp(string, subString);
755}
756
757inline
762{
763 return createImp(string, begin, length);
764}
765
766inline
771{
772 return createImp(string, begin, length);
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
813IndexSpanStringUtil::create(const std::string& string,
814 std::string::const_iterator begin,
816{
817 return createImp(string, begin, length);
818}
819
820inline
822IndexSpanStringUtil::create(const std::wstring& string,
823 std::wstring::const_iterator begin,
825{
826 return createImp(string, begin, length);
827}
828
829#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
830inline
832IndexSpanStringUtil::create(const std::pmr::string& string,
833 std::pmr::string::const_iterator begin,
835{
836 return createImp(string, begin, length);
837}
838
839inline
840IndexSpan
841IndexSpanStringUtil::create(const std::pmr::wstring& string,
842 std::pmr::wstring::const_iterator begin,
844{
845 return createImp(string, begin, length);
846}
847#endif
848
849inline
850IndexSpan
854{
855 return createImp(string, begin, end);
856}
857
858inline
863{
864 return createImp(string, begin, end);
865}
866
867inline
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
905IndexSpanStringUtil::create(const std::string& string,
906 std::string::const_iterator begin,
907 std::string::const_iterator end)
908{
909 return createImp(string, begin, end);
910}
911
912inline
914IndexSpanStringUtil::create(const std::wstring& string,
915 std::wstring::const_iterator begin,
916 std::wstring::const_iterator end)
917{
918 return createImp(string, begin, end);
919}
920
921#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
922inline
924IndexSpanStringUtil::create(const std::pmr::string& string,
925 std::pmr::string::const_iterator begin,
926 std::pmr::string::const_iterator end)
927{
928 return createImp(string, begin, end);
929}
930
931inline
932IndexSpan
933IndexSpanStringUtil::create(const std::pmr::wstring& string,
934 std::pmr::wstring::const_iterator begin,
935 std::pmr::wstring::const_iterator end)
936{
937 return createImp(string, begin, end);
938}
939#endif
940
941} // close package namespace
942
943
944#endif
945
946// ----------------------------------------------------------------------------
947// Copyright 2021 Bloomberg Finance L.P.
948//
949// Licensed under the Apache License, Version 2.0 (the "License");
950// you may not use this file except in compliance with the License.
951// You may obtain a copy of the License at
952//
953// http://www.apache.org/licenses/LICENSE-2.0
954//
955// Unless required by applicable law or agreed to in writing, software
956// distributed under the License is distributed on an "AS IS" BASIS,
957// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
958// See the License for the specific language governing permissions and
959// limitations under the License.
960// ----------------------------- END-OF-FILE ----------------------------------
961
962/** @} */
963/** @} */
964/** @} */
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
Definition bslstl_stringview.h:441
const value_type * const_pointer
Definition bslstl_stringview.h:448
BSLS_KEYWORD_CONSTEXPR size_type length() const BSLS_KEYWORD_NOEXCEPT
Return the length of this view.
Definition bslstl_stringview.h:1685
BSLS_KEYWORD_CONSTEXPR const_pointer data() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1760
const value_type * const_iterator
Definition bslstl_stringview.h:451
Definition bslstl_string.h:1281
const CHAR_TYPE * const_iterator
Definition bslstl_string.h:1311
Definition bslstl_stringref.h:372
const CHAR_TYPE * const_iterator
Definition bslstl_stringref.h:384
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
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:1495
T::iterator end(T &container)
Definition bslstl_iterator.h:1523
Definition bdlb_indexspanstringutil.h:179
static bsl::string_view bind(const bsl::string_view &string, const IndexSpan &span)
Definition bdlb_indexspanstringutil.h:691
static IndexSpan createFromPositions(const bsl::string_view &string, IndexSpan::size_type begin, IndexSpan::size_type end)
Definition bdlb_indexspanstringutil.h:707
static IndexSpan create(const bsl::string_view &string, IndexSpan::size_type begin, IndexSpan::size_type length)
Definition bdlb_indexspanstringutil.h:725