Ninja
string_piece.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NINJA_STRINGPIECE_H_
16 #define NINJA_STRINGPIECE_H_
17 
18 #include <string>
19 
20 #include <string.h>
21 
22 /// StringPiece represents a slice of a string whose memory is managed
23 /// externally. It is useful for reducing the number of std::strings
24 /// we need to allocate.
25 struct StringPiece {
26  typedef const char* const_iterator;
27 
28  StringPiece() : str_(NULL), len_(0) {}
29 
30  /// The constructors intentionally allow for implicit conversions.
31  StringPiece(const std::string& str) : str_(str.data()), len_(str.size()) {}
32  StringPiece(const char* str) : str_(str), len_(strlen(str)) {}
33 
34  StringPiece(const char* str, size_t len) : str_(str), len_(len) {}
35 
36  bool operator==(const StringPiece& other) const {
37  return len_ == other.len_ && memcmp(str_, other.str_, len_) == 0;
38  }
39 
40  bool operator!=(const StringPiece& other) const {
41  return !(*this == other);
42  }
43 
44  /// Convert the slice into a full-fledged std::string, copying the
45  /// data into a new string.
46  std::string AsString() const {
47  return len_ ? std::string(str_, len_) : std::string();
48  }
49 
51  return str_;
52  }
53 
54  const_iterator end() const {
55  return str_ + len_;
56  }
57 
58  char operator[](size_t pos) const {
59  return str_[pos];
60  }
61 
62  size_t size() const {
63  return len_;
64  }
65 
66  size_t empty() const {
67  return len_ == 0;
68  }
69 
70  const char* str_;
71  size_t len_;
72 };
73 
74 #endif // NINJA_STRINGPIECE_H_
StringPiece represents a slice of a string whose memory is managed externally.
Definition: string_piece.h:25
const char * str_
Definition: string_piece.h:70
bool operator!=(const StringPiece &other) const
Definition: string_piece.h:40
StringPiece(const char *str)
Definition: string_piece.h:32
size_t empty() const
Definition: string_piece.h:66
size_t len_
Definition: string_piece.h:71
StringPiece(const char *str, size_t len)
Definition: string_piece.h:34
const_iterator end() const
Definition: string_piece.h:54
char operator[](size_t pos) const
Definition: string_piece.h:58
size_t size() const
Definition: string_piece.h:62
bool operator==(const StringPiece &other) const
Definition: string_piece.h:36
std::string AsString() const
Convert the slice into a full-fledged std::string, copying the data into a new string.
Definition: string_piece.h:46
StringPiece(const std::string &str)
The constructors intentionally allow for implicit conversions.
Definition: string_piece.h:31
const_iterator begin() const
Definition: string_piece.h:50
const char * const_iterator
Definition: string_piece.h:26