string_view.h 3.3 KB

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