1 // Wrapper class for Nasal strings
3 // Copyright (C) 2013 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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
19 #include "NasalString.hxx"
20 #include "to_nasal.hxx"
32 * Predicate (eg. for std::find_if) returning true if no character of the
33 * stored string given by the range [begin, end) matches.
36 public std::unary_function<const char, bool>
38 no_match(const char* begin, const char* end):
43 bool operator()(const char c) const
45 return std::find(_begin, _end, c) == _end;
53 //template<typename Iterator>
55 //rfind_first_of( Iterator rbegin_src, Iterator rend_src,
56 // Iterator begin_find, Iterator end_find )
58 // for(; rbegin_src != rend_src; --rbegin_src)
60 // for(Iterator chr = begin_find; chr != end_find; ++chr)
62 // if( *rbegin_src == *chr )
70 const size_t String::npos = static_cast<size_t>(-1);
72 //----------------------------------------------------------------------------
73 String::String(naContext c, const char* str):
74 _str( to_nasal(c, str) )
76 assert( naIsString(_str) );
79 //----------------------------------------------------------------------------
80 String::String(naRef str):
83 assert( naIsString(_str) );
86 //----------------------------------------------------------------------------
87 const char* String::c_str() const
89 return naStr_data(_str);
92 //----------------------------------------------------------------------------
93 const char* String::begin() const
98 //----------------------------------------------------------------------------
99 const char* String::end() const
101 return c_str() + size();
104 //----------------------------------------------------------------------------
105 size_t String::size() const
107 return naStr_len(_str);
110 //----------------------------------------------------------------------------
111 size_t String::length() const
116 //----------------------------------------------------------------------------
117 bool String::empty() const
122 //----------------------------------------------------------------------------
123 int String::compare(size_t pos, size_t len, const String& rhs) const
126 throw std::out_of_range("nasal::String::compare: pos");
128 return memcmp( begin() + pos,
130 std::min(rhs.size(), std::min(size() - pos, len)) );
133 //----------------------------------------------------------------------------
134 bool String::starts_with(const String& rhs) const
136 return rhs.size() <= size() && compare(0, npos, rhs) == 0;
139 //----------------------------------------------------------------------------
140 bool String::ends_with(const String& rhs) const
142 return rhs.size() <= size() && compare(size() - rhs.size(), npos, rhs) == 0;
145 //----------------------------------------------------------------------------
146 size_t String::find(const char c, size_t pos) const
151 const char* result = std::find(begin() + pos, end(), c);
153 return result != end() ? result - begin() : npos;
156 //----------------------------------------------------------------------------
157 size_t String::find_first_of(const String& chr, size_t pos) const
162 const char* result = std::find_first_of( begin() + pos, end(),
163 chr.begin(), chr.end() );
165 return result != end() ? result - begin() : npos;
168 //----------------------------------------------------------------------------
169 size_t String::find_first_not_of(const String& chr, size_t pos) const
174 const char* result = std::find_if( begin() + pos, end(),
175 no_match(chr.begin(), chr.end()) );
177 return result != end() ? result - begin() : npos;
180 //----------------------------------------------------------------------------
181 const naRef String::get_naRef() const