5 * Created: Jun 03, 2007
7 * Author: Jaroslaw Zola <jaroslaw.zola@gmail.com>
8 * Copyright (c) 2004-2008 Jaroslaw Zola
9 * Distributed under the Boost Software License.
10 * See accompanying LICENSE.
12 * This file is part of jaz.
15 #ifndef JAZ_STRING_ADD_HPP
16 #define JAZ_STRING_ADD_HPP
28 /** Split a string into a list of strings.
29 * @param pat is a string separator.
30 * @param s is a string to split.
31 * @param out is a model of OutputIterator
32 * where the output should be stored.
34 template <typename charT
, typename traits
, typename Alloc
, typename Iter
>
36 const std::basic_string
<charT
, traits
, Alloc
>& s
, Iter out
) {
39 for (unsigned int i
= 0; i
< s
.size(); ++i
) {
43 std::basic_string
<charT
, traits
, Alloc
>(s
, pos
, i
- pos
);
50 std::basic_string
<charT
, traits
, Alloc
>(s
, pos
, s
.size() - pos
);
54 /** Join the list [@a first, @a last) of strings into a single string.
55 * @param pat is a separator.
56 * @param init is a prefix for the joint string.
58 template <typename Iter
, typename charT
, typename traits
, typename Alloc
>
59 std::basic_string
<charT
, traits
, Alloc
>
60 join(charT pat
, Iter first
, Iter last
, const std::basic_string
<charT
, traits
, Alloc
>& init
) {
61 std::basic_string
<charT
, traits
, Alloc
> s(init
);
62 if (s
.empty() == true) s
= *(first
++);
63 for (; first
!= last
; ++first
) s
+= pat
+ *first
;
67 template <typename Iter
> inline std::string
join(char pat
, Iter first
, Iter last
) {
68 return join(pat
, first
, last
, std::string(""));
72 /** Remove spaces from a string.
73 * @param s a string to be processed.
74 * @param loc a locale to assess if a given char is space.
75 * @return a new string without spaces.
77 template <typename charT
, typename traits
, typename Alloc
>
78 std::basic_string
<charT
, traits
, Alloc
>
79 remove_space(const std::basic_string
<charT
, traits
, Alloc
>& s
,
80 const std::locale
& loc
= std::locale::classic()) {
81 std::basic_string
<charT
, traits
, Alloc
> ns
;
82 for (std::size_t i
= 0; i
< s
.size(); ++i
) {
83 if (std::isspace(s
[i
], loc
) == false) ns
.push_back(s
[i
]);
89 template <typename charT
,
90 typename traits
= std::char_traits
<charT
>,
91 typename Alloc
= std::allocator
<charT
> >
92 class basic_uc_compare
:
93 public std::binary_function
<std::basic_string
<charT
, traits
, Alloc
>,
94 std::basic_string
<charT
, traits
, Alloc
>,
97 explicit basic_uc_compare(const std::locale
& loc
= std::locale::classic())
98 : ct_(std::use_facet
<std::ctype
<char> >(loc
)) { }
100 int operator()(const std::basic_string
<charT
, traits
, Alloc
>& s1
,
101 const std::basic_string
<charT
, traits
, Alloc
>& s2
) const {
103 std::size_t l1
= s1
.size();
104 std::size_t l2
= s2
.size();
106 if (l1
< l2
) return -1; else if (l2
< l1
) return 1;
110 for (std::size_t i
= 0; i
< l1
; ++i
) {
111 c1
= ct_
.toupper(s1
[i
]);
112 c2
= ct_
.toupper(s2
[i
]);
113 if (c1
< c2
) return -1; else if (c2
< c1
) return 1;
120 const std::ctype
<charT
>& ct_
;
122 }; // class basic_uc_compare
124 /** Functor to compare upper case version of strings.
126 typedef basic_uc_compare
<char> uc_compare
;
129 template <typename charT
,
130 typename traits
= std::char_traits
<charT
>,
131 typename Alloc
= std::allocator
<charT
> >
132 class basic_to_upper
:
133 public std::unary_function
<std::basic_string
<charT
, traits
, Alloc
>,
134 std::basic_string
<charT
, traits
, Alloc
> > {
136 explicit basic_to_upper(const std::locale
& L
= std::locale::classic())
137 : ct_(std::use_facet
<std::ctype
<char> >(L
)), s_() { }
139 const std::basic_string
<charT
, traits
, Alloc
>&
140 operator()(const std::basic_string
<charT
, traits
, Alloc
>& s
) const {
142 for (std::size_t i
= 0; i
< s
.size(); ++i
) s_
[i
] = ct_
.toupper(s
[i
]);
147 const std::ctype
<charT
>& ct_
;
148 mutable std::basic_string
<charT
, traits
, Alloc
> s_
;
150 }; // class basic_to_upper
152 /** Functor to transform a string into its upper case form.
154 typedef basic_to_upper
<char> to_upper
;
157 /** Simple approximate string matching.
159 template <typename charT
, typename traits
, typename Alloc
>
160 std::pair
<long int, bool> approx_match(const std::basic_string
<charT
, traits
, Alloc
>& T
,
161 const std::basic_string
<charT
, traits
, Alloc
>& P
,
163 unsigned int n
= T
.size();
164 unsigned int m
= P
.size();
166 long int pos
= std::string::npos
;
169 long int l
= n
- m
+ 1;
170 unsigned int cmm
= mm
+ 1;
172 for (long int i
= 0; i
< l
; ++i
) {
177 if (T
[i
+ j
] != P
[j
]) t
++;
186 } else if (t
== cmm
- 1) {
187 if (pos
== std::string::npos
) pos
= i
;
193 return std::make_pair(pos
, mult
);
197 template <typename charT
,
198 typename traits
= std::char_traits
<charT
>,
199 typename Alloc
= std::allocator
<charT
> >
200 class basic_to_string
{
203 template <typename T
>
204 std::basic_string
<charT
, traits
, Alloc
> operator()(const T
& t
) {
212 std::basic_ostringstream
<charT
, traits
, Alloc
> os_
;
214 }; // class basic_to_string
216 /** Functor to transform any serializable object to a string.
218 typedef basic_to_string
<char> to_string
;
221 template <typename charT
,
222 typename traits
= std::char_traits
<charT
>,
223 typename Alloc
= std::allocator
<charT
> >
224 class basic_string_to
{
227 template <typename T
>
228 bool operator()(const std::basic_string
<charT
, traits
, Alloc
>& s
, T
& t
) {
236 std::basic_istringstream
<charT
, traits
, Alloc
> is_
;
238 }; // class basic_string_to
240 /** Functor to transform a string into any unserializable object.
242 typedef basic_string_to
<char> string_to
;
246 #endif // JAZ_STRING_ADD_HPP