StringView.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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, bool keep_empty) 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 || keep_empty)
  26. v.append(substring_view(substart, sublen));
  27. substart = i + 1;
  28. }
  29. }
  30. ssize_t taillen = length() - substart;
  31. if (taillen != 0 || keep_empty)
  32. v.append(substring_view(substart, taillen));
  33. if (characters_without_null_termination()[length() - 1] == separator && keep_empty)
  34. v.append(String::empty());
  35. return v;
  36. }
  37. Vector<StringView> StringView::lines(bool consider_cr) const
  38. {
  39. if (is_empty())
  40. return {};
  41. if (!consider_cr)
  42. return split_view('\n', true);
  43. Vector<StringView> v;
  44. ssize_t substart = 0;
  45. bool last_ch_was_cr = false;
  46. bool split_view = false;
  47. for (ssize_t i = 0; i < length(); ++i) {
  48. char ch = characters_without_null_termination()[i];
  49. if (ch == '\n') {
  50. split_view = true;
  51. if (last_ch_was_cr) {
  52. substart = i + 1;
  53. split_view = false;
  54. last_ch_was_cr = false;
  55. }
  56. }
  57. if (ch == '\r') {
  58. split_view = true;
  59. last_ch_was_cr = true;
  60. }
  61. if (split_view) {
  62. ssize_t sublen = i - substart;
  63. v.append(substring_view(substart, sublen));
  64. substart = i + 1;
  65. }
  66. split_view = false;
  67. }
  68. ssize_t taillen = length() - substart;
  69. if (taillen != 0)
  70. v.append(substring_view(substart, taillen));
  71. return v;
  72. }
  73. bool StringView::starts_with(const StringView& str) const
  74. {
  75. if (str.is_empty())
  76. return true;
  77. if (is_empty())
  78. return false;
  79. if (str.length() > length())
  80. return false;
  81. if (characters_without_null_termination() == str.characters_without_null_termination())
  82. return true;
  83. return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length());
  84. }
  85. StringView StringView::substring_view(int start, int length) const
  86. {
  87. if (!length)
  88. return {};
  89. ASSERT(start + length <= m_length);
  90. return { m_characters + start, length };
  91. }
  92. StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
  93. {
  94. const char* remaining_characters = substring.characters_without_null_termination();
  95. ASSERT(remaining_characters >= m_characters);
  96. ASSERT(remaining_characters <= m_characters + m_length);
  97. int remaining_length = m_length - (remaining_characters - m_characters);
  98. return { remaining_characters, remaining_length };
  99. }
  100. StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
  101. {
  102. const char* remaining_characters = substring.characters_without_null_termination() + substring.length();
  103. ASSERT(remaining_characters >= m_characters);
  104. ASSERT(remaining_characters <= m_characters + m_length);
  105. int remaining_length = m_length - (remaining_characters - m_characters);
  106. return { remaining_characters, remaining_length };
  107. }
  108. int StringView::to_int(bool& ok) const
  109. {
  110. bool negative = false;
  111. int value = 0;
  112. int i = 0;
  113. if (is_empty()) {
  114. ok = false;
  115. return 0;
  116. }
  117. if (characters_without_null_termination()[0] == '-') {
  118. i++;
  119. negative = true;
  120. }
  121. for (; i < length(); i++) {
  122. if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
  123. ok = false;
  124. return 0;
  125. }
  126. value = value * 10;
  127. value += characters_without_null_termination()[i] - '0';
  128. }
  129. ok = true;
  130. return negative ? -value : value;
  131. }
  132. unsigned StringView::to_uint(bool& ok) const
  133. {
  134. unsigned value = 0;
  135. for (ssize_t i = 0; i < length(); ++i) {
  136. if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
  137. ok = false;
  138. return 0;
  139. }
  140. value = value * 10;
  141. value += characters_without_null_termination()[i] - '0';
  142. }
  143. ok = true;
  144. return value;
  145. }
  146. unsigned StringView::hash() const
  147. {
  148. if (is_empty())
  149. return 0;
  150. if (m_impl)
  151. return m_impl->hash();
  152. return string_hash(characters_without_null_termination(), length());
  153. }
  154. }