|
Packit |
78a954 |
// Copyright (c) 2005, Google Inc.
|
|
Packit |
78a954 |
// All rights reserved.
|
|
Packit |
78a954 |
//
|
|
Packit |
78a954 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
78a954 |
// modification, are permitted provided that the following conditions are
|
|
Packit |
78a954 |
// met:
|
|
Packit |
78a954 |
//
|
|
Packit |
78a954 |
// * Redistributions of source code must retain the above copyright
|
|
Packit |
78a954 |
// notice, this list of conditions and the following disclaimer.
|
|
Packit |
78a954 |
// * Redistributions in binary form must reproduce the above
|
|
Packit |
78a954 |
// copyright notice, this list of conditions and the following disclaimer
|
|
Packit |
78a954 |
// in the documentation and/or other materials provided with the
|
|
Packit |
78a954 |
// distribution.
|
|
Packit |
78a954 |
// * Neither the name of Google Inc. nor the names of its
|
|
Packit |
78a954 |
// contributors may be used to endorse or promote products derived from
|
|
Packit |
78a954 |
// this software without specific prior written permission.
|
|
Packit |
78a954 |
//
|
|
Packit |
78a954 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
78a954 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
78a954 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit |
78a954 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit |
78a954 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit |
78a954 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit |
78a954 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit |
78a954 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit |
78a954 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit |
78a954 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit |
78a954 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
78a954 |
//
|
|
Packit |
78a954 |
// Author: Sanjay Ghemawat
|
|
Packit |
78a954 |
//
|
|
Packit |
78a954 |
// A string like object that points into another piece of memory.
|
|
Packit |
78a954 |
// Useful for providing an interface that allows clients to easily
|
|
Packit |
78a954 |
// pass in either a "const char*" or a "string".
|
|
Packit |
78a954 |
//
|
|
Packit |
78a954 |
// Arghh! I wish C++ literals were automatically of type "string".
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
#ifndef _PCRE_STRINGPIECE_H
|
|
Packit |
78a954 |
#define _PCRE_STRINGPIECE_H
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
#include <cstring>
|
|
Packit |
78a954 |
#include <string>
|
|
Packit |
78a954 |
#include <iosfwd> // for ostream forward-declaration
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
#if @pcre_have_type_traits@
|
|
Packit |
78a954 |
#define HAVE_TYPE_TRAITS
|
|
Packit |
78a954 |
#include <type_traits.h>
|
|
Packit |
78a954 |
#elif @pcre_have_bits_type_traits@
|
|
Packit |
78a954 |
#define HAVE_TYPE_TRAITS
|
|
Packit |
78a954 |
#include <bits/type_traits.h>
|
|
Packit |
78a954 |
#endif
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
#include <pcre.h>
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
using std::memcmp;
|
|
Packit |
78a954 |
using std::strlen;
|
|
Packit |
78a954 |
using std::string;
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
namespace pcrecpp {
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
class PCRECPP_EXP_DEFN StringPiece {
|
|
Packit |
78a954 |
private:
|
|
Packit |
78a954 |
const char* ptr_;
|
|
Packit |
78a954 |
int length_;
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
public:
|
|
Packit |
78a954 |
// We provide non-explicit singleton constructors so users can pass
|
|
Packit |
78a954 |
// in a "const char*" or a "string" wherever a "StringPiece" is
|
|
Packit |
78a954 |
// expected.
|
|
Packit |
78a954 |
StringPiece()
|
|
Packit |
78a954 |
: ptr_(NULL), length_(0) { }
|
|
Packit |
78a954 |
StringPiece(const char* str)
|
|
Packit |
78a954 |
: ptr_(str), length_(static_cast<int>(strlen(ptr_))) { }
|
|
Packit |
78a954 |
StringPiece(const unsigned char* str)
|
|
Packit |
78a954 |
: ptr_(reinterpret_cast<const char*>(str)),
|
|
Packit |
78a954 |
length_(static_cast<int>(strlen(ptr_))) { }
|
|
Packit |
78a954 |
StringPiece(const string& str)
|
|
Packit |
78a954 |
: ptr_(str.data()), length_(static_cast<int>(str.size())) { }
|
|
Packit |
78a954 |
StringPiece(const char* offset, int len)
|
|
Packit |
78a954 |
: ptr_(offset), length_(len) { }
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
// data() may return a pointer to a buffer with embedded NULs, and the
|
|
Packit |
78a954 |
// returned buffer may or may not be null terminated. Therefore it is
|
|
Packit |
78a954 |
// typically a mistake to pass data() to a routine that expects a NUL
|
|
Packit |
78a954 |
// terminated string. Use "as_string().c_str()" if you really need to do
|
|
Packit |
78a954 |
// this. Or better yet, change your routine so it does not rely on NUL
|
|
Packit |
78a954 |
// termination.
|
|
Packit |
78a954 |
const char* data() const { return ptr_; }
|
|
Packit |
78a954 |
int size() const { return length_; }
|
|
Packit |
78a954 |
bool empty() const { return length_ == 0; }
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
void clear() { ptr_ = NULL; length_ = 0; }
|
|
Packit |
78a954 |
void set(const char* buffer, int len) { ptr_ = buffer; length_ = len; }
|
|
Packit |
78a954 |
void set(const char* str) {
|
|
Packit |
78a954 |
ptr_ = str;
|
|
Packit |
78a954 |
length_ = static_cast<int>(strlen(str));
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
void set(const void* buffer, int len) {
|
|
Packit |
78a954 |
ptr_ = reinterpret_cast<const char*>(buffer);
|
|
Packit |
78a954 |
length_ = len;
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
char operator[](int i) const { return ptr_[i]; }
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
void remove_prefix(int n) {
|
|
Packit |
78a954 |
ptr_ += n;
|
|
Packit |
78a954 |
length_ -= n;
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
void remove_suffix(int n) {
|
|
Packit |
78a954 |
length_ -= n;
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
bool operator==(const StringPiece& x) const {
|
|
Packit |
78a954 |
return ((length_ == x.length_) &&
|
|
Packit |
78a954 |
(memcmp(ptr_, x.ptr_, length_) == 0));
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
bool operator!=(const StringPiece& x) const {
|
|
Packit |
78a954 |
return !(*this == x);
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
#define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp) \
|
|
Packit |
78a954 |
bool operator cmp (const StringPiece& x) const { \
|
|
Packit |
78a954 |
int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_); \
|
|
Packit |
78a954 |
return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_))); \
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
STRINGPIECE_BINARY_PREDICATE(<, <);
|
|
Packit |
78a954 |
STRINGPIECE_BINARY_PREDICATE(<=, <);
|
|
Packit |
78a954 |
STRINGPIECE_BINARY_PREDICATE(>=, >);
|
|
Packit |
78a954 |
STRINGPIECE_BINARY_PREDICATE(>, >);
|
|
Packit |
78a954 |
#undef STRINGPIECE_BINARY_PREDICATE
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
int compare(const StringPiece& x) const {
|
|
Packit |
78a954 |
int r = memcmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);
|
|
Packit |
78a954 |
if (r == 0) {
|
|
Packit |
78a954 |
if (length_ < x.length_) r = -1;
|
|
Packit |
78a954 |
else if (length_ > x.length_) r = +1;
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
return r;
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
string as_string() const {
|
|
Packit |
78a954 |
return string(data(), size());
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
void CopyToString(string* target) const {
|
|
Packit |
78a954 |
target->assign(ptr_, length_);
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
// Does "this" start with "x"
|
|
Packit |
78a954 |
bool starts_with(const StringPiece& x) const {
|
|
Packit |
78a954 |
return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
|
|
Packit |
78a954 |
}
|
|
Packit |
78a954 |
};
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
} // namespace pcrecpp
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
// ------------------------------------------------------------------
|
|
Packit |
78a954 |
// Functions used to create STL containers that use StringPiece
|
|
Packit |
78a954 |
// Remember that a StringPiece's lifetime had better be less than
|
|
Packit |
78a954 |
// that of the underlying string or char*. If it is not, then you
|
|
Packit |
78a954 |
// cannot safely store a StringPiece into an STL container
|
|
Packit |
78a954 |
// ------------------------------------------------------------------
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
#ifdef HAVE_TYPE_TRAITS
|
|
Packit |
78a954 |
// This makes vector<StringPiece> really fast for some STL implementations
|
|
Packit |
78a954 |
template<> struct __type_traits<pcrecpp::StringPiece> {
|
|
Packit |
78a954 |
typedef __true_type has_trivial_default_constructor;
|
|
Packit |
78a954 |
typedef __true_type has_trivial_copy_constructor;
|
|
Packit |
78a954 |
typedef __true_type has_trivial_assignment_operator;
|
|
Packit |
78a954 |
typedef __true_type has_trivial_destructor;
|
|
Packit |
78a954 |
typedef __true_type is_POD_type;
|
|
Packit |
78a954 |
};
|
|
Packit |
78a954 |
#endif
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
// allow StringPiece to be logged
|
|
Packit |
78a954 |
PCRECPP_EXP_DECL std::ostream& operator<<(std::ostream& o,
|
|
Packit |
78a954 |
const pcrecpp::StringPiece& piece);
|
|
Packit |
78a954 |
|
|
Packit |
78a954 |
#endif /* _PCRE_STRINGPIECE_H */
|