mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Add a StringView class.
This commit is contained in:
parent
7972303405
commit
461aa550eb
Notes:
sideshowbarker
2024-07-19 14:42:07 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/461aa550eb8
2 changed files with 40 additions and 6 deletions
|
@ -1,11 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "ByteBuffer.h"
|
#include <AK/ByteBuffer.h>
|
||||||
#include "RetainPtr.h"
|
#include <AK/RetainPtr.h>
|
||||||
#include "StringImpl.h"
|
#include <AK/StringImpl.h>
|
||||||
#include "Traits.h"
|
#include <AK/Traits.h>
|
||||||
#include "Vector.h"
|
#include <AK/Vector.h>
|
||||||
#include "kstdio.h"
|
#include <AK/StringView.h>
|
||||||
|
#include <AK/kstdio.h>
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
@ -14,6 +15,12 @@ public:
|
||||||
~String() { }
|
~String() { }
|
||||||
|
|
||||||
String() { }
|
String() { }
|
||||||
|
|
||||||
|
String(StringView view)
|
||||||
|
: m_impl(StringImpl::create(view.characters(), view.length()))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
String(const String& other)
|
String(const String& other)
|
||||||
: m_impl(const_cast<String&>(other).m_impl.copy_ref())
|
: m_impl(const_cast<String&>(other).m_impl.copy_ref())
|
||||||
{
|
{
|
||||||
|
@ -111,6 +118,8 @@ public:
|
||||||
|
|
||||||
static String format(const char*, ...);
|
static String format(const char*, ...);
|
||||||
|
|
||||||
|
StringView view() const { return { characters(), length() }; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RetainPtr<StringImpl> m_impl;
|
RetainPtr<StringImpl> m_impl;
|
||||||
};
|
};
|
||||||
|
|
25
AK/StringView.h
Normal file
25
AK/StringView.h
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
class StringView {
|
||||||
|
public:
|
||||||
|
StringView() { }
|
||||||
|
StringView(const char* characters, int length) : m_characters(characters), m_length(length) { }
|
||||||
|
StringView(const unsigned char* characters, int length) : m_characters((const char*)characters), m_length(length) { }
|
||||||
|
StringView(const char* cstring)
|
||||||
|
: m_characters(cstring)
|
||||||
|
{
|
||||||
|
if (cstring) {
|
||||||
|
while (*(cstring++))
|
||||||
|
++m_length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_empty() const { return m_length == 0; }
|
||||||
|
const char* characters() const { return m_characters; }
|
||||||
|
int length() const { return m_length; }
|
||||||
|
char operator[](int index) const { return m_characters[index]; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const char* m_characters { nullptr };
|
||||||
|
int m_length { 0 };
|
||||||
|
};
|
Loading…
Reference in a new issue