string_view.h 3.5 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. 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. public:
  27. static constexpr size_t npos = static_cast<size_t>(-1);
  28. static_assert(APT::StringView::npos == std::string::npos, "npos values are different");
  29. /* Constructors */
  30. constexpr StringView() : data_(""), size_(0) {}
  31. constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {}
  32. StringView(const char *data) : data_(data), size_(strlen(data)) {}
  33. StringView(std::string const & str): data_(str.data()), size_(str.size()) {}
  34. /* Viewers */
  35. constexpr StringView substr(size_t pos, size_t n = npos) const {
  36. return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n);
  37. }
  38. size_t find(int c, size_t pos) const {
  39. if (pos == 0)
  40. return find(c);
  41. size_t const found = substr(pos).find(c);
  42. if (found == npos)
  43. return npos;
  44. return pos + found;
  45. }
  46. size_t find(int c) const {
  47. const char *found = static_cast<const char*>(memchr(data_, c, size_));
  48. if (found == NULL)
  49. return npos;
  50. return found - data_;
  51. }
  52. size_t rfind(int c, size_t pos) const {
  53. if (pos == npos)
  54. return rfind(c);
  55. return APT::StringView(data_, pos).rfind(c);
  56. }
  57. size_t rfind(int c) const {
  58. const char *found = static_cast<const char*>(memrchr(data_, c, size_));
  59. if (found == NULL)
  60. return npos;
  61. return found - data_;
  62. }
  63. /* Conversions */
  64. std::string to_string() const {
  65. return std::string(data_, size_);
  66. }
  67. /* Comparisons */
  68. int compare(size_t pos, size_t n, StringView other) const {
  69. return substr(pos, n).compare(other);
  70. }
  71. int compare(StringView other) const {
  72. int res;
  73. res = memcmp(data_, other.data_, std::min(size_, other.size_));
  74. if (res != 0)
  75. return res;
  76. if (size_ == other.size_)
  77. return res;
  78. return (size_ > other.size_) ? 1 : -1;
  79. }
  80. /* Optimization: If size not equal, string cannot be equal */
  81. bool operator ==(StringView other) const { return size_ == other.size_ && compare(other) == 0; }
  82. bool operator !=(StringView other) const { return !(*this == other); }
  83. /* Accessors */
  84. constexpr bool empty() const { return size_ == 0; }
  85. constexpr const char* data() const { return data_; }
  86. constexpr const char* begin() const { return data_; }
  87. constexpr const char* end() const { return data_ + size_; }
  88. constexpr char operator [](size_t i) const { return data_[i]; }
  89. constexpr size_t size() const { return size_; }
  90. constexpr size_t length() const { return size_; }
  91. };
  92. }
  93. inline bool operator ==(const char *other, APT::StringView that);
  94. inline bool operator ==(const char *other, APT::StringView that) { return that.operator==(other); }
  95. #endif