/
bslstl_stringref.h
1535 lines (1371 loc) · 54 KB
/
bslstl_stringref.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// bslstl_stringref.h -*-C++-*-
#ifndef INCLUDED_BSLSTL_STRINGREF
#define INCLUDED_BSLSTL_STRINGREF
#ifndef INCLUDED_BSLS_IDENT
#include <bsls_ident.h>
#endif
BSLS_IDENT("$Id: $")
//@PURPOSE: Provide a reference to a 'const' string.
//
//@CLASSES:
// bslstl::StringRefImp: reference wrapper for a generic string
// bslstl::StringRef: reference wrapper for a 'char' string
// bslstl::StringRefWide: reference wrapper for a 'wchar_t' string
//
//@DESCRIPTION: This component defines two classes, 'bslstl::StringRef' and
// 'bslstl::StringRefWide', each providing a reference to a non-modifiable
// string value having an external representation. The type of characters in
// the string value can be either 'char' (for 'bslstl::StringRef') or 'wchar_t'
// (for 'bslstl::StringRefWide').
//
// The invariant of 'bslstl::StringRef' is that it always has a valid
// non-modifiable 'std::string' value, where non-empty string values have an
// external representation. Empty string values do not need to have an
// external representation. Most operations on 'bslstl::StringRef' objects
// have reference semantics and apply to the string value: e.g., 'operator=='
// compares string values, not whether 'bslstl::StringRef' objects reference
// the same string object.
//
// The only operations that do not apply to the string value (i.e., that have
// pointer semantics) are copy construction and assignment. These operations
// produce a 'bslstl::StringRef' object with the same external representation
// as the original 'bslstl::StringRef' object, which is a stronger
// post-condition than having 'operator==' return 'true' for two
// 'bslstl::StringRef' objects that have the same value.
//
// The standard notion of substitutability defined by the 'operator==' does not
// necessarily apply to 'bslstl::StringRef' since 'bslstl::StringRef' is not a
// value-semantic type (because of the external representation). Therefore
// there can be a plausible sequence of operations applied to two "equal"
// 'bslstl::StringRef' objects that result in objects that don't compare equal.
//
// The string value that is represented by a 'bslstl::StringRef' object need
// not be null-terminated. Moreover, the string may contain embedded null
// characters. As such, the string referenced by 'bslstl::StringRef', in
// general, is not a C-style string. Moreover, the notion of a null-string
// value is not supported.
//
// The address of the string referenced by 'bslstl::StringRef' is indicated by
// the 'data' accessor. Its extent is indicated by the 'length' and 'size'
// accessors. The referenced string is also indicated by the 'begin' and 'end'
// accessors that return STL-compatible iterators to the beginning of the
// string and one character past the end of the string, respectively. An
// overloaded 'operator[]' is also provided for direct by-index access to
// individual characters in the string.
//
// Several overloaded free operators are provided for 'bslstl::StringRef'
// objects (as well as variants for all combinations involving
// 'bslstl::StringRef' and 'std::string', and 'bslstl::StringRef' and 'char *')
// for (1) lexicographic comparison of values, and (2) concatenation of values
// (producing an 'std::string'); also provided is an overloaded free
// 'operator<<' for writing the value of a 'bslstl::StringRef' object to a
// specified output stream.
//
// The 'bsl::hash' template class is specialized for 'bslstl::StringRef' to
// enable the use of 'bslstl::StringRef' with STL hash containers (e.g.,
// 'bsl::unordered_set' and 'bsl::unordered_map').
//
///How to include 'bslstl::StringRef'
///----------------------------------
// To include 'bslstl::StringRef' use '#include <bsl_string.h>' (*not*
// '#include <bslstl_stringref.h>').
//
// This is unfortunate and confusing, and would be redesigned if it could be
// done without breaking existing clients. The explanation is that we disallow
// directly including all types in the 'bslstl' package because of the
// 'BSL_OVERRIDES_STD' mechanism. In retrospect, we should have located the
// 'bslstl::StringRef' in a higher level package so it could be included
// directly. In the future, if we remove the 'BSL_OVERRIDES_STD' mechanism,
// this unfortunate restriction will go away.
//
///Efficiency and Usage Considerations
///-----------------------------------
// Using 'bslstl::StringRef' to pass strings as function arguments can be
// considerably more efficient than passing 'bsl::string' objects by 'const'
// reference. First, consider a hypothetical class method in which the
// parameter is a reference to a non-modifiable 'bsl::string':
//..
// void MyClass::setLabel(const bsl::string& label)
// {
// d_label = label; // 'MyClass::d_label' is of type 'bsl::string'
// }
//..
// Then, consider a typical call to this method:
//..
// MyClass myClassObj;
// myClassObj.setLabel("hello");
//..
// As a side-effect of this call, a temporary 'bsl::string' containing a *copy*
// of "hello" is created (using the default allocator), that value is copied to
// 'd_label', and the temporary is eventually destroyed. The call thus
// requires the string data to be copied twice (as well as a possible
// allocation and deallocation).
//
// Next, consider the same method taking a reference to a non-modifiable
// 'bslstl::StringRef':
//..
// void MyClass::setLabel(const bslstl::StringRef& label)
// {
// d_label.assign(label.begin(), label.end());
// }
//..
// Now:
//..
// myClassObj.setLabel("hello");
//..
// This call has the side-effect of creating a temporary 'bslstl::StringRef'
// object, which is likely to be more efficient than creating a temporary
// 'bsl::string' (even which is implemented using the short-string
// optimization). In this case, instead of copying the *contents* of "hello",
// the *address* of the literal string is copied. In addition, 'bsl::strlen'
// is applied to the string in order to locate its end. There are *no*
// allocations done on behalf of the temporary object.
//
///Caveats
///-------
// 1) The string referenced by 'bslstl::StringRef' need not be null-terminated,
// and, in fact, may *contain* embedded null characters. Thus, it is generally
// not valid to pass the address returned by the 'data' accessor to Standard C
// functions that expect a null-terminated string (e.g., 'std::strlen',
// 'std::strcmp', etc.).
//
// 2) The string referenced by 'bslstl::StringRef' must remain valid as long as
// the 'bslstl::StringRef' references that string. Lifetime issues should be
// carefully considered when, for example, returning a 'bslstl::StringRef'
// object from a function or storing a 'bslstl::StringRef' object in a
// container.
//
// 3) Passing a null string to any function (e.g., 'operator==') without also
// passing a 0 length results in undefined behavior.
//
///Usage
///-----
// This section illustrates intended use of this component.
//
///Example 1: Basic Operations
///- - - - - - - - - - - - - -
// The following snippets of code illustrate basic and varied use of the
// 'bslstl::StringRef' class.
//
// First, we define a function, 'getNumBlanks', that returns the number of
// blank (' ') characters contained in the string referenced by a specified
// 'bslstl::StringRef':
//..
// #include <algorithm>
//
// bslstl::StringRef::size_type
// getNumBlanks(const bslstl::StringRef& stringRef)
// // Return the number of blank (' ') characters in the string referenced
// // by the specified 'stringRef'.
// {
// return std::count(stringRef.begin(), stringRef.end(), ' ');
// }
//..
// Notice that the function delegates the work to the 'std::count' STL
// algorithm. This delegation is made possible by the STL-compatible iterators
// provided by the 'begin' and 'end' accessors.
//
// Then, call 'getNumBlanks' on a default constructed 'bslstl::StringRef':
//..
// bslstl::StringRef emptyRef;
// bslstl::StringRef::size_type numBlanks = getNumBlanks(emptyRef);
// assert(0 == numBlanks);
//
// assert("" == emptyRef);
// assert("anything" >= emptyRef);
//..
// Notice that the behavior a default constructed 'bslstl::StringRef' object
// behaves the same as if it referenced an empty string.
//
// Next, we (implicitly) construct a 'bsl::string' object from
// 'bslstl::StringRef':
//..
// bsl::string empty(emptyRef);
// assert(0 == empty.size());
//..
// Then, we call 'getNumBlanks' on a string literal and assert that the number
// of blanks returned is as expected:
//..
// numBlanks = getNumBlanks("Good things come to those who wait.");
// assert(6 == numBlanks);
//..
// Next, we define a longer string literal, 'poem', that we will use in the
// rest of this usage example:
//..
// const char poem[] = // by William Butler Yeats (1865-1939)
// |....5....|....5....|....5....|....5....| // length blanks
// //
// "O love is the crooked thing,\n" // 29 5
// "There is nobody wise enough\n" // 28 4
// "To find out all that is in it,\n" // 31 7
// "For he would be thinking of love\n" // 33 6
// "Till the stars had run away\n" // 28 5
// "And the shadows eaten the moon.\n" // 32 5
// "Ah, penny, brown penny, brown penny,\n" // 37 5
// "One cannot begin it too soon."; // 29 5
// // ----
// // total: 42
//
// numBlanks = getNumBlanks(poem);
// assert(42 == numBlanks);
//..
// Then, we construct a 'bslstl::StringRef' object, 'line', that refers to only
// the first line of the 'poem':
//..
// bslstl::StringRef line(poem, 29);
// numBlanks = getNumBlanks(line);
//
// assert( 5 == numBlanks);
// assert(29 == line.length());
// assert( 0 == std::strncmp(poem, line.data(), line.length()));
//..
// Next, we use the 'assign' method to make 'line' refer to the second line of
// the 'poem':
//..
// line.assign(poem + 29, poem + 57);
// numBlanks = getNumBlanks(line);
// assert(4 == numBlanks);
// assert((57 - 29) == line.length());
// assert("There is nobody wise enough\n" == line);
//..
// Then, we call 'getNumBlanks' with a 'bsl::string' initialized to the
// contents of the 'poem':
//..
// const bsl::string poemString(poem);
// numBlanks = getNumBlanks(poemString);
// assert(42 == numBlanks);
// assert(bslstl::StringRef(poemString) == poemString);
// assert(bslstl::StringRef(poemString) == poemString.c_str());
//..
// Next, we make a 'bslstl::StringRef' object that refers to a string that will
// be able to hold embedded null characters:
//..
// char poemWithNulls[512];
// const bsl::size_t poemLength = std::strlen(poem);
// assert(poemLength < 512);
//
// std::memcpy(poemWithNulls, poem, poemLength + 1);
// assert(0 == std::strcmp(poem, poemWithNulls));
//..
// Now, we replace each occurrence of a '\n' in 'poemWithNulls' with a yielding
// '\0':
//..
// std::replace(poemWithNulls, poemWithNulls + poemLength, '\n', '\0');
// assert(0 != std::strcmp(poem, poemWithNulls));
//..
// Finally, we observe that 'poemWithNulls' has the same number of blank
// characters as the original 'poem':
//..
// numBlanks = getNumBlanks(bslstl::StringRef(poemWithNulls, poemLength));
// assert(42 == numBlanks);
//..
#ifndef INCLUDED_BSLSCM_VERSION
#include <bslscm_version.h>
#endif
#ifndef INCLUDED_BSLMF_ENABLEIF
#include <bslmf_enableif.h>
#endif
#ifndef INCLUDED_BSLMF_ISINTEGRAL
#include <bslmf_isintegral.h>
#endif
#ifndef INCLUDED_BSLMF_NIL
#include <bslmf_nil.h>
#endif
#ifndef INCLUDED_BSLS_ASSERT
#include <bsls_assert.h>
#endif
#ifndef INCLUDED_BSLSTL_ITERATOR
#include <bslstl_iterator.h>
#endif
#ifndef INCLUDED_BSLSTL_STRING
#include <bslstl_string.h>
#endif
#ifndef INCLUDED_BSLSTL_STRINGREFDATA
#include <bslstl_stringrefdata.h>
#endif
#ifndef INCLUDED_IOSFWD
#include <iosfwd>
#define INCLUDED_IOSFWD
#endif
#ifndef INCLUDED_ALGORITHM
#include <algorithm>
#define INCLUDED_ALGORITHM
#endif
#ifndef INCLUDED_CSTDDEF
#include <cstddef> // for 'std::size_t'
#define INCLUDED_CSTDDEF
#endif
namespace BloombergLP {
namespace bslstl {
// =============================
// class StringRefImp<CHAR_TYPE>
// =============================
template <class CHAR_TYPE>
class StringRefImp : public StringRefData<CHAR_TYPE> {
// This class provides a reference-semantic-like (see below) mechanism that
// allows 'const' 'std::string' values, which are represented externally as
// either an 'std::string' or null-terminated c-style string (or parts
// thereof), to be treated both uniformly and efficiently when passed as an
// argument to a function in which the string's length will be needed. The
// interface of this class provides a subset of accessor methods found on
// 'std::string' (but none of the manipulators) -- all of which apply to
// the referenced string. But, because only non-modifiable access is
// afforded to the referenced string value, each of the manipulators on
// this type -- assignment in particular -- apply to this string-reference
// object itself (as if it had pointer semantics). Hence, this class has a
// hybrid of reference- and pointer-semantics.
//
// This class:
//: o supports a complete set of *value-semantic* operations
//: o except for 'bdex' serialization
//: o is *exception-neutral* (agnostic)
//: o is *alias-safe*
//: o is 'const' *thread-safe*
// For terminology see 'bsldoc_glossary'.
private:
typedef StringRefData<CHAR_TYPE> Base;
// PRIVATE ACCESSORS
void write(std::basic_ostream<CHAR_TYPE>& stream) const;
// Write the value of this string reference to the specified output
// 'stream' in the unformatted way.
template <class OTHER_CHAR_TYPE>
friend
std::basic_ostream<OTHER_CHAR_TYPE>& operator<<(
std::basic_ostream<OTHER_CHAR_TYPE>& stream,
const bslstl::StringRefImp<OTHER_CHAR_TYPE>& stringRef);
public:
// PUBLIC TYPES
typedef const CHAR_TYPE value_type;
typedef const CHAR_TYPE& reference;
typedef const CHAR_TYPE& const_reference;
typedef const CHAR_TYPE *iterator;
typedef const CHAR_TYPE *const_iterator;
typedef bsl::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::ptrdiff_t difference_type;
typedef std::size_t size_type;
// Standard Library general container requirements.
public:
// CREATORS
StringRefImp();
// Create an object representing an empty 'std::string' value that is
// independent of any external representation and with the following
// attribute values:
//..
// begin() == end()
// isEmpty() == true
//..
template <class INT_TYPE>
StringRefImp(const CHAR_TYPE *data,
INT_TYPE length,
typename bsl::enable_if<bsl::is_integral<INT_TYPE>::value,
bslmf::Nil>::type = bslmf::Nil());
StringRefImp(const CHAR_TYPE *data, size_type length);
// Create a string-reference object having a valid 'std::string' value,
// whose external representation begins at the specified 'data' address
// and extends for the specified 'length'. The external representation
// must remain valid as long as it is bound to this string reference.
// Passing 0 has the same effect as default construction. The behavior
// is undefined unless '0 <= length' and, if '0 == data', then
// '0 == length'. Note that, like an 'std::string', the 'data' need
// not be null-terminated and may contain embedded null characters.
// Note that the template and non-template versions combine to allow
// various integral and enumeration types to be used for length while
// preventing '(char *, 0)' initializer arguments from matching the
// two-iterator constructor below.
StringRefImp(const_iterator begin, const_iterator end);
// Create a string-reference object having a valid 'std::string' value,
// whose external representation begins at the specified 'begin'
// iterator and extends up to, but not including, the specified 'end'
// iterator. The external representation must remain valid as long as
// it is bound to this string reference. The behavior is undefined
// unless 'begin <= end'. Note that, like an 'std::string', the string
// need not be null-terminated and may contain embedded null
// characters.
StringRefImp(const CHAR_TYPE *data);
// Create a string-reference object having a valid 'std::string' value,
// whose external representation begins at the specified 'data' address
// and extends for 'std::char_traits<CHAR_TYPE>::length(data)'
// characters. The external representation must remain valid as long
// as it is bound to this string reference. The behavior is undefined
// unless 'data' is null-terminated.
StringRefImp(const native_std::basic_string<CHAR_TYPE>& str);
StringRefImp(const bsl::basic_string<CHAR_TYPE>& str);
// Create a string-reference object having a valid 'std::string' value,
// whose external representation is defined by the specified 'str'
// object. The external representation must remain valid as long as it
// is bound to this string reference.
//! StringRefImp(const StringRefImp& original) = default;
// Create a string-reference object having a valid 'std::string' value,
// whose external representation is defined by the specified 'original'
// object. The external representation must remain valid as long as it
// is bound to this string reference. Note that this trivial copy
// constructor's definition is compiler generated.
StringRefImp(const StringRefImp& original,
size_type startIndex,
size_type numCharacters);
// Create a string-reference object having a valid 'std::string' value,
// whose external representation begins at the specified 'startIndex'
// in the specified 'original' string reference, and extends either the
// specified 'numCharacters' or until the end of the 'original' string
// reference, whichever comes first. The external representation must
// remain valid as long as it is bound to this string reference. The
// behavior is undefined unless 'startIndex <= original.length()'.
// Note that if 'startIndex' is 'original.length()' an empty string
// reference is returned.
//! ~StringRefImp() = default;
// Destroy this object.
// MANIPULATORS
StringRefImp& operator=(const StringRefImp& rhs);
// Modify this string reference to refer to the same string as the
// specified 'rhs' string reference and return a reference providing
// modifiable access to this object. The assigned object is guaranteed
// to have values of attributes 'begin' and 'end' equal to the 'rhs'
// object's attributes.
template <class INT_TYPE>
void assign(const CHAR_TYPE *data, INT_TYPE length,
typename bsl::enable_if<bsl::is_integral<INT_TYPE>::value,
bslmf::Nil>::type = bslmf::Nil());
void assign(const CHAR_TYPE *data, size_type length);
// Bind this string reference to the string at the specified 'data'
// address and extending for the specified 'length' characters. The
// string indicated by 'data' and 'length' must remain valid as long as
// it is bound to this object. The behavior is undefined unless
// '0 <= length' or '0 == data && 0 == length'. Note that the string
// need not be null-terminated and may contain embedded null
// characters. Note that the template and non-template versions
// combine to allow various integral and enumeration types to be used
// for length while preventing '(char *, 0)' initializer arguments from
// matching the two-iterator overload of 'assign' below.
void assign(const_iterator begin, const_iterator end);
// Bind this string reference to the string at the specified 'begin'
// iterator, extending up to, but not including, the character at the
// specified 'end' iterator. The string indicated by 'begin' and 'end'
// must remain valid as long as it is bound to this object. The
// behavior is undefined unless 'begin <= end'. Note that the string
// need not be null-terminated and may contain embedded null
// characters.
void assign(const CHAR_TYPE *data);
// Bind this string reference to the string at the specified 'data'
// address and extending for
// 'std::char_traits<CHAR_TYPE>::length(data)' characters. The string
// at the 'data' address must remain valid as long as it is bound to
// this string reference. The behavior is undefined unless 'data' is
// null-terminated.
void assign(const bsl::basic_string<CHAR_TYPE>& str);
// Bind this string reference to the specified 'str' string. The
// string indicated by 'str' must remain valid as long as it is bound
// to this object.
void assign(const StringRefImp<CHAR_TYPE>& stringRef);
// Modify this string reference to refer to the same string as the
// specified 'stringRef'. Note, that the string bound to 'stringRef'
// must remain valid as long as it is bound to this object.
void reset();
// Reset this string reference to the default-constructed state having
// an empty 'std::string' value and the following attribute values:
//..
// begin() == end()
// isEmpty() == true
//..
// ACCESSORS
const_reference operator[](size_type index) const;
// Return a reference providing a non-modifiable access to the
// character at the specified 'index' in the string bound to this
// reference. This reference remains valid as long as the string
// currently bound to this object remains valid. The behavior is
// undefined unless '0 <= index < length()'.
operator native_std::basic_string<CHAR_TYPE>() const;
// Return an 'std::basic_string' (synonymous with
// 'native_std::basic_string') having the value of the string bound to
// this string reference.
const_iterator begin() const;
// Return an STL-compatible iterator to the first character of the
// string bound to this string reference or 'end()' if the string
// reference is empty. The iterator remains valid as long as this
// object is valid and is bound to the same string.
const_iterator end() const;
// Return an STL-compatible iterator one-past-the-last character of the
// string bound to this string reference or 'begin()' if the string
// reference is empty. The iterator remains valid as long as this
// object is valid and is bound to the same string.
const_reverse_iterator rbegin() const;
// Return an STL-compatible reverse iterator to the last character of
// the string bound to this string reference or 'rend()' if the string
// reference is empty. The iterator remains valid as long as this
// object is valid and is bound to the same string.
const_reverse_iterator rend() const;
// Return an STL-compatible reverse iterator to the
// prior-to-the-beginning character of the string bound to this string
// reference or 'rbegin()' if the string reference is empty. The
// iterator remains valid as long as this object is valid and is bound
// to the same string.
const CHAR_TYPE *data() const;
// Return the address of the first character of the string bound to
// this string reference such that '[data() .. data()+length())' is a
// valid half-open range of characters. Note that the range of
// characters might not be null-terminated and may contain embedded
// null characters.
bool empty() const;
// Return 'true' if this object represents an empty string value, and
// 'false' otherwise. This object represents an empty string value if
// 'begin() == end()'. Note that this method is functionally identical
// with the 'isEmpty' method and allows developers to avoid distracting
// syntax differences when 'StringRef' appears in juxtaposition with
// 'string', which defines 'empty' but not 'isEmpty'.
bool isEmpty() const;
// Return 'true' if this object represents an empty string value, and
// 'false' otherwise. This object represents an empty string value if
// 'begin() == end()'.
size_type length() const;
// Return the length of the string referred to by this object. Note
// that this call is equivalent to 'end() - begin()'.
size_type size() const;
// Return the number of characters in the string referred to by this
// object. Note that this call is equivalent to 'end() - begin()'.
int compare(const StringRefImp& other) const;
// Compare this and 'other' string objects using a lexicographical
// comparison and return a negative value if this string is less than
// 'other' string, a positive value if this string is greater than
// 'other' string, and 0 if this string is equal to 'other' string.
};
// FREE OPERATORS
template <class CHAR_TYPE>
bool operator==(const StringRefImp<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator==(const bsl::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator==(const StringRefImp<CHAR_TYPE>& lhs,
const native_std::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator==(const native_std::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator==(const StringRefImp<CHAR_TYPE>& lhs,
const bsl::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator==(const CHAR_TYPE *lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator==(const StringRefImp<CHAR_TYPE>& lhs,
const CHAR_TYPE *rhs);
// Return 'true' if the strings referred to by the specified 'lhs' and
// 'rhs' have the same lexicographic value, and 'false' otherwise. Two
// strings have the same lexicographic value if they have the same length,
// and the respective values at each character position are the same.
template <class CHAR_TYPE>
bool operator!=(const StringRefImp<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator!=(const bsl::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator!=(const StringRefImp<CHAR_TYPE>& lhs,
const bsl::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator!=(const StringRefImp<CHAR_TYPE>& lhs,
const native_std::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator!=(const native_std::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator!=(const CHAR_TYPE *lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator!=(const StringRefImp<CHAR_TYPE>& lhs,
const CHAR_TYPE *rhs);
// Return 'true' if the strings referred to by the specified 'lhs' and
// 'rhs' do not have the same lexicographic value, and 'false' otherwise.
// Two strings do not have the same lexicographic value if they do not have
// the same length, or respective values at any character position are not
// the same.
template <class CHAR_TYPE>
bool operator<(const StringRefImp<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<(const bsl::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<(const StringRefImp<CHAR_TYPE>& lhs,
const bsl::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<(const StringRefImp<CHAR_TYPE>& lhs,
const native_std::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<(const native_std::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<(const CHAR_TYPE *lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<(const StringRefImp<CHAR_TYPE>& lhs,
const CHAR_TYPE *rhs);
// Return 'true' if the string referred to by the specified 'lhs' is
// lexicographically less than the string referred to by the specified
// 'rhs', and 'false' otherwise.
template <class CHAR_TYPE>
bool operator>(const StringRefImp<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>(const bsl::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>(const StringRefImp<CHAR_TYPE>& lhs,
const bsl::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>(const StringRefImp<CHAR_TYPE>& lhs,
const native_std::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>(const native_std::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>(const CHAR_TYPE *lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>(const StringRefImp<CHAR_TYPE>& lhs,
const CHAR_TYPE *rhs);
// Return 'true' if the string referred to by the specified 'lhs' is
// lexicographically greater than the string referred to by the specified
// 'rhs', and 'false' otherwise.
template <class CHAR_TYPE>
bool operator<=(const StringRefImp<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<=(const bsl::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<=(const StringRefImp<CHAR_TYPE>& lhs,
const bsl::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<=(const StringRefImp<CHAR_TYPE>& lhs,
const native_std::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<=(const native_std::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<=(const CHAR_TYPE *lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator<=(const StringRefImp<CHAR_TYPE>& lhs,
const CHAR_TYPE *rhs);
// Return 'true' if the string referred to by the specified 'lhs' is
// lexicographically less than or equal to the string referred to by the
// specified 'rhs', and 'false' otherwise.
template <class CHAR_TYPE>
bool operator>=(const StringRefImp<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>=(const bsl::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>=(const StringRefImp<CHAR_TYPE>& lhs,
const bsl::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>=(const StringRefImp<CHAR_TYPE>& lhs,
const native_std::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>=(const native_std::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>=(const CHAR_TYPE *lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bool operator>=(const StringRefImp<CHAR_TYPE>& lhs,
const CHAR_TYPE *rhs);
// Return 'true' if the string referred to by the specified 'lhs' is
// lexicographically greater than or equal to the string referred to by the
// specified 'rhs', and 'false' otherwise.
template <class CHAR_TYPE>
bsl::basic_string<CHAR_TYPE>
operator+(const StringRefImp<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bsl::basic_string<CHAR_TYPE>
operator+(const bsl::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bsl::basic_string<CHAR_TYPE>
operator+(const StringRefImp<CHAR_TYPE>& lhs,
const bsl::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bsl::basic_string<CHAR_TYPE>
operator+(const StringRefImp<CHAR_TYPE>& lhs,
const native_std::basic_string<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bsl::basic_string<CHAR_TYPE>
operator+(const native_std::basic_string<CHAR_TYPE>& lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bsl::basic_string<CHAR_TYPE>
operator+(const CHAR_TYPE *lhs,
const StringRefImp<CHAR_TYPE>& rhs);
template <class CHAR_TYPE>
bsl::basic_string<CHAR_TYPE>
operator+(const StringRefImp<CHAR_TYPE>& lhs,
const CHAR_TYPE *rhs);
// Return a 'bsl::string' having the value of the concatenation of the
// strings referred to by the specified 'lhs' and rhs' values.
template <class CHAR_TYPE>
std::basic_ostream<CHAR_TYPE>&
operator<<(std::basic_ostream<CHAR_TYPE>& stream,
const StringRefImp<CHAR_TYPE>& stringRef);
// Write the value of the string bound to the specified 'stringRef' to the
// specified output 'stream' and return a reference to the modifiable
// 'stream'.
// FREE FUNCTIONS
template <class CHAR_TYPE, class HASHALG>
void hashAppend(HASHALG& hashAlg, const StringRefImp<CHAR_TYPE>& input);
// Pass the specified 'input' to the specified 'hashAlg'
// ============================================================================
// TYPEDEFS
// ============================================================================
typedef StringRefImp<char> StringRef;
typedef StringRefImp<wchar_t> StringRefWide;
// ============================================================================
// INLINE FUNCTION DEFINITIONS
// ============================================================================
// ------------------
// class StringRefImp
// ------------------
// PRIVATE ACCESSORS
template <class CHAR_TYPE>
inline
void StringRefImp<CHAR_TYPE>::write(
std::basic_ostream<CHAR_TYPE>& stream) const
{
if (data()) {
stream.write(data(), length());
}
else {
BSLS_ASSERT_SAFE(length() == 0);
}
}
// CREATORS
template <class CHAR_TYPE>
inline
StringRefImp<CHAR_TYPE>::StringRefImp()
: Base(0, 0)
{
}
template <class CHAR_TYPE>
template <class INT_TYPE>
inline
StringRefImp<CHAR_TYPE>::StringRefImp(
const CHAR_TYPE *data,
INT_TYPE length,
typename bsl::enable_if<bsl::is_integral<INT_TYPE>::value,
bslmf::Nil>::type)
: Base(data, data + length)
{
BSLS_ASSERT_SAFE(0 <= length);
BSLS_ASSERT_SAFE(data || 0 == length);
}
template <class CHAR_TYPE>
inline
StringRefImp<CHAR_TYPE>::StringRefImp(const CHAR_TYPE *data, size_type length)
: Base(data, data + length)
{
BSLS_ASSERT_SAFE(data || 0 == length);
}
template <class CHAR_TYPE>
inline
StringRefImp<CHAR_TYPE>::StringRefImp(const_iterator begin, const_iterator end)
: Base(begin, end)
{
BSLS_ASSERT_SAFE((begin == 0) == (end == 0));
BSLS_ASSERT_SAFE(begin <= end);
}
template <class CHAR_TYPE>
inline
StringRefImp<CHAR_TYPE>::StringRefImp(const CHAR_TYPE *data)
: Base(data, data + native_std::char_traits<CHAR_TYPE>::length(data))
{
BSLS_ASSERT_SAFE(data);
}
template <class CHAR_TYPE>
inline
StringRefImp<CHAR_TYPE>::StringRefImp(const bsl::basic_string<CHAR_TYPE>& str)
: Base(str.data(), str.data() + str.length())
{
}
template <class CHAR_TYPE>
inline
StringRefImp<CHAR_TYPE>::StringRefImp(
const native_std::basic_string<CHAR_TYPE>& str)
: Base(str.data(), str.data() + str.length())
{
}
template <class CHAR_TYPE>
inline
StringRefImp<CHAR_TYPE>::StringRefImp(
const StringRefImp<CHAR_TYPE>& original,
size_type startIndex,
size_type numCharacters)
: Base(original.begin() + startIndex,
original.begin() + startIndex +
native_std::min(numCharacters, original.length() - startIndex))
{
BSLS_ASSERT_SAFE(startIndex <= original.length());
}
// MANIPULATORS
template <class CHAR_TYPE>
inline
StringRefImp<CHAR_TYPE>&
StringRefImp<CHAR_TYPE>::operator=(const StringRefImp& rhs)
{
Base::operator=(rhs);
return *this;
}
template <class CHAR_TYPE>
template <class INT_TYPE>
inline
void StringRefImp<CHAR_TYPE>::assign(const CHAR_TYPE *data, INT_TYPE length,
typename bsl::enable_if<bsl::is_integral<INT_TYPE>::value,
bslmf::Nil>::type)
{
BSLS_ASSERT_SAFE(data || 0 == length);
*this = StringRefImp(data, data + length);
}
template <class CHAR_TYPE>
inline
void StringRefImp<CHAR_TYPE>::assign(const CHAR_TYPE *data, size_type length)
{
BSLS_ASSERT_SAFE(data || 0 == length);
*this = StringRefImp(data, data + length);
}
template <class CHAR_TYPE>
inline
void StringRefImp<CHAR_TYPE>::assign(const_iterator begin, const_iterator end)
{
*this = StringRefImp(begin, end);
}
template <class CHAR_TYPE>
inline
void StringRefImp<CHAR_TYPE>::assign(const CHAR_TYPE *data)
{
BSLS_ASSERT_SAFE(data);
*this = StringRefImp(
data,
data + native_std::char_traits<CHAR_TYPE>::length(data));
}
template <class CHAR_TYPE>
inline
void StringRefImp<CHAR_TYPE>::assign(const bsl::basic_string<CHAR_TYPE>& str)
{
*this = StringRefImp(str.data(), str.data() + str.length());
}
template <class CHAR_TYPE>
inline
void StringRefImp<CHAR_TYPE>::assign(const StringRefImp<CHAR_TYPE>& stringRef)
{
*this = stringRef;
}
template <class CHAR_TYPE>
inline
void StringRefImp<CHAR_TYPE>::reset()
{
*this = StringRefImp();
}
// ACCESSORS
template <class CHAR_TYPE>
inline
typename StringRefImp<CHAR_TYPE>::const_reference
StringRefImp<CHAR_TYPE>::operator[](size_type index) const
{
BSLS_ASSERT_SAFE(index < length());
return begin()[index];
}
} // close package namespace
template <class CHAR_TYPE>
inline
bslstl::StringRefImp<CHAR_TYPE>::
operator native_std::basic_string<CHAR_TYPE>() const
{
return native_std::basic_string<CHAR_TYPE>(begin(), end());
}
namespace bslstl {
template <class CHAR_TYPE>
inline
typename StringRefImp<CHAR_TYPE>::const_iterator
StringRefImp<CHAR_TYPE>::begin() const
{
return Base::begin();
}
template <class CHAR_TYPE>
inline
typename StringRefImp<CHAR_TYPE>::const_iterator
StringRefImp<CHAR_TYPE>::end() const
{
return Base::end();
}
template <class CHAR_TYPE>
inline
typename StringRefImp<CHAR_TYPE>::const_reverse_iterator
StringRefImp<CHAR_TYPE>::rbegin() const
{
return const_reverse_iterator(end());
}
template <class CHAR_TYPE>
inline