Skip to content

Commit a8eddd0

Browse files
committed
String usage cleanups
1 parent 80f7a35 commit a8eddd0

7 files changed

+18
-20
lines changed

src/backtrace.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ String Backtrace::desc() const
3232
char** symbols = backtrace_symbols(stackframes, num_frames);
3333
ByteCount size = 0;
3434
for (int i = 0; i < num_frames; ++i)
35-
size += StringView::strlen(symbols[i]) + 1;
35+
size += strlen(symbols[i]) + 1;
3636

3737
String res; res.reserve(size);
3838
for (int i = 0; i < num_frames; ++i)

src/insert_completer.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos, const
8888
if (not is_word(*begin))
8989
++begin;
9090

91-
String prefix{begin.base(), end.base()};
91+
auto prefix = buffer.string(begin.base().coord(), end.base().coord());
9292

9393
while (end != buffer.end() and is_word(*end))
9494
++end;
9595

96-
String current_word{begin.base(), end.base()};
96+
auto current_word = buffer.string(begin.base().coord(), end.base().coord());
9797

9898
struct RankedMatchAndBuffer : RankedMatch
9999
{
@@ -189,7 +189,7 @@ InsertCompletion complete_filename(const Buffer& buffer, ByteCoord cursor_pos,
189189
if (begin == pos)
190190
return {};
191191

192-
String prefix{begin, pos};
192+
auto prefix = buffer.string(begin.coord(), pos.coord());
193193
if (require_slash and not contains(prefix, '/'))
194194
return {};
195195

src/normal.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ void command(Context& context, NormalParams params)
366366
{
367367
EnvVarMap env_vars = {
368368
{ "count", to_string(params.count) },
369-
{ "register", String{params.reg} }
369+
{ "register", String{&params.reg, 1} }
370370
};
371371
CommandManager::instance().execute(
372372
cmdline, context, { {}, env_vars });

src/regex.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ namespace Kakoune
77

88
String option_to_string(const Regex& re)
99
{
10-
const auto& str = re.str();
11-
return {str.begin(), str.end()};
10+
return re.str();
1211
}
1312

1413
void option_from_string(StringView str, Regex& re)

src/regex.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct Regex : boost::regex
5959
: boost::regex(begin, end, flags) {}
6060
catch (std::runtime_error& err) { throw regex_error(err.what()); }
6161

62-
String str() const { auto s = boost::regex::str(); return {s.begin(), s.end()}; }
62+
String str() const { auto s = boost::regex::str(); return {s.data(), (int)s.length()}; }
6363
};
6464
namespace regex_ns = boost;
6565
#endif

src/selectors.hh

+2-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ Selection find_next_match(const Buffer& buffer, const Selection& sel, const Rege
289289
begin = ensure_char_start(buffer, matches[0].first);
290290
end = ensure_char_start(buffer, matches[0].second);
291291
for (auto& match : matches)
292-
captures.emplace_back(match.first, match.second);
292+
captures.push_back(buffer.string(match.first.coord(),
293+
match.second.coord()));
293294
}
294295
if (not found or begin == buffer.end())
295296
throw runtime_error(format("'{}': no matches found", regex.str()));

src/string.hh

+9-11
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,27 @@ private:
8585
const Type& type() const { return *static_cast<const Type*>(this); }
8686
};
8787

88+
[[gnu::optimize(3)]] // this is recursive for constexpr reason
89+
constexpr ByteCount strlen(const char* s)
90+
{
91+
return *s == 0 ? 0 : strlen(s+1) + 1;
92+
}
93+
8894
class String : public StringOps<String, char>
8995
{
9096
public:
9197
using Content = std::basic_string<char, std::char_traits<char>,
9298
Allocator<char, MemoryDomain::String>>;
9399

94100
String() {}
95-
String(const char* content) : m_data(content) {}
101+
String(const char* content) : m_data(content, (size_t)(int)strlen(content)) {}
96102
String(const char* content, ByteCount len) : m_data(content, (size_t)(int)len) {}
97-
explicit String(char content, CharCount count = 1) : m_data((size_t)(int)count, content) {}
98103
explicit String(Codepoint cp, CharCount count = 1)
99104
{
100105
while (count-- > 0)
101106
utf8::dump(std::back_inserter(*this), cp);
102107
}
103-
template<typename Iterator>
104-
String(Iterator begin, Iterator end) : m_data(begin, end) {}
108+
String(const char* begin, const char* end) : m_data(begin, end-begin) {}
105109

106110
[[gnu::always_inline]]
107111
char* data() { return &m_data[0]; }
@@ -148,7 +152,7 @@ public:
148152
[[gnu::always_inline]]
149153
constexpr ByteCount length() const { return m_length; }
150154

151-
String str() const { return {begin(), end()}; }
155+
String str() const { return {m_data, m_length}; }
152156

153157
struct ZeroTerminatedString
154158
{
@@ -168,12 +172,6 @@ public:
168172
};
169173
ZeroTerminatedString zstr() const { return {begin(), end()}; }
170174

171-
[[gnu::optimize(3)]] // this is recursive for constexpr reason
172-
static constexpr ByteCount strlen(const char* s)
173-
{
174-
return *s == 0 ? 0 : strlen(s+1) + 1;
175-
}
176-
177175
private:
178176
const char* m_data = nullptr;
179177
ByteCount m_length = 0;

0 commit comments

Comments
 (0)