Skip to content

Commit

Permalink
Use std::clamp instead of std::max and std::min
Browse files Browse the repository at this point in the history
Note that the behavior of `std::clamp` is [undefined if `lo` is greater than `hi`](https://en.cppreference.com/w/cpp/algorithm/clamp). MSVC 14.37.32822 fails an assertion "invalid bounds arguments passed to std::clamp". This patch adds a guard for this case to `DateWriter`.

Also updates some comments to match the [C++ Style Guide](https://google.github.io/styleguide/cppguide.html#Punctuation,_Spelling_and_Grammar).

This patch has no behavior changes.

PiperOrigin-RevId: 639661484
  • Loading branch information
kojiishi authored and hiroyuki-komatsu committed Jun 3, 2024
1 parent 7e818ac commit 864c543
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/converter/immutable_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1926,8 +1926,7 @@ void ImmutableConverter::InsertCandidates(const ConversionRequest &request,
prev = node;
}

const size_t expand_size =
std::max<size_t>(1, std::min<size_t>(512, max_candidates_size));
const size_t expand_size = std::clamp<size_t>(max_candidates_size, 1, 512);

const bool is_single_segment =
(type == SINGLE_SEGMENT || type == FIRST_INNER_SEGMENT);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/config_dialog/config_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ void ConfigDialog::ConvertFromProto(const config::Config &config) {
SET_CHECKBOX(realtimeConversionCheckBox, use_realtime_conversion);

suggestionsSizeSpinBox->setValue(
std::max(1, std::min<int>(9, config.suggestions_size())));
std::clamp<int>(config.suggestions_size(), 1, 9));

// tab5
SetSendStatsCheckBox();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/dictionary_tool/find_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void FindDialog::FindBackward() {
void FindDialog::Find(FindDialog::Direction direction) {
const QString &query = QuerylineEdit->text();
const int start_row = std::max(0, table_->currentRow());
int start_column = std::min(1, std::max(0, table_->currentColumn()));
int start_column = std::clamp(table_->currentColumn(), 0, 1);
int matched_column = -1;
int matched_row = -1;

Expand Down
2 changes: 1 addition & 1 deletion src/prediction/predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ bool DefaultPredictor::PredictForRequest(const ConversionRequest &request,

int size = kPredictionSize;
if (request.request_type() == ConversionRequest::SUGGESTION) {
size = std::min(9, std::max<int>(1, request.config().suggestions_size()));
size = std::clamp<int>(request.config().suggestions_size(), 1, 9);
}

bool result = false;
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/qt/qt_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ QtServer::QtServer() : timeout_(0) {
std::min(absl::GetFlag(FLAGS_timeout), 60));
}

timeout_ =
1000 * std::max(3, std::min(24 * 60 * 60, absl::GetFlag(FLAGS_timeout)));
timeout_ = 1000 * std::clamp(absl::GetFlag(FLAGS_timeout), 3, 24 * 60 * 60);
MOZC_VLOG(2) << "timeout is set to be : " << timeout_;

#ifndef MOZC_NO_LOGGING
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/renderer_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ RendererServer::RendererServer()
std::min(absl::GetFlag(FLAGS_timeout), 60));
}

timeout_ =
1000 * std::max(3, std::min(24 * 60 * 60, absl::GetFlag(FLAGS_timeout)));
timeout_ = 1000 * std::clamp(absl::GetFlag(FLAGS_timeout), 3, 24 * 60 * 60);
MOZC_VLOG(2) << "timeout is set to be : " << timeout_;

#ifndef MOZC_NO_LOGGING
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/win32/win32_image_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,7 @@ HBITMAP BalloonImage::CreateInternal(const BalloonImageInfo &info,
const int offset_y_;
};

const double normalized_blur_alpha =
std::min(std::max(info.blur_alpha, 0.0), 1.0);
const double normalized_blur_alpha = std::clamp(info.blur_alpha, 0.0, 1.0);
Accessor accessor(frame_buffer, -info.blur_offset_x, -info.blur_offset_y);
for (int y = begin_y; y < begin_y + bmp_height; ++y) {
for (int x = begin_x; x < begin_x + bmp_width; ++x) {
Expand Down
4 changes: 2 additions & 2 deletions src/rewriter/date_rewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -991,8 +991,8 @@ bool DateRewriter::RewriteDate(Segment *segment,

// Date candidates are too many, therefore highest candidate show at most 3rd.
// TODO(nona): learn date candidate even if the date is changed.
constexpr size_t kMinIdx = 3;
size_t insert_idx = std::min(std::max(kMinIdx, cand_idx + 1), end_idx);
const size_t min_idx = std::min<size_t>(3, end_idx);
const size_t insert_idx = std::clamp(cand_idx + 1, min_idx, end_idx);
segment->insert_candidates(insert_idx, std::move(candidates));
return true;
}
Expand Down
7 changes: 3 additions & 4 deletions src/session/session_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,16 @@ SessionHandler::SessionHandler(std::unique_ptr<EngineInterface> engine)
absl::SetFlag(&FLAGS_last_command_timeout, 60);
}

// allow [2..128] sessions
max_session_size_ =
std::max(2, std::min(absl::GetFlag(FLAGS_max_session_size), 128));
// Allow [2..128] sessions.
max_session_size_ = std::clamp(absl::GetFlag(FLAGS_max_session_size), 2, 128);
session_map_ = std::make_unique<SessionMap>(max_session_size_);

if (!engine_) {
return;
}


// everything is OK
// Everything is OK.
is_available_ = true;
}

Expand Down

0 comments on commit 864c543

Please sign in to comment.