string_view.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Basic implementation of string_view
  3. *
  4. * (C) 2015 Julian Andres Klode <jak@debian.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #if !defined(APT_STRINGVIEW_H) && defined(APT_PKG_EXPOSE_STRING_VIEW)
  12. #define APT_STRINGVIEW_H
  13. #include <string.h>
  14. #include <string>
  15. namespace APT {
  16. /**
  17. * \brief Simple subset of std::string_view from C++17
  18. *
  19. * This is an internal implementation of the subset of std::string_view
  20. * used by APT. It is not meant to be used in programs, only inside the
  21. * library for performance critical paths.
  22. */
  23. class StringView {
  24. const char *data_;
  25. size_t size_;
  26. // without this little stunt the "char const *" overload is always preferred
  27. // over char[], but if we got a char[] we can deduct the length at compile time
  28. #define APT_T_IS_CHARPOINTER template<class T, typename std::enable_if<std::is_pointer<T>{} && !std::is_array<T>{}>::type* = nullptr>
  29. public:
  30. static constexpr size_t npos = static_cast<size_t>(-1);
  31. static_assert(APT::StringView::npos == std::string::npos, "npos values are different");
  32. /* Constructors */
  33. constexpr StringView() : data_(""), size_(0) {}
  34. constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {}
  35. template<size_t N> constexpr StringView(char const (&data)[N]) : StringView(data, N-1) {}
  36. APT_T_IS_CHARPOINTER StringView(T data) : data_(data), size_(strlen(data)) {}
  37. StringView(std::string const & str): data_(str.data()), size_(str.size()) {}
  38. /* Viewers */
  39. constexpr StringView substr(size_t pos, size_t n = npos) const {
  40. return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n);
  41. }
  42. size_t find(int c, size_t pos) const {
  43. if (pos == 0)
  44. return find(c);
  45. size_t const found = substr(pos).find(c);
  46. if (found == npos)
  47. return npos;
  48. return pos + found;
  49. }
  50. size_t find(int c) const {
  51. const char *found = static_cast<const char*>(memchr(data_, c, size_));
  52. if (found == NULL)
  53. return npos;
  54. return found - data_;
  55. }
  56. size_t rfind(int c, size_t pos) const {
  57. if (pos == npos)
  58. return rfind(c);
  59. return APT::StringView(data_, pos).rfind(c);
  60. }
  61. size_t rfind(int c) const {
  62. const char *found = static_cast<const char*>(memrchr(data_, c, size_));
  63. if (found == NULL)
  64. return npos;
  65. return found - data_;
  66. }
  67. /* Conversions */
  68. std::string to_string() const {
  69. return std::string(data_, size_);
  70. }
  71. /* Comparisons */
  72. int compare(size_t pos, size_t n, StringView other) const {
  73. return substr(pos, n).compare(other);
  74. }
  75. int compare(StringView other) const {
  76. int res;
  77. res = memcmp(data_, other.data_, std::min(size_, other.size_));
  78. if (res != 0)
  79. return res;
  80. if (size_ == other.size_)
  81. return res;
  82. return (size_ > other.size_) ? 1 : -1;
  83. }
  84. /* Optimization: If size not equal, string cannot be equal */
  85. bool operator ==(StringView other) const { return size_ == other.size_ && compare(other) == 0; }
  86. bool operator !=(StringView other) const { return !(*this == other); }
  87. /* Accessors */
  88. constexpr bool empty() const { return size_ == 0; }
  89. constexpr const char* data() const { return data_; }
  90. constexpr const char* begin() const { return data_; }
  91. constexpr const char* end() const { return data_ + size_; }
  92. constexpr char operator [](size_t i) const { return data_[i]; }
  93. constexpr size_t size() const { return size_; }
  94. constexpr size_t length() const { return size_; }
  95. };
  96. }
  97. template<size_t N> inline bool operator ==(char const (&other)[N], APT::StringView that) { return that.operator==(other); }
  98. APT_T_IS_CHARPOINTER inline bool operator ==(T other, APT::StringView that) { return that.operator==(other); }
  99. #undef APT_T_IS_CHARPOINTER
  100. #endif