Skip to content

Commit 94cbd5a

Browse files
committed
More string usage cleanup
1 parent ff6eacf commit 94cbd5a

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

src/input_handler.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ class LineEditor
420420

421421
void insert(Codepoint cp)
422422
{
423-
m_line = m_line.substr(0, m_cursor_pos) + codepoint_to_str(cp)
423+
m_line = m_line.substr(0, m_cursor_pos) + String{cp}
424424
+ m_line.substr(m_cursor_pos);
425425
++m_cursor_pos;
426426
}
@@ -1187,7 +1187,7 @@ class Insert : public InputMode
11871187

11881188
void insert(Codepoint key)
11891189
{
1190-
auto str = codepoint_to_str(key);
1190+
String str{key};
11911191
context().selections().insert(str, InsertMode::InsertCursor);
11921192
context().hooks().run_hook("InsertChar", str, context());
11931193
}

src/keys.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ String key_to_str(Key key)
131131
res = "F" + to_string((int)(key.key - Key::F1 + 1));
132132
}
133133
else
134-
res = codepoint_to_str(key.key);
134+
res = String{key.key};
135135

136136
switch (key.modifiers)
137137
{

src/string.hh

+1-7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public:
102102
String(const char* content, ByteCount len) : m_data(content, (size_t)(int)len) {}
103103
explicit String(Codepoint cp, CharCount count = 1)
104104
{
105+
reserve(utf8::codepoint_size(cp) * (int)count);
105106
while (count-- > 0)
106107
utf8::dump(std::back_inserter(*this), cp);
107108
}
@@ -252,13 +253,6 @@ inline String operator"" _str(const char* str, size_t)
252253
return String(str);
253254
}
254255

255-
inline String codepoint_to_str(Codepoint cp)
256-
{
257-
String str;
258-
utf8::dump(std::back_inserter(str), cp);
259-
return str;
260-
}
261-
262256
int str_to_int(StringView str); // throws on error
263257
Optional<int> str_to_int_ifp(StringView str);
264258

src/utf8.hh

+14
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ ByteCount codepoint_size(char byte)
178178

179179
struct invalid_codepoint{};
180180

181+
inline ByteCount codepoint_size(Codepoint cp)
182+
{
183+
if (cp <= 0x7F)
184+
return 1;
185+
else if (cp <= 0x7FF)
186+
return 2;
187+
else if (cp <= 0xFFFF)
188+
return 3;
189+
else if (cp <= 0x10FFFF)
190+
return 4;
191+
else
192+
throw invalid_codepoint{};
193+
}
194+
181195
template<typename OutputIterator>
182196
void dump(OutputIterator&& it, Codepoint cp)
183197
{

0 commit comments

Comments
 (0)