pcre_stringpiece.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: Sanjay Ghemawat
  31. //
  32. // A string like object that points into another piece of memory.
  33. // Useful for providing an interface that allows clients to easily
  34. // pass in either a "const char*" or a "string".
  35. //
  36. // Arghh! I wish C++ literals were automatically of type "string".
  37. #ifndef _PCRE_STRINGPIECE_H
  38. #define _PCRE_STRINGPIECE_H
  39. #include <cstring>
  40. #include <string>
  41. #include <iosfwd> // for ostream forward-declaration
  42. #if 0
  43. #define HAVE_TYPE_TRAITS
  44. #include <type_traits.h>
  45. #elif 0
  46. #define HAVE_TYPE_TRAITS
  47. #error #include <bits/type_traits.h>
  48. #endif
  49. #include "pcre.h"
  50. namespace pcrecpp {
  51. using std::memcmp;
  52. using std::strlen;
  53. using std::string;
  54. class PCRECPP_EXP_DEFN StringPiece {
  55. private:
  56. const char* ptr_;
  57. int length_;
  58. public:
  59. // We provide non-explicit singleton constructors so users can pass
  60. // in a "const char*" or a "string" wherever a "StringPiece" is
  61. // expected.
  62. StringPiece()
  63. : ptr_(NULL), length_(0) { }
  64. StringPiece(const char* str)
  65. : ptr_(str), length_(static_cast<int>(strlen(ptr_))) { }
  66. StringPiece(const unsigned char* str)
  67. : ptr_(reinterpret_cast<const char*>(str)),
  68. length_(static_cast<int>(strlen(ptr_))) { }
  69. StringPiece(const string& str)
  70. : ptr_(str.data()), length_(static_cast<int>(str.size())) { }
  71. StringPiece(const char* offset, int len)
  72. : ptr_(offset), length_(len) { }
  73. // data() may return a pointer to a buffer with embedded NULs, and the
  74. // returned buffer may or may not be null terminated. Therefore it is
  75. // typically a mistake to pass data() to a routine that expects a NUL
  76. // terminated string. Use "as_string().c_str()" if you really need to do
  77. // this. Or better yet, change your routine so it does not rely on NUL
  78. // termination.
  79. const char* data() const { return ptr_; }
  80. int size() const { return length_; }
  81. bool empty() const { return length_ == 0; }
  82. void clear() { ptr_ = NULL; length_ = 0; }
  83. void set(const char* buffer, int len) { ptr_ = buffer; length_ = len; }
  84. void set(const char* str) {
  85. ptr_ = str;
  86. length_ = static_cast<int>(strlen(str));
  87. }
  88. void set(const void* buffer, int len) {
  89. ptr_ = reinterpret_cast<const char*>(buffer);
  90. length_ = len;
  91. }
  92. char operator[](int i) const { return ptr_[i]; }
  93. void remove_prefix(int n) {
  94. ptr_ += n;
  95. length_ -= n;
  96. }
  97. void remove_suffix(int n) {
  98. length_ -= n;
  99. }
  100. bool operator==(const StringPiece& x) const {
  101. return ((length_ == x.length_) &&
  102. (memcmp(ptr_, x.ptr_, length_) == 0));
  103. }
  104. bool operator!=(const StringPiece& x) const {
  105. return !(*this == x);
  106. }
  107. #define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp) \
  108. bool operator cmp (const StringPiece& x) const { \
  109. int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); \
  110. return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_))); \
  111. }
  112. STRINGPIECE_BINARY_PREDICATE(<, <);
  113. STRINGPIECE_BINARY_PREDICATE(<=, <);
  114. STRINGPIECE_BINARY_PREDICATE(>=, >);
  115. STRINGPIECE_BINARY_PREDICATE(>, >);
  116. #undef STRINGPIECE_BINARY_PREDICATE
  117. int compare(const StringPiece& x) const {
  118. int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);
  119. if (r == 0) {
  120. if (length_ < x.length_) r = -1;
  121. else if (length_ > x.length_) r = +1;
  122. }
  123. return r;
  124. }
  125. string as_string() const {
  126. return string(data(), size());
  127. }
  128. void CopyToString(string* target) const {
  129. target->assign(ptr_, length_);
  130. }
  131. // Does "this" start with "x"
  132. bool starts_with(const StringPiece& x) const {
  133. return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
  134. }
  135. };
  136. } // namespace pcrecpp
  137. // ------------------------------------------------------------------
  138. // Functions used to create STL containers that use StringPiece
  139. // Remember that a StringPiece's lifetime had better be less than
  140. // that of the underlying string or char*. If it is not, then you
  141. // cannot safely store a StringPiece into an STL container
  142. // ------------------------------------------------------------------
  143. #ifdef HAVE_TYPE_TRAITS
  144. // This makes vector<StringPiece> really fast for some STL implementations
  145. template<> struct __type_traits<pcrecpp::StringPiece> {
  146. typedef __true_type has_trivial_default_constructor;
  147. typedef __true_type has_trivial_copy_constructor;
  148. typedef __true_type has_trivial_assignment_operator;
  149. typedef __true_type has_trivial_destructor;
  150. typedef __true_type is_POD_type;
  151. };
  152. #endif
  153. // allow StringPiece to be logged
  154. PCRECPP_EXP_DECL std::ostream& operator<<(std::ostream& o,
  155. const pcrecpp::StringPiece& piece);
  156. #endif /* _PCRE_STRINGPIECE_H */