StringBase.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Badge.h>
  7. #include <AK/FlyString.h>
  8. #include <AK/StringBase.h>
  9. #include <AK/StringData.h>
  10. namespace AK::Detail {
  11. ReadonlyBytes ShortString::bytes() const
  12. {
  13. return { storage, byte_count() };
  14. }
  15. size_t ShortString::byte_count() const
  16. {
  17. return byte_count_and_short_string_flag >> 1;
  18. }
  19. StringBase::StringBase(NonnullRefPtr<Detail::StringData const> data)
  20. : m_data(&data.leak_ref())
  21. {
  22. }
  23. StringBase::StringBase(StringBase const& other)
  24. : m_data(other.m_data)
  25. {
  26. if (!is_short_string())
  27. m_data->ref();
  28. }
  29. StringBase& StringBase::operator=(StringBase&& other)
  30. {
  31. if (!is_short_string())
  32. m_data->unref();
  33. m_data = exchange(other.m_data, nullptr);
  34. other.m_short_string.byte_count_and_short_string_flag = SHORT_STRING_FLAG;
  35. return *this;
  36. }
  37. StringBase& StringBase::operator=(StringBase const& other)
  38. {
  39. if (&other != this) {
  40. if (!is_short_string())
  41. m_data->unref();
  42. m_data = other.m_data;
  43. if (!is_short_string())
  44. m_data->ref();
  45. }
  46. return *this;
  47. }
  48. ReadonlyBytes StringBase::bytes() const
  49. {
  50. if (is_short_string())
  51. return m_short_string.bytes();
  52. return m_data->bytes();
  53. }
  54. u32 StringBase::hash() const
  55. {
  56. if (is_short_string()) {
  57. auto bytes = this->bytes();
  58. return string_hash(reinterpret_cast<char const*>(bytes.data()), bytes.size());
  59. }
  60. return m_data->hash();
  61. }
  62. size_t StringBase::byte_count() const
  63. {
  64. if (is_short_string())
  65. return m_short_string.byte_count_and_short_string_flag >> 1;
  66. return m_data->byte_count();
  67. }
  68. bool StringBase::operator==(StringBase const& other) const
  69. {
  70. if (is_short_string())
  71. return m_data == other.m_data;
  72. if (other.is_short_string())
  73. return false;
  74. if (m_data->is_fly_string() && other.m_data->is_fly_string())
  75. return m_data == other.m_data;
  76. return bytes() == other.bytes();
  77. }
  78. void StringBase::did_create_fly_string(Badge<FlyString>) const
  79. {
  80. VERIFY(!is_short_string());
  81. m_data->set_fly_string(true);
  82. }
  83. ErrorOr<Bytes> StringBase::replace_with_uninitialized_buffer(size_t byte_count)
  84. {
  85. if (byte_count <= MAX_SHORT_STRING_BYTE_COUNT)
  86. return replace_with_uninitialized_short_string(byte_count);
  87. u8* buffer = nullptr;
  88. destroy_string();
  89. m_data = &TRY(StringData::create_uninitialized(byte_count, buffer)).leak_ref();
  90. return Bytes { buffer, byte_count };
  91. }
  92. ErrorOr<StringBase> StringBase::substring_from_byte_offset_with_shared_superstring(size_t start, size_t length) const
  93. {
  94. VERIFY(start + length <= byte_count());
  95. if (length == 0)
  96. return StringBase {};
  97. if (length <= MAX_SHORT_STRING_BYTE_COUNT) {
  98. StringBase result;
  99. bytes().slice(start, length).copy_to(result.replace_with_uninitialized_short_string(length));
  100. return result;
  101. }
  102. return StringBase { TRY(Detail::StringData::create_substring(*m_data, start, length)) };
  103. }
  104. void StringBase::destroy_string()
  105. {
  106. if (!is_short_string())
  107. m_data->unref();
  108. }
  109. }