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