Page Speed Optimization Libraries
1.3.25.1
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00024 00025 #ifndef NET_INSTAWEB_UTIL_PUBLIC_SHARED_STRING_H_ 00026 #define NET_INSTAWEB_UTIL_PUBLIC_SHARED_STRING_H_ 00027 00028 #include <cstddef> 00029 00030 #include "base/logging.h" 00031 #include "net/instaweb/util/public/ref_counted_ptr.h" 00032 #include "net/instaweb/util/public/string.h" 00033 #include "net/instaweb/util/public/string_util.h" 00034 00035 namespace net_instaweb { 00036 00040 class SharedString { 00041 public: 00042 SharedString(); 00043 00044 explicit SharedString(const StringPiece& str); 00045 00049 explicit SharedString(const GoogleString& str); 00050 00055 explicit SharedString(const char* str); 00056 00059 SharedString(const SharedString& src); 00060 SharedString& operator=(const SharedString& src); 00061 00066 StringPiece Value() const; 00067 00075 void Assign(StringPiece str) { Assign(str.data(), str.size()); } 00076 void Assign(const char* data, int size); 00077 00090 void Append(StringPiece str) { Append(str.data(), str.size()); } 00091 void Append(const char* data, size_t size); 00092 00104 void Extend(int new_size); 00105 00108 void SwapWithString(GoogleString* str); 00109 00112 void DetachAndClear(); 00113 00117 void RemovePrefix(int n); 00118 00122 void RemoveSuffix(int n); 00123 00125 int size() const { return size_; } 00126 bool empty() const { return size_ == 0; } 00127 const char* data() const { return ref_string_->data() + skip_; } 00128 00133 void WriteAt(int dest_offset, const char* source, int count); 00134 00137 void DetachRetainingContent() { 00138 if (!unique()) { 00139 *this = SharedString(Value()); 00140 DCHECK(unique()); 00141 } 00142 } 00143 00146 bool unique() const { return ref_string_.unique(); } 00147 00151 bool trimmed() const { 00152 return size_ != static_cast<int>(ref_string_->size()); 00153 } 00154 00166 const GoogleString* StringValue() const { 00167 return ref_string_.get(); 00168 } 00169 00171 bool SharesStorage(const SharedString& that) const { 00172 return ref_string_.get() == that.ref_string_.get(); 00173 } 00174 00175 private: 00176 void UniquifyIfTruncated(); 00177 char* mutable_data() { return &(*ref_string_.get())[0] + skip_; } 00178 void ClearIfShared() { 00179 if (!unique()) { 00180 DetachAndClear(); 00181 } 00182 } 00183 00184 RefCountedObj<GoogleString> ref_string_; 00185 00186 int skip_; 00187 int size_; 00188 }; 00189 00190 } 00191 00192 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_SHARED_STRING_H_