Serenity Operating System
at master 147 lines 3.9 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/CharacterTypes.h> 8#include <AK/DeprecatedFlyString.h> 9#include <AK/StringHash.h> 10#include <AK/StringImpl.h> 11#include <AK/kmalloc.h> 12 13namespace AK { 14 15static StringImpl* s_the_empty_stringimpl = nullptr; 16 17StringImpl& StringImpl::the_empty_stringimpl() 18{ 19 if (!s_the_empty_stringimpl) { 20 void* slot = kmalloc(sizeof(StringImpl) + sizeof(char)); 21 s_the_empty_stringimpl = new (slot) StringImpl(ConstructTheEmptyStringImpl); 22 } 23 return *s_the_empty_stringimpl; 24} 25 26StringImpl::StringImpl(ConstructWithInlineBufferTag, size_t length) 27 : m_length(length) 28{ 29} 30 31StringImpl::~StringImpl() 32{ 33 if (m_fly) 34 DeprecatedFlyString::did_destroy_impl({}, *this); 35} 36 37NonnullRefPtr<StringImpl const> StringImpl::create_uninitialized(size_t length, char*& buffer) 38{ 39 VERIFY(length); 40 void* slot = kmalloc(allocation_size_for_stringimpl(length)); 41 VERIFY(slot); 42 auto new_stringimpl = adopt_ref(*new (slot) StringImpl(ConstructWithInlineBuffer, length)); 43 buffer = const_cast<char*>(new_stringimpl->characters()); 44 buffer[length] = '\0'; 45 return new_stringimpl; 46} 47 48RefPtr<StringImpl const> StringImpl::create(char const* cstring, size_t length, ShouldChomp should_chomp) 49{ 50 if (!cstring) 51 return nullptr; 52 53 if (should_chomp) { 54 while (length) { 55 char last_ch = cstring[length - 1]; 56 if (!last_ch || last_ch == '\n' || last_ch == '\r') 57 --length; 58 else 59 break; 60 } 61 } 62 63 if (!length) 64 return the_empty_stringimpl(); 65 66 char* buffer; 67 auto new_stringimpl = create_uninitialized(length, buffer); 68 memcpy(buffer, cstring, length * sizeof(char)); 69 70 return new_stringimpl; 71} 72 73RefPtr<StringImpl const> StringImpl::create(char const* cstring, ShouldChomp shouldChomp) 74{ 75 if (!cstring) 76 return nullptr; 77 78 if (!*cstring) 79 return the_empty_stringimpl(); 80 81 return create(cstring, strlen(cstring), shouldChomp); 82} 83 84RefPtr<StringImpl const> StringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp) 85{ 86 return StringImpl::create(reinterpret_cast<char const*>(bytes.data()), bytes.size(), shouldChomp); 87} 88 89RefPtr<StringImpl const> StringImpl::create_lowercased(char const* cstring, size_t length) 90{ 91 if (!cstring) 92 return nullptr; 93 if (!length) 94 return the_empty_stringimpl(); 95 char* buffer; 96 auto impl = create_uninitialized(length, buffer); 97 for (size_t i = 0; i < length; ++i) 98 buffer[i] = (char)to_ascii_lowercase(cstring[i]); 99 return impl; 100} 101 102RefPtr<StringImpl const> StringImpl::create_uppercased(char const* cstring, size_t length) 103{ 104 if (!cstring) 105 return nullptr; 106 if (!length) 107 return the_empty_stringimpl(); 108 char* buffer; 109 auto impl = create_uninitialized(length, buffer); 110 for (size_t i = 0; i < length; ++i) 111 buffer[i] = (char)to_ascii_uppercase(cstring[i]); 112 return impl; 113} 114 115NonnullRefPtr<StringImpl const> StringImpl::to_lowercase() const 116{ 117 for (size_t i = 0; i < m_length; ++i) { 118 if (is_ascii_upper_alpha(characters()[i])) 119 return create_lowercased(characters(), m_length).release_nonnull(); 120 } 121 return const_cast<StringImpl&>(*this); 122} 123 124NonnullRefPtr<StringImpl const> StringImpl::to_uppercase() const 125{ 126 for (size_t i = 0; i < m_length; ++i) { 127 if (is_ascii_lower_alpha(characters()[i])) 128 return create_uppercased(characters(), m_length).release_nonnull(); 129 } 130 return const_cast<StringImpl&>(*this); 131} 132 133unsigned StringImpl::case_insensitive_hash() const 134{ 135 return case_insensitive_string_hash(characters(), length()); 136} 137 138void StringImpl::compute_hash() const 139{ 140 if (!length()) 141 m_hash = 0; 142 else 143 m_hash = string_hash(characters(), m_length); 144 m_has_hash = true; 145} 146 147}