Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(534)

Side by Side Diff: net/der/input.h

Issue 1573243011: Refactor der::Input helper methods into new constructors (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add documentation Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_DER_INPUT_H_ 5 #ifndef NET_DER_INPUT_H_
6 #define NET_DER_INPUT_H_ 6 #define NET_DER_INPUT_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
11 #include <string> 11 #include <string>
12 12
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/strings/string_piece.h"
14 #include "net/base/net_export.h" 15 #include "net/base/net_export.h"
15 16
16 namespace net { 17 namespace net {
17 18
18 namespace der { 19 namespace der {
19 20
20 class Mark; 21 class Mark;
21 22
22 // An opaque class that represents a fixed buffer of data of a fixed length, 23 // An opaque class that represents a fixed buffer of data of a fixed length,
23 // to be used as an input to other operations. An Input object does not own 24 // to be used as an input to other operations. An Input object does not own
(...skipping 20 matching lines...) Expand all
44 Input(); 45 Input();
45 46
46 // Creates an Input from a constant array |data|. 47 // Creates an Input from a constant array |data|.
47 template <size_t N> 48 template <size_t N>
48 explicit Input(const uint8_t(&data)[N]) 49 explicit Input(const uint8_t(&data)[N])
49 : data_(data), len_(N) {} 50 : data_(data), len_(N) {}
50 51
51 // Creates an Input from the given |data| and |len|. 52 // Creates an Input from the given |data| and |len|.
52 Input(const uint8_t* data, size_t len); 53 Input(const uint8_t* data, size_t len);
53 54
55 // Creates an Input from a base::StringPiece.
56 Input(const base::StringPiece& sp);
eroman 2016/01/13 22:54:00 Mark as explicit
nharper 2016/01/13 23:04:47 Done.
57
58 // Creates an Input from a std::string. The lifetimes are a bit subtle when
59 // using this function: The constructed Input is only valid so long as |s| is
60 // still alive and not mutated.
61 Input(const std::string* s);
eroman 2016/01/13 22:54:00 explicit
nharper 2016/01/13 23:04:48 Done.
62
54 // Returns the length in bytes of an Input's data. 63 // Returns the length in bytes of an Input's data.
55 size_t Length() const { return len_; } 64 size_t Length() const { return len_; }
56 65
57 // Return true if the Input's data and |other|'s data are byte-wise equal. 66 // Return true if the Input's data and |other|'s data are byte-wise equal.
58 bool Equals(const Input& other) const; 67 bool Equals(const Input& other) const;
59 68
60 // Returns a pointer to the Input's data. This method is marked as "unsafe" 69 // Returns a pointer to the Input's data. This method is marked as "unsafe"
61 // because access to the Input's data should be done through ByteReader 70 // because access to the Input's data should be done through ByteReader
62 // instead. This method should only be used where using a ByteReader truly 71 // instead. This method should only be used where using a ByteReader truly
63 // is not an option. 72 // is not an option.
64 const uint8_t* UnsafeData() const { return data_; } 73 const uint8_t* UnsafeData() const { return data_; }
65 74
66 // Returns a copy of the data represented by this object as a std::string. 75 // Returns a copy of the data represented by this object as a std::string.
67 std::string AsString() const; 76 std::string AsString() const;
68 77
78 // Returns a StringPiece pointing to the same data as the Input. The resulting
79 // StringPiece cannot outlive the data that was used to construct this Input.
eroman 2016/01/13 22:54:00 nit: cannot --> must not
nharper 2016/01/13 23:04:48 Done.
80 base::StringPiece AsStringPiece() const;
81
69 private: 82 private:
83 // This constructor is deleted to prevent constructing an Input from a
84 // std::string r-value. Since the Input points to memory owned by another
85 // object, such an Input would point to invalid memory. Without this deleted
86 // constructor, a std::string could be passed in to the base::StringPiece
87 // constructor because of StringPiece's implicit constructor.
88 Input(std::string) = delete;
eroman 2016/01/13 22:54:00 Does this need to be const std::string& ?
nharper 2016/01/13 23:04:47 I tested the following, and they all failed to com
eroman 2016/01/13 23:21:06 Sounds good. My only other test suggestions would
70 const uint8_t* data_; 89 const uint8_t* data_;
71 size_t len_; 90 size_t len_;
72 }; 91 };
73 92
74 // Returns true if |lhs|'s data is lexicographically less than |rhs|'s data. 93 // Returns true if |lhs|'s data is lexicographically less than |rhs|'s data.
75 NET_EXPORT_PRIVATE bool operator<(const Input& lhs, const Input& rhs); 94 NET_EXPORT_PRIVATE bool operator<(const Input& lhs, const Input& rhs);
76 95
77 // This class provides ways to read data from an Input in a bounds-checked way. 96 // This class provides ways to read data from an Input in a bounds-checked way.
78 // The ByteReader is designed to read through the input sequentially. Once a 97 // The ByteReader is designed to read through the input sequentially. Once a
79 // byte has been read with a ByteReader, the caller can't go back and re-read 98 // byte has been read with a ByteReader, the caller can't go back and re-read
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 explicit Mark(const uint8_t* ptr); 175 explicit Mark(const uint8_t* ptr);
157 Mark(); 176 Mark();
158 const uint8_t* ptr_; 177 const uint8_t* ptr_;
159 }; 178 };
160 179
161 } // namespace der 180 } // namespace der
162 181
163 } // namespace net 182 } // namespace net
164 183
165 #endif // NET_DER_INPUT_H_ 184 #endif // NET_DER_INPUT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698