Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shared_string.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
24 
25 #ifndef PAGESPEED_KERNEL_BASE_SHARED_STRING_H_
26 #define PAGESPEED_KERNEL_BASE_SHARED_STRING_H_
27 
28 #include <cstddef>
29 
30 #include "base/logging.h"
34 
35 namespace net_instaweb {
36 
40 class SharedString {
41  public:
42  SharedString();
43 
44  explicit SharedString(const StringPiece& str);
45 
49  explicit SharedString(const GoogleString& str);
50 
55  explicit SharedString(const char* str);
56 
59  SharedString(const SharedString& src);
60  SharedString& operator=(const SharedString& src);
61 
66  StringPiece Value() const;
67 
75  void Assign(StringPiece str) { Assign(str.data(), str.size()); }
76  void Assign(const char* data, int size);
77 
90  void Append(StringPiece str) { Append(str.data(), str.size()); }
91  void Append(const char* data, size_t size);
92 
104  void Extend(int new_size);
105 
108  void SwapWithString(GoogleString* str);
109 
112  void DetachAndClear();
113 
117  void RemovePrefix(int n);
118 
122  void RemoveSuffix(int n);
123 
125  int size() const { return size_; }
126  bool empty() const { return size_ == 0; }
127  const char* data() const { return ref_string_->data() + skip_; }
128 
133  void WriteAt(int dest_offset, const char* source, int count);
134 
138  if (!unique()) {
139  *this = SharedString(Value());
140  DCHECK(unique());
141  }
142  }
143 
146  bool unique() const { return ref_string_.unique(); }
147 
151  bool trimmed() const {
152  return size_ != static_cast<int>(ref_string_->size());
153  }
154 
166  const GoogleString* StringValue() const {
167  return ref_string_.get();
168  }
169 
171  bool SharesStorage(const SharedString& that) const {
172  return ref_string_.get() == that.ref_string_.get();
173  }
174 
175  private:
176  void UniquifyIfTruncated();
177  char* mutable_data() { return &(*ref_string_.get())[0] + skip_; }
178  void ClearIfShared() {
179  if (!unique()) {
180  DetachAndClear();
181  }
182  }
183 
184  RefCountedObj<GoogleString> ref_string_;
185 
186  int skip_;
187  int size_;
188 };
189 
190 }
191 
192 #endif
void Assign(StringPiece str)
Definition: shared_string.h:75
void Append(StringPiece str)
Definition: shared_string.h:90
void SwapWithString(GoogleString *str)
void WriteAt(int dest_offset, const char *source, int count)
bool unique() const
Definition: shared_string.h:146
const GoogleString * StringValue() const
Definition: shared_string.h:166
StringPiece Value() const
bool unique() const
Definition: ref_counted_ptr.h:170
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
void DetachRetainingContent()
Definition: shared_string.h:137
bool trimmed() const
Definition: shared_string.h:151
bool SharesStorage(const SharedString &that) const
Determines whether this and that share the same storage.
Definition: shared_string.h:171
Definition: shared_string.h:40
void Extend(int new_size)
int size() const
Computes the size, taking into account any removed prefix or suffix.
Definition: shared_string.h:125