mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add begin() and end() to String and StringView
Now it's possible to use range-based for loops with String and StringView.
This commit is contained in:
parent
a0e18f4450
commit
e07f50c398
Notes:
sideshowbarker
2024-07-19 17:36:49 +09:00
Author: https://github.com/f-eiwu Commit: https://github.com/SerenityOS/serenity/commit/e07f50c3986 Pull-request: https://github.com/SerenityOS/serenity/pull/1401 Reviewed-by: https://github.com/awesomekling
3 changed files with 15 additions and 6 deletions
10
AK/String.h
10
AK/String.h
|
@ -57,6 +57,8 @@ namespace AK {
|
||||||
|
|
||||||
class String {
|
class String {
|
||||||
public:
|
public:
|
||||||
|
using ConstIterator = const char*;
|
||||||
|
|
||||||
~String() {}
|
~String() {}
|
||||||
|
|
||||||
String() {}
|
String() {}
|
||||||
|
@ -144,12 +146,14 @@ public:
|
||||||
bool is_empty() const { return length() == 0; }
|
bool is_empty() const { return length() == 0; }
|
||||||
size_t length() const { return m_impl ? m_impl->length() : 0; }
|
size_t length() const { return m_impl ? m_impl->length() : 0; }
|
||||||
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
|
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
|
||||||
char operator[](size_t i) const
|
const char& operator[](size_t i) const
|
||||||
{
|
{
|
||||||
ASSERT(m_impl);
|
|
||||||
return (*m_impl)[i];
|
return (*m_impl)[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConstIterator begin() const { return characters(); }
|
||||||
|
ConstIterator end() const { return begin() + length(); }
|
||||||
|
|
||||||
bool starts_with(const StringView&) const;
|
bool starts_with(const StringView&) const;
|
||||||
bool ends_with(const StringView&) const;
|
bool ends_with(const StringView&) const;
|
||||||
bool starts_with(char) const;
|
bool starts_with(char) const;
|
||||||
|
@ -302,5 +306,5 @@ String escape_html_entities(const StringView& html);
|
||||||
}
|
}
|
||||||
|
|
||||||
using AK::CaseInsensitiveStringTraits;
|
using AK::CaseInsensitiveStringTraits;
|
||||||
using AK::String;
|
|
||||||
using AK::escape_html_entities;
|
using AK::escape_html_entities;
|
||||||
|
using AK::String;
|
||||||
|
|
|
@ -26,8 +26,8 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/RefPtr.h>
|
|
||||||
#include <AK/RefCounted.h>
|
#include <AK/RefCounted.h>
|
||||||
|
#include <AK/RefPtr.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <AK/kmalloc.h>
|
#include <AK/kmalloc.h>
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ public:
|
||||||
|
|
||||||
size_t length() const { return m_length; }
|
size_t length() const { return m_length; }
|
||||||
const char* characters() const { return &m_inline_buffer[0]; }
|
const char* characters() const { return &m_inline_buffer[0]; }
|
||||||
char operator[](size_t i) const
|
const char& operator[](size_t i) const
|
||||||
{
|
{
|
||||||
ASSERT(i < m_length);
|
ASSERT(i < m_length);
|
||||||
return characters()[i];
|
return characters()[i];
|
||||||
|
|
|
@ -34,6 +34,8 @@ namespace AK {
|
||||||
|
|
||||||
class StringView {
|
class StringView {
|
||||||
public:
|
public:
|
||||||
|
using ConstIterator = const char*;
|
||||||
|
|
||||||
StringView() {}
|
StringView() {}
|
||||||
StringView(const char* characters, size_t length)
|
StringView(const char* characters, size_t length)
|
||||||
: m_characters(characters)
|
: m_characters(characters)
|
||||||
|
@ -58,7 +60,10 @@ public:
|
||||||
bool is_empty() const { return m_length == 0; }
|
bool is_empty() const { return m_length == 0; }
|
||||||
const char* characters_without_null_termination() const { return m_characters; }
|
const char* characters_without_null_termination() const { return m_characters; }
|
||||||
size_t length() const { return m_length; }
|
size_t length() const { return m_length; }
|
||||||
char operator[](size_t index) const { return m_characters[index]; }
|
const char& operator[](size_t index) const { return m_characters[index]; }
|
||||||
|
|
||||||
|
ConstIterator begin() const { return characters_without_null_termination(); }
|
||||||
|
ConstIterator end() const { return begin() + length(); }
|
||||||
|
|
||||||
unsigned hash() const;
|
unsigned hash() const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue