Rearrange Comments::set (#1278)

* slightly optimize Comments::set

Avoid allocation if the set is going to be rejected anyway.

Prototype suggestion from #1277 review thread
This commit is contained in:
Billy Donahue 2021-03-18 05:22:35 -04:00 committed by GitHub
parent 1ee39a6752
commit 94cda30dbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1398,14 +1398,12 @@ String Value::Comments::get(CommentPlacement slot) const {
} }
void Value::Comments::set(CommentPlacement slot, String comment) { void Value::Comments::set(CommentPlacement slot, String comment) {
if (!ptr_) { if (slot >= CommentPlacement::numberOfCommentPlacement)
return;
if (!ptr_)
ptr_ = std::unique_ptr<Array>(new Array()); ptr_ = std::unique_ptr<Array>(new Array());
}
// check comments array boundry.
if (slot < CommentPlacement::numberOfCommentPlacement) {
(*ptr_)[slot] = std::move(comment); (*ptr_)[slot] = std::move(comment);
} }
}
void Value::setComment(String comment, CommentPlacement placement) { void Value::setComment(String comment, CommentPlacement placement) {
if (!comment.empty() && (comment.back() == '\n')) { if (!comment.empty() && (comment.back() == '\n')) {