Remove the segments_size param of ConverterInterface::ResizeSegment().

* Also renamed one of overloaded ResizeSegment functions to ResizeSegments.

#codehealth

PiperOrigin-RevId: 704137708
This commit is contained in:
Hiroyuki Komatsu
2024-12-09 05:24:07 +00:00
parent 8354f11f28
commit bc3c4136b3
9 changed files with 59 additions and 71 deletions

View File

@ -525,11 +525,10 @@ bool Converter::ResizeSegment(Segments *segments,
return true; return true;
} }
bool Converter::ResizeSegment(Segments *segments, bool Converter::ResizeSegments(Segments *segments,
const ConversionRequest &request, const ConversionRequest &request,
size_t start_segment_index, size_t start_segment_index,
size_t unused_segments_size, absl::Span<const uint8_t> new_size_array) const {
absl::Span<const uint8_t> new_size_array) const {
if (request.request_type() != ConversionRequest::CONVERSION) { if (request.request_type() != ConversionRequest::CONVERSION) {
return false; return false;
} }

View File

@ -106,9 +106,9 @@ class Converter final : public ConverterInterface {
const ConversionRequest &request, const ConversionRequest &request,
size_t segment_index, size_t segment_index,
int offset_length) const override; int offset_length) const override;
ABSL_MUST_USE_RESULT bool ResizeSegment( ABSL_MUST_USE_RESULT bool ResizeSegments(
Segments *segments, const ConversionRequest &request, Segments *segments, const ConversionRequest &request,
size_t start_segment_index, size_t unused_segments_size, size_t start_segment_index,
absl::Span<const uint8_t> new_size_array) const override; absl::Span<const uint8_t> new_size_array) const override;
// Execute ImmutableConverter, Rewriters, SuppressionDictionary. // Execute ImmutableConverter, Rewriters, SuppressionDictionary.

View File

@ -144,9 +144,9 @@ class ConverterInterface {
// Resize [start_segment_index, start_segment_index + segment_size] // Resize [start_segment_index, start_segment_index + segment_size]
// segments with the new size in new_size_array. // segments with the new size in new_size_array.
ABSL_MUST_USE_RESULT virtual bool ResizeSegment( ABSL_MUST_USE_RESULT virtual bool ResizeSegments(
Segments *segments, const ConversionRequest &request, Segments *segments, const ConversionRequest &request,
size_t start_segment_index, size_t segments_size, size_t start_segment_index,
absl::Span<const uint8_t> new_size_array) const = 0; absl::Span<const uint8_t> new_size_array) const = 0;
protected: protected:

View File

@ -388,15 +388,20 @@ bool ExecCommand(const ConverterInterface &converter, const std::string &line,
return converter.ResizeSegment(segments, conversion_request, return converter.ResizeSegment(segments, conversion_request,
NumberUtil::SimpleAtoi(fields[1]), NumberUtil::SimpleAtoi(fields[1]),
NumberUtil::SimpleAtoi(fields[2])); NumberUtil::SimpleAtoi(fields[2]));
} else if (fields.size() > 3) { }
} else if (func == "resizesegments" || func == "resizes") {
options.request_type = ConversionRequest::CONVERSION;
const ConversionRequest conversion_request = ConversionRequest(
composer, request, context, *config, std::move(options));
if (fields.size() > 3) {
std::vector<uint8_t> new_arrays; std::vector<uint8_t> new_arrays;
for (size_t i = 3; i < fields.size(); ++i) { for (size_t i = 2; i < fields.size(); ++i) {
new_arrays.push_back( new_arrays.push_back(
static_cast<uint8_t>(NumberUtil::SimpleAtoi(fields[i]))); static_cast<uint8_t>(NumberUtil::SimpleAtoi(fields[i])));
} }
return converter.ResizeSegment( return converter.ResizeSegments(
segments, conversion_request, NumberUtil::SimpleAtoi(fields[1]), segments, conversion_request, NumberUtil::SimpleAtoi(fields[1]),
NumberUtil::SimpleAtoi(fields[2]), new_arrays); new_arrays);
} }
} else if (func == "disableuserhistory") { } else if (func == "disableuserhistory") {
config->set_history_learning_level(config::Config::NO_HISTORY); config->set_history_learning_level(config::Config::NO_HISTORY);

View File

@ -84,9 +84,9 @@ class StrictMockConverter : public ConverterInterface {
(Segments * segments, const ConversionRequest &request, (Segments * segments, const ConversionRequest &request,
size_t segment_index, int offset_length), size_t segment_index, int offset_length),
(const, override)); (const, override));
MOCK_METHOD(bool, ResizeSegment, MOCK_METHOD(bool, ResizeSegments,
(Segments * segments, const ConversionRequest &request, (Segments * segments, const ConversionRequest &request,
size_t start_segment_index, size_t segments_size, size_t start_segment_index,
absl::Span<const uint8_t> new_size_array), absl::Span<const uint8_t> new_size_array),
(const, override)); (const, override));
}; };

View File

@ -2062,7 +2062,7 @@ TEST_F(ConverterTest, ResizeSegmentWithOffset) {
} }
} }
TEST_F(ConverterTest, ResizeSegmentWithArray) { TEST_F(ConverterTest, ResizeSegmentsWithArray) {
constexpr Segment::SegmentType kFixedBoundary = Segment::FIXED_BOUNDARY; constexpr Segment::SegmentType kFixedBoundary = Segment::FIXED_BOUNDARY;
constexpr Segment::SegmentType kFree = Segment::FREE; constexpr Segment::SegmentType kFree = Segment::FREE;
@ -2076,11 +2076,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) {
AddSegment("あいうえ", kFree, segments); AddSegment("あいうえ", kFree, segments);
const ConversionRequest convreq; const ConversionRequest convreq;
const int start_segment_index = 0; const int start_segment_index = 0;
const int segments_size = 1;
const std::array<uint8_t, 2> size_array = {3, 1}; const std::array<uint8_t, 2> size_array = {3, 1};
EXPECT_EQ(segments.conversion_segments_size(), 1); EXPECT_EQ(segments.conversion_segments_size(), 1);
EXPECT_TRUE(converter->ResizeSegment( EXPECT_TRUE(converter->ResizeSegments(&segments, convreq,
&segments, convreq, start_segment_index, segments_size, size_array)); start_segment_index, size_array));
ASSERT_EQ(segments.conversion_segments_size(), 2); ASSERT_EQ(segments.conversion_segments_size(), 2);
EXPECT_EQ(segments.conversion_segment(0).key(), "あいう"); EXPECT_EQ(segments.conversion_segment(0).key(), "あいう");
EXPECT_EQ(segments.conversion_segment(1).key(), ""); EXPECT_EQ(segments.conversion_segment(1).key(), "");
@ -2100,11 +2099,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) {
AddSegment("あいうえ", Segment::FREE, segments); AddSegment("あいうえ", Segment::FREE, segments);
const ConversionRequest convreq; const ConversionRequest convreq;
const int start_segment_index = 0; const int start_segment_index = 0;
const int segments_size = 1;
const std::array<uint8_t, 2> size_array = {3, 1}; const std::array<uint8_t, 2> size_array = {3, 1};
EXPECT_EQ(segments.conversion_segments_size(), 1); EXPECT_EQ(segments.conversion_segments_size(), 1);
EXPECT_TRUE(converter->ResizeSegment( EXPECT_TRUE(converter->ResizeSegments(&segments, convreq,
&segments, convreq, start_segment_index, segments_size, size_array)); start_segment_index, size_array));
ASSERT_EQ(segments.conversion_segments_size(), 2); ASSERT_EQ(segments.conversion_segments_size(), 2);
EXPECT_EQ(segments.conversion_segment(0).key(), "あいう"); EXPECT_EQ(segments.conversion_segment(0).key(), "あいう");
EXPECT_EQ(segments.conversion_segment(1).key(), ""); EXPECT_EQ(segments.conversion_segment(1).key(), "");
@ -2119,11 +2117,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) {
AddSegment("あいうえ", Segment::FREE, segments); AddSegment("あいうえ", Segment::FREE, segments);
const ConversionRequest convreq; const ConversionRequest convreq;
const int start_segment_index = 0; const int start_segment_index = 0;
const int segments_size = 1;
const std::array<uint8_t, 8> size_array = {3, 1, 0, 0, 0, 0, 0, 0}; const std::array<uint8_t, 8> size_array = {3, 1, 0, 0, 0, 0, 0, 0};
EXPECT_EQ(segments.conversion_segments_size(), 1); EXPECT_EQ(segments.conversion_segments_size(), 1);
EXPECT_TRUE(converter->ResizeSegment( EXPECT_TRUE(converter->ResizeSegments(&segments, convreq,
&segments, convreq, start_segment_index, segments_size, size_array)); start_segment_index, size_array));
ASSERT_EQ(segments.conversion_segments_size(), 2); ASSERT_EQ(segments.conversion_segments_size(), 2);
EXPECT_EQ(segments.conversion_segment(0).key(), "あいう"); EXPECT_EQ(segments.conversion_segment(0).key(), "あいう");
EXPECT_EQ(segments.conversion_segment(1).key(), ""); EXPECT_EQ(segments.conversion_segment(1).key(), "");
@ -2137,11 +2134,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) {
AddSegment("あいうえおかきくけ", Segment::FREE, segments); AddSegment("あいうえおかきくけ", Segment::FREE, segments);
const ConversionRequest convreq; const ConversionRequest convreq;
const int start_segment_index = 0; const int start_segment_index = 0;
const int segments_size = 1;
const std::array<uint8_t, 5> size_array = {2, 2, 1, 2, 2}; const std::array<uint8_t, 5> size_array = {2, 2, 1, 2, 2};
EXPECT_EQ(segments.conversion_segments_size(), 1); EXPECT_EQ(segments.conversion_segments_size(), 1);
EXPECT_TRUE(converter->ResizeSegment( EXPECT_TRUE(converter->ResizeSegments(&segments, convreq,
&segments, convreq, start_segment_index, segments_size, size_array)); start_segment_index, size_array));
ASSERT_EQ(segments.conversion_segments_size(), 5); ASSERT_EQ(segments.conversion_segments_size(), 5);
EXPECT_EQ(segments.conversion_segment(0).key(), "あい"); EXPECT_EQ(segments.conversion_segment(0).key(), "あい");
EXPECT_EQ(segments.conversion_segment(1).key(), "うえ"); EXPECT_EQ(segments.conversion_segment(1).key(), "うえ");
@ -2165,11 +2161,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) {
AddSegment("くけ", Segment::FREE, segments); AddSegment("くけ", Segment::FREE, segments);
const ConversionRequest convreq; const ConversionRequest convreq;
const int start_segment_index = 0; const int start_segment_index = 0;
const int segments_size = 4;
const std::array<uint8_t, 3> size_array = {4, 1, 4}; const std::array<uint8_t, 3> size_array = {4, 1, 4};
EXPECT_EQ(segments.conversion_segments_size(), 4); EXPECT_EQ(segments.conversion_segments_size(), 4);
EXPECT_TRUE(converter->ResizeSegment( EXPECT_TRUE(converter->ResizeSegments(&segments, convreq,
&segments, convreq, start_segment_index, segments_size, size_array)); start_segment_index, size_array));
ASSERT_EQ(segments.conversion_segments_size(), 3); ASSERT_EQ(segments.conversion_segments_size(), 3);
EXPECT_EQ(segments.conversion_segment(0).key(), "あいうえ"); EXPECT_EQ(segments.conversion_segment(0).key(), "あいうえ");
EXPECT_EQ(segments.conversion_segment(1).key(), ""); EXPECT_EQ(segments.conversion_segment(1).key(), "");
@ -2189,11 +2184,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) {
AddSegment("くけ", Segment::FREE, segments); AddSegment("くけ", Segment::FREE, segments);
const ConversionRequest convreq; const ConversionRequest convreq;
const int start_segment_index = 0; const int start_segment_index = 0;
const int segments_size = 2;
const std::array<uint8_t, 2> size_array = {4, 1}; const std::array<uint8_t, 2> size_array = {4, 1};
EXPECT_EQ(segments.conversion_segments_size(), 4); EXPECT_EQ(segments.conversion_segments_size(), 4);
EXPECT_TRUE(converter->ResizeSegment( EXPECT_TRUE(converter->ResizeSegments(&segments, convreq,
&segments, convreq, start_segment_index, segments_size, size_array)); start_segment_index, size_array));
ASSERT_EQ(segments.conversion_segments_size(), 4); ASSERT_EQ(segments.conversion_segments_size(), 4);
EXPECT_EQ(segments.conversion_segment(0).key(), "あいうえ"); EXPECT_EQ(segments.conversion_segment(0).key(), "あいうえ");
EXPECT_EQ(segments.conversion_segment(1).key(), ""); EXPECT_EQ(segments.conversion_segment(1).key(), "");
@ -2211,11 +2205,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) {
AddSegment("あいうえ", Segment::FREE, segments); AddSegment("あいうえ", Segment::FREE, segments);
const ConversionRequest convreq; const ConversionRequest convreq;
const int start_segment_index = 0; const int start_segment_index = 0;
const int segments_size = 1;
const std::array<uint8_t, 1> size_array = {3}; const std::array<uint8_t, 1> size_array = {3};
EXPECT_EQ(segments.conversion_segments_size(), 1); EXPECT_EQ(segments.conversion_segments_size(), 1);
EXPECT_TRUE(converter->ResizeSegment( EXPECT_TRUE(converter->ResizeSegments(&segments, convreq,
&segments, convreq, start_segment_index, segments_size, size_array)); start_segment_index, size_array));
ASSERT_EQ(segments.conversion_segments_size(), 2); ASSERT_EQ(segments.conversion_segments_size(), 2);
EXPECT_EQ(segments.conversion_segment(0).key(), "あいう"); EXPECT_EQ(segments.conversion_segment(0).key(), "あいう");
EXPECT_EQ(segments.conversion_segment(1).key(), ""); EXPECT_EQ(segments.conversion_segment(1).key(), "");
@ -2235,11 +2228,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) {
AddSegment("くけ", Segment::FREE, segments); AddSegment("くけ", Segment::FREE, segments);
const ConversionRequest convreq; const ConversionRequest convreq;
const int start_segment_index = 2; const int start_segment_index = 2;
const int segments_size = 2;
const std::array<uint8_t, 1> size_array = {4}; const std::array<uint8_t, 1> size_array = {4};
EXPECT_EQ(segments.conversion_segments_size(), 4); EXPECT_EQ(segments.conversion_segments_size(), 4);
EXPECT_TRUE(converter->ResizeSegment( EXPECT_TRUE(converter->ResizeSegments(&segments, convreq,
&segments, convreq, start_segment_index, segments_size, size_array)); start_segment_index, size_array));
// Since {"あいう", "えお"} may be modified too, the segment index for // Since {"あいう", "えお"} may be modified too, the segment index for
// "かきくけ" may be different from 2. // "かきくけ" may be different from 2.

View File

@ -146,9 +146,9 @@ class MinimalConverter : public ConverterInterface {
return true; return true;
} }
bool ResizeSegment(Segments *segments, const ConversionRequest &request, bool ResizeSegments(Segments *segments, const ConversionRequest &request,
size_t start_segment_index, size_t segments_size, size_t start_segment_index,
absl::Span<const uint8_t> new_size_array) const override { absl::Span<const uint8_t> new_size_array) const override {
return true; return true;
} }
}; };

View File

@ -312,8 +312,8 @@ bool UserBoundaryHistoryRewriter::Resize(
MOZC_VLOG(2) << "ResizeSegment key: " << key << " segments: [" << seg_idx MOZC_VLOG(2) << "ResizeSegment key: " << key << " segments: [" << seg_idx
<< ", " << seg_size << "] " << ", " << seg_size << "] "
<< "resize: [" << absl::StrJoin(updated_array, " ") << "]"; << "resize: [" << absl::StrJoin(updated_array, " ") << "]";
if (parent_converter_->ResizeSegment(&segments, request, seg_idx, if (parent_converter_->ResizeSegments(&segments, request, seg_idx,
seg_size, updated_array)) { updated_array)) {
result = true; result = true;
} else { } else {
LOG(WARNING) << "ResizeSegment failed for key: " << key; LOG(WARNING) << "ResizeSegment failed for key: " << key;

View File

@ -142,10 +142,9 @@ TEST_F(UserBoundaryHistoryRewriterTest, SplitSegmentByHistory) {
// TODO(noriyukit): The current implementation always sets the length array // TODO(noriyukit): The current implementation always sets the length array
// size to 8 with padded zeros. Better to set the actual length. // size to 8 with padded zeros. Better to set the actual length.
Segments segments = MakeSegments({"たんぽぽ"}, Segment::FREE); Segments segments = MakeSegments({"たんぽぽ"}, Segment::FREE);
EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq),
/*start_segment_index=*/0, /*start_segment_index=*/0,
/*segments_size=*/1, ElementsAre(2, 2, 0, 0, 0, 0, 0, 0)))
ElementsAre(2, 2, 0, 0, 0, 0, 0, 0)))
.WillOnce(Return(true)); .WillOnce(Return(true));
EXPECT_TRUE(rewriter.Rewrite(convreq, &segments)); EXPECT_TRUE(rewriter.Rewrite(convreq, &segments));
} }
@ -175,10 +174,9 @@ TEST_F(UserBoundaryHistoryRewriterTest, JoinSegmentsByHistory) {
// TODO(noriyukit): The current implementation always sets the length array // TODO(noriyukit): The current implementation always sets the length array
// size to 8 with padded zeros. Better to set the actual length. // size to 8 with padded zeros. Better to set the actual length.
Segments segments = MakeSegments({"たん", "ぽぽ"}, Segment::FREE); Segments segments = MakeSegments({"たん", "ぽぽ"}, Segment::FREE);
EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq),
/*start_segment_index=*/0, /*start_segment_index=*/0,
/*segments_size=*/2, ElementsAre(4, 0, 0, 0, 0, 0, 0, 0)))
ElementsAre(4, 0, 0, 0, 0, 0, 0, 0)))
.WillOnce(Return(true)); .WillOnce(Return(true));
EXPECT_TRUE(rewriter.Rewrite(convreq, &segments)); EXPECT_TRUE(rewriter.Rewrite(convreq, &segments));
} }
@ -416,13 +414,11 @@ TEST_F(UserBoundaryHistoryRewriterTest, FailureOfSplitIsNotFatal) {
{ {
const ConversionRequest convreq = CreateConversionRequest(); const ConversionRequest convreq = CreateConversionRequest();
Segments segments = MakeSegments({"たんぽぽ", "わたげ"}, Segment::FREE); Segments segments = MakeSegments({"たんぽぽ", "わたげ"}, Segment::FREE);
EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq),
/*start_segment_index=*/0, /*start_segment_index=*/0, _))
/*segments_size=*/1, _))
.WillOnce(Return(false)); .WillOnce(Return(false));
EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq),
/*start_segment_index=*/1, /*start_segment_index=*/1, _))
/*segments_size=*/1, _))
.WillOnce(Return(false)); .WillOnce(Return(false));
EXPECT_FALSE(rewriter.Rewrite(convreq, &segments)); EXPECT_FALSE(rewriter.Rewrite(convreq, &segments));
} }
@ -431,13 +427,11 @@ TEST_F(UserBoundaryHistoryRewriterTest, FailureOfSplitIsNotFatal) {
Segments segments = MakeSegments({"たんぽぽ", "わたげ"}, Segment::FREE); Segments segments = MakeSegments({"たんぽぽ", "わたげ"}, Segment::FREE);
const Segments resized = const Segments resized =
MakeSegments({"たん", "ぽぽ", "わたげ"}, Segment::FREE); MakeSegments({"たん", "ぽぽ", "わたげ"}, Segment::FREE);
EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq),
/*start_segment_index=*/0, /*start_segment_index=*/0, _))
/*segments_size=*/1, _))
.WillOnce(DoAll(SetArgPointee<0>(resized), Return(true))); .WillOnce(DoAll(SetArgPointee<0>(resized), Return(true)));
EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq),
/*start_segment_index=*/1, /*start_segment_index=*/1, _))
/*segments_size=*/1, _))
.WillOnce(Return(false)); .WillOnce(Return(false));
EXPECT_TRUE(rewriter.Rewrite(convreq, &segments)); EXPECT_TRUE(rewriter.Rewrite(convreq, &segments));
} }
@ -446,13 +440,11 @@ TEST_F(UserBoundaryHistoryRewriterTest, FailureOfSplitIsNotFatal) {
Segments segments = MakeSegments({"たんぽぽ", "わたげ"}, Segment::FREE); Segments segments = MakeSegments({"たんぽぽ", "わたげ"}, Segment::FREE);
const Segments resized = const Segments resized =
MakeSegments({"たんぽぽ", "わた", ""}, Segment::FREE); MakeSegments({"たんぽぽ", "わた", ""}, Segment::FREE);
EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq),
/*start_segment_index=*/0, /*start_segment_index=*/0, _))
/*segments_size=*/1, _))
.WillOnce(Return(false)); .WillOnce(Return(false));
EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq),
/*start_segment_index=*/1, /*start_segment_index=*/1, _))
/*segments_size=*/1, _))
.WillOnce(DoAll(SetArgPointee<0>(resized), Return(true))); .WillOnce(DoAll(SetArgPointee<0>(resized), Return(true)));
EXPECT_TRUE(rewriter.Rewrite(convreq, &segments)); EXPECT_TRUE(rewriter.Rewrite(convreq, &segments));
} }