string_view.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*-
  2. * Copyright 2012-1017 Matthew Endsley
  3. * All rights reserved
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted providing that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef TINYSTL_STRING_VIEW_H
  27. #define TINYSTL_STRING_VIEW_H
  28. #include <tinystl/stddef.h>
  29. namespace tinystl {
  30. class string_view
  31. {
  32. public:
  33. typedef char value_type;
  34. typedef char* pointer;
  35. typedef const char* const_pointer;
  36. typedef char& reference;
  37. typedef const char& const_reference;
  38. typedef const_pointer iterator;
  39. typedef const_pointer const_iterator;
  40. typedef size_t size_type;
  41. typedef ptrdiff_t difference_type;
  42. static constexpr size_type npos = size_type(-1);
  43. constexpr string_view();
  44. constexpr string_view(const char* s, size_type count);
  45. constexpr string_view(const char* s);
  46. constexpr string_view(const string_view&) = default;
  47. string_view& operator=(const string_view&) = default;
  48. constexpr const char* data() const;
  49. constexpr char operator[](size_type pos) const;
  50. constexpr size_type size() const;
  51. constexpr bool empty() const;
  52. constexpr iterator begin() const;
  53. constexpr const_iterator cbegin() const;
  54. constexpr iterator end() const;
  55. constexpr const_iterator cend() const;
  56. constexpr string_view substr(size_type pos = 0, size_type count = npos) const;
  57. constexpr void swap(string_view& v);
  58. private:
  59. string_view(decltype(nullptr)) = delete;
  60. static constexpr size_type strlen(const char*);
  61. const char* m_str;
  62. size_type m_size;
  63. };
  64. constexpr string_view::string_view()
  65. : m_str(nullptr)
  66. , m_size(0)
  67. {
  68. }
  69. constexpr string_view::string_view(const char* s, size_type count)
  70. : m_str(s)
  71. , m_size(count)
  72. {
  73. }
  74. constexpr string_view::string_view(const char* s)
  75. : m_str(s)
  76. , m_size(strlen(s))
  77. {
  78. }
  79. constexpr const char* string_view::data() const {
  80. return m_str;
  81. }
  82. constexpr char string_view::operator[](size_type pos) const {
  83. return m_str[pos];
  84. }
  85. constexpr string_view::size_type string_view::size() const {
  86. return m_size;
  87. }
  88. constexpr bool string_view::empty() const {
  89. return 0 == m_size;
  90. }
  91. constexpr string_view::iterator string_view::begin() const {
  92. return m_str;
  93. }
  94. constexpr string_view::const_iterator string_view::cbegin() const {
  95. return m_str;
  96. }
  97. constexpr string_view::iterator string_view::end() const {
  98. return m_str + m_size;
  99. }
  100. constexpr string_view::const_iterator string_view::cend() const {
  101. return m_str + m_size;
  102. }
  103. constexpr string_view string_view::substr(size_type pos, size_type count) const {
  104. return string_view(m_str + pos, npos == count ? m_size - pos : count);
  105. }
  106. constexpr void string_view::swap(string_view& v) {
  107. const char* strtmp = m_str;
  108. size_type sizetmp = m_size;
  109. m_str = v.m_str;
  110. m_size = v.m_size;
  111. v.m_str = strtmp;
  112. v.m_size = sizetmp;
  113. }
  114. constexpr string_view::size_type string_view::strlen(const char* s) {
  115. for (size_t len = 0; ; ++len) {
  116. if (0 == s[len]) {
  117. return len;
  118. }
  119. }
  120. }
  121. }
  122. #endif // TINYSTL_STRING_VIEW_H