browser/components/translation/cld2/internal/stringpiece.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright 2013 Google Inc. All Rights Reserved.
michael@0 2 //
michael@0 3 // Licensed under the Apache License, Version 2.0 (the "License");
michael@0 4 // you may not use this file except in compliance with the License.
michael@0 5 // You may obtain a copy of the License at
michael@0 6 //
michael@0 7 // http://www.apache.org/licenses/LICENSE-2.0
michael@0 8 //
michael@0 9 // Unless required by applicable law or agreed to in writing, software
michael@0 10 // distributed under the License is distributed on an "AS IS" BASIS,
michael@0 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 12 // See the License for the specific language governing permissions and
michael@0 13 // limitations under the License.
michael@0 14
michael@0 15 //
michael@0 16 // A StringPiece points to part or all of a string, double-quoted string
michael@0 17 // literal, or other string-like object. A StringPiece does *not* own the
michael@0 18 // string to which it points. A StringPiece is not null-terminated. [subset]
michael@0 19 //
michael@0 20
michael@0 21 #ifndef STRINGS_STRINGPIECE_H_
michael@0 22 #define STRINGS_STRINGPIECE_H_
michael@0 23
michael@0 24 #include <string.h>
michael@0 25 #include <string>
michael@0 26
michael@0 27
michael@0 28 typedef int stringpiece_ssize_type;
michael@0 29
michael@0 30 class StringPiece {
michael@0 31 private:
michael@0 32 const char* ptr_;
michael@0 33 stringpiece_ssize_type length_;
michael@0 34
michael@0 35 public:
michael@0 36 // We provide non-explicit singleton constructors so users can pass
michael@0 37 // in a "const char*" or a "string" wherever a "StringPiece" is
michael@0 38 // expected.
michael@0 39 StringPiece() : ptr_(NULL), length_(0) {}
michael@0 40
michael@0 41 StringPiece(const char* str) // NOLINT(runtime/explicit)
michael@0 42 : ptr_(str), length_(0) {
michael@0 43 if (str != NULL) {
michael@0 44 length_ = strlen(str);
michael@0 45 }
michael@0 46 }
michael@0 47
michael@0 48 StringPiece(const std::string& str) // NOLINT(runtime/explicit)
michael@0 49 : ptr_(str.data()), length_(0) {
michael@0 50 length_ = str.size();
michael@0 51 }
michael@0 52
michael@0 53 StringPiece(const char* offset, stringpiece_ssize_type len)
michael@0 54 : ptr_(offset), length_(len) {
michael@0 55 }
michael@0 56
michael@0 57 void remove_prefix(stringpiece_ssize_type n) {
michael@0 58 ptr_ += n;
michael@0 59 length_ -= n;
michael@0 60 }
michael@0 61
michael@0 62 void remove_suffix(stringpiece_ssize_type n) {
michael@0 63 length_ -= n;
michael@0 64 }
michael@0 65
michael@0 66 // data() may return a pointer to a buffer with embedded NULs, and the
michael@0 67 // returned buffer may or may not be null terminated. Therefore it is
michael@0 68 // typically a mistake to pass data() to a routine that expects a NUL
michael@0 69 // terminated string.
michael@0 70 const char* data() const { return ptr_; }
michael@0 71 stringpiece_ssize_type size() const { return length_; }
michael@0 72 stringpiece_ssize_type length() const { return length_; }
michael@0 73 bool empty() const { return length_ == 0; }
michael@0 74 };
michael@0 75
michael@0 76 class StringPiece;
michael@0 77
michael@0 78 #endif // STRINGS_STRINGPIECE_H__

mercurial