FlyString.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/FlyString.h>
  7. #include <AK/HashTable.h>
  8. #include <AK/Optional.h>
  9. #include <AK/Singleton.h>
  10. #include <AK/String.h>
  11. #include <AK/StringUtils.h>
  12. #include <AK/StringView.h>
  13. namespace AK {
  14. struct FlyStringImplTraits : public Traits<StringImpl*> {
  15. static unsigned hash(const StringImpl* s) { return s ? s->hash() : 0; }
  16. static bool equals(const StringImpl* a, const StringImpl* b)
  17. {
  18. VERIFY(a);
  19. VERIFY(b);
  20. return *a == *b;
  21. }
  22. };
  23. static Singleton<HashTable<StringImpl*, FlyStringImplTraits>> s_table;
  24. static HashTable<StringImpl*, FlyStringImplTraits>& fly_impls()
  25. {
  26. return *s_table;
  27. }
  28. void FlyString::did_destroy_impl(Badge<StringImpl>, StringImpl& impl)
  29. {
  30. fly_impls().remove(&impl);
  31. }
  32. FlyString::FlyString(const String& string)
  33. {
  34. if (string.is_null())
  35. return;
  36. if (string.impl()->is_fly()) {
  37. m_impl = string.impl();
  38. return;
  39. }
  40. auto it = fly_impls().find(const_cast<StringImpl*>(string.impl()));
  41. if (it == fly_impls().end()) {
  42. fly_impls().set(const_cast<StringImpl*>(string.impl()));
  43. string.impl()->set_fly({}, true);
  44. m_impl = string.impl();
  45. } else {
  46. VERIFY((*it)->is_fly());
  47. m_impl = *it;
  48. }
  49. }
  50. FlyString::FlyString(StringView string)
  51. {
  52. if (string.is_null())
  53. return;
  54. auto it = fly_impls().find(string.hash(), [&](auto& candidate) {
  55. return string == candidate;
  56. });
  57. if (it == fly_impls().end()) {
  58. auto new_string = string.to_string();
  59. fly_impls().set(new_string.impl());
  60. new_string.impl()->set_fly({}, true);
  61. m_impl = new_string.impl();
  62. } else {
  63. VERIFY((*it)->is_fly());
  64. m_impl = *it;
  65. }
  66. }
  67. template<typename T>
  68. Optional<T> FlyString::to_int(TrimWhitespace trim_whitespace) const
  69. {
  70. return StringUtils::convert_to_int<T>(view(), trim_whitespace);
  71. }
  72. template Optional<i8> FlyString::to_int(TrimWhitespace) const;
  73. template Optional<i16> FlyString::to_int(TrimWhitespace) const;
  74. template Optional<i32> FlyString::to_int(TrimWhitespace) const;
  75. template Optional<i64> FlyString::to_int(TrimWhitespace) const;
  76. template<typename T>
  77. Optional<T> FlyString::to_uint(TrimWhitespace trim_whitespace) const
  78. {
  79. return StringUtils::convert_to_uint<T>(view(), trim_whitespace);
  80. }
  81. template Optional<u8> FlyString::to_uint(TrimWhitespace) const;
  82. template Optional<u16> FlyString::to_uint(TrimWhitespace) const;
  83. template Optional<u32> FlyString::to_uint(TrimWhitespace) const;
  84. template Optional<u64> FlyString::to_uint(TrimWhitespace) const;
  85. bool FlyString::equals_ignoring_case(StringView other) const
  86. {
  87. return StringUtils::equals_ignoring_case(view(), other);
  88. }
  89. bool FlyString::starts_with(StringView str, CaseSensitivity case_sensitivity) const
  90. {
  91. return StringUtils::starts_with(view(), str, case_sensitivity);
  92. }
  93. bool FlyString::ends_with(StringView str, CaseSensitivity case_sensitivity) const
  94. {
  95. return StringUtils::ends_with(view(), str, case_sensitivity);
  96. }
  97. FlyString FlyString::to_lowercase() const
  98. {
  99. return String(*m_impl).to_lowercase();
  100. }
  101. bool FlyString::operator==(const String& other) const
  102. {
  103. if (m_impl == other.impl())
  104. return true;
  105. if (!m_impl)
  106. return !other.impl();
  107. if (!other.impl())
  108. return false;
  109. if (length() != other.length())
  110. return false;
  111. return !__builtin_memcmp(characters(), other.characters(), length());
  112. }
  113. bool FlyString::operator==(StringView string) const
  114. {
  115. return *this == String(string);
  116. }
  117. bool FlyString::operator==(const char* string) const
  118. {
  119. if (is_null())
  120. return !string;
  121. if (!string)
  122. return false;
  123. return !__builtin_strcmp(m_impl->characters(), string);
  124. }
  125. }