modified: myjupyterlab.sh
[GalaxyCodeBases.git] / c_cpp / etc / mVicuna / src / jaz / string_add.hpp
blobb9d845b74dee4cdcb1763c717e0286f537644157
1 /***
2 * $Id$
3 **
4 * File: string_add.hpp
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
18 #include <cstddef>
19 #include <iostream>
20 #include <functional>
21 #include <locale>
22 #include <sstream>
23 #include <string>
26 namespace jaz {
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>
35 void split(charT pat,
36 const std::basic_string<charT, traits, Alloc>& s, Iter out) {
37 unsigned int pos = 0;
39 for (unsigned int i = 0; i < s.size(); ++i) {
40 if (s[i] == pat) {
41 if (i - pos > 0) {
42 *(out++) =
43 std::basic_string<charT, traits, Alloc>(s, pos, i - pos);
45 pos = i + 1;
49 *(out++) =
50 std::basic_string<charT, traits, Alloc>(s, pos, s.size() - pos);
51 } // split
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;
64 return s;
65 } // join
67 template <typename Iter> inline std::string join(char pat, Iter first, Iter last) {
68 return join(pat, first, last, std::string(""));
69 } // join
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]);
85 return ns;
86 } // remove_space
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>,
95 int> {
96 public:
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;
108 charT c1, c2;
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;
116 return 0;
117 } // operator()
119 private:
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> > {
135 public:
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 {
141 s_ = s;
142 for (std::size_t i = 0; i < s.size(); ++i) s_[i] = ct_.toupper(s[i]);
143 return s_;
144 } // operator
146 private:
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,
162 unsigned int mm) {
163 unsigned int n = T.size();
164 unsigned int m = P.size();
166 long int pos = std::string::npos;
167 bool mult = false;
169 long int l = n - m + 1;
170 unsigned int cmm = mm + 1;
172 for (long int i = 0; i < l; ++i) {
173 unsigned int t = 0;
174 unsigned int j = 0;
176 for (; j < m; ++j) {
177 if (T[i + j] != P[j]) t++;
178 if (t == cmm) break;
181 if (j == m) {
182 if (t < cmm - 1) {
183 pos = i;
184 mult = false;
185 cmm = t + 1;
186 } else if (t == cmm - 1) {
187 if (pos == std::string::npos) pos = i;
188 else mult = true;
191 } // for i
193 return std::make_pair(pos, mult);
194 } // approx_match
197 template <typename charT,
198 typename traits = std::char_traits<charT>,
199 typename Alloc = std::allocator<charT> >
200 class basic_to_string {
201 public:
203 template <typename T>
204 std::basic_string<charT, traits, Alloc> operator()(const T& t) {
205 os_.clear();
206 os_.str("");
207 os_ << t;
208 return os_.str();
209 } // operator()
211 private:
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 {
225 public:
227 template <typename T>
228 bool operator()(const std::basic_string<charT, traits, Alloc>& s, T& t) {
229 is_.clear();
230 is_.str(s + '\n');
231 is_ >> t;
232 return is_.good();
233 } // operator()
235 private:
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;
244 } // namespace jaz
246 #endif // JAZ_STRING_ADD_HPP