1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-30 21:27:46 +00:00

AK: Add String::copy(BufferType) helper.

This will create a String from any BufferType that has data() and size().
This commit is contained in:
Andreas Kling 2019-04-20 14:13:40 +02:00
parent 5eedb22834
commit ab94a6be00
11 changed files with 31 additions and 27 deletions

View file

@ -118,7 +118,16 @@ public:
}
ByteBuffer to_byte_buffer() const;
static String from_byte_buffer(const ByteBuffer&, ShouldChomp = NoChomp);
template<typename BufferType>
static String copy(const BufferType& buffer, ShouldChomp should_chomp = NoChomp)
{
if (buffer.is_null())
return { };
if (buffer.is_empty())
return empty();
return String((const char*)buffer.data(), buffer.size(), should_chomp);
}
static String format(const char*, ...);

View file

@ -104,6 +104,9 @@ public:
bool is_empty() const { return !m_impl || m_impl->is_empty(); }
ssize_t size() const { return m_impl ? m_impl->size() : 0; }
byte* data() { return pointer(); }
const byte* data() const { return pointer(); }
byte* pointer() { return m_impl ? m_impl->pointer() : nullptr; }
const byte* pointer() const { return m_impl ? m_impl->pointer() : nullptr; }

View file

@ -122,15 +122,6 @@ ByteBuffer String::to_byte_buffer() const
return ByteBuffer::copy(reinterpret_cast<const byte*>(characters()), length());
}
String String::from_byte_buffer(const ByteBuffer& buffer, ShouldChomp should_chomp)
{
if (buffer.is_null())
return { };
if (buffer.is_empty())
return empty();
return String((const char*)buffer.pointer(), buffer.size(), should_chomp);
}
// FIXME: Duh.
int String::to_int(bool& ok) const
{

View file

@ -93,6 +93,8 @@ public:
return false;
}
// NOTE: Vector::is_null() exists for the benefit of String::copy().
bool is_null() const { return is_empty(); }
bool is_empty() const { return size() == 0; }
int size() const { return m_size; }
int capacity() const { return m_capacity; }