From 7c0e4747ed6897185608163af1c7f1a55bb297f5 Mon Sep 17 00:00:00 2001 From: Stefan Koch Date: Fri, 6 Oct 2017 17:04:01 +0200 Subject: [PATCH] small perf fix chaching the eol string instead of switching all the time --- src/dfmt/formatter.d | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 141e7c2..92f747c 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -92,6 +92,18 @@ struct TokenFormatter(OutputRange) this.output = output; this.astInformation = astInformation; this.config = config; + + { + auto eol = config.end_of_line; + if (eol == eol.cr) + this.eolString = "\r"; + else if (eol == eol.lf) + this.eolString = "\n"; + else if (eol == eol.crlf) + this.eolString = "\r\n"; + else if (eol == eol.unspecified) + assert(false, "config.end_of_line was unspecified"); + } } /// Runs the formatting process @@ -136,6 +148,9 @@ private: /// Configuration const Config* config; + /// chached end of line string + const string eolString; + /// Keep track of whether or not an extra newline was just added because of /// an import statement. bool justAddedExtraNewline; @@ -1247,20 +1262,7 @@ private: { import dfmt.editorconfig : EOL; - final switch (config.end_of_line) - { - case EOL.cr: - output.put("\r"); - break; - case EOL.lf: - output.put("\n"); - break; - case EOL.crlf: - output.put("\r\n"); - break; - case EOL.unspecified: - assert(false, "config.end_of_line was unspecified"); - } + output.put(eolString); } void newline()