From 689680a5315a51cfe347968a893c38bb60be56eb Mon Sep 17 00:00:00 2001 From: sapics Date: Thu, 4 Jun 2020 16:08:43 +0900 Subject: [PATCH] src: simplify Reindent function in json_utils.cc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/33722 Reviewed-By: Ben Noordhuis Reviewed-By: Richard Lau Reviewed-By: Zeyu Yang Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Juan José Arboleda --- src/json_utils.cc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/json_utils.cc b/src/json_utils.cc index aa03a6d7305d75..96f178cf351d96 100644 --- a/src/json_utils.cc +++ b/src/json_utils.cc @@ -41,12 +41,11 @@ std::string EscapeJsonChars(const std::string& str) { } std::string Reindent(const std::string& str, int indent_depth) { - std::string indent; - for (int i = 0; i < indent_depth; i++) indent += ' '; - + if (indent_depth <= 0) return str; + const std::string indent(indent_depth, ' '); std::string out; std::string::size_type pos = 0; - do { + for (;;) { std::string::size_type prev_pos = pos; pos = str.find('\n', pos); @@ -59,7 +58,7 @@ std::string Reindent(const std::string& str, int indent_depth) { pos++; out.append(str, prev_pos, pos - prev_pos); } - } while (true); + } return out; }