~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/base/shared_string.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
// Author: jmarantz@google.com (Joshua Marantz)
18
 
//
19
 
// Implements a ref-counted string class, with full sharing.  This
20
 
// class does *not* implement copy-on-write semantics, however, it
21
 
// does support a unique() method for determining, prior to writing,
22
 
// whether other references exist.  Thus it is feasible to implement
23
 
// copy-on-write as a layer over this class.
24
 
 
25
 
#ifndef PAGESPEED_KERNEL_BASE_SHARED_STRING_H_
26
 
#define PAGESPEED_KERNEL_BASE_SHARED_STRING_H_
27
 
 
28
 
#include <cstddef>                     // for size_t
29
 
 
30
 
#include "base/logging.h"
31
 
#include "pagespeed/kernel/base/string.h"
32
 
#include "pagespeed/kernel/base/string_util.h"
33
 
#include "pagespeed/kernel/base/ref_counted_ptr.h"
34
 
 
35
 
namespace net_instaweb {
36
 
 
37
 
// Reference-counted string.  This class allows for shared underlying
38
 
// storage with other SharedString instances, but for trimming a
39
 
// SharedString instance's view of it via RemoveSuffix() and RemovePrefix().
40
 
class SharedString {
41
 
 public:
42
 
  SharedString();
43
 
 
44
 
  explicit SharedString(const StringPiece& str);
45
 
 
46
 
  // When constructing with a GoogleString, going through the StringPiece
47
 
  // ctor above causes an extra copy compared with string implementations that
48
 
  // use copy-on-write.  So we make an explicit GoogleString constructor.
49
 
  explicit SharedString(const GoogleString& str);
50
 
 
51
 
  // Given the two constructors above, it is ambiguous which one gets
52
 
  // called when passed a string-literal, so making an explicit const char*
53
 
  // constructor eliminates the ambiguity.  This is likely beneficial mostly
54
 
  // for tests.
55
 
  explicit SharedString(const char* str);
56
 
 
57
 
  // Storage-sharing occurs as a result of the copy-constructor and
58
 
  // assignment operator.
59
 
  SharedString(const SharedString& src);
60
 
  SharedString& operator=(const SharedString& src);
61
 
 
62
 
  // Returns the value as a StringPiece, taking into account any calls
63
 
  // to RemovePrefix, RemoveSuffix, and any string-mutations due to
64
 
  // Append or WriteAt on this or any other SharedStrings sharing
65
 
  // storage due to the assignment operator or operator=.
66
 
  StringPiece Value() const;
67
 
 
68
 
  // Resets SharedString to be a copy of str, erasing any previous
69
 
  // prefix/suffix.  Calling this function detaches any connected
70
 
  // SharedStrings.
71
 
  //
72
 
  // It is valid to assign from a value inside the SharedString.  In
73
 
  // other words, shared_string.Assign(shared_string.Value().substr(...))
74
 
  // will work.
75
 
  void Assign(StringPiece str) { Assign(str.data(), str.size()); }
76
 
  void Assign(const char* data, int size);
77
 
 
78
 
  // Appends a new string to the underlying storage.  Other SharedStrings will
79
 
  // not be affected by this mutation.
80
 
  //
81
 
  // This function tries to avoid detaching from other SharedStrings, and only
82
 
  // needs to do so if this has been truncated.
83
 
  //
84
 
  // Unlike Assign, it is invalid to append characters managed by this
85
 
  // SharedString.  In other words, shared_string.Append(shared_string.Value())
86
 
  // will fail.
87
 
  //
88
 
  // Note: Append() is not thread-safe.  Concurrent accesses to any
89
 
  // SharedStrings with the same storage will fail.
90
 
  void Append(StringPiece str) { Append(str.data(), str.size()); }
91
 
  void Append(const char* data, size_t size);
92
 
 
93
 
  // Makes the string representation at least 'new_size' large,
94
 
  // without specifying how new bytes should be filled in.  Typically
95
 
  // this will be followed by a call to WriteAt().
96
 
  //
97
 
  // This function does *not* detach other SharedStrings -- the
98
 
  // underlying storage will still be shared.  Consequently this
99
 
  // function does not shrink strings, as that could invalidate
100
 
  // trimmed SharedStrings sharing the storage.
101
 
  //
102
 
  // If this method is called on a truncated SharedString, then it will be
103
 
  // detached prior to extending it.
104
 
  void Extend(int new_size);
105
 
 
106
 
  // Swaps storage with the the passed-in string, detaching from any other
107
 
  // previously-linked SharedStrings.
108
 
  void SwapWithString(GoogleString* str);
109
 
 
110
 
  // Clears the contents of the string, and erases any removed prefix
111
 
  // or suffix, detaching from any other previously-linked SharedStrings.
112
 
  void DetachAndClear();
113
 
 
114
 
  // Removes the first n characters from the string.  Other linked
115
 
  // SharedStrings remain linked, but are unaffected by this removal
116
 
  // because each has its own skip_ and size_.
117
 
  void RemovePrefix(int n);
118
 
 
119
 
  // Removes the last n characters from the string.  Other linked
120
 
  // SharedStrings remain linked, but are unaffected by this removal
121
 
  // because each has its own skip_ and size_.
122
 
  void RemoveSuffix(int n);
123
 
 
124
 
  // Computes the size, taking into account any removed prefix or suffix.
125
 
  int size() const { return size_; }
126
 
  bool empty() const { return size_ == 0; }
127
 
  const char* data() const { return ref_string_->data() + skip_; }
128
 
 
129
 
  // WriteAt allows mutation of the underlying string data.  The
130
 
  // string must already be sized as needed via previous Append() or
131
 
  // Extend() calls.  Mutations done via this method will affect all
132
 
  // references to the underlying storage.
133
 
  void WriteAt(int dest_offset, const char* source, int count);
134
 
 
135
 
  // Disassociates this SharedString with any others that have linked
136
 
  // the same storage.  Retains the same string value.
137
 
  void DetachRetainingContent() {
138
 
    if (!unique()) {
139
 
      *this = SharedString(Value());
140
 
      DCHECK(unique());
141
 
    }
142
 
  }
143
 
 
144
 
  // Determines whether this SharedString shares storage from other
145
 
  // SharedStrings.
146
 
  bool unique() const { return ref_string_.unique(); }
147
 
 
148
 
  // Determines whether RemovePrefix or RemoveSuffix has every been called
149
 
  // on this SharedString.  Note that other SharedStrings sharing the
150
 
  // same storage as this may be trimmed differently.
151
 
  bool trimmed() const {
152
 
    return size_ != static_cast<int>(ref_string_->size());
153
 
  }
154
 
 
155
 
  // Returns back a GoogleString* representation for the contained value.
156
 
  //
157
 
  // This is makes sense to call only if the string is not trimmed.  If
158
 
  // RemovePrefix or RemoveSuffix has been called on this SharedString, the
159
 
  // returned string may have extra characters in it.
160
 
  //
161
 
  // Note: we suggest against using this routine.  It is better to consume
162
 
  // the data via the StringPiece returned from Value().
163
 
  //
164
 
  // This routine is, however, useful to call from tests to determine
165
 
  // storage uniqueness.
166
 
  const GoogleString* StringValue() const {
167
 
    return ref_string_.get();
168
 
  }
169
 
 
170
 
  // Determins whether this and that share the same storage.
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_;  // Number of bytes to skip at the beginning of the string.
187
 
  int size_;  // Number of bytes visible in the current view.
188
 
};
189
 
 
190
 
}  // namespace net_instaweb
191
 
 
192
 
#endif  // PAGESPEED_KERNEL_BASE_SHARED_STRING_H_