1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/translation/cld2/internal/stringpiece.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,78 @@ 1.4 +// Copyright 2013 Google Inc. All Rights Reserved. 1.5 +// 1.6 +// Licensed under the Apache License, Version 2.0 (the "License"); 1.7 +// you may not use this file except in compliance with the License. 1.8 +// You may obtain a copy of the License at 1.9 +// 1.10 +// http://www.apache.org/licenses/LICENSE-2.0 1.11 +// 1.12 +// Unless required by applicable law or agreed to in writing, software 1.13 +// distributed under the License is distributed on an "AS IS" BASIS, 1.14 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.15 +// See the License for the specific language governing permissions and 1.16 +// limitations under the License. 1.17 + 1.18 +// 1.19 +// A StringPiece points to part or all of a string, double-quoted string 1.20 +// literal, or other string-like object. A StringPiece does *not* own the 1.21 +// string to which it points. A StringPiece is not null-terminated. [subset] 1.22 +// 1.23 + 1.24 +#ifndef STRINGS_STRINGPIECE_H_ 1.25 +#define STRINGS_STRINGPIECE_H_ 1.26 + 1.27 +#include <string.h> 1.28 +#include <string> 1.29 + 1.30 + 1.31 +typedef int stringpiece_ssize_type; 1.32 + 1.33 +class StringPiece { 1.34 + private: 1.35 + const char* ptr_; 1.36 + stringpiece_ssize_type length_; 1.37 + 1.38 + public: 1.39 + // We provide non-explicit singleton constructors so users can pass 1.40 + // in a "const char*" or a "string" wherever a "StringPiece" is 1.41 + // expected. 1.42 + StringPiece() : ptr_(NULL), length_(0) {} 1.43 + 1.44 + StringPiece(const char* str) // NOLINT(runtime/explicit) 1.45 + : ptr_(str), length_(0) { 1.46 + if (str != NULL) { 1.47 + length_ = strlen(str); 1.48 + } 1.49 + } 1.50 + 1.51 + StringPiece(const std::string& str) // NOLINT(runtime/explicit) 1.52 + : ptr_(str.data()), length_(0) { 1.53 + length_ = str.size(); 1.54 + } 1.55 + 1.56 + StringPiece(const char* offset, stringpiece_ssize_type len) 1.57 + : ptr_(offset), length_(len) { 1.58 + } 1.59 + 1.60 + void remove_prefix(stringpiece_ssize_type n) { 1.61 + ptr_ += n; 1.62 + length_ -= n; 1.63 + } 1.64 + 1.65 + void remove_suffix(stringpiece_ssize_type n) { 1.66 + length_ -= n; 1.67 + } 1.68 + 1.69 + // data() may return a pointer to a buffer with embedded NULs, and the 1.70 + // returned buffer may or may not be null terminated. Therefore it is 1.71 + // typically a mistake to pass data() to a routine that expects a NUL 1.72 + // terminated string. 1.73 + const char* data() const { return ptr_; } 1.74 + stringpiece_ssize_type size() const { return length_; } 1.75 + stringpiece_ssize_type length() const { return length_; } 1.76 + bool empty() const { return length_ == 0; } 1.77 +}; 1.78 + 1.79 +class StringPiece; 1.80 + 1.81 +#endif // STRINGS_STRINGPIECE_H__