From 1442e1982195d0511471fe1c58c54d75f87de8e5 Mon Sep 17 00:00:00 2001
From: Steven Hahn <hahnse@ornl.gov>
Date: Mon, 28 Nov 2016 09:00:31 -0500
Subject: [PATCH] Avoid extra allocations in String::getLine.

---
 .../src/PDLoadCharacterizations.cpp           | 26 +++++++++--------
 Framework/Kernel/inc/MantidKernel/Strings.h   |  6 +++-
 Framework/Kernel/src/Strings.cpp              | 29 +++++++++++--------
 3 files changed, 36 insertions(+), 25 deletions(-)

diff --git a/Framework/DataHandling/src/PDLoadCharacterizations.cpp b/Framework/DataHandling/src/PDLoadCharacterizations.cpp
index 56ab2785176..1944e637506 100644
--- a/Framework/DataHandling/src/PDLoadCharacterizations.cpp
+++ b/Framework/DataHandling/src/PDLoadCharacterizations.cpp
@@ -59,8 +59,9 @@ extra_columns(const std::vector<std::string> &filenames) {
     throw Exception::FileError("Unable to open file", filenames[F_INDEX_V1]);
   }
 
-  for (std::string line = Strings::getLine(file); !file.eof();
-       line = Strings::getLine(file)) {
+  std::string line;
+  for (Strings::getLine(file, line); !file.eof();
+       Strings::getLine(file, line)) {
     boost::smatch result;
     // all instances of table headers
     if (boost::regex_search(line, result, V1_TABLE_REG_EXP)) {
@@ -268,8 +269,9 @@ void PDLoadCharacterizations::readFocusInfo(std::ifstream &file) {
   std::vector<double> polar;
 
   // parse the file
-  for (std::string line = Strings::getLine(file); !file.eof();
-       line = Strings::getLine(file)) {
+  std::string line;
+  for (Strings::getLine(file, line); !file.eof();
+       Strings::getLine(file, line)) {
     line = Strings::strip(line);
     // skip empty lines and "comments"
     if (line.empty())
@@ -318,10 +320,10 @@ void PDLoadCharacterizations::readCharInfo(std::ifstream &file,
     return;
 
   // parse the file
-  for (std::string line = Strings::getLine(file); !file.eof();
-       line = Strings::getLine(file)) {
+  std::string line;
+  for (Strings::getLine(file, line); !file.eof();
+       Strings::getLine(file, line)) {
     line = Strings::strip(line);
-
     // skip empty lines and "comments"
     if (line.empty())
       continue;
@@ -453,9 +455,8 @@ void PDLoadCharacterizations::readVersion1(const std::string &filename,
 
   // store the names of the columns in order
   std::vector<std::string> columnNames;
-
-  for (std::string line = Strings::getLine(file); !file.eof();
-       line = Strings::getLine(file)) {
+  for (Strings::getLine(file, line); !file.eof();
+       Strings::getLine(file, line)) {
     if (line.empty())
       continue;
     if (line.substr(0, 1) == "#")
@@ -539,8 +540,9 @@ void PDLoadCharacterizations::readExpIni(const std::string &filename,
   }
 
   // parse the file
-  for (std::string line = Strings::getLine(file); !file.eof();
-       line = Strings::getLine(file)) {
+  std::string line;
+  for (Strings::getLine(file, line); !file.eof();
+       Strings::getLine(file, line)) {
     line = Strings::strip(line);
     // skip empty lines and "comments"
     if (line.empty())
diff --git a/Framework/Kernel/inc/MantidKernel/Strings.h b/Framework/Kernel/inc/MantidKernel/Strings.h
index e1f9f2d8738..a199e97c369 100644
--- a/Framework/Kernel/inc/MantidKernel/Strings.h
+++ b/Framework/Kernel/inc/MantidKernel/Strings.h
@@ -101,7 +101,11 @@ MANTID_KERNEL_DLL int isEmpty(const std::string &A);
 /// Determines if a string starts with a #
 MANTID_KERNEL_DLL bool skipLine(const std::string &line);
 /// Get a line and strip comments
-MANTID_KERNEL_DLL std::string getLine(std::istream &fh, const int spc = 256);
+/// Use only for a single call
+MANTID_KERNEL_DLL std::string getLine(std::istream &fh);
+/// Get a line and strip comments
+/// Use within a loop
+MANTID_KERNEL_DLL void getLine(std::istream &fh, std::string &Line);
 /// Peek at a line without extracting it from the stream
 MANTID_KERNEL_DLL std::string peekLine(std::istream &fh);
 /// get a part of a long line
diff --git a/Framework/Kernel/src/Strings.cpp b/Framework/Kernel/src/Strings.cpp
index ae7dd875e87..d008c0de89a 100644
--- a/Framework/Kernel/src/Strings.cpp
+++ b/Framework/Kernel/src/Strings.cpp
@@ -273,28 +273,33 @@ std::string removeSpace(const std::string &CLine) {
   return Out;
 }
 
+//------------------------------------------------------------------------------------------------
+/**
+  *  Reads a line from the stream of max length spc.
+  *  Trailing comments are removed. (with # or ! character)
+  *  @param fh :: already open file handle
+  *  @return String read.
+  */
+std::string getLine(std::istream &fh) {
+  std::string line;
+  getLine(fh, line);
+  return line;
+}
+
 //------------------------------------------------------------------------------------------------
 /**
  *  Reads a line from the stream of max length spc.
  *  Trailing comments are removed. (with # or ! character)
  *  @param fh :: already open file handle
- *  @param spc :: max number of characters to read
- *  @return String read.
+ *  @param Line :: string read
  */
-std::string getLine(std::istream &fh, const int spc) {
-  auto ss = new char[spc + 1];
-  std::string Line;
-  if (fh.good()) {
-    fh.getline(ss, spc, '\n');
-    ss[spc] = 0; // incase line failed to read completely
-    Line = ss;
+void getLine(std::istream &fh, std::string &Line) {
+  if (std::getline(fh, Line, '\n')) {
     // remove trailing comments
-    std::string::size_type pos = Line.find_first_of("#!");
+    auto pos = Line.find_first_of("#!");
     if (pos != std::string::npos)
       Line.erase(pos);
   }
-  delete[] ss;
-  return Line;
 }
 
 /**
-- 
GitLab