source: trunk/yat/utility/split.cc @ 2879

Last change on this file since 2879 was 2879, checked in by Peter, 10 years ago

split function that splits on several chars

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 1.6 KB
Line 
1// $Id: split.cc 2879 2012-11-16 01:47:56Z peter $
2
3/*
4  Copyright (C) 2010, 2012 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "split.h"
23
24#include <string>
25#include <vector>
26
27namespace theplu {
28namespace yat {
29namespace utility {
30
31namespace detail {
32
33  /*
34    implementation of split functions is declared in private namespace
35    and only available in this file.
36   */
37  template<typename T>
38  void split(std::vector<std::string>& vec, const std::string& str, T delim)
39  {
40    size_t pos=0;
41    while (true) {
42      size_t end = str.find_first_of(delim, pos);
43      vec.push_back(str.substr(pos, end-pos));
44      if (end == std::string::npos)
45        break;
46      pos = end+1;
47    }
48  }
49} // end of namespace detail
50
51
52  void split(std::vector<std::string>& vec, const std::string& str, char delim)
53  {
54    detail::split(vec, str, delim);
55  }
56
57
58  void split(std::vector<std::string>& vec, const std::string& str,
59             const std::string& delim)
60  {
61    detail::split(vec, str, delim);
62  }
63
64}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.