string_view.h 3.6 KB

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