StringImpl.cpp 3.7 KB

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