pcre_stringpiece.h 6.3 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 <string.h>
  40. #include <string>
  41. #include <iosfwd> // for ostream forward-declaration
  42. // Don't use bits/type_traits.h on Linux - Andrew Galante, GG 8/2/2009
  43. #if !defined(_MSC_VER) && !defined(__CELLOS_LV2__) && !defined(__APPLE__) && !defined(__linux__)
  44. #ifdef __MINGW32__
  45. #define HAVE_TYPE_TRAITS
  46. #include <bits/type_traits.h>
  47. #else
  48. #define HAVE_TYPE_TRAITS
  49. #include <type_traits.h>
  50. #endif
  51. #endif
  52. #include <pcre.h>
  53. using std::string;
  54. namespace pcrecpp {
  55. class PCRECPP_EXP_DEFN StringPiece {
  56. private:
  57. const char* ptr_;
  58. int length_;
  59. public:
  60. // We provide non-explicit singleton constructors so users can pass
  61. // in a "const char*" or a "string" wherever a "StringPiece" is
  62. // expected.
  63. StringPiece()
  64. : ptr_(NULL), length_(0) { }
  65. StringPiece(const char* str)
  66. : ptr_(str), length_(static_cast<int>(strlen(ptr_))) { }
  67. StringPiece(const unsigned char* str)
  68. : ptr_(reinterpret_cast<const char*>(str)),
  69. length_(static_cast<int>(strlen(ptr_))) { }
  70. StringPiece(const string& str)
  71. : ptr_(str.data()), length_(static_cast<int>(str.size())) { }
  72. StringPiece(const char* offset, int len)
  73. : ptr_(offset), length_(len) { }
  74. // data() may return a pointer to a buffer with embedded NULs, and the
  75. // returned buffer may or may not be null terminated. Therefore it is
  76. // typically a mistake to pass data() to a routine that expects a NUL
  77. // terminated string. Use "as_string().c_str()" if you really need to do
  78. // this. Or better yet, change your routine so it does not rely on NUL
  79. // termination.
  80. const char* data() const { return ptr_; }
  81. int size() const { return length_; }
  82. bool empty() const { return length_ == 0; }
  83. void clear() { ptr_ = NULL; length_ = 0; }
  84. void set(const char* buffer, int len) { ptr_ = buffer; length_ = len; }
  85. void set(const char* str) {
  86. ptr_ = str;
  87. length_ = static_cast<int>(strlen(str));
  88. }
  89. void set(const void* buffer, int len) {
  90. ptr_ = reinterpret_cast<const char*>(buffer);
  91. length_ = len;
  92. }
  93. char operator[](int i) const { return ptr_[i]; }
  94. void remove_prefix(int n) {
  95. ptr_ += n;
  96. length_ -= n;
  97. }
  98. void remove_suffix(int n) {
  99. length_ -= n;
  100. }
  101. bool operator==(const StringPiece& x) const {
  102. return ((length_ == x.length_) &&
  103. (memcmp(ptr_, x.ptr_, length_) == 0));
  104. }
  105. bool operator!=(const StringPiece& x) const {
  106. return !(*this == x);
  107. }
  108. #define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp) \
  109. bool operator cmp (const StringPiece& x) const { \
  110. int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); \
  111. return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_))); \
  112. }
  113. STRINGPIECE_BINARY_PREDICATE(<, <);
  114. STRINGPIECE_BINARY_PREDICATE(<=, <);
  115. STRINGPIECE_BINARY_PREDICATE(>=, >);
  116. STRINGPIECE_BINARY_PREDICATE(>, >);
  117. #undef STRINGPIECE_BINARY_PREDICATE
  118. int compare(const StringPiece& x) const {
  119. int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);
  120. if (r == 0) {
  121. if (length_ < x.length_) r = -1;
  122. else if (length_ > x.length_) r = +1;
  123. }
  124. return r;
  125. }
  126. string as_string() const {
  127. return string(data(), size());
  128. }
  129. void CopyToString(string* target) const {
  130. target->assign(ptr_, length_);
  131. }
  132. // Does "this" start with "x"
  133. bool starts_with(const StringPiece& x) const {
  134. return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
  135. }
  136. };
  137. } // namespace pcrecpp
  138. // ------------------------------------------------------------------
  139. // Functions used to create STL containers that use StringPiece
  140. // Remember that a StringPiece's lifetime had better be less than
  141. // that of the underlying string or char*. If it is not, then you
  142. // cannot safely store a StringPiece into an STL container
  143. // ------------------------------------------------------------------
  144. #ifdef HAVE_TYPE_TRAITS
  145. // This makes vector<StringPiece> really fast for some STL implementations
  146. template<> struct __type_traits<pcrecpp::StringPiece> {
  147. typedef __true_type has_trivial_default_constructor;
  148. typedef __true_type has_trivial_copy_constructor;
  149. typedef __true_type has_trivial_assignment_operator;
  150. typedef __true_type has_trivial_destructor;
  151. typedef __true_type is_POD_type;
  152. };
  153. #endif
  154. // allow StringPiece to be logged
  155. std::ostream& operator<<(std::ostream& o, const pcrecpp::StringPiece& piece);
  156. #endif /* _PCRE_STRINGPIECE_H */