2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-03-22 12:12:55 +03:00
|
|
|
#include <AK/Badge.h>
|
2019-09-13 15:37:25 +03:00
|
|
|
#include <AK/RefCounted.h>
|
2020-03-10 11:13:29 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2020-07-27 15:15:37 +03:00
|
|
|
#include <AK/Span.h>
|
2019-06-18 10:26:36 +03:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/kmalloc.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-07 18:13:23 +03:00
|
|
|
enum ShouldChomp {
|
2019-05-28 12:53:16 +03:00
|
|
|
NoChomp,
|
|
|
|
Chomp
|
|
|
|
};
|
2018-11-07 02:19:35 +03:00
|
|
|
|
2019-06-21 16:29:31 +03:00
|
|
|
class StringImpl : public RefCounted<StringImpl> {
|
2018-10-10 12:53:07 +03:00
|
|
|
public:
|
2019-12-09 19:45:40 +03:00
|
|
|
static NonnullRefPtr<StringImpl> create_uninitialized(size_t length, char*& buffer);
|
2019-06-21 19:37:47 +03:00
|
|
|
static RefPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
|
2019-12-09 19:45:40 +03:00
|
|
|
static RefPtr<StringImpl> create(const char* cstring, size_t length, ShouldChomp = NoChomp);
|
2020-08-05 11:37:34 +03:00
|
|
|
static RefPtr<StringImpl> create(ReadonlyBytes, ShouldChomp = NoChomp);
|
2019-06-21 19:37:47 +03:00
|
|
|
NonnullRefPtr<StringImpl> to_lowercase() const;
|
|
|
|
NonnullRefPtr<StringImpl> to_uppercase() const;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-06-18 10:26:36 +03:00
|
|
|
void operator delete(void* ptr)
|
|
|
|
{
|
|
|
|
kfree(ptr);
|
|
|
|
}
|
|
|
|
|
2018-12-21 04:10:45 +03:00
|
|
|
static StringImpl& the_empty_stringimpl();
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
~StringImpl();
|
|
|
|
|
2019-12-09 19:45:40 +03:00
|
|
|
size_t length() const { return m_length; }
|
2020-08-23 13:56:46 +03:00
|
|
|
// Includes NUL-terminator.
|
2019-06-20 14:21:56 +03:00
|
|
|
const char* characters() const { return &m_inline_buffer[0]; }
|
2020-07-27 15:15:37 +03:00
|
|
|
|
|
|
|
ALWAYS_INLINE ReadonlyBytes bytes() const { return { characters(), length() }; }
|
|
|
|
|
2020-03-10 11:13:29 +03:00
|
|
|
const char& operator[](size_t i) const
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
2019-12-09 19:45:40 +03:00
|
|
|
ASSERT(i < m_length);
|
2019-06-20 14:21:56 +03:00
|
|
|
return characters()[i];
|
2019-05-28 12:53:16 +03:00
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
unsigned hash() const
|
|
|
|
{
|
2019-06-20 14:21:56 +03:00
|
|
|
if (!m_has_hash)
|
2018-12-21 04:10:45 +03:00
|
|
|
compute_hash();
|
2018-10-10 12:53:07 +03:00
|
|
|
return m_hash;
|
|
|
|
}
|
|
|
|
|
2020-04-13 13:05:19 +03:00
|
|
|
unsigned existing_hash() const
|
|
|
|
{
|
|
|
|
return m_hash;
|
|
|
|
}
|
|
|
|
|
2020-03-22 12:12:55 +03:00
|
|
|
bool is_fly() const { return m_fly; }
|
|
|
|
void set_fly(Badge<FlyString>, bool fly) const { m_fly = fly; }
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
private:
|
2019-06-07 18:13:23 +03:00
|
|
|
enum ConstructTheEmptyStringImplTag {
|
2019-05-28 12:53:16 +03:00
|
|
|
ConstructTheEmptyStringImpl
|
|
|
|
};
|
|
|
|
explicit StringImpl(ConstructTheEmptyStringImplTag)
|
2020-03-22 12:12:55 +03:00
|
|
|
: m_fly(true)
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
2019-06-20 14:21:56 +03:00
|
|
|
m_inline_buffer[0] = '\0';
|
2019-05-28 12:53:16 +03:00
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-06-07 18:13:23 +03:00
|
|
|
enum ConstructWithInlineBufferTag {
|
2019-05-28 12:53:16 +03:00
|
|
|
ConstructWithInlineBuffer
|
|
|
|
};
|
2019-12-09 19:45:40 +03:00
|
|
|
StringImpl(ConstructWithInlineBufferTag, size_t length);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-21 04:10:45 +03:00
|
|
|
void compute_hash() const;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-12-09 19:45:40 +03:00
|
|
|
size_t m_length { 0 };
|
2018-10-10 12:53:07 +03:00
|
|
|
mutable unsigned m_hash { 0 };
|
2019-06-20 14:21:56 +03:00
|
|
|
mutable bool m_has_hash { false };
|
2020-03-22 12:12:55 +03:00
|
|
|
mutable bool m_fly { false };
|
2018-12-21 04:10:45 +03:00
|
|
|
char m_inline_buffer[0];
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
2019-12-09 19:45:40 +03:00
|
|
|
inline constexpr u32 string_hash(const char* characters, size_t length)
|
2019-03-12 02:56:33 +03:00
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 hash = 0;
|
2019-12-09 19:45:40 +03:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2019-07-03 22:17:35 +03:00
|
|
|
hash += (u32)characters[i];
|
2019-03-12 02:56:33 +03:00
|
|
|
hash += (hash << 10);
|
|
|
|
hash ^= (hash >> 6);
|
|
|
|
}
|
|
|
|
hash += hash << 3;
|
|
|
|
hash ^= hash >> 11;
|
|
|
|
hash += hash << 15;
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-07 02:19:35 +03:00
|
|
|
using AK::Chomp;
|
2019-03-12 02:56:33 +03:00
|
|
|
using AK::string_hash;
|
2019-05-28 12:53:16 +03:00
|
|
|
using AK::StringImpl;
|