summaryrefslogtreecommitdiff
path: root/src/mt/split.h
blob: 52e66e366f8a120667deaf05ecc475d61da5f711 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef MSPLITH
#define MSPLITH

#include <mt/stringref.h>

inline sref Skip(const sref& a,const charset& ws=cset_ws) {
  return a.right_first_not_of(ws);
}

inline sref Trim(const sref& a,const charset& ws=cset_ws) {
  return a.right_first_not_of(ws).leftand_last_not_of(ws);
}

inline sref Trim(const sref& a,const bracket& enc,const charset& ws=cset_ws) {
  sref s=a.right_first_not_of(ws).leftand_last_not_of(ws);
  while(s.nempty()&&s.skip(enc)==s.size())
    s=s(1,-3).right_first_not_of(ws).leftand_last_not_of(ws);
  return s;
}

inline sref First(const sref& a,const charset& ws=cset_ws) {
  return a.right_first_not_of(ws).left_first_of(ws);
}

inline sref Rest(const sref& a,const charset& ws=cset_ws) {
  return a.right_first_not_of(ws).right_first_of(ws).right_first_not_of(ws);
}

inline sref Last(const sref& a,const charset& ws=cset_ws) {
  sref p=a.leftand_last_not_of(ws);
  sref e=p.past_last_of(ws);
  return e.nempty()?e:p;
}

inline int Split(sref& first,sref& rest,const charset& ws=cset_ws) {
  rest=rest.right_first_not_of(ws);
  int s=rest.find_of(ws);
  first=rest.left(s);
  rest=rest.right(s);
  return first.nempty();
}

inline int Splitln(sref& first,sref& rest) {
  int s=rest.find_of(cset_newl);
  first=rest.left(s);
  rest=rest.right(s).past_nl();
  return first.begin()!=rest.begin();
}

typedef int (*split_t)(sref& first,sref& rest);

#endif