1 /*==========================================================================
2 SeqAn - The Library for Sequence Analysis
4 ============================================================================
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 ============================================================================
18 $Id: std_string.h,v 1.1 2008/08/25 16:20:04 langmead Exp $
19 ==========================================================================*/
21 #ifndef SEQAN_HEADER_STD_STRING_H
22 #define SEQAN_HEADER_STD_STRING_H
25 //Adaption for ::std::basic_string
29 namespace SEQAN_NAMESPACE_MAIN
32 //////////////////////////////////////////////////////////////////////////////
34 .Adaption."std::basic_string":
35 ..summary:Standard library string class.
40 //////////////////////////////////////////////////////////////////////////////
42 ///.Metafunction.IsContiguous.param.T.type:Adaption.std::basic_string
44 template <typename TChar, typename TCharTraits, typename TAlloc>
45 struct IsContiguous< ::std::basic_string<TChar, TCharTraits, TAlloc> >
47 enum { VALUE = true };
50 template <typename TChar, typename TCharTraits, typename TAlloc>
51 struct IsContiguous< ::std::basic_string<TChar, TCharTraits, TAlloc> const>
53 enum { VALUE = true };
56 ///.Metafunction.Value.param.T.type:Adaption.std::basic_string
57 template <typename TChar, typename TCharTraits, typename TAlloc>
58 struct Value< ::std::basic_string<TChar, TCharTraits, TAlloc> >
60 typedef typename ::std::basic_string<TChar, TCharTraits, TAlloc>::value_type Type;
62 template <typename TChar, typename TCharTraits, typename TAlloc>
63 struct Value< ::std::basic_string<TChar, TCharTraits, TAlloc> const>
65 typedef typename ::std::basic_string<TChar, TCharTraits, TAlloc>::value_type Type;
68 ///.Metafunction.GetValue.param.T.type:Adaption.std::basic_string
69 template <typename TChar, typename TCharTraits, typename TAlloc>
70 struct GetValue< ::std::basic_string<TChar, TCharTraits, TAlloc> >
72 typedef typename ::std::basic_string<TChar, TCharTraits, TAlloc>::reference Type;
74 template <typename TChar, typename TCharTraits, typename TAlloc>
75 struct GetValue< ::std::basic_string<TChar, TCharTraits, TAlloc> const>
77 typedef typename ::std::basic_string<TChar, TCharTraits, TAlloc>::const_reference Type;
80 //???GetValue<vector<bool> > ist bool
82 //____________________________________________________________________________
84 ///.Metafunction.Iterator.param.T.type:Adaption.std::basic_string
86 template <typename TChar, typename TCharTraits, typename TAlloc>
87 struct Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc>, Rooted>
89 typedef ::std::basic_string<TChar, TCharTraits, TAlloc> TString;
90 typedef Iter<TString, StdIteratorAdaptor> TIterator;
91 typedef Iter<TString, AdaptorIterator<TIterator> > Type;
93 template <typename TChar, typename TCharTraits, typename TAlloc>
94 struct Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc> const, Rooted>
96 typedef ::std::basic_string<TChar, TCharTraits, TAlloc> const TString;
97 typedef Iter<TString, StdIteratorAdaptor> TIterator;
98 typedef Iter<TString, AdaptorIterator<TIterator> > Type;
102 template <typename TChar, typename TCharTraits, typename TAlloc>
103 struct Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc>, Standard >
105 typedef Iter< ::std::basic_string<TChar, TCharTraits, TAlloc>, StdIteratorAdaptor > Type;
107 template <typename TChar, typename TCharTraits, typename TAlloc>
108 struct Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc> const, Standard>
110 typedef Iter< ::std::basic_string<TChar, TCharTraits, TAlloc> const, StdIteratorAdaptor > Type;
113 //____________________________________________________________________________
115 ///.Metafunction.Position.param.T.type:Adaption.std::basic_string
116 template <typename TChar, typename TCharTraits, typename TAlloc>
117 struct Position< ::std::basic_string<TChar, TCharTraits, TAlloc> >
119 typedef typename ::std::basic_string<TChar, TCharTraits, TAlloc>::size_type Type;
122 //____________________________________________________________________________
124 ///.Metafunction.Size.param.T.type:Adaption.std::basic_string
125 template <typename TChar, typename TCharTraits, typename TAlloc>
126 struct Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >
128 typedef typename ::std::basic_string<TChar, TCharTraits, TAlloc>::size_type Type;
131 //////////////////////////////////////////////////////////////////////////////
133 ///.Metafunction.Size.param.T.type:Adaption.std::basic_string
134 template <typename TChar, typename TCharTraits, typename TAlloc>
135 struct DefaultOverflowImplicit< ::std::basic_string<TChar, TCharTraits, TAlloc> >
137 typedef Generous Type;
140 //////////////////////////////////////////////////////////////////////////////
142 ///.Function.id.param.object.type:Adaption.std::basic_string
144 template <typename TChar, typename TCharTraits, typename TAlloc>
146 id(::std::basic_string<TChar, TCharTraits, TAlloc> const & me)
149 return & *end(me, Standard());
152 //////////////////////////////////////////////////////////////////////////////
154 ///.Function.begin.param.object.type:Adaption.std::basic_string
155 template <typename TChar, typename TCharTraits, typename TAlloc>
156 inline typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc>, Standard>::Type
157 begin(::std::basic_string<TChar, TCharTraits, TAlloc> & me,
161 return typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc>, Standard>::Type(me.begin());
163 template <typename TChar, typename TCharTraits, typename TAlloc>
164 inline typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc> const, Standard>::Type
165 begin(::std::basic_string<TChar, TCharTraits, TAlloc> const & me,
169 return typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc> const, Standard>::Type(me.begin());
172 //////////////////////////////////////////////////////////////////////////////
174 ///.Function.end.param.object.type:Adaption.std::basic_string
176 template <typename TChar, typename TCharTraits, typename TAlloc>
177 inline typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc>, Standard>::Type
178 end(::std::basic_string<TChar, TCharTraits, TAlloc> & me,
182 return typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc>, Standard>::Type(me.end());
184 template <typename TChar, typename TCharTraits, typename TAlloc>
185 inline typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc> const, Standard>::Type
186 end(::std::basic_string<TChar, TCharTraits, TAlloc> const & me,
190 return typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc> const, Standard>::Type(me.end());
193 //////////////////////////////////////////////////////////////////////////////
195 ///.Function.value.param.container.type:Adaption.std::basic_string
197 template <typename TChar, typename TCharTraits, typename TAlloc, typename TPos>
198 inline typename GetValue< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type
199 value(::std::basic_string<TChar, TCharTraits, TAlloc> & me,
205 template <typename TChar, typename TCharTraits, typename TAlloc, typename TPos>
206 inline typename GetValue< ::std::basic_string<TChar, TCharTraits, TAlloc> const>::Type
207 value(::std::basic_string<TChar, TCharTraits, TAlloc> const & me,
214 //////////////////////////////////////////////////////////////////////////////
216 ///.Function.length.param.object.type:Adaption.std::basic_string
218 template <typename TChar, typename TCharTraits, typename TAlloc>
219 inline typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type
220 length(::std::basic_string<TChar, TCharTraits, TAlloc> const & me)
226 //////////////////////////////////////////////////////////////////////////////
228 ///.Function.capacity.param.object.type:Adaption.std::basic_string
230 template <typename TChar, typename TCharTraits, typename TAlloc>
231 inline typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type
232 capacity(::std::basic_string<TChar, TCharTraits, TAlloc> const & me)
235 return me.capacity();
238 //////////////////////////////////////////////////////////////////////////////
240 ///.Function.empty.param.object.type:Adaption.std::basic_string
242 template <typename TChar, typename TCharTraits, typename TAlloc>
244 empty(::std::basic_string<TChar, TCharTraits, TAlloc> const & me)
250 //////////////////////////////////////////////////////////////////////////////
252 ///.Function.clear.param.object.type:Adaption.std::basic_string
254 template <typename TChar, typename TCharTraits, typename TAlloc>
256 clear(::std::basic_string<TChar, TCharTraits, TAlloc> & me)
262 //////////////////////////////////////////////////////////////////////////////
263 //assign to ::std::basic_string
265 ///.Function.assign.param.target.type:Adaption.std::basic_string
266 ///.Function.assign.param.source.type:Adaption.std::basic_string
268 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
270 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
274 assign(target, source, Generous());
276 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
278 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
279 TSource const & source)
282 assign(target, source, Generous());
285 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource, typename TSize>
287 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
292 assign(target, source, limit, Generous());
294 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource, typename TSize>
296 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
297 TSource const & source,
301 assign(target, source, limit, Generous());
304 //____________________________________________________________________________
306 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
308 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
313 target.assign(begin(source, Standard()), end(source, Standard()));
315 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
317 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
318 TSource const & source,
322 target.assign(begin(source, Standard()), end(source, Standard()));
326 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
328 assign_std_string_Generous_impl(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
330 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type limit)
333 typename Iterator<TSource const, Standard>::Type source_begin = begin(source, Standard());
334 typename Size<TSource const>::Type source_length = length(source);
335 if (source_length > limit)
337 source_length = limit;
339 target.assign(source_begin, source_begin + source_length);
341 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
343 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
345 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type limit,
349 assign_std_string_Generous_impl(target, source, limit);
351 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
353 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
354 TSource const & source,
355 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type limit,
359 assign_std_string_Generous_impl(target, source, limit);
362 //____________________________________________________________________________
364 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
366 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
371 assign(target, source, target.capacity(), Generous());
373 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
375 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
376 TSource const & source,
380 assign(target, source, target.capacity(), Generous());
383 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
385 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
387 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type limit,
391 if (limit > target.capacity())
393 limit = target.capacity();
396 assign(target, source, limit, Generous());
398 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
400 assign(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
401 TSource const & source,
402 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type limit,
406 if (limit > target.capacity())
408 limit = target.capacity();
411 assign(target, source, limit, Generous());
414 //////////////////////////////////////////////////////////////////////////////
415 //append to ::std::basic_string
417 ///.Function.append.param.target.type:Adaption.std::basic_string
418 ///.Function.append.param.source.type:Adaption.std::basic_string
420 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
422 append(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
423 TSource const & source,
427 target.append(begin(source, Standard()), end(source, Standard()));
430 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
432 append(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
433 TSource const & source,
434 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type limit,
438 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type target_length = target.length();
439 if (target_length > limit)
441 target.resize(limit);
445 limit -= target_length;
446 typename Iterator<TSource const, Standard>::Type source_begin = begin(source, Standard());
447 typename Size<TSource const>::Type source_length = length(source);
448 if (source_length > limit)
450 source_length = limit;
452 target.append(source_begin, source_begin + source_length);
456 //____________________________________________________________________________
458 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
460 append(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
461 TSource const & source,
465 append(target, source, target.capacity(), Generous());
468 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
470 append(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
471 TSource const & source,
472 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type limit,
476 if (limit > target.capacity())
478 limit = target.capacity();
481 append(target, source, limit, Generous());
484 //////////////////////////////////////////////////////////////////////////////
485 ///.Function.appendValue.param.target.type:Adaption.std::basic_string
487 template <typename TChar, typename TCharTraits, typename TAlloc, typename TValue, typename TTag>
489 appendValue(::std::basic_string<TChar, TCharTraits, TAlloc> & me,
490 TValue const & _value,
494 me.push_back(_value);
497 template <typename TChar, typename TCharTraits, typename TAlloc, typename TValue>
499 appendValue(::std::basic_string<TChar, TCharTraits, TAlloc> & me,
500 TValue const & _value,
504 if (capacity(me) > length(me)) me.push_back(_value);
507 //////////////////////////////////////////////////////////////////////////////
508 //replace to ::std::basic_string
510 ///.Function.replace.param.target.type:Adaption.std::basic_string
511 ///.Function.replace.param.source.type:Adaption.std::basic_string
513 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
515 replace(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
516 typename Position< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type pos_begin,
517 typename Position< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type pos_end,
518 TSource const & source,
522 target.replace(target.begin() + pos_begin, target.begin() + pos_end, begin(source, Standard()), end(source, Standard()));
525 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
527 replace(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
528 typename Position< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type pos_begin,
529 typename Position< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type pos_end,
530 TSource const & source,
531 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type limit,
535 if (pos_begin >= limit)
537 target.resize(limit);
541 typename Iterator<TSource const, Standard>::Type source_begin = begin(source, Standard());
542 typename Size<TSource const>::Type source_length = length(source);
543 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type pos_mid = pos_begin + source_length;
546 target.replace(target.begin() + pos_begin, target.begin() + limit, source_begin, source_begin + limit - pos_begin);
547 target.resize(limit);
551 target.replace(target.begin() + pos_begin, target.begin() + pos_end, source_begin, end(source, Standard()));
552 if (target.length() > limit)
554 target.resize(limit);
560 //____________________________________________________________________________
562 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
564 replace(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
565 typename Position< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type pos_begin,
566 typename Position< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type pos_end,
567 TSource const & source,
571 replace(target, pos_begin, pos_end, source, target.capacity(), Generous());
574 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSource>
576 replace(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
577 typename Position< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type pos_begin,
578 typename Position< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type pos_end,
579 TSource const & source,
580 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type limit,
584 if (limit > target.capacity())
586 limit = target.capacity();
589 replace(target, pos_begin, pos_end, source, limit, Generous());
592 //////////////////////////////////////////////////////////////////////////////
593 // handling of iterators as begin and end
596 template<typename TChar, typename TCharTraits, typename TAlloc, typename TSource, typename TExpand>
598 replace(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
599 typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc>, Rooted>::Type pos_begin,
600 typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc>, Rooted>::Type pos_end,
602 Tag<TExpand> const tag)
604 replace(target, position(pos_begin), position(pos_end), source, tag);
607 template<typename TChar, typename TCharTraits, typename TAlloc, typename TSource, typename TExpand>
609 replace(::std::basic_string<TChar, TCharTraits, TAlloc> & target,
610 typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc>, Rooted>::Type pos_begin,
611 typename Iterator< ::std::basic_string<TChar, TCharTraits, TAlloc>, Rooted>::Type pos_end,
613 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type limit,
614 Tag<TExpand> const tag)
616 replace(target, position(pos_begin), position(pos_end), source, tag);
620 //////////////////////////////////////////////////////////////////////////////
622 ///.Function.reserve.param.object.type:Adaption.std::basic_string
624 template <typename TChar, typename TCharTraits, typename TAlloc, typename TExpand>
625 inline typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type
627 ::std::basic_string<TChar, TCharTraits, TAlloc> & seq,
628 typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type new_capacity,
629 Tag<TExpand> const &)
632 seq.reserve(new_capacity);
633 if (new_capacity < seq.capacity())
635 return seq.capacity();
640 //////////////////////////////////////////////////////////////////////////////
642 ///.Function.resize.param.object.type:Adaption.std::basic_string
644 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSize, typename TExpand>
645 inline typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type
647 ::std::basic_string<TChar, TCharTraits, TAlloc> & me,
649 Tag<TExpand> const &)
652 me.resize(new_length);
656 //////////////////////////////////////////////////////////////////////////////
658 ///.Function.fill.param.object.type:Adaption.std::basic_string
660 template <typename TChar, typename TCharTraits, typename TAlloc, typename TSize, typename TExpand>
661 inline typename Size< ::std::basic_string<TChar, TCharTraits, TAlloc> >::Type
663 ::std::basic_string<TChar, TCharTraits, TAlloc> & me,
666 Tag<TExpand> const &)
669 me.resize(new_length, val);
675 //////////////////////////////////////////////////////////////////////////////
677 //////////////////////////////////////////////////////////////////////////////
680 template <typename TChar, typename TContainer>
681 struct GetValue< ::__gnu_cxx::__normal_iterator<TChar, TContainer> >
683 typedef typename ::__gnu_cxx::__normal_iterator<TChar, TContainer>::reference Type;
687 //////////////////////////////////////////////////////////////////////////////
689 } //namespace SEQAN_NAMESPACE_MAIN
691 #endif //#ifndef SEQAN_HEADER_...