String usage cleanups

This commit is contained in:
Maxime Coste 2016-02-04 23:52:06 +00:00
parent 80f7a350e4
commit a8eddd03f0
7 changed files with 18 additions and 20 deletions

View File

@ -32,7 +32,7 @@ String Backtrace::desc() const
char** symbols = backtrace_symbols(stackframes, num_frames); char** symbols = backtrace_symbols(stackframes, num_frames);
ByteCount size = 0; ByteCount size = 0;
for (int i = 0; i < num_frames; ++i) for (int i = 0; i < num_frames; ++i)
size += StringView::strlen(symbols[i]) + 1; size += strlen(symbols[i]) + 1;
String res; res.reserve(size); String res; res.reserve(size);
for (int i = 0; i < num_frames; ++i) for (int i = 0; i < num_frames; ++i)

View File

@ -88,12 +88,12 @@ InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos, const
if (not is_word(*begin)) if (not is_word(*begin))
++begin; ++begin;
String prefix{begin.base(), end.base()}; auto prefix = buffer.string(begin.base().coord(), end.base().coord());
while (end != buffer.end() and is_word(*end)) while (end != buffer.end() and is_word(*end))
++end; ++end;
String current_word{begin.base(), end.base()}; auto current_word = buffer.string(begin.base().coord(), end.base().coord());
struct RankedMatchAndBuffer : RankedMatch struct RankedMatchAndBuffer : RankedMatch
{ {
@ -189,7 +189,7 @@ InsertCompletion complete_filename(const Buffer& buffer, ByteCoord cursor_pos,
if (begin == pos) if (begin == pos)
return {}; return {};
String prefix{begin, pos}; auto prefix = buffer.string(begin.coord(), pos.coord());
if (require_slash and not contains(prefix, '/')) if (require_slash and not contains(prefix, '/'))
return {}; return {};

View File

@ -366,7 +366,7 @@ void command(Context& context, NormalParams params)
{ {
EnvVarMap env_vars = { EnvVarMap env_vars = {
{ "count", to_string(params.count) }, { "count", to_string(params.count) },
{ "register", String{params.reg} } { "register", String{&params.reg, 1} }
}; };
CommandManager::instance().execute( CommandManager::instance().execute(
cmdline, context, { {}, env_vars }); cmdline, context, { {}, env_vars });

View File

@ -7,8 +7,7 @@ namespace Kakoune
String option_to_string(const Regex& re) String option_to_string(const Regex& re)
{ {
const auto& str = re.str(); return re.str();
return {str.begin(), str.end()};
} }
void option_from_string(StringView str, Regex& re) void option_from_string(StringView str, Regex& re)

View File

@ -59,7 +59,7 @@ struct Regex : boost::regex
: boost::regex(begin, end, flags) {} : boost::regex(begin, end, flags) {}
catch (std::runtime_error& err) { throw regex_error(err.what()); } catch (std::runtime_error& err) { throw regex_error(err.what()); }
String str() const { auto s = boost::regex::str(); return {s.begin(), s.end()}; } String str() const { auto s = boost::regex::str(); return {s.data(), (int)s.length()}; }
}; };
namespace regex_ns = boost; namespace regex_ns = boost;
#endif #endif

View File

@ -289,7 +289,8 @@ Selection find_next_match(const Buffer& buffer, const Selection& sel, const Rege
begin = ensure_char_start(buffer, matches[0].first); begin = ensure_char_start(buffer, matches[0].first);
end = ensure_char_start(buffer, matches[0].second); end = ensure_char_start(buffer, matches[0].second);
for (auto& match : matches) for (auto& match : matches)
captures.emplace_back(match.first, match.second); captures.push_back(buffer.string(match.first.coord(),
match.second.coord()));
} }
if (not found or begin == buffer.end()) if (not found or begin == buffer.end())
throw runtime_error(format("'{}': no matches found", regex.str())); throw runtime_error(format("'{}': no matches found", regex.str()));

View File

@ -85,6 +85,12 @@ private:
const Type& type() const { return *static_cast<const Type*>(this); } const Type& type() const { return *static_cast<const Type*>(this); }
}; };
[[gnu::optimize(3)]] // this is recursive for constexpr reason
constexpr ByteCount strlen(const char* s)
{
return *s == 0 ? 0 : strlen(s+1) + 1;
}
class String : public StringOps<String, char> class String : public StringOps<String, char>
{ {
public: public:
@ -92,16 +98,14 @@ public:
Allocator<char, MemoryDomain::String>>; Allocator<char, MemoryDomain::String>>;
String() {} String() {}
String(const char* content) : m_data(content) {} String(const char* content) : m_data(content, (size_t)(int)strlen(content)) {}
String(const char* content, ByteCount len) : m_data(content, (size_t)(int)len) {} String(const char* content, ByteCount len) : m_data(content, (size_t)(int)len) {}
explicit String(char content, CharCount count = 1) : m_data((size_t)(int)count, content) {}
explicit String(Codepoint cp, CharCount count = 1) explicit String(Codepoint cp, CharCount count = 1)
{ {
while (count-- > 0) while (count-- > 0)
utf8::dump(std::back_inserter(*this), cp); utf8::dump(std::back_inserter(*this), cp);
} }
template<typename Iterator> String(const char* begin, const char* end) : m_data(begin, end-begin) {}
String(Iterator begin, Iterator end) : m_data(begin, end) {}
[[gnu::always_inline]] [[gnu::always_inline]]
char* data() { return &m_data[0]; } char* data() { return &m_data[0]; }
@ -148,7 +152,7 @@ public:
[[gnu::always_inline]] [[gnu::always_inline]]
constexpr ByteCount length() const { return m_length; } constexpr ByteCount length() const { return m_length; }
String str() const { return {begin(), end()}; } String str() const { return {m_data, m_length}; }
struct ZeroTerminatedString struct ZeroTerminatedString
{ {
@ -168,12 +172,6 @@ public:
}; };
ZeroTerminatedString zstr() const { return {begin(), end()}; } ZeroTerminatedString zstr() const { return {begin(), end()}; }
[[gnu::optimize(3)]] // this is recursive for constexpr reason
static constexpr ByteCount strlen(const char* s)
{
return *s == 0 ? 0 : strlen(s+1) + 1;
}
private: private:
const char* m_data = nullptr; const char* m_data = nullptr;
ByteCount m_length = 0; ByteCount m_length = 0;