string_view.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <apt-pkg/macros.h>
  16. namespace APT {
  17. /**
  18. * \brief Simple subset of std::string_view from C++17
  19. *
  20. * This is an internal implementation of the subset of std::string_view
  21. * used by APT. It is not meant to be used in programs, only inside the
  22. * library for performance critical paths.
  23. */
  24. class APT_HIDDEN StringView {
  25. const char *data_;
  26. size_t size_;
  27. public:
  28. static constexpr size_t npos = static_cast<size_t>(-1);
  29. static_assert(APT::StringView::npos == std::string::npos, "npos values are different");
  30. /* Constructors */
  31. constexpr StringView() : data_(""), size_(0) {}
  32. constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {}
  33. StringView(const char *data) : data_(data), size_(strlen(data)) {}
  34. StringView(std::string const & str): data_(str.data()), size_(str.size()) {}
  35. /* Viewers */
  36. constexpr StringView substr(size_t pos, size_t n = npos) const {
  37. return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n);
  38. }
  39. size_t find(int c, size_t pos) const {
  40. if (pos == 0)
  41. return find(c);
  42. size_t const found = substr(pos).find(c);
  43. if (found == npos)
  44. return npos;
  45. return pos + found;
  46. }
  47. size_t find(int c) const {
  48. const char *found = static_cast<const char*>(memchr(data_, c, size_));
  49. if (found == NULL)
  50. return npos;
  51. return found - data_;
  52. }
  53. size_t rfind(int c, size_t pos) const {
  54. if (pos == npos)
  55. return rfind(c);
  56. return APT::StringView(data_, pos).rfind(c);
  57. }
  58. size_t rfind(int c) const {
  59. const char *found = static_cast<const char*>(memrchr(data_, c, size_));
  60. if (found == NULL)
  61. return npos;
  62. return found - data_;
  63. }
  64. /* Conversions */
  65. std::string to_string() const {
  66. return std::string(data_, size_);
  67. }
  68. /* Comparisons */
  69. int compare(size_t pos, size_t n, StringView other) const {
  70. return substr(pos, n).compare(other);
  71. }
  72. int compare(StringView other) const {
  73. int res;
  74. res = memcmp(data_, other.data_, std::min(size_, other.size_));
  75. if (res != 0)
  76. return res;
  77. if (size_ == other.size_)
  78. return res;
  79. return (size_ > other.size_) ? 1 : -1;
  80. }
  81. /* Optimization: If size not equal, string cannot be equal */
  82. bool operator ==(StringView other) const { return size_ == other.size_ && compare(other) == 0; }
  83. bool operator !=(StringView other) const { return !(*this == other); }
  84. /* Accessors */
  85. constexpr bool empty() const { return size_ == 0; }
  86. constexpr const char* data() const { return data_; }
  87. constexpr const char* begin() const { return data_; }
  88. constexpr const char* end() const { return data_ + size_; }
  89. constexpr char operator [](size_t i) const { return data_[i]; }
  90. constexpr size_t size() const { return size_; }
  91. constexpr size_t length() const { return size_; }
  92. };
  93. }
  94. inline bool operator ==(const char *other, APT::StringView that);
  95. inline bool operator ==(const char *other, APT::StringView that) { return that.operator==(other); }
  96. #endif