diff --git a/src/file.cc b/src/file.cc index a19d40bb..8331c596 100644 --- a/src/file.cc +++ b/src/file.cc @@ -383,7 +383,7 @@ std::vector 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) { diff --git a/src/string.hh b/src/string.hh index eb241e76..fa627b90 100644 --- a/src/string.hh +++ b/src/string.hh @@ -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 split(const String& str, char separator, char escape = 0); String escape(const String& str, char character, char escape);