mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 07:54:58 +00:00
AK: Made Strings reversible
`AK::String` can now be reversed via AK::String::reverse(). This makes life a lot easier for functions like `itoa()`, where the output ends up being backwards. Very much not like the normal STL (which requires an `std::reverse` object) way of doing things. A call to reverse returns a new `AK::String` so as to not upset any of the possible references to the same `StringImpl` shared between Strings.
This commit is contained in:
parent
093961d2d9
commit
26e81ad574
4 changed files with 28 additions and 4 deletions
|
@ -4,7 +4,7 @@
|
|||
#include "kmalloc.h"
|
||||
|
||||
#ifndef __serenity__
|
||||
#include <new>
|
||||
# include <new>
|
||||
#endif
|
||||
|
||||
//#define DEBUG_STRINGIMPL
|
||||
|
@ -172,4 +172,19 @@ void StringImpl::compute_hash() const
|
|||
m_has_hash = true;
|
||||
}
|
||||
|
||||
NonnullRefPtr<StringImpl> StringImpl::reversed() const
|
||||
{
|
||||
if (m_length == 0)
|
||||
return the_empty_stringimpl();
|
||||
|
||||
char* buffer;
|
||||
const char* pos = &m_inline_buffer[m_length - 1];
|
||||
auto new_impl = create_uninitialized(m_length, buffer);
|
||||
|
||||
for (int i = 0; i < m_length; i++)
|
||||
buffer[i] = *pos--;
|
||||
|
||||
return new_impl;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue