OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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-2020 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 
65  const_iterator find(char p) const {
66  const char* s=begin_;
67  while (s!=end_) {
68  if (*s==p) {
69  return s;
70  }
71  ++s;
72  }
73  return s;
74  }
75 
84  StringRef substr(size_t pos, size_t n=std::string::npos) const
85  {
86  if (n==std::string::npos) {
87  assert(begin_+pos<=end_);
88  return StringRef(begin_+pos, this->length()-pos);
89  } else {
90  assert(begin_+pos+n<=end_);
91  return StringRef(begin_+pos, n);
92  }
93  }
94  std::string str() const
95  {
96  return std::string(begin_, end_-begin_);
97  }
98  char back() const
99  {
100  assert(!this->empty());
101  return *(end_-1);
102  }
103 
104  const char& operator[](int index) const { return begin_[index]; }
105 
106  bool operator==(const StringRef& rhs) const {
107  return this->length()==rhs.length() &&
108  !memcmp(rhs.data(), this->data(), this->size());
109  }
110 
111  bool operator!=(const StringRef& rhs) const {
112  return !this->operator==(rhs);
113  }
115  StringRef rtrim() const {
116  const char* s=end_;
117  while(--s>begin_ && isspace(*s)) {
118  }
119  return StringRef(begin_, s+1-begin_);
120  }
122  StringRef ltrim() const {
123  const char* s=begin_;
124  while(s<end_ && isspace(*s)) {
125  ++s;
126  }
127  return StringRef(s, end_-s);
128  }
130  StringRef trim() const {
131  return this->rtrim().ltrim();
132  }
133 
138  std::pair<bool, int> to_int() const;
139 
144  std::pair<bool, float> to_float() const;
145 
146  bool empty() const { return begin_==end_; }
147 
149  std::vector<StringRef> split(char p) const;
150 
152  std::vector<StringRef> split() const;
153 
156  std::string str_no_whitespace() const;
157 private:
158  const char* begin_;
159  const char* end_;
160 
161 };
162 //std::stringstream& operator<<(std::stringstream& stream, const StringRef& strref);
163 
164 DLLEXPORT_OST_BASE std::ostream& operator<<(std::ostream& stream, const StringRef& strref);
165 
166 }
167 #endif
convenient datatype for referencing character data
Definition: string_ref.hh:39
StringRef(const char *begin, size_t len)
Definition: string_ref.hh:46
char * iterator
Definition: string_ref.hh:42
const char * begin() const
Definition: string_ref.hh:50
size_t size() const
Definition: string_ref.hh:54
bool DLLEXPORT_OST_GEOM operator==(const Line2 &l1, const Line2 &l2)
const char * const_iterator
Definition: string_ref.hh:43
size_t length() const
Definition: string_ref.hh:55
bool operator!=(const StringRef &rhs) const
Definition: string_ref.hh:111
pointer_it< T > begin(const std::vector< T > &values)
StringRef rtrim() const
strip space characters on the right
Definition: string_ref.hh:115
bool empty() const
Definition: string_ref.hh:146
StringRef substr(size_t pos, size_t n=std::string::npos) const
returns a substring of the string
Definition: string_ref.hh:84
const char & operator[](int index) const
Definition: string_ref.hh:104
const_iterator find(char p) const
find character in StringRef
Definition: string_ref.hh:65
char front() const
Definition: string_ref.hh:57
bool operator==(const StringRef &rhs) const
Definition: string_ref.hh:106
DLLEXPORT_OST_BASE std::ostream & operator<<(std::ostream &stream, const StringRef &strref)
std::string str() const
Definition: string_ref.hh:94
#define DLLEXPORT_OST_BASE
const char * data() const
Definition: string_ref.hh:52
StringRef ltrim() const
strip space characters on the left
Definition: string_ref.hh:122
const char * end() const
Definition: string_ref.hh:51
StringRef trim() const
strip space characters on the left and the right
Definition: string_ref.hh:130
char back() const
Definition: string_ref.hh:98