summaryrefslogtreecommitdiff
path: root/AK/String.h
diff options
context:
space:
mode:
authorhowar6hill <f.eiwu@yahoo.com>2020-03-10 16:13:29 +0800
committerAndreas Kling <kling@serenityos.org>2020-03-10 10:25:21 +0100
commite07f50c39860ec006a7110dc13ea7001a6a54fc9 (patch)
treeebe170954e6b364adc6efbe5d80d821581ccfef5 /AK/String.h
parenta0e18f4450d788785d75633bb45279ed33f8ceaf (diff)
downloadserenity-e07f50c39860ec006a7110dc13ea7001a6a54fc9.zip
AK: Add begin() and end() to String and StringView
Now it's possible to use range-based for loops with String and StringView.
Diffstat (limited to 'AK/String.h')
-rw-r--r--AK/String.h10
1 files changed, 7 insertions, 3 deletions
diff --git a/AK/String.h b/AK/String.h
index c01c771219..a77799c0c1 100644
--- a/AK/String.h
+++ b/AK/String.h
@@ -57,6 +57,8 @@ namespace AK {
class String {
public:
+ using ConstIterator = const char*;
+
~String() {}
String() {}
@@ -144,12 +146,14 @@ public:
bool is_empty() const { return length() == 0; }
size_t length() const { return m_impl ? m_impl->length() : 0; }
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];
}
+ ConstIterator begin() const { return characters(); }
+ ConstIterator end() const { return begin() + length(); }
+
bool starts_with(const StringView&) const;
bool ends_with(const StringView&) const;
bool starts_with(char) const;
@@ -302,5 +306,5 @@ String escape_html_entities(const StringView& html);
}
using AK::CaseInsensitiveStringTraits;
-using AK::String;
using AK::escape_html_entities;
+using AK::String;