44
44
#include " absl/strings/string_view.h"
45
45
#include " absl/types/span.h"
46
46
#include " base/number_util.h"
47
+ #include " converter/inner_segment.h"
47
48
48
49
#ifdef MOZC_CANDIDATE_DEBUG
49
50
#include " absl/strings/str_cat.h"
@@ -85,54 +86,6 @@ void Candidate::Dlog(absl::string_view filename, int line,
85
86
}
86
87
#endif // MOZC_CANDIDATE_DEBUG
87
88
88
- bool Candidate::IsValid () const {
89
- if (inner_segment_boundary.empty ()) {
90
- return true ;
91
- }
92
- // The sums of the lengths of key, value components must coincide with those
93
- // of key, value, respectively.
94
- size_t sum_key_len = 0 , sum_value_len = 0 ;
95
- for (InnerSegmentIterator iter (this ); !iter.Done (); iter.Next ()) {
96
- sum_key_len += iter.GetKey ().size ();
97
- sum_value_len += iter.GetValue ().size ();
98
- }
99
- return sum_key_len == key.size () && sum_value_len == value.size ();
100
- }
101
-
102
- bool Candidate::EncodeLengths (size_t key_len, size_t value_len,
103
- size_t content_key_len, size_t content_value_len,
104
- uint32_t *result) {
105
- if (key_len > std::numeric_limits<uint8_t >::max () ||
106
- value_len > std::numeric_limits<uint8_t >::max () ||
107
- content_key_len > std::numeric_limits<uint8_t >::max () ||
108
- content_value_len > std::numeric_limits<uint8_t >::max ()) {
109
- return false ;
110
- }
111
- *result = (static_cast <uint32_t >(key_len) << 24 ) |
112
- (static_cast <uint32_t >(value_len) << 16 ) |
113
- (static_cast <uint32_t >(content_key_len) << 8 ) |
114
- static_cast <uint32_t >(content_value_len);
115
- return true ;
116
- }
117
-
118
- std::tuple<size_t , size_t , size_t , size_t > Candidate::DecodeLengths (
119
- uint32_t encoded) {
120
- return std::make_tuple (encoded >> 24 , (encoded >> 16 ) & 0xff ,
121
- (encoded >> 8 ) & 0xff , (encoded & 0xff ));
122
- }
123
-
124
- bool Candidate::PushBackInnerSegmentBoundary (size_t key_len, size_t value_len,
125
- size_t content_key_len,
126
- size_t content_value_len) {
127
- uint32_t encoded;
128
- if (EncodeLengths (key_len, value_len, content_key_len, content_value_len,
129
- &encoded)) {
130
- inner_segment_boundary.push_back (encoded);
131
- return true ;
132
- }
133
- return false ;
134
- }
135
-
136
89
std::string Candidate::DebugString () const {
137
90
std::stringstream os;
138
91
os << " (key=" << key << " ckey=" << content_key << " val=" << value
@@ -151,70 +104,15 @@ std::string Candidate::DebugString() const {
151
104
}
152
105
if (!inner_segment_boundary.empty ()) {
153
106
os << " segbdd=" ;
154
- for (size_t i = 0 ; i < inner_segment_boundary.size (); ++i) {
155
- const uint32_t encoded_lengths = inner_segment_boundary[i];
156
- const auto [key_len, value_len, content_key_len, content_value_len] =
157
- DecodeLengths (encoded_lengths);
158
- os << absl::StreamFormat (" <%d,%d,%d,%d>" , key_len, value_len,
159
- content_key_len, content_value_len);
107
+ for (const auto &iter : inner_segments ()) {
108
+ os << absl::StreamFormat (
109
+ " <%d,%d,%d,%d>" , iter.GetKey ().size (), iter.GetValue ().size (),
110
+ iter.GetContentKey ().size (), iter.GetContentValue ().size ());
160
111
}
161
112
}
162
113
os << " )" << std::endl;
163
114
return os.str ();
164
115
}
165
116
166
- void Candidate::InnerSegmentIterator::Next () {
167
- DCHECK_LT (index_, inner_segment_boundary_.size ());
168
- const uint32_t encoded_lengths = inner_segment_boundary_[index_++];
169
- key_offset_ += encoded_lengths >> 24 ;
170
- value_offset_ += (encoded_lengths >> 16 ) & 0xff ;
171
- }
172
-
173
- absl::string_view Candidate::InnerSegmentIterator::GetKey () const {
174
- DCHECK_LT (index_, inner_segment_boundary_.size ());
175
- const uint32_t encoded_lengths = inner_segment_boundary_[index_];
176
- return absl::string_view (key_offset_, encoded_lengths >> 24 );
177
- }
178
-
179
- absl::string_view Candidate::InnerSegmentIterator::GetValue () const {
180
- DCHECK_LT (index_, inner_segment_boundary_.size ());
181
- const uint32_t encoded_lengths = inner_segment_boundary_[index_];
182
- return absl::string_view (value_offset_, (encoded_lengths >> 16 ) & 0xff );
183
- }
184
-
185
- absl::string_view Candidate::InnerSegmentIterator::GetContentKey () const {
186
- DCHECK_LT (index_, inner_segment_boundary_.size ());
187
- const uint32_t encoded_lengths = inner_segment_boundary_[index_];
188
- return absl::string_view (key_offset_, (encoded_lengths >> 8 ) & 0xff );
189
- }
190
-
191
- absl::string_view Candidate::InnerSegmentIterator::GetContentValue () const {
192
- DCHECK_LT (index_, inner_segment_boundary_.size ());
193
- const uint32_t encoded_lengths = inner_segment_boundary_[index_];
194
- return absl::string_view (value_offset_, encoded_lengths & 0xff );
195
- }
196
-
197
- absl::string_view Candidate::InnerSegmentIterator::GetFunctionalKey () const {
198
- DCHECK_LT (index_, inner_segment_boundary_.size ());
199
- const uint32_t encoded_lengths = inner_segment_boundary_[index_];
200
- const int key_len = encoded_lengths >> 24 ;
201
- const int content_key_len = (encoded_lengths >> 8 ) & 0xff ;
202
- if (const int key_size = key_len - content_key_len; key_size > 0 ) {
203
- return absl::string_view (key_offset_ + content_key_len, key_size);
204
- }
205
- return absl::string_view ();
206
- }
207
-
208
- absl::string_view Candidate::InnerSegmentIterator::GetFunctionalValue () const {
209
- DCHECK_LT (index_, inner_segment_boundary_.size ());
210
- const uint32_t encoded_lengths = inner_segment_boundary_[index_];
211
- const int value_len = (encoded_lengths >> 16 ) & 0xff ;
212
- const int content_value_len = encoded_lengths & 0xff ;
213
- if (const int value_size = value_len - content_value_len; value_size > 0 ) {
214
- return absl::string_view (value_offset_ + content_value_len, value_size);
215
- }
216
- return absl::string_view ();
217
- }
218
-
219
117
} // namespace converter
220
118
} // namespace mozc
0 commit comments