FlyString.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/FlyString.h>
  27. #include <AK/HashTable.h>
  28. #include <AK/Optional.h>
  29. #include <AK/Singleton.h>
  30. #include <AK/String.h>
  31. #include <AK/StringUtils.h>
  32. #include <AK/StringView.h>
  33. namespace AK {
  34. struct FlyStringImplTraits : public AK::Traits<StringImpl*> {
  35. static unsigned hash(const StringImpl* s) { return s ? s->hash() : 0; }
  36. static bool equals(const StringImpl* a, const StringImpl* b)
  37. {
  38. VERIFY(a);
  39. VERIFY(b);
  40. return *a == *b;
  41. }
  42. };
  43. static AK::Singleton<HashTable<StringImpl*, FlyStringImplTraits>> s_table;
  44. static HashTable<StringImpl*, FlyStringImplTraits>& fly_impls()
  45. {
  46. return *s_table;
  47. }
  48. void FlyString::did_destroy_impl(Badge<StringImpl>, StringImpl& impl)
  49. {
  50. fly_impls().remove(&impl);
  51. }
  52. FlyString::FlyString(const String& string)
  53. {
  54. if (string.is_null())
  55. return;
  56. if (string.impl()->is_fly()) {
  57. m_impl = string.impl();
  58. return;
  59. }
  60. auto it = fly_impls().find(const_cast<StringImpl*>(string.impl()));
  61. if (it == fly_impls().end()) {
  62. fly_impls().set(const_cast<StringImpl*>(string.impl()));
  63. string.impl()->set_fly({}, true);
  64. m_impl = string.impl();
  65. } else {
  66. VERIFY((*it)->is_fly());
  67. m_impl = *it;
  68. }
  69. }
  70. FlyString::FlyString(const StringView& string)
  71. : FlyString(static_cast<String>(string))
  72. {
  73. }
  74. FlyString::FlyString(const char* string)
  75. : FlyString(static_cast<String>(string))
  76. {
  77. }
  78. template<typename T>
  79. Optional<T> FlyString::to_int() const
  80. {
  81. return StringUtils::convert_to_int<T>(view());
  82. }
  83. template Optional<i8> FlyString::to_int() const;
  84. template Optional<i16> FlyString::to_int() const;
  85. template Optional<i32> FlyString::to_int() const;
  86. template Optional<i64> FlyString::to_int() const;
  87. template<typename T>
  88. Optional<T> FlyString::to_uint() const
  89. {
  90. return StringUtils::convert_to_uint<T>(view());
  91. }
  92. template Optional<u8> FlyString::to_uint() const;
  93. template Optional<u16> FlyString::to_uint() const;
  94. template Optional<u32> FlyString::to_uint() const;
  95. template Optional<u64> FlyString::to_uint() const;
  96. bool FlyString::equals_ignoring_case(const StringView& other) const
  97. {
  98. return StringUtils::equals_ignoring_case(view(), other);
  99. }
  100. bool FlyString::starts_with(const StringView& str, CaseSensitivity case_sensitivity) const
  101. {
  102. return StringUtils::starts_with(view(), str, case_sensitivity);
  103. }
  104. bool FlyString::ends_with(const StringView& str, CaseSensitivity case_sensitivity) const
  105. {
  106. return StringUtils::ends_with(view(), str, case_sensitivity);
  107. }
  108. FlyString FlyString::to_lowercase() const
  109. {
  110. return String(*m_impl).to_lowercase();
  111. }
  112. StringView FlyString::view() const
  113. {
  114. return { characters(), length() };
  115. }
  116. bool FlyString::operator==(const String& other) const
  117. {
  118. if (m_impl == other.impl())
  119. return true;
  120. if (!m_impl)
  121. return !other.impl();
  122. if (!other.impl())
  123. return false;
  124. if (length() != other.length())
  125. return false;
  126. return !__builtin_memcmp(characters(), other.characters(), length());
  127. }
  128. bool FlyString::operator==(const StringView& string) const
  129. {
  130. return *this == String(string);
  131. }
  132. bool FlyString::operator==(const char* string) const
  133. {
  134. if (is_null())
  135. return !string;
  136. if (!string)
  137. return false;
  138. return !__builtin_strcmp(m_impl->characters(), string);
  139. }
  140. }