String::substr now returns a StringView

This commit is contained in:
Maxime Coste 2014-05-11 12:44:51 +01:00
parent 05b79921a9
commit f4d338e605
2 changed files with 22 additions and 11 deletions

View File

@ -383,7 +383,7 @@ std::vector<String> complete_command(StringView prefix, ByteCount cursor_pos)
dirname += '/';
struct stat st;
if (stat(dirname.substr(0_byte, dirname.length() - 1).c_str(), &st))
if (stat(dirname.substr(0_byte, dirname.length() - 1).zstr(), &st))
continue;
auto filter = [&](const dirent& entry) {

View File

@ -13,6 +13,8 @@ namespace Kakoune
using Regex = boost::regex;
class StringView;
class String : public std::string
{
public:
@ -47,16 +49,8 @@ public:
String operator+(Codepoint cp) const { String res = *this; utf8::dump(back_inserter(res), cp); return res; }
String& operator+=(Codepoint cp) { utf8::dump(back_inserter(*this), cp); return *this; }
String substr(ByteCount pos, ByteCount length = -1) const
{
return String{std::string::substr((int)pos, (int)length)};
}
String substr(CharCount pos, CharCount length = INT_MAX) const
{
auto b = utf8::advance(begin(), end(), (int)pos);
auto e = utf8::advance(b, end(), (int)length);
return String(b,e);
}
StringView substr(ByteCount pos, ByteCount length = INT_MAX) const;
StringView substr(CharCount pos, CharCount length = INT_MAX) const;
};
class StringView
@ -179,6 +173,16 @@ inline StringView StringView::substr(CharCount from, CharCount length) const
return StringView{ beg, utf8::advance(beg, end(), length) };
}
inline StringView String::substr(ByteCount pos, ByteCount length) const
{
return StringView{*this}.substr(pos, length);
}
inline StringView String::substr(CharCount pos, CharCount length) const
{
return StringView{*this}.substr(pos, length);
}
inline const char* begin(StringView str)
{
return str.begin();
@ -248,6 +252,13 @@ inline String operator+(Codepoint lhs, const String& rhs)
return String(lhs) + rhs;
}
inline String operator+(StringView lhs, StringView rhs)
{
String res{lhs.begin(), lhs.end()};
res += rhs;
return res;
}
std::vector<String> split(const String& str, char separator, char escape = 0);
String escape(const String& str, char character, char escape);