OpenStructure
string_ref.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_STRING_REF_HH
20 #define OST_STRING_REF_HH
21 
22 /*
23  Author: Marco Biasini
24  */
25 #include <ctype.h>
26 #include <cassert>
27 #include <iostream>
28 #include <ost/base.hh>
29 #include <string.h>
30 #include <vector>
31 #include <ost/module_config.hh>
32 
33 
34 namespace ost {
35 
40 public:
41 
42  typedef char* iterator;
43  typedef const char* const_iterator;
44  typedef char value_type;
45 
46  StringRef(const char* begin, size_t len): begin_(begin), end_(begin+len) { }
47  StringRef(): begin_(NULL), end_(NULL) { }
48 
49 
50  const char* begin() const { return begin_; }
51  const char* end() const { return end_; }
52  const char* data() const { return begin_; }
53 
54  size_t size() const { return end_-begin_; }
55  size_t length() const { return this->size(); }
56 
57  char front() const
58  {
59  assert(!this->empty());
60  return *begin_;
61  }
62 
63  const_iterator find(char p) const {
64  const char* s=begin_;
65  while (s!=end_) {
66  if (*s==p) {
67  return s;
68  }
69  ++s;
70  }
71  return s;
72  }
73  StringRef substr(size_t pos, size_t n=std::string::npos) const
74  {
75  if (n==std::string::npos) {
76  assert(pos>=0 && begin_+pos<=end_);
77  return StringRef(begin_+pos, this->length()-pos);
78  } else {
79  assert(pos>=0 && begin_+pos+n<=end_);
80  return StringRef(begin_+pos, n);
81  }
82  }
83  std::string str() const
84  {
85  return std::string(begin_, end_-begin_);
86  }
87  char back() const
88  {
89  assert(!this->empty());
90  return *(end_-1);
91  }
92 
93  const char& operator[](int index) const { return begin_[index]; }
94 
95  bool operator==(const StringRef& rhs) const {
96  return this->length()==rhs.length() &&
97  !memcmp(rhs.data(), this->data(), this->size());
98  }
99 
100  bool operator!=(const StringRef& rhs) const {
101  return !this->operator==(rhs);
102  }
104  StringRef rtrim() const {
105  const char* s=end_;
106  while(--s>begin_ && isspace(*s)) {
107  }
108  return StringRef(begin_, s+1-begin_);
109  }
111  StringRef ltrim() const {
112  const char* s=begin_;
113  while(s<end_ && isspace(*s)) {
114  ++s;
115  }
116  return StringRef(s, end_-s);
117  }
119  StringRef trim() const {
120  return this->rtrim().ltrim();
121  }
122 
127  std::pair<bool, int> to_int() const;
128 
133  std::pair<bool, float> to_float() const;
134 
135  bool empty() const { return begin_==end_; }
136 
138  std::vector<StringRef> split(char p) const;
139 private:
140  const char* begin_;
141  const char* end_;
142 
143 
144 };
145 //std::stringstream& operator<<(std::stringstream& stream, const StringRef& strref);
146 
147 DLLEXPORT_OST_BASE std::ostream& operator<<(std::ostream& stream, const StringRef& strref);
148 
149 }
150 #endif