]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/NasalString.cxx
cppbind.Ghost: clean up a bit
[simgear.git] / simgear / nasal / cppbind / NasalString.cxx
1 // Wrapper class for Nasal strings
2 //
3 // Copyright (C) 2013  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
18
19 #include "NasalString.hxx"
20 #include "to_nasal.hxx"
21
22 #include <algorithm>
23 #include <cassert>
24 #include <cstring>
25 #include <functional>
26 #include <stdexcept>
27
28 namespace nasal
29 {
30
31   /**
32    *  Predicate (eg. for std::find_if) returning true if no character of the
33    *  stored string given by the range [begin, end) matches.
34    */
35   struct no_match:
36     public std::unary_function<const char, bool>
37   {
38     no_match(const char* begin, const char* end):
39       _begin(begin),
40       _end(end)
41     {}
42
43     bool operator()(const char c) const
44     {
45       return std::find(_begin, _end, c) == _end;
46     }
47
48     private:
49       const char* _begin;
50       const char* _end;
51   };
52
53 //template<typename Iterator>
54 //Iterator
55 //rfind_first_of( Iterator rbegin_src, Iterator rend_src,
56 //                Iterator begin_find, Iterator end_find )
57 //{
58 //  for(; rbegin_src != rend_src; --rbegin_src)
59 //  {
60 //    for(Iterator chr = begin_find; chr != end_find; ++chr)
61 //    {
62 //      if( *rbegin_src == *chr )
63 //        return rbegin_src;
64 //    }
65 //  }
66 //  return rend_src;
67 //}
68
69
70   const size_t String::npos = static_cast<size_t>(-1);
71
72   //----------------------------------------------------------------------------
73   String::String(naContext c, const char* str):
74     _str( to_nasal(c, str) )
75   {
76     assert( naIsString(_str) );
77   }
78
79   //----------------------------------------------------------------------------
80   String::String(naRef str):
81     _str(str)
82   {
83     assert( naIsString(_str) );
84   }
85
86   //----------------------------------------------------------------------------
87   const char* String::c_str() const
88   {
89     return naStr_data(_str);
90   }
91
92   //----------------------------------------------------------------------------
93   const char* String::begin() const
94   {
95     return c_str();
96   }
97
98   //----------------------------------------------------------------------------
99   const char* String::end() const
100   {
101     return c_str() + size();
102   }
103
104   //----------------------------------------------------------------------------
105   size_t String::size() const
106   {
107     return naStr_len(_str);
108   }
109
110   //----------------------------------------------------------------------------
111   size_t String::length() const
112   {
113     return size();
114   }
115
116   //----------------------------------------------------------------------------
117   bool String::empty() const
118   {
119     return size() == 0;
120   }
121
122   //----------------------------------------------------------------------------
123   int String::compare(size_t pos, size_t len, const String& rhs) const
124   {
125     if( pos >= size() )
126       throw std::out_of_range("nasal::String::compare: pos");
127
128     return memcmp( begin() + pos,
129                    rhs.begin(),
130                    std::min(rhs.size(), std::min(size() - pos, len)) );
131   }
132
133   //----------------------------------------------------------------------------
134   bool String::starts_with(const String& rhs) const
135   {
136     return rhs.size() <= size() && compare(0, npos, rhs) == 0;
137   }
138
139   //----------------------------------------------------------------------------
140   bool String::ends_with(const String& rhs) const
141   {
142     return rhs.size() <= size() && compare(size() - rhs.size(), npos, rhs) == 0;
143   }
144
145   //----------------------------------------------------------------------------
146   size_t String::find(const char c, size_t pos) const
147   {
148     if( pos >= size() )
149       return npos;
150
151     const char* result = std::find(begin() + pos, end(), c);
152
153     return result != end() ? result - begin() : npos;
154   }
155
156   //----------------------------------------------------------------------------
157   size_t String::find_first_of(const String& chr, size_t pos) const
158   {
159     if( pos >= size() )
160       return npos;
161
162     const char* result = std::find_first_of( begin() + pos, end(),
163                                              chr.begin(), chr.end() );
164
165     return result != end() ? result - begin() : npos;
166   }
167
168   //----------------------------------------------------------------------------
169   size_t String::find_first_not_of(const String& chr, size_t pos) const
170   {
171     if( pos >= size() )
172       return npos;
173
174     const char* result = std::find_if( begin() + pos, end(),
175                                        no_match(chr.begin(), chr.end()) );
176
177     return result != end() ? result - begin() : npos;
178   }
179
180   //----------------------------------------------------------------------------
181   const naRef String::get_naRef() const
182   {
183     return _str;
184   }
185
186 } // namespace nasal