SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
manipulate.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_MANIPULATE_HPP
9 #define SIG_UTIL_MANIPULATE_HPP
10 
11 #include "../helper/type_convert.hpp"
12 #include "../helper/container_helper.hpp"
13 
14 
16 
17 namespace sig
18 {
19 
21 
35 template <
36  template <class T_, class = std::allocator<T_>> class CSeq = std::vector,
37  class S = std::string,
38  class TS = impl::string_t<S>
39 >
40 auto split(
41  S const& src,
42  impl::string_t<S> const& delimiter)
43 ->CSeq<TS>
44 {
45  CSeq<TS> result;
46  const uint mag = delimiter.size();
47  const uint length = src.size();
48  uint pos = 0, ppos = 0;
49 
50  while (pos < length){
51  if (src[pos] == delimiter[0]){
52  // check to match delimiter
53  bool cf = true;
54  for (uint d = 1; d < mag; ++d){
55  if (src[pos+d] != delimiter[d]){
56  cf = false;
57  break;
58  }
59  }
60 
61  if (cf){
62  if (pos - ppos > 0) result.push_back(src.substr(ppos, pos - ppos));
63  //impl::container_traits<CSeq>::add_element(result, src.substr(ppos, pos));
64  pos += mag;
65  ppos = pos;
66  }
67  else{
68  pos += mag;
69  }
70  }
71  else{
72  ++pos;
73  }
74  }
75  if (pos - ppos > 0)result.push_back(src.substr(ppos, pos - ppos));
76 
77  return result;
78 }
79 
83 template < template <class T_, class = std::allocator<T_>> class CSeq = std::vector >
84 auto split(
85  char const* const src,
86  char const* const delimiter)
87 ->CSeq<std::string>
88 {
89  return split<CSeq>(std::string(src), std::string(delimiter));
90 }
91 
95 template < template < class T_, class = std::allocator<T_>> class CSeq = std::vector >
96 auto split(
97  wchar_t const* const src,
98  wchar_t const* const delimiter)
99 ->CSeq<std::wstring>
100 {
101  return split<CSeq>(std::wstring(src), std::wstring(delimiter));
102 }
103 
104 
105 namespace impl
106 {
107 // コンテナに格納された各文字列(数値の場合は文字列に変換)を順番に結合して1つの文字列に(delimiterで区切り指定)
108 template <class It, class S, class OSS>
109 auto cat_impl(
110  It begin,
111  It end,
112  S const& delimiter,
113  OSS& osstream,
114  std::locale osstream_locale = std::locale(""))
115 ->decltype(osstream.str())
116 {
117  if (begin == end) return osstream.str();
118 
119  std::locale::global(osstream_locale);
120  std::locale ctype_default(std::locale::classic(), osstream_locale, std::locale::ctype);
121  osstream.imbue(ctype_default);
122 
123  for (osstream << *begin++; begin != end; ++begin){
124  osstream << delimiter << *begin;
125  }
126 
127  return osstream.str();
128 }
129 
130 } // impl
131 
132 
134 
151 template <class C,
152  class CR = typename impl::remove_const_reference<C>::type,
153  class S = typename impl::SStreamSelector<typename impl::container_traits<CR>::value_type>::string
154 >
155 auto cat(
156  C&& container,
157  typename sig::impl::identity<S>::type const& delimiter,
158  std::locale osstream_locale = std::locale(""))
159 ->S
160 {
162  return impl::cat_impl(
163  impl::begin(std::forward<C>(container)),
164  impl::end(std::forward<C>(container)),
165  delimiter,
166  oss,
167  osstream_locale
168  );
169 }
170 
176 template <class T, class S = typename impl::SStreamSelector<T>::string>
177 auto cat(
178  std::initializer_list<T> container,
179  typename sig::impl::identity<S>::type const& delimiter,
180  std::locale osstream_locale = std::locale(""))
181 ->S
182 {
184  return impl::cat_impl(
185  impl::begin(container),
186  impl::end(container),
187  delimiter,
188  oss,
189  osstream_locale
190  );
191 }
192 
193 }
194 #endif
auto split(S const &src, impl::string_t< S > const &delimiter) -> CSeq< TS >
文字列(src)をある文字列(delimiter)を目印に分割する
Definition: manipulate.hpp:40
auto end(C &&c) -> std::move_iterator< typename RC::iterator >
auto cat(C &&container, typename sig::impl::identity< S >::type const &delimiter, std::locale osstream_locale=std::locale("")) -> S
コンテナ中の各文字列や数値を結合
Definition: manipulate.hpp:155
auto cat_impl(It begin, It end, S const &delimiter, OSS &osstream, std::locale osstream_locale=std::locale("")) -> decltype(osstream.str())
Definition: manipulate.hpp:109
std::ostringstream ostringstream
Definition: array.hpp:15
typename StringId< typename remove_const_reference< typename std::decay< T >::type >::type >::type string_t
auto begin(C &&c) -> std::move_iterator< typename RC::iterator >