str_split.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/strings/str_split.h"
  15. #include <algorithm>
  16. #include <cstddef>
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include "absl/base/config.h"
  20. #include "absl/base/internal/raw_logging.h"
  21. #include "absl/strings/string_view.h"
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace {
  25. // This GenericFind() template function encapsulates the finding algorithm
  26. // shared between the ByString and ByAnyChar delimiters. The FindPolicy
  27. // template parameter allows each delimiter to customize the actual find
  28. // function to use and the length of the found delimiter. For example, the
  29. // Literal delimiter will ultimately use absl::string_view::find(), and the
  30. // AnyOf delimiter will use absl::string_view::find_first_of().
  31. template <typename FindPolicy>
  32. absl::string_view GenericFind(absl::string_view text,
  33. absl::string_view delimiter, size_t pos,
  34. FindPolicy find_policy) {
  35. if (delimiter.empty() && text.length() > 0) {
  36. // Special case for empty string delimiters: always return a zero-length
  37. // absl::string_view referring to the item at position 1 past pos.
  38. return absl::string_view(text.data() + pos + 1, 0);
  39. }
  40. size_t found_pos = absl::string_view::npos;
  41. absl::string_view found(text.data() + text.size(),
  42. 0); // By default, not found
  43. found_pos = find_policy.Find(text, delimiter, pos);
  44. if (found_pos != absl::string_view::npos) {
  45. found = absl::string_view(text.data() + found_pos,
  46. find_policy.Length(delimiter));
  47. }
  48. return found;
  49. }
  50. // Finds using absl::string_view::find(), therefore the length of the found
  51. // delimiter is delimiter.length().
  52. struct LiteralPolicy {
  53. static size_t Find(absl::string_view text, absl::string_view delimiter,
  54. size_t pos) {
  55. return text.find(delimiter, pos);
  56. }
  57. static size_t Length(absl::string_view delimiter) {
  58. return delimiter.length();
  59. }
  60. };
  61. // Finds using absl::string_view::find_first_of(), therefore the length of the
  62. // found delimiter is 1.
  63. struct AnyOfPolicy {
  64. static size_t Find(absl::string_view text, absl::string_view delimiter,
  65. size_t pos) {
  66. return text.find_first_of(delimiter, pos);
  67. }
  68. static size_t Length(absl::string_view /* delimiter */) { return 1; }
  69. };
  70. } // namespace
  71. //
  72. // ByString
  73. //
  74. ByString::ByString(absl::string_view sp) : delimiter_(sp) {}
  75. absl::string_view ByString::Find(absl::string_view text, size_t pos) const {
  76. if (delimiter_.length() == 1) {
  77. // Much faster to call find on a single character than on an
  78. // absl::string_view.
  79. size_t found_pos = text.find(delimiter_[0], pos);
  80. if (found_pos == absl::string_view::npos)
  81. return absl::string_view(text.data() + text.size(), 0);
  82. return text.substr(found_pos, 1);
  83. }
  84. return GenericFind(text, delimiter_, pos, LiteralPolicy());
  85. }
  86. absl::string_view ByAsciiWhitespace::Find(absl::string_view text,
  87. size_t pos) const {
  88. return GenericFind(text, " \t\v\f\r\n", pos, AnyOfPolicy());
  89. }
  90. //
  91. // ByChar
  92. //
  93. absl::string_view ByChar::Find(absl::string_view text, size_t pos) const {
  94. size_t found_pos = text.find(c_, pos);
  95. if (found_pos == absl::string_view::npos)
  96. return absl::string_view(text.data() + text.size(), 0);
  97. return text.substr(found_pos, 1);
  98. }
  99. //
  100. // ByAnyChar
  101. //
  102. ByAnyChar::ByAnyChar(absl::string_view sp) : delimiters_(sp) {}
  103. absl::string_view ByAnyChar::Find(absl::string_view text, size_t pos) const {
  104. return GenericFind(text, delimiters_, pos, AnyOfPolicy());
  105. }
  106. //
  107. // ByLength
  108. //
  109. ByLength::ByLength(ptrdiff_t length) : length_(length) {
  110. ABSL_RAW_CHECK(length > 0, "");
  111. }
  112. absl::string_view ByLength::Find(absl::string_view text, size_t pos) const {
  113. pos = std::min(pos, text.size()); // truncate `pos`
  114. absl::string_view substr = text.substr(pos);
  115. // If the string is shorter than the chunk size we say we
  116. // "can't find the delimiter" so this will be the last chunk.
  117. if (substr.length() <= static_cast<size_t>(length_))
  118. return absl::string_view(text.data() + text.size(), 0);
  119. return absl::string_view(substr.data() + length_, 0);
  120. }
  121. ABSL_NAMESPACE_END
  122. } // namespace absl