StringView.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <AK/String.h>
  2. #include <AK/StringView.h>
  3. namespace AK {
  4. StringView::StringView(const String& string)
  5. : m_impl(string.impl())
  6. , m_characters(string.characters())
  7. , m_length(string.length())
  8. {
  9. }
  10. StringView::StringView(const ByteBuffer& buffer)
  11. : m_characters((const char*)buffer.data())
  12. , m_length(buffer.size())
  13. {
  14. }
  15. Vector<StringView> StringView::split_view(const char separator) const
  16. {
  17. if (is_empty())
  18. return {};
  19. Vector<StringView> v;
  20. ssize_t substart = 0;
  21. for (ssize_t i = 0; i < length(); ++i) {
  22. char ch = characters_without_null_termination()[i];
  23. if (ch == separator) {
  24. ssize_t sublen = i - substart;
  25. if (sublen != 0)
  26. v.append(substring_view(substart, sublen));
  27. substart = i + 1;
  28. }
  29. }
  30. ssize_t taillen = length() - substart;
  31. if (taillen != 0)
  32. v.append(substring_view(substart, taillen));
  33. if (characters_without_null_termination()[length() - 1] == separator)
  34. v.append(String::empty());
  35. return v;
  36. }
  37. bool StringView::starts_with(const StringView& str) const
  38. {
  39. if (str.is_empty())
  40. return true;
  41. if (is_empty())
  42. return false;
  43. if (str.length() > length())
  44. return false;
  45. if (characters_without_null_termination() == str.characters_without_null_termination())
  46. return true;
  47. return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length());
  48. }
  49. StringView StringView::substring_view(int start, int length) const
  50. {
  51. if (!length)
  52. return {};
  53. ASSERT(start + length <= m_length);
  54. return { m_characters + start, length };
  55. }
  56. StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
  57. {
  58. const char* remaining_characters = substring.characters_without_null_termination();
  59. ASSERT(remaining_characters >= m_characters);
  60. ASSERT(remaining_characters <= m_characters + m_length);
  61. int remaining_length = m_length - (remaining_characters - m_characters);
  62. return { remaining_characters, remaining_length };
  63. }
  64. StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
  65. {
  66. const char* remaining_characters = substring.characters_without_null_termination() + substring.length();
  67. ASSERT(remaining_characters >= m_characters);
  68. ASSERT(remaining_characters <= m_characters + m_length);
  69. int remaining_length = m_length - (remaining_characters - m_characters);
  70. return { remaining_characters, remaining_length };
  71. }
  72. int StringView::to_int(bool& ok) const
  73. {
  74. bool negative = false;
  75. int value = 0;
  76. int i = 0;
  77. if (is_empty()) {
  78. ok = false;
  79. return 0;
  80. }
  81. if (characters_without_null_termination()[0] == '-') {
  82. i++;
  83. negative = true;
  84. }
  85. for (; i < length(); i++) {
  86. if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
  87. ok = false;
  88. return 0;
  89. }
  90. value = value * 10;
  91. value += characters_without_null_termination()[i] - '0';
  92. }
  93. ok = true;
  94. return negative ? -value : value;
  95. }
  96. unsigned StringView::to_uint(bool& ok) const
  97. {
  98. unsigned value = 0;
  99. for (ssize_t i = 0; i < length(); ++i) {
  100. if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
  101. ok = false;
  102. return 0;
  103. }
  104. value = value * 10;
  105. value += characters_without_null_termination()[i] - '0';
  106. }
  107. ok = true;
  108. return value;
  109. }
  110. unsigned StringView::hash() const
  111. {
  112. if (is_empty())
  113. return 0;
  114. if (m_impl)
  115. return m_impl->hash();
  116. return string_hash(characters_without_null_termination(), length());
  117. }
  118. }