1
// Boost string_algo library replace.hpp header file ---------------------------//
3
// Copyright Pavol Droba 2002-2006.
5
// Distributed under the Boost Software License, Version 1.0.
6
// (See accompanying file LICENSE_1_0.txt or copy at
7
// http://www.boost.org/LICENSE_1_0.txt)
9
// See http://www.boost.org/ for updates, documentation, and revision history.
11
#ifndef BOOST_STRING_REPLACE_HPP
12
#define BOOST_STRING_REPLACE_HPP
14
#include <boost/algorithm/string/config.hpp>
16
#include <boost/range/iterator_range.hpp>
17
#include <boost/range/begin.hpp>
18
#include <boost/range/end.hpp>
19
#include <boost/range/iterator.hpp>
20
#include <boost/range/const_iterator.hpp>
22
#include <boost/algorithm/string/find_format.hpp>
23
#include <boost/algorithm/string/finder.hpp>
24
#include <boost/algorithm/string/formatter.hpp>
25
#include <boost/algorithm/string/compare.hpp>
28
Defines various replace algorithms. Each algorithm replaces
29
part(s) of the input according to set of searching and replace criteria.
35
// replace_range --------------------------------------------------------------------//
37
//! Replace range algorithm
39
Replace the given range in the input string.
40
The result is a modified copy of the input. It is returned as a sequence
41
or copied to the output iterator.
43
\param Output An output iterator to which the result will be copied
44
\param Input An input string
45
\param SearchRange A range in the input to be substituted
46
\param Format A substitute string
47
\return An output iterator pointing just after the last inserted character or
48
a modified copy of the input
50
\note The second variant of this function provides the strong exception-safety guarantee
53
typename OutputIteratorT,
56
inline OutputIteratorT replace_range_copy(
57
OutputIteratorT Output,
61
range_const_iterator<Range1T>::type>& SearchRange,
62
const Range2T& Format)
64
return ::boost::algorithm::find_format_copy(
67
::boost::algorithm::range_finder(SearchRange),
68
::boost::algorithm::const_formatter(Format));
71
//! Replace range algorithm
75
template<typename SequenceT, typename RangeT>
76
inline SequenceT replace_range_copy(
77
const SequenceT& Input,
80
range_const_iterator<SequenceT>::type>& SearchRange,
83
return ::boost::algorithm::find_format_copy(
85
::boost::algorithm::range_finder(SearchRange),
86
::boost::algorithm::const_formatter(Format));
89
//! Replace range algorithm
91
Replace the given range in the input string.
92
The input sequence is modified in-place.
94
\param Input An input string
95
\param SearchRange A range in the input to be substituted
96
\param Format A substitute string
98
template<typename SequenceT, typename RangeT>
99
inline void replace_range(
101
const iterator_range<
102
BOOST_STRING_TYPENAME
103
range_iterator<SequenceT>::type>& SearchRange,
104
const RangeT& Format)
106
::boost::algorithm::find_format(
108
::boost::algorithm::range_finder(SearchRange),
109
::boost::algorithm::const_formatter(Format));
112
// replace_first --------------------------------------------------------------------//
114
//! Replace first algorithm
116
Replace the first match of the search substring in the input
117
with the format string.
118
The result is a modified copy of the input. It is returned as a sequence
119
or copied to the output iterator.
121
\param Output An output iterator to which the result will be copied
122
\param Input An input string
123
\param Search A substring to be searched for
124
\param Format A substitute string
125
\return An output iterator pointing just after the last inserted character or
126
a modified copy of the input
128
\note The second variant of this function provides the strong exception-safety guarantee
131
typename OutputIteratorT,
135
inline OutputIteratorT replace_first_copy(
136
OutputIteratorT Output,
137
const Range1T& Input,
138
const Range2T& Search,
139
const Range3T& Format)
141
return ::boost::algorithm::find_format_copy(
144
::boost::algorithm::first_finder(Search),
145
::boost::algorithm::const_formatter(Format) );
148
//! Replace first algorithm
152
template<typename SequenceT, typename Range1T, typename Range2T>
153
inline SequenceT replace_first_copy(
154
const SequenceT& Input,
155
const Range1T& Search,
156
const Range2T& Format )
158
return ::boost::algorithm::find_format_copy(
160
::boost::algorithm::first_finder(Search),
161
::boost::algorithm::const_formatter(Format) );
164
//! Replace first algorithm
166
replace the first match of the search substring in the input
167
with the format string. The input sequence is modified in-place.
169
\param Input An input string
170
\param Search A substring to be searched for
171
\param Format A substitute string
173
template<typename SequenceT, typename Range1T, typename Range2T>
174
inline void replace_first(
176
const Range1T& Search,
177
const Range2T& Format )
179
::boost::algorithm::find_format(
181
::boost::algorithm::first_finder(Search),
182
::boost::algorithm::const_formatter(Format) );
185
// replace_first ( case insensitive ) ---------------------------------------------//
187
//! Replace first algorithm ( case insensitive )
189
Replace the first match of the search substring in the input
190
with the format string.
191
The result is a modified copy of the input. It is returned as a sequence
192
or copied to the output iterator.
193
Searching is case insensitive.
195
\param Output An output iterator to which the result will be copied
196
\param Input An input string
197
\param Search A substring to be searched for
198
\param Format A substitute string
199
\param Loc A locale used for case insensitive comparison
200
\return An output iterator pointing just after the last inserted character or
201
a modified copy of the input
203
\note The second variant of this function provides the strong exception-safety guarantee
206
typename OutputIteratorT,
210
inline OutputIteratorT ireplace_first_copy(
211
OutputIteratorT Output,
212
const Range1T& Input,
213
const Range2T& Search,
214
const Range3T& Format,
215
const std::locale& Loc=std::locale() )
217
return ::boost::algorithm::find_format_copy(
220
::boost::algorithm::first_finder(Search, is_iequal(Loc)),
221
::boost::algorithm::const_formatter(Format) );
224
//! Replace first algorithm ( case insensitive )
228
template<typename SequenceT, typename Range2T, typename Range1T>
229
inline SequenceT ireplace_first_copy(
230
const SequenceT& Input,
231
const Range2T& Search,
232
const Range1T& Format,
233
const std::locale& Loc=std::locale() )
235
return ::boost::algorithm::find_format_copy(
237
::boost::algorithm::first_finder(Search, is_iequal(Loc)),
238
::boost::algorithm::const_formatter(Format) );
241
//! Replace first algorithm ( case insensitive )
243
Replace the first match of the search substring in the input
244
with the format string. Input sequence is modified in-place.
245
Searching is case insensitive.
247
\param Input An input string
248
\param Search A substring to be searched for
249
\param Format A substitute string
250
\param Loc A locale used for case insensitive comparison
252
template<typename SequenceT, typename Range1T, typename Range2T>
253
inline void ireplace_first(
255
const Range1T& Search,
256
const Range2T& Format,
257
const std::locale& Loc=std::locale() )
259
::boost::algorithm::find_format(
261
::boost::algorithm::first_finder(Search, is_iequal(Loc)),
262
::boost::algorithm::const_formatter(Format) );
265
// replace_last --------------------------------------------------------------------//
267
//! Replace last algorithm
269
Replace the last match of the search string in the input
270
with the format string.
271
The result is a modified copy of the input. It is returned as a sequence
272
or copied to the output iterator.
274
\param Output An output iterator to which the result will be copied
275
\param Input An input string
276
\param Search A substring to be searched for
277
\param Format A substitute string
278
\return An output iterator pointing just after the last inserted character or
279
a modified copy of the input
281
\note The second variant of this function provides the strong exception-safety guarantee
284
typename OutputIteratorT,
288
inline OutputIteratorT replace_last_copy(
289
OutputIteratorT Output,
290
const Range1T& Input,
291
const Range2T& Search,
292
const Range3T& Format )
294
return ::boost::algorithm::find_format_copy(
297
::boost::algorithm::last_finder(Search),
298
::boost::algorithm::const_formatter(Format) );
301
//! Replace last algorithm
305
template<typename SequenceT, typename Range1T, typename Range2T>
306
inline SequenceT replace_last_copy(
307
const SequenceT& Input,
308
const Range1T& Search,
309
const Range2T& Format )
311
return ::boost::algorithm::find_format_copy(
313
::boost::algorithm::last_finder(Search),
314
::boost::algorithm::const_formatter(Format) );
317
//! Replace last algorithm
319
Replace the last match of the search string in the input
320
with the format string. Input sequence is modified in-place.
322
\param Input An input string
323
\param Search A substring to be searched for
324
\param Format A substitute string
326
template<typename SequenceT, typename Range1T, typename Range2T>
327
inline void replace_last(
329
const Range1T& Search,
330
const Range2T& Format )
332
::boost::algorithm::find_format(
334
::boost::algorithm::last_finder(Search),
335
::boost::algorithm::const_formatter(Format) );
338
// replace_last ( case insensitive ) -----------------------------------------------//
340
//! Replace last algorithm ( case insensitive )
342
Replace the last match of the search string in the input
343
with the format string.
344
The result is a modified copy of the input. It is returned as a sequence
345
or copied to the output iterator.
346
Searching is case insensitive.
348
\param Output An output iterator to which the result will be copied
349
\param Input An input string
350
\param Search A substring to be searched for
351
\param Format A substitute string
352
\param Loc A locale used for case insensitive comparison
353
\return An output iterator pointing just after the last inserted character or
354
a modified copy of the input
356
\note The second variant of this function provides the strong exception-safety guarantee
359
typename OutputIteratorT,
363
inline OutputIteratorT ireplace_last_copy(
364
OutputIteratorT Output,
365
const Range1T& Input,
366
const Range2T& Search,
367
const Range3T& Format,
368
const std::locale& Loc=std::locale() )
370
return ::boost::algorithm::find_format_copy(
373
::boost::algorithm::last_finder(Search, is_iequal(Loc)),
374
::boost::algorithm::const_formatter(Format) );
377
//! Replace last algorithm ( case insensitive )
381
template<typename SequenceT, typename Range1T, typename Range2T>
382
inline SequenceT ireplace_last_copy(
383
const SequenceT& Input,
384
const Range1T& Search,
385
const Range2T& Format,
386
const std::locale& Loc=std::locale() )
388
return ::boost::algorithm::find_format_copy(
390
::boost::algorithm::last_finder(Search, is_iequal(Loc)),
391
::boost::algorithm::const_formatter(Format) );
394
//! Replace last algorithm ( case insensitive )
396
Replace the last match of the search string in the input
397
with the format string.The input sequence is modified in-place.
398
Searching is case insensitive.
400
\param Input An input string
401
\param Search A substring to be searched for
402
\param Format A substitute string
403
\param Loc A locale used for case insensitive comparison
404
\return A reference to the modified input
406
template<typename SequenceT, typename Range1T, typename Range2T>
407
inline void ireplace_last(
409
const Range1T& Search,
410
const Range2T& Format,
411
const std::locale& Loc=std::locale() )
413
::boost::algorithm::find_format(
415
::boost::algorithm::last_finder(Search, is_iequal(Loc)),
416
::boost::algorithm::const_formatter(Format) );
419
// replace_nth --------------------------------------------------------------------//
421
//! Replace nth algorithm
423
Replace an Nth (zero-indexed) match of the search string in the input
424
with the format string.
425
The result is a modified copy of the input. It is returned as a sequence
426
or copied to the output iterator.
428
\param Output An output iterator to which the result will be copied
429
\param Input An input string
430
\param Search A substring to be searched for
431
\param Nth An index of the match to be replaced. The index is 0-based.
432
For negative N, matches are counted from the end of string.
433
\param Format A substitute string
434
\return An output iterator pointing just after the last inserted character or
435
a modified copy of the input
437
\note The second variant of this function provides the strong exception-safety guarantee
440
typename OutputIteratorT,
444
inline OutputIteratorT replace_nth_copy(
445
OutputIteratorT Output,
446
const Range1T& Input,
447
const Range2T& Search,
449
const Range3T& Format )
451
return ::boost::algorithm::find_format_copy(
454
::boost::algorithm::nth_finder(Search, Nth),
455
::boost::algorithm::const_formatter(Format) );
458
//! Replace nth algorithm
462
template<typename SequenceT, typename Range1T, typename Range2T>
463
inline SequenceT replace_nth_copy(
464
const SequenceT& Input,
465
const Range1T& Search,
467
const Range2T& Format )
469
return ::boost::algorithm::find_format_copy(
471
::boost::algorithm::nth_finder(Search, Nth),
472
::boost::algorithm::const_formatter(Format) );
475
//! Replace nth algorithm
477
Replace an Nth (zero-indexed) match of the search string in the input
478
with the format string. Input sequence is modified in-place.
480
\param Input An input string
481
\param Search A substring to be searched for
482
\param Nth An index of the match to be replaced. The index is 0-based.
483
For negative N, matches are counted from the end of string.
484
\param Format A substitute string
486
template<typename SequenceT, typename Range1T, typename Range2T>
487
inline void replace_nth(
489
const Range1T& Search,
491
const Range2T& Format )
493
::boost::algorithm::find_format(
495
::boost::algorithm::nth_finder(Search, Nth),
496
::boost::algorithm::const_formatter(Format) );
499
// replace_nth ( case insensitive ) -----------------------------------------------//
501
//! Replace nth algorithm ( case insensitive )
503
Replace an Nth (zero-indexed) match of the search string in the input
504
with the format string.
505
The result is a modified copy of the input. It is returned as a sequence
506
or copied to the output iterator.
507
Searching is case insensitive.
509
\param Output An output iterator to which the result will be copied
510
\param Input An input string
511
\param Search A substring to be searched for
512
\param Nth An index of the match to be replaced. The index is 0-based.
513
For negative N, matches are counted from the end of string.
514
\param Format A substitute string
515
\param Loc A locale used for case insensitive comparison
516
\return An output iterator pointing just after the last inserted character or
517
a modified copy of the input
519
\note The second variant of this function provides the strong exception-safety guarantee
522
typename OutputIteratorT,
526
inline OutputIteratorT ireplace_nth_copy(
527
OutputIteratorT Output,
528
const Range1T& Input,
529
const Range2T& Search,
531
const Range3T& Format,
532
const std::locale& Loc=std::locale() )
534
return ::boost::algorithm::find_format_copy(
537
::boost::algorithm::nth_finder(Search, Nth, is_iequal(Loc) ),
538
::boost::algorithm::const_formatter(Format) );
541
//! Replace nth algorithm ( case insensitive )
545
template<typename SequenceT, typename Range1T, typename Range2T>
546
inline SequenceT ireplace_nth_copy(
547
const SequenceT& Input,
548
const Range1T& Search,
550
const Range2T& Format,
551
const std::locale& Loc=std::locale() )
553
return ::boost::algorithm::find_format_copy(
555
::boost::algorithm::nth_finder(Search, Nth, is_iequal(Loc)),
556
::boost::algorithm::const_formatter(Format) );
559
//! Replace nth algorithm ( case insensitive )
561
Replace an Nth (zero-indexed) match of the search string in the input
562
with the format string. Input sequence is modified in-place.
563
Searching is case insensitive.
565
\param Input An input string
566
\param Search A substring to be searched for
567
\param Nth An index of the match to be replaced. The index is 0-based.
568
For negative N, matches are counted from the end of string.
569
\param Format A substitute string
570
\param Loc A locale used for case insensitive comparison
572
template<typename SequenceT, typename Range1T, typename Range2T>
573
inline void ireplace_nth(
575
const Range1T& Search,
577
const Range2T& Format,
578
const std::locale& Loc=std::locale() )
580
::boost::algorithm::find_format(
582
::boost::algorithm::nth_finder(Search, Nth, is_iequal(Loc)),
583
::boost::algorithm::const_formatter(Format) );
586
// replace_all --------------------------------------------------------------------//
588
//! Replace all algorithm
590
Replace all occurrences of the search string in the input
591
with the format string.
592
The result is a modified copy of the input. It is returned as a sequence
593
or copied to the output iterator.
595
\param Output An output iterator to which the result will be copied
596
\param Input An input string
597
\param Search A substring to be searched for
598
\param Format A substitute string
599
\return An output iterator pointing just after the last inserted character or
600
a modified copy of the input
602
\note The second variant of this function provides the strong exception-safety guarantee
605
typename OutputIteratorT,
609
inline OutputIteratorT replace_all_copy(
610
OutputIteratorT Output,
611
const Range1T& Input,
612
const Range2T& Search,
613
const Range3T& Format )
615
return ::boost::algorithm::find_format_all_copy(
618
::boost::algorithm::first_finder(Search),
619
::boost::algorithm::const_formatter(Format) );
622
//! Replace all algorithm
626
template<typename SequenceT, typename Range1T, typename Range2T>
627
inline SequenceT replace_all_copy(
628
const SequenceT& Input,
629
const Range1T& Search,
630
const Range2T& Format )
632
return ::boost::algorithm::find_format_all_copy(
634
::boost::algorithm::first_finder(Search),
635
::boost::algorithm::const_formatter(Format) );
638
//! Replace all algorithm
640
Replace all occurrences of the search string in the input
641
with the format string. The input sequence is modified in-place.
643
\param Input An input string
644
\param Search A substring to be searched for
645
\param Format A substitute string
646
\return A reference to the modified input
648
template<typename SequenceT, typename Range1T, typename Range2T>
649
inline void replace_all(
651
const Range1T& Search,
652
const Range2T& Format )
654
::boost::algorithm::find_format_all(
656
::boost::algorithm::first_finder(Search),
657
::boost::algorithm::const_formatter(Format) );
660
// replace_all ( case insensitive ) -----------------------------------------------//
662
//! Replace all algorithm ( case insensitive )
664
Replace all occurrences of the search string in the input
665
with the format string.
666
The result is a modified copy of the input. It is returned as a sequence
667
or copied to the output iterator.
668
Searching is case insensitive.
670
\param Output An output iterator to which the result will be copied
671
\param Input An input string
672
\param Search A substring to be searched for
673
\param Format A substitute string
674
\param Loc A locale used for case insensitive comparison
675
\return An output iterator pointing just after the last inserted character or
676
a modified copy of the input
678
\note The second variant of this function provides the strong exception-safety guarantee
681
typename OutputIteratorT,
685
inline OutputIteratorT ireplace_all_copy(
686
OutputIteratorT Output,
687
const Range1T& Input,
688
const Range2T& Search,
689
const Range3T& Format,
690
const std::locale& Loc=std::locale() )
692
return ::boost::algorithm::find_format_all_copy(
695
::boost::algorithm::first_finder(Search, is_iequal(Loc)),
696
::boost::algorithm::const_formatter(Format) );
699
//! Replace all algorithm ( case insensitive )
703
template<typename SequenceT, typename Range1T, typename Range2T>
704
inline SequenceT ireplace_all_copy(
705
const SequenceT& Input,
706
const Range1T& Search,
707
const Range2T& Format,
708
const std::locale& Loc=std::locale() )
710
return ::boost::algorithm::find_format_all_copy(
712
::boost::algorithm::first_finder(Search, is_iequal(Loc)),
713
::boost::algorithm::const_formatter(Format) );
716
//! Replace all algorithm ( case insensitive )
718
Replace all occurrences of the search string in the input
719
with the format string.The input sequence is modified in-place.
720
Searching is case insensitive.
722
\param Input An input string
723
\param Search A substring to be searched for
724
\param Format A substitute string
725
\param Loc A locale used for case insensitive comparison
727
template<typename SequenceT, typename Range1T, typename Range2T>
728
inline void ireplace_all(
730
const Range1T& Search,
731
const Range2T& Format,
732
const std::locale& Loc=std::locale() )
734
::boost::algorithm::find_format_all(
736
::boost::algorithm::first_finder(Search, is_iequal(Loc)),
737
::boost::algorithm::const_formatter(Format) );
740
// replace_head --------------------------------------------------------------------//
742
//! Replace head algorithm
744
Replace the head of the input with the given format string.
745
The head is a prefix of a string of given size.
746
If the sequence is shorter then required, whole string if
747
considered to be the head.
748
The result is a modified copy of the input. It is returned as a sequence
749
or copied to the output iterator.
751
\param Output An output iterator to which the result will be copied
752
\param Input An input string
753
\param N Length of the head.
754
For N>=0, at most N characters are extracted.
755
For N<0, size(Input)-|N| characters are extracted.
756
\param Format A substitute string
757
\return An output iterator pointing just after the last inserted character or
758
a modified copy of the input
760
\note The second variant of this function provides the strong exception-safety guarantee
763
typename OutputIteratorT,
766
inline OutputIteratorT replace_head_copy(
767
OutputIteratorT Output,
768
const Range1T& Input,
770
const Range2T& Format )
772
return ::boost::algorithm::find_format_copy(
775
::boost::algorithm::head_finder(N),
776
::boost::algorithm::const_formatter(Format) );
779
//! Replace head algorithm
783
template<typename SequenceT, typename RangeT>
784
inline SequenceT replace_head_copy(
785
const SequenceT& Input,
787
const RangeT& Format )
789
return ::boost::algorithm::find_format_copy(
791
::boost::algorithm::head_finder(N),
792
::boost::algorithm::const_formatter(Format) );
795
//! Replace head algorithm
797
Replace the head of the input with the given format string.
798
The head is a prefix of a string of given size.
799
If the sequence is shorter then required, the whole string is
800
considered to be the head. The input sequence is modified in-place.
802
\param Input An input string
803
\param N Length of the head.
804
For N>=0, at most N characters are extracted.
805
For N<0, size(Input)-|N| characters are extracted.
806
\param Format A substitute string
808
template<typename SequenceT, typename RangeT>
809
inline void replace_head(
812
const RangeT& Format )
814
::boost::algorithm::find_format(
816
::boost::algorithm::head_finder(N),
817
::boost::algorithm::const_formatter(Format) );
820
// replace_tail --------------------------------------------------------------------//
822
//! Replace tail algorithm
824
Replace the tail of the input with the given format string.
825
The tail is a suffix of a string of given size.
826
If the sequence is shorter then required, whole string is
827
considered to be the tail.
828
The result is a modified copy of the input. It is returned as a sequence
829
or copied to the output iterator.
831
\param Output An output iterator to which the result will be copied
832
\param Input An input string
833
\param N Length of the tail.
834
For N>=0, at most N characters are extracted.
835
For N<0, size(Input)-|N| characters are extracted.
836
\param Format A substitute string
837
\return An output iterator pointing just after the last inserted character or
838
a modified copy of the input
840
\note The second variant of this function provides the strong exception-safety guarantee
843
typename OutputIteratorT,
846
inline OutputIteratorT replace_tail_copy(
847
OutputIteratorT Output,
848
const Range1T& Input,
850
const Range2T& Format )
852
return ::boost::algorithm::find_format_copy(
855
::boost::algorithm::tail_finder(N),
856
::boost::algorithm::const_formatter(Format) );
859
//! Replace tail algorithm
863
template<typename SequenceT, typename RangeT>
864
inline SequenceT replace_tail_copy(
865
const SequenceT& Input,
867
const RangeT& Format )
869
return ::boost::algorithm::find_format_copy(
871
::boost::algorithm::tail_finder(N),
872
::boost::algorithm::const_formatter(Format) );
875
//! Replace tail algorithm
877
Replace the tail of the input with the given format sequence.
878
The tail is a suffix of a string of given size.
879
If the sequence is shorter then required, the whole string is
880
considered to be the tail. The input sequence is modified in-place.
882
\param Input An input string
883
\param N Length of the tail.
884
For N>=0, at most N characters are extracted.
885
For N<0, size(Input)-|N| characters are extracted.
886
\param Format A substitute string
888
template<typename SequenceT, typename RangeT>
889
inline void replace_tail(
892
const RangeT& Format )
894
::boost::algorithm::find_format(
896
::boost::algorithm::tail_finder(N),
897
::boost::algorithm::const_formatter(Format) );
900
} // namespace algorithm
902
// pull names to the boost namespace
903
using algorithm::replace_range_copy;
904
using algorithm::replace_range;
905
using algorithm::replace_first_copy;
906
using algorithm::replace_first;
907
using algorithm::ireplace_first_copy;
908
using algorithm::ireplace_first;
909
using algorithm::replace_last_copy;
910
using algorithm::replace_last;
911
using algorithm::ireplace_last_copy;
912
using algorithm::ireplace_last;
913
using algorithm::replace_nth_copy;
914
using algorithm::replace_nth;
915
using algorithm::ireplace_nth_copy;
916
using algorithm::ireplace_nth;
917
using algorithm::replace_all_copy;
918
using algorithm::replace_all;
919
using algorithm::ireplace_all_copy;
920
using algorithm::ireplace_all;
921
using algorithm::replace_head_copy;
922
using algorithm::replace_head;
923
using algorithm::replace_tail_copy;
924
using algorithm::replace_tail;
928
#endif // BOOST_REPLACE_HPP