BAN: StringView::Split() now precomputes number of elements
We dont have to resize vector on PushBack()'s if we reserve required size
This commit is contained in:
parent
d5a068f90c
commit
4c559f50a4
|
@ -62,9 +62,27 @@ namespace BAN
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<StringView> StringView::Split(char delim, bool allow_empties)
|
ErrorOr<Vector<StringView>> StringView::Split(char delim, bool allow_empties)
|
||||||
{
|
{
|
||||||
|
size_type count = 0;
|
||||||
|
{
|
||||||
|
size_type start = 0;
|
||||||
|
for (size_type i = 0; i < m_size; i++)
|
||||||
|
{
|
||||||
|
if (m_data[i] == delim)
|
||||||
|
{
|
||||||
|
if (allow_empties || start != i)
|
||||||
|
count++;
|
||||||
|
start = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (start != m_size)
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
Vector<StringView> result;
|
Vector<StringView> result;
|
||||||
|
TRY(result.Reserve(count));
|
||||||
|
|
||||||
size_type start = 0;
|
size_type start = 0;
|
||||||
for (size_type i = 0; i < m_size; i++)
|
for (size_type i = 0; i < m_size; i++)
|
||||||
{
|
{
|
||||||
|
|
|
@ -40,6 +40,7 @@ class ErrorOr
|
||||||
public:
|
public:
|
||||||
ErrorOr(const T& value) : m_has_error(false) { m_data = (void*)new T(value); }
|
ErrorOr(const T& value) : m_has_error(false) { m_data = (void*)new T(value); }
|
||||||
ErrorOr(const Error& error) : m_has_error(true) { m_data = (void*)new Error(error); }
|
ErrorOr(const Error& error) : m_has_error(true) { m_data = (void*)new Error(error); }
|
||||||
|
template<typename S> ErrorOr(const ErrorOr<S>& other) : ErrorOr(other.GetError()) {}
|
||||||
~ErrorOr() { IsError() ? (delete reinterpret_cast<Error*>(m_data)) : (delete reinterpret_cast<T*>(m_data)); }
|
~ErrorOr() { IsError() ? (delete reinterpret_cast<Error*>(m_data)) : (delete reinterpret_cast<T*>(m_data)); }
|
||||||
|
|
||||||
bool IsError() const { return m_has_error; }
|
bool IsError() const { return m_has_error; }
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace BAN
|
||||||
|
|
||||||
StringView Substring(size_type, size_type = -1) const;
|
StringView Substring(size_type, size_type = -1) const;
|
||||||
|
|
||||||
Vector<StringView> Split(char, bool = false);
|
ErrorOr<Vector<StringView>> Split(char, bool = false);
|
||||||
|
|
||||||
size_type Count(char) const;
|
size_type Count(char) const;
|
||||||
|
|
||||||
|
|
|
@ -228,7 +228,7 @@ namespace Kernel
|
||||||
case Keyboard::Key::NumpadEnter:
|
case Keyboard::Key::NumpadEnter:
|
||||||
{
|
{
|
||||||
kprint("\n");
|
kprint("\n");
|
||||||
ProcessCommand(m_buffer.SV().Split(' '));
|
ProcessCommand(MUST(m_buffer.SV().Split(' ')));
|
||||||
m_buffer.Clear();
|
m_buffer.Clear();
|
||||||
PrintPrompt();
|
PrintPrompt();
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue